⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pid.c

📁 Linux内核编程模块的简单实现
💻 C
字号:
#include <linux/kernel.h>#include <linux/module.h>#include <linux/proc_fs.h>#include <linux/init.h>#include <asm/uaccess.h>#include <linux/sched.h>#define MAX_LENGTH 64static struct proc_dir_entry *pid_entry;static char pid_buffer[MAX_LENGTH];static unsigned long pid_buffer_size = 0;static char pid[MAX_LENGTH] = { "1" };module_param_string (pid, pid, MAX_LENGTH, 0644);MODULE_LICENSE ("GPL");static intpid_write (struct file *file,	   const char *buffer, unsigned long count, void *data){  struct task_struct *_process;  pid_buffer_size = count;  if (pid_buffer_size > MAX_LENGTH)    pid_buffer_size = MAX_LENGTH;  if (copy_from_user (pid_buffer, buffer, pid_buffer_size))    return -EFAULT;  for_each_process (_process)  {    char _tmp[MAX_LENGTH];    sprintf (_tmp, "%d\n", _process->pid);    if (!strcmp (_tmp, pid_buffer))      printk ("<1>  The EXE file is: %20s \n", _process->comm);  }  return pid_buffer_size;}static intpid_read (char *buffer,	  char **buffer_location,	  off_t offset, int buffer_length, int *eof, void *data){  if (offset > 0)    return 0;  memcpy (buffer, pid_buffer, pid_buffer_size);  return pid_buffer_size;}static int __initpid_init (void){  struct task_struct *_process;  pid_entry = create_proc_entry ("pid", 0666, &proc_root);  if (!pid_entry)    {      remove_proc_entry ("pid",&proc_root);      printk (KERN_ALERT " \"/proc/pid\" created failed  ^_^ \n");      return -ENOMEM;    }  pid_entry->read_proc = pid_read;  pid_entry->write_proc = pid_write;  pid_entry->owner = THIS_MODULE;  pid_entry->uid = 0;  pid_entry->gid = 0;  pid_entry->size = 0;  printk ("<1> create \"/proc/pid\" SUCCESSFULLY.\n");  for_each_process (_process)  {    char _temp[MAX_LENGTH];    sprintf (_temp, "%d", _process->pid);    if (strcmp (_temp, pid) == 0)      printk ("<1> EXE file is: %20s \n ", _process->comm);  }  return 0;}static voidpid_exit (void){  remove_proc_entry ("pid", &proc_root);  printk ("<1> Exit.\n");}module_init (pid_init);module_exit (pid_exit);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -