Untitled
unknown
plain_text
7 months ago
2.5 kB
1
Indexable
Never
#include <linux/module.h> #include <linux/kernel.h> #include <linux/proc_fs.h> #include <linux/sched/signal.h> #include <linux/sched.h> #include <linux/uaccess.h> static struct proc_dir_entry *proc_entry; static pid_t my_pid; static ssize_t mykpid_write(struct file *file, const char __user *buff, size_t len, loff_t *pos) { char cmd[10]; int ret; if (len > sizeof(cmd) - 1) return -EINVAL; if (raw_copy_from_user(cmd, buff, len)) return -EFAULT; cmd[len] = '\0'; if (strncmp(cmd, "INT", 3) == 0) { if (my_pid > 0) { struct pid *pid = find_get_pid(my_pid); if (pid) { struct task_struct *task = pid_task(pid, PIDTYPE_PID); if (task) { force_sig(SIGINT, task); ret = len; } else { printk(KERN_ERR "Failed to find task for PID %d\n", my_pid); ret = -ESRCH; } put_pid(pid); } else { printk(KERN_ERR "Failed to find PID %d\n", my_pid); ret = -ESRCH; } } else { printk(KERN_ERR "Invalid PID %d\n", my_pid); ret = -EINVAL; } } else { ret = kstrtoint(cmd, 10, &my_pid); if (ret) { printk(KERN_ERR "Failed to parse PID: %d\n", ret); ret = -EINVAL; } else { ret = len; } } return ret; } static ssize_t mykpid_read(struct file *filp, char __user *buff, size_t len, loff_t *pos) { char pid_str[20]; int pid_len = snprintf(pid_str, sizeof(pid_str), "%d\n", my_pid); if (*pos > 0 || len < pid_len) return 0; if (raw_copy_to_user(buff, pid_str, pid_len)) return -EFAULT; *pos += pid_len; return pid_len; } static const struct file_operations mykpid_fops = { .owner = THIS_MODULE, .read = mykpid_read, .write = mykpid_write, }; static int __init mykpid_init(void) { proc_entry = proc_create("mykpid", 0666, NULL, &mykpid_fops); if (!proc_entry) { printk(KERN_ERR "Failed to create /proc/mykpid entry\n"); return -ENOMEM; } return 0; } static void __exit mykpid_exit(void) { if (proc_entry) { remove_proc_entry("mykpid", NULL); proc_entry = NULL; } } module_init(mykpid_init); module_exit(mykpid_exit); MODULE_LICENSE("GPL");