From 6482d30b67b92fd32f0d503080fb0a3565dd8e21 Mon Sep 17 00:00:00 2001 From: Uzair Mohammed Date: Sat, 7 Dec 2024 16:25:28 -0800 Subject: [PATCH] Finished --- Module/helper.sh | 36 ++++++++++++++++++++++++++ Module/tempMonitor.c | 39 ++++++++++++++++++++++++----- Test/Mohammed_UzairHamed_HW6_main.c | 8 +++++- 3 files changed, 76 insertions(+), 7 deletions(-) create mode 100755 Module/helper.sh diff --git a/Module/helper.sh b/Module/helper.sh new file mode 100755 index 0000000..1d24584 --- /dev/null +++ b/Module/helper.sh @@ -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 diff --git a/Module/tempMonitor.c b/Module/tempMonitor.c index 56b2801..6237cc5 100644 --- a/Module/tempMonitor.c +++ b/Module/tempMonitor.c @@ -5,11 +5,15 @@ #include #include #include +#include +#include +#include +#include #define DEVICE_NAME "temp_monitor" #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 struct class* temp_monitor_class = 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) { switch (cmd) { - case IOCTL_GET_TEMP: - // Simulate reading temperature - temp = 50; // Example temperature + case IOCTL_GET_TEMP: { + int temp = 0; + 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))) { return -EACCES; } break; + } default: 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"); 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)) { unregister_chrdev(major_number, DEVICE_NAME); 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"); 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; } diff --git a/Test/Mohammed_UzairHamed_HW6_main.c b/Test/Mohammed_UzairHamed_HW6_main.c index 584bb16..51f5be0 100644 --- a/Test/Mohammed_UzairHamed_HW6_main.c +++ b/Test/Mohammed_UzairHamed_HW6_main.c @@ -18,7 +18,13 @@ int main() { close(fd); 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); return 0; }