First Linux Module (Memo)

There seems no way to use kernel internal data and function outside the kernel itself and kernel modules. I have tried the for_each_process macro mentioned in Linux Kernel Development. Here is the code. All of them are retrieved from  the web.

C Code

#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>

MODULE_LICENSE("Dual BSD/GPL");

static int iter_init(void)
{
  printk(KERN_ALERT "for_each_process init\n");
  struct task_struct *task;
  for_each_process(task) {
    printk("%s [%d]\n", task->comm, task->pid);
  }
  return 0;
}

static void iter_exit(void)
{
  printk(KERN_ALERT "for_each_process exit\n");
}

module_init(iter_init);
module_exit(iter_exit);

 Makefile

obj-m += iter.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

 Place them in the same directory.Run "make" to build the module. Run "sudo insmod iter.ko" to register the module. Check "/var/log/kern.log". Run "sudo rmmod iter" to unregister the module.

 

你可能感兴趣的:(C++,c,linux,Web,C#)