Added comments
This commit is contained in:
parent
8ee6467330
commit
caebcff431
@ -1,120 +1,144 @@
|
|||||||
#include <linux/module.h>
|
/**************************************************************
|
||||||
#include <linux/kernel.h>
|
* Class:: CSC-415-01 Fall 2024
|
||||||
#include <linux/init.h>
|
* Name:: Uzair Hamed Mohammed
|
||||||
#include <linux/fs.h>
|
* Student ID:: 920142896
|
||||||
#include <linux/uaccess.h>
|
* GitHub-Name:: gamersekofy
|
||||||
#include <linux/ioctl.h>
|
* Project:: Assignment 6 - Device Driver
|
||||||
|
*
|
||||||
|
* File:: tempMonitor.c
|
||||||
|
*
|
||||||
|
* Description:: This is a complex kernel module that retrieves CPU temperature
|
||||||
|
*data from /sys/class/thermal/thermal_zone*\/temp
|
||||||
|
*
|
||||||
|
**************************************************************/
|
||||||
|
|
||||||
#include <linux/device.h>
|
#include <linux/device.h>
|
||||||
|
#include <linux/err.h>
|
||||||
|
#include <linux/fs.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/ioctl.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/random.h>
|
||||||
#include <linux/slab.h>
|
#include <linux/slab.h>
|
||||||
#include <linux/stat.h>
|
#include <linux/stat.h>
|
||||||
#include <linux/fs.h>
|
#include <linux/uaccess.h>
|
||||||
#include <linux/err.h>
|
|
||||||
#include <linux/random.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)
|
||||||
|
|
||||||
|
// The CPU temperature will be accessed from thermal_zone0/temp
|
||||||
#define TEMP_SENSOR_PATH "/sys/class/thermal/thermal_zone0/temp"
|
#define TEMP_SENSOR_PATH "/sys/class/thermal/thermal_zone0/temp"
|
||||||
#define BUF_LEN 100
|
#define BUF_LEN 100
|
||||||
|
|
||||||
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;
|
||||||
static char temp_unit[BUF_LEN] = "C"; // Default unit is Celsius
|
static char temp_unit[BUF_LEN] = "C"; // Default unit is Celsius
|
||||||
|
|
||||||
|
/* A similar flag exists in the test/user application. If it's detected that
|
||||||
|
* the user is in a virtual machine, a smulated temperature is returned */
|
||||||
static bool simulate_temp = false;
|
static bool simulate_temp = false;
|
||||||
|
|
||||||
static int device_open(struct inode *inode, struct file *file) {
|
static int device_open(struct inode *inode, struct file *file) { return 0; }
|
||||||
return 0;
|
|
||||||
|
static int device_release(struct inode *inode, struct file *file) { return 0; }
|
||||||
|
|
||||||
|
static ssize_t device_read(struct file *file, char __user *buffer, size_t len,
|
||||||
|
loff_t *offset) {
|
||||||
|
int temp = 0;
|
||||||
|
char temp_str[BUF_LEN];
|
||||||
|
struct file *f;
|
||||||
|
char *buf;
|
||||||
|
loff_t pos = 0;
|
||||||
|
bool is_simulated = false;
|
||||||
|
|
||||||
|
// Check if the temperature file exists. If it doesn't, simulate the
|
||||||
|
// temperature.
|
||||||
|
f = filp_open(TEMP_SENSOR_PATH, O_RDONLY, 0);
|
||||||
|
if (IS_ERR(f)) {
|
||||||
|
printk(KERN_INFO "temp_monitor: Temperature file not found\n");
|
||||||
|
is_simulated = true;
|
||||||
|
get_random_bytes(&temp, sizeof(temp));
|
||||||
|
// Simulate a temperature between 10 and 50 degrees
|
||||||
|
temp = (temp % 40) + 10;
|
||||||
|
} 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);
|
||||||
|
// Convert to degrees Celsius
|
||||||
|
temp /= 1000;
|
||||||
|
filp_close(f, NULL);
|
||||||
|
kfree(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to desired unit
|
||||||
|
if (strcmp(temp_unit, "F") == 0) {
|
||||||
|
temp = temp * 9 / 5 + 32;
|
||||||
|
// Convert to Fahrenheit
|
||||||
|
} else if (strcmp(temp_unit, "K") == 0) {
|
||||||
|
temp += 273;
|
||||||
|
// Convert to Kelvin
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(temp_str, BUF_LEN, "%d %s%s\n", temp, temp_unit,
|
||||||
|
is_simulated ? " (simulated temperature)" : "");
|
||||||
|
|
||||||
|
// Copy the result to user-space
|
||||||
|
if (copy_to_user(buffer, temp_str, strlen(temp_str) + 1)) {
|
||||||
|
return -EFAULT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return strlen(temp_str) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int device_release(struct inode *inode, struct file *file) {
|
static ssize_t device_write(struct file *file, const char __user *buffer,
|
||||||
return 0;
|
size_t len, loff_t *offset) {
|
||||||
|
char msg[BUF_LEN];
|
||||||
|
|
||||||
|
// Check if input too long
|
||||||
|
if (len > BUF_LEN - 1) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (copy_from_user(msg, buffer, len)) {
|
||||||
|
return -EFAULT;
|
||||||
|
}
|
||||||
|
msg[len] = '\0';
|
||||||
|
|
||||||
|
if (strcmp(msg, "simulate") == 0) {
|
||||||
|
simulate_temp = true;
|
||||||
|
} else {
|
||||||
|
simulate_temp = false;
|
||||||
|
strncpy(temp_unit, msg, BUF_LEN - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
printk(KERN_INFO "temp_monitor: Write operation: %s\n", msg);
|
||||||
|
|
||||||
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t device_read(struct file *file, char __user *buffer, size_t len, loff_t *offset) {
|
static long device_ioctl(struct file *file, unsigned int cmd,
|
||||||
|
unsigned long arg) {
|
||||||
|
switch (cmd) {
|
||||||
|
case IOCTL_GET_TEMP: {
|
||||||
int temp = 0;
|
int temp = 0;
|
||||||
char temp_str[BUF_LEN];
|
// Retrieve the temperature data
|
||||||
struct file *f;
|
if (copy_to_user((int *)arg, &temp, sizeof(temp))) {
|
||||||
char *buf;
|
return -EACCES;
|
||||||
loff_t pos = 0;
|
|
||||||
bool is_simulated = false;
|
|
||||||
|
|
||||||
// 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");
|
|
||||||
is_simulated = true;
|
|
||||||
get_random_bytes(&temp, sizeof(temp));
|
|
||||||
temp = (temp % 40) + 10; // Simulate a temperature between 10 and 50 degrees
|
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
// Convert to desired unit
|
}
|
||||||
if (strcmp(temp_unit, "F") == 0) {
|
default:
|
||||||
temp = temp * 9 / 5 + 32; // Convert to Fahrenheit
|
return -EINVAL;
|
||||||
} else if (strcmp(temp_unit, "K") == 0) {
|
}
|
||||||
temp += 273; // Convert to Kelvin
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(temp_str, BUF_LEN, "%d %s%s\n", temp, temp_unit, is_simulated ? " (simulated temperature)" : "");
|
|
||||||
|
|
||||||
// Copy the result to user-space
|
|
||||||
if (copy_to_user(buffer, temp_str, strlen(temp_str) + 1)) {
|
|
||||||
return -EFAULT;
|
|
||||||
}
|
|
||||||
|
|
||||||
return strlen(temp_str) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static ssize_t device_write(struct file *file, const char __user *buffer, size_t len, loff_t *offset) {
|
|
||||||
char msg[BUF_LEN];
|
|
||||||
|
|
||||||
if (len > BUF_LEN - 1) {
|
|
||||||
return -EINVAL; // Input too long
|
|
||||||
}
|
|
||||||
|
|
||||||
if (copy_from_user(msg, buffer, len)) {
|
|
||||||
return -EFAULT;
|
|
||||||
}
|
|
||||||
msg[len] = '\0'; // Null-terminate the string
|
|
||||||
|
|
||||||
if (strcmp(msg, "simulate") == 0) {
|
|
||||||
simulate_temp = true;
|
|
||||||
} else {
|
|
||||||
simulate_temp = false;
|
|
||||||
strncpy(temp_unit, msg, BUF_LEN - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printk(KERN_INFO "temp_monitor: Write operation: %s\n", msg);
|
|
||||||
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
|
|
||||||
switch (cmd) {
|
|
||||||
case IOCTL_GET_TEMP: {
|
|
||||||
int temp = 0;
|
|
||||||
// Retrieve the temperature data here
|
|
||||||
if (copy_to_user((int *)arg, &temp, sizeof(temp))) {
|
|
||||||
return -EACCES;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct file_operations fops = {
|
static struct file_operations fops = {
|
||||||
@ -126,39 +150,41 @@ static struct file_operations fops = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static int __init temp_monitor_init(void) {
|
static int __init temp_monitor_init(void) {
|
||||||
major_number = register_chrdev(0, DEVICE_NAME, &fops);
|
major_number = register_chrdev(0, DEVICE_NAME, &fops);
|
||||||
if (major_number < 0) {
|
if (major_number < 0) {
|
||||||
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(THIS_MODULE->name); // Updated this line
|
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");
|
||||||
return PTR_ERR(temp_monitor_class);
|
return PTR_ERR(temp_monitor_class);
|
||||||
}
|
}
|
||||||
temp_monitor_device = device_create(temp_monitor_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
|
temp_monitor_device = device_create(
|
||||||
if (IS_ERR(temp_monitor_device)) {
|
temp_monitor_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
|
||||||
class_destroy(temp_monitor_class);
|
if (IS_ERR(temp_monitor_device)) {
|
||||||
unregister_chrdev(major_number, DEVICE_NAME);
|
class_destroy(temp_monitor_class);
|
||||||
printk(KERN_ALERT "temp_monitor: Failed to create the device\n");
|
unregister_chrdev(major_number, DEVICE_NAME);
|
||||||
return PTR_ERR(temp_monitor_device);
|
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);
|
}
|
||||||
return 0;
|
printk(KERN_INFO "temp_monitor: Device initialized with major number %d\n",
|
||||||
|
major_number);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __exit temp_monitor_exit(void) {
|
static void __exit temp_monitor_exit(void) {
|
||||||
device_destroy(temp_monitor_class, MKDEV(major_number, 0));
|
device_destroy(temp_monitor_class, MKDEV(major_number, 0));
|
||||||
class_unregister(temp_monitor_class);
|
class_unregister(temp_monitor_class);
|
||||||
class_destroy(temp_monitor_class);
|
class_destroy(temp_monitor_class);
|
||||||
unregister_chrdev(major_number, DEVICE_NAME);
|
unregister_chrdev(major_number, DEVICE_NAME);
|
||||||
printk(KERN_INFO "temp_monitor: Device removed\n");
|
printk(KERN_INFO "temp_monitor: Device removed\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(temp_monitor_init);
|
module_init(temp_monitor_init);
|
||||||
module_exit(temp_monitor_exit);
|
module_exit(temp_monitor_exit);
|
||||||
|
|
||||||
MODULE_LICENSE("GPL");
|
MODULE_AUTHOR("Uzair Hamed Mohammed");
|
||||||
MODULE_AUTHOR("Your Name");
|
MODULE_DESCRIPTION(
|
||||||
MODULE_DESCRIPTION("A CPU Temperature Monitoring Kernel Module");
|
"An Awesome and Super Complex CPU Temperature Monitoring Kernel Module");
|
||||||
|
|||||||
@ -1,8 +1,23 @@
|
|||||||
#include <stdio.h>
|
/**************************************************************
|
||||||
|
* Class:: CSC-415-01 Fall 2024
|
||||||
|
* Name:: Uzair Hamed Mohammed
|
||||||
|
* Student ID:: 920142896
|
||||||
|
* GitHub-Name:: gamersekofy
|
||||||
|
* Project:: Assignment 6 - Device Driver
|
||||||
|
|
||||||
|
*
|
||||||
|
* File:: Mohammed_UzairHamed_HW6_main.c
|
||||||
|
*
|
||||||
|
* Description:: This file contains a very easy to use and application that
|
||||||
|
reads and writes data
|
||||||
|
* to the tempMonitor kernel module.
|
||||||
|
**************************************************************/
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <stdio.h>
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define IOCTL_GET_TEMP _IOR('t', 1, int)
|
#define IOCTL_GET_TEMP _IOR('t', 1, int)
|
||||||
|
|
||||||
@ -10,62 +25,67 @@
|
|||||||
#define RESET "\033[0m"
|
#define RESET "\033[0m"
|
||||||
|
|
||||||
void read_temp(int fd, const char *unit) {
|
void read_temp(int fd, const char *unit) {
|
||||||
char buffer[128];
|
char buffer[128];
|
||||||
ssize_t bytes_read;
|
ssize_t bytes_read;
|
||||||
int is_simulated = 0;
|
|
||||||
|
|
||||||
// Writing the desired unit or "simulate" command to the device
|
/* Declare this variable because /sys/class/thermal/thermal_zonex/ isn't
|
||||||
ssize_t bytes_written = write(fd, unit, strlen(unit));
|
available on virtual machines. In case of a virtual machine, set this flag
|
||||||
if (bytes_written < 0) {
|
that will make the kernel module return a simulated temperature. */
|
||||||
return; // Exit the function if writing fails
|
int is_simulated = 0;
|
||||||
|
|
||||||
|
// Writing the desired unit or "simulate" command to the device
|
||||||
|
ssize_t bytes_written = write(fd, unit, strlen(unit));
|
||||||
|
if (bytes_written < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reading the temperature
|
||||||
|
bytes_read = read(fd, buffer, sizeof(buffer));
|
||||||
|
if (bytes_read < 0) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
buffer[bytes_read] = '\0';
|
||||||
|
|
||||||
|
// Check if the temperature is simulated. If it is, set flag here.
|
||||||
|
if (strstr(buffer, "(simulated temperature)") != NULL) {
|
||||||
|
is_simulated = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reading the temperature
|
if (is_simulated) {
|
||||||
bytes_read = read(fd, buffer, sizeof(buffer));
|
printf(GREEN "CPU Temperature\n--- -----------\n" RESET "%s\n", buffer);
|
||||||
if (bytes_read < 0) {
|
|
||||||
return; // Exit the function if reading fails
|
|
||||||
} else {
|
} else {
|
||||||
buffer[bytes_read] = '\0'; // Null-terminate the string
|
printf(GREEN "CPU Temperature\n--- -----------\n" RESET "%s\n", buffer);
|
||||||
|
|
||||||
// Check if the temperature is simulated
|
|
||||||
if (strstr(buffer, "(simulated temperature)") != NULL) {
|
|
||||||
is_simulated = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_simulated) {
|
|
||||||
printf(GREEN "CPU Temperature\n--- -----------\n" RESET "%s\n", buffer);
|
|
||||||
} else {
|
|
||||||
printf(GREEN "CPU Temperature\n--- -----------\n" RESET "%s\n", buffer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int fd;
|
int fd;
|
||||||
char choice;
|
char choice;
|
||||||
|
|
||||||
fd = open("/dev/temp_monitor", O_RDWR);
|
// Access kernel module here
|
||||||
if (fd < 0) {
|
fd = open("/dev/temp_monitor", O_RDWR);
|
||||||
perror("Failed to open the device");
|
if (fd < 0) {
|
||||||
return -1;
|
perror("Failed to open the device");
|
||||||
}
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Ask the user for the preferred temperature unit
|
// Ask the user for the preferred temperature unit
|
||||||
printf("Choose temperature unit (C/F): ");
|
printf("Choose temperature unit (C/F): ");
|
||||||
scanf(" %c", &choice);
|
scanf(" %c", &choice);
|
||||||
|
|
||||||
if (choice == 'C' || choice == 'c') {
|
|
||||||
// Requesting the temperature in Celsius
|
|
||||||
read_temp(fd, "C");
|
|
||||||
} else if (choice == 'F' || choice == 'f') {
|
|
||||||
// Requesting the temperature in Fahrenheit
|
|
||||||
read_temp(fd, "F");
|
|
||||||
} else {
|
|
||||||
printf("Invalid choice. Please choose 'C' or 'F'.\n");
|
|
||||||
close(fd);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (choice == 'C' || choice == 'c') {
|
||||||
|
// Request the kernel module provide temperature in Celsius
|
||||||
|
read_temp(fd, "C");
|
||||||
|
} else if (choice == 'F' || choice == 'f') {
|
||||||
|
// Request the kernel module provide the temperature in Fahrenheit
|
||||||
|
read_temp(fd, "F");
|
||||||
|
} else {
|
||||||
|
printf("Invalid choice. Please choose 'C' or 'F'.\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
return 0;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user