0% found this document useful (0 votes)
12 views4 pages

Sheet4

Uploaded by

d7153096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Sheet4

Uploaded by

d7153096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

NAME : Ahmed Ala Mohammed Al-Abbasi

ID :20210080

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/proc_fs.h>

#include <linux/uaccess.h>

#include <linux/sched/signal.h>

#define PROCFS_MAX_SIZE 1024

#define PROCFS_NAME “pid”

Static char procfs_buffer[PROCFS_MAX_SIZE];

Static unsigned long procfs_buffer_size = 0;

Static struct proc_dir_entry* our_proc_file;

Static int pid_value;

Static ssize_t procfile_read(struct file* file, char* buffer, size_t length, loff_t* offset)

Int len;

Struct task_struct* task;

Char* state;

Char* command;

If (*offset > 0) {

Return 0;

Task = pid_task(find_vpid(pid_value), PIDTYPE_PID);


If (task == NULL) {

Return 0;

Switch (task->state) {

Case TASK_RUNNING:

State = “TASK_RUNNING”;

Break;

Case TASK_INTERRUPTIBLE:

State = “TASK_INTERRUPTIBLE”;

Break;

Case TASK_UNINTERRUPTIBLE:

State = “TASK_UNINTERRUPTIBLE”;

Break;

Case __TASK_STOPPED:

State = “__TASK_STOPPED”;

Break;

Case __TASK_TRACED:

State = “__TASK_TRACED”;

Break;

Case TASK_DEAD:

State = “TASK_DEAD”;

Break;

Default:

State = “UNKNOWN”;

Command = (char*)task->comm;
Len = sprintf(procfs_buffer, “command = [%s] pid = [%d] state = [%s]\n”, command, pid_value,
state);

If (len <= length) {

Copy_to_user(buffer, procfs_buffer, len);

*offset += len;

Return len;

} else {

Return -EFAULT;

Static ssize_t procfile_write(struct file* file, const char* buffer, size_t length, loff_t* offset)

Char* temp_buffer;

Temp_buffer = kmalloc(length, GFP_KERNEL);

If (temp_buffer == NULL) {

Return -ENOMEM;

If (copy_from_user(temp_buffer, buffer, length)) {

Kfree(temp_buffer);

Return -EFAULT;

Kstrtol(temp_buffer, 10, &pid_value);

Kfree(temp_buffer);
Return length;

Static const struct file_operations procfile_fops = {

.owner = THIS_MODULE,

.read = procfile_read,

.write = procfile_write,

};

Static int __init init_module(void)

Our_proc_file = proc_create(PROCFS_NAME, 0666, NULL, &procfile_fops);

If (our_proc_file == NULL) {

Return -ENOMEM;

Return 0;

Static void __exit cleanup_module(void)

Remove_proc_entry(PROCFS_NAME, NULL);

Module_init(init_module);

Module_exit(cleanup_module);

You might also like