This commit is contained in:
uzy lol 2024-12-07 16:25:28 -08:00
parent b1087b461d
commit 6482d30b67
3 changed files with 76 additions and 7 deletions

36
Module/helper.sh Executable file
View File

@ -0,0 +1,36 @@
#!/bin/bash
MODULE_NAME="tempMonitor"
DEVICE_NAME="/dev/temp_monitor"
echo "What would you like to do?"
echo "1. Install"
echo "2. Uninstall"
read -p "Enter option [1 or 2]: " option
case $option in
1)
echo "Installing the kernel module..."
make clean
make
sudo rmmod $MODULE_NAME 2>/dev/null
sudo insmod ${MODULE_NAME}.ko
MAJOR_NUMBER=$(sudo dmesg | grep "temp_monitor: Device initialized with major number" | tail -1 | awk '{print $NF}')
if [ -z "$MAJOR_NUMBER" ]; then
echo "Failed to get major number from dmesg. Aborting."
exit 1
fi
sudo mknod $DEVICE_NAME c $MAJOR_NUMBER 0
sudo chmod 666 $DEVICE_NAME
echo "Module installed and device file created successfully."
;;
2)
echo "Uninstalling the kernel module..."
sudo rmmod $MODULE_NAME
sudo rm -f $DEVICE_NAME
echo "Module and device file removed successfully."
;;
*)
echo "Invalid option."
;;
esac

View File

@ -5,11 +5,15 @@
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/ioctl.h> #include <linux/ioctl.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/slab.h>
#include <linux/stat.h>
#include <linux/fs.h>
#include <linux/err.h>
#define DEVICE_NAME "temp_monitor" #define DEVICE_NAME "temp_monitor"
#define IOCTL_GET_TEMP _IOR('t', 1, int) #define IOCTL_GET_TEMP _IOR('t', 1, int)
#define TEMP_SENSOR_PATH "/sys/class/thermal/thermal_zone0/temp"
static int temp = 0;
static int major_number; static int major_number;
static struct class* temp_monitor_class = NULL; static struct class* temp_monitor_class = NULL;
static struct device* temp_monitor_device = NULL; static struct device* temp_monitor_device = NULL;
@ -24,13 +28,36 @@ static int device_release(struct inode *inode, struct file *file) {
static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
switch (cmd) { switch (cmd) {
case IOCTL_GET_TEMP: case IOCTL_GET_TEMP: {
// Simulate reading temperature int temp = 0;
temp = 50; // Example temperature struct file *f;
char *buf;
loff_t pos = 0;
// Check if the temperature file exists
f = filp_open(TEMP_SENSOR_PATH, O_RDONLY, 0);
if (IS_ERR(f)) {
printk(KERN_INFO "temp_monitor: Temperature file not found\n");
} else {
buf = kmalloc(16, GFP_KERNEL);
if (!buf) {
filp_close(f, NULL);
return -ENOMEM;
}
// Read the temperature
kernel_read(f, buf, 16, &pos);
sscanf(buf, "%d", &temp);
temp /= 1000; // Convert to degrees Celsius
filp_close(f, NULL);
kfree(buf);
}
if (copy_to_user((int *)arg, &temp, sizeof(temp))) { if (copy_to_user((int *)arg, &temp, sizeof(temp))) {
return -EACCES; return -EACCES;
} }
break; break;
}
default: default:
return -EINVAL; return -EINVAL;
} }
@ -49,7 +76,7 @@ static int __init temp_monitor_init(void) {
printk(KERN_ALERT "temp_monitor: Failed to register a major number\n"); printk(KERN_ALERT "temp_monitor: Failed to register a major number\n");
return major_number; return major_number;
} }
temp_monitor_class = class_create(DEVICE_NAME); temp_monitor_class = class_create(THIS_MODULE->name); // Updated this line
if (IS_ERR(temp_monitor_class)) { if (IS_ERR(temp_monitor_class)) {
unregister_chrdev(major_number, DEVICE_NAME); unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_ALERT "temp_monitor: Failed to register device class\n"); printk(KERN_ALERT "temp_monitor: Failed to register device class\n");
@ -62,7 +89,7 @@ static int __init temp_monitor_init(void) {
printk(KERN_ALERT "temp_monitor: Failed to create the device\n"); printk(KERN_ALERT "temp_monitor: Failed to create the device\n");
return PTR_ERR(temp_monitor_device); return PTR_ERR(temp_monitor_device);
} }
printk(KERN_INFO "temp_monitor: Device initialized with major number %d\n", major_number); // Added this line printk(KERN_INFO "temp_monitor: Device initialized with major number %d\n", major_number);
return 0; return 0;
} }

View File

@ -18,7 +18,13 @@ int main() {
close(fd); close(fd);
return -1; return -1;
} }
printf("Current temperature: %d\n", temp);
if (temp == 0) {
printf("Couldn't determine current temperature. Are you in a virtual machine?\n");
} else {
printf("Current temperature: %d°C\n", temp);
}
close(fd); close(fd);
return 0; return 0;
} }