32位linux内核2.6.38.2增加体系调用,并编写模块模仿ps指令显现进程信息ITeye - 亚美娱乐

32位linux内核2.6.38.2增加体系调用,并编写模块模仿ps指令显现进程信息ITeye

2019年02月18日08时20分16秒 | 作者: 水风 | 标签: 模块,调用,体系 | 浏览: 2814

remove_proc_entry(MODULE_NAME,NULL); printk(KERN_INFO "%s %s removed\n",MODULE_NAME,MODULE_VERSION); MODULE_DESCRIPTION("proc example"); MODULE_LICENSE("GPL"); module_init(hello_init); module_exit(hello_exit);  

5.2;编写模块的Makefile:
obj-m+=pro_example.o
 make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
 make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean

5.3; 加载模块sudo insmod pro_example.ko
  检查当时已加载模块信息:lsmod
  卸载模块sudo rmmod pro_example
  当加载完模块后,就可以检查/proc目录下是否生成了相应的文件,并运用cat指令检查文件内容,还可以运用dmesg检查后台打印信息,比方printk的输出,假如要看的更铲除一点,运用sudo dmesg -c铲除之前的信息
使命2;编写一个程序,调用自定义的体系调用:
5.4,运用syscall函数:
#include linux/unistd.h 
#include stdio.h 
int main()
printf("studentnum=%d\n", syscall(341,100));
return 0;

运转程序,输出成果studentnum=100,检查dmesg,有success;则体系调用增加成功,调用该体系调用正确;
5.5;给出我这两次试验的patch文件,不过由于我编程需求,我在proc_dir_entry中增加了一个成员struct proc_dir_entry *owner;
diff -ruNa linux-2.6.38.2.org/arch/x86/include/asm/unistd_32.h linux-2.6.38.2/arch/x86/include/asm/unistd_32.h
- linux-2.6.38.2.org/arch/x86/include/asm/unistd_32.h 2011-04-12 08:25:50.384313140 -0700
+++ linux-2.6.38.2/arch/x86/include/asm/unistd_32.h 2011-04-12 08:28:59.612312392 -0700
@@ -347,9 +347,11 @@
 #define __NR_fanotify_mark 339
 #define __NR_prlimit64 340
+#define __NR_mycall 341
 #ifdef __KERNEL__
-#define NR_syscalls 341
+#define NR_syscalls 342
 #define __ARCH_WANT_IPC_PARSE_VERSION
 #define __ARCH_WANT_OLD_READDIR
diff -ruNa linux-2.6.38.2.org/arch/x86/kernel/syscall_table_32.S linux-2.6.38.2/arch/x86/kernel/syscall_table_32.S
- linux-2.6.38.2.org/arch/x86/kernel/syscall_table_32.S 2011-04-12 08:25:50.580313137 -0700
+++ linux-2.6.38.2/arch/x86/kernel/syscall_table_32.S 2011-04-12 08:29:22.992312038 -0700
@@ -340,3 +340,4 @@
 .long sys_fanotify_init
 .long sys_fanotify_mark
 .long sys_prlimit64 /* 340 */
+ .long sys_mycall
diff -ruNa linux-2.6.38.2.org/include/linux/proc_fs.h linux-2.6.38.2/include/linux/proc_fs.h
- linux-2.6.38.2.org/include/linux/proc_fs.h 2011-04-12 08:25:47.060313136 -0700
+++ linux-2.6.38.2/include/linux/proc_fs.h 2011-04-12 08:38:31.888315451 -0700
@@ -49,6 +49,7 @@
 unsigned long count, void *data);
 struct proc_dir_entry {
+ struct proc_dir_entry *owner;
 unsigned int low_ino;
 unsigned short namelen;
 const char *name;
diff -ruNa linux-2.6.38.2.org/include/linux/sched.h linux-2.6.38.2/include/linux/sched.h
- linux-2.6.38.2.org/include/linux/sched.h 2011-04-12 08:25:47.680313137 -0700
+++ linux-2.6.38.2/include/linux/sched.h 2011-04-12 08:34:24.872313536 -0700
@@ -1191,6 +1191,7 @@
 struct task_struct {
+ unsigned int ctx;
 volatile long state; /* -1 unrunnable, 0 runnable, 0 stopped */
 void *stack;
 atomic_t usage;
diff -ruNa linux-2.6.38.2.org/kernel/fork.c linux-2.6.38.2/kernel/fork.c
- linux-2.6.38.2.org/kernel/fork.c 2011-04-12 08:25:47.780313137 -0700
+++ linux-2.6.38.2/kernel/fork.c 2011-04-12 08:35:05.188316355 -0700
@@ -1441,6 +1441,8 @@
 nr = task_pid_vnr(p);
+ p- ctx=0;
 if (clone_flags CLONE_PARENT_SETTID)
 put_user(nr, parent_tidptr);
diff -ruNa linux-2.6.38.2.org/kernel/sched.c linux-2.6.38.2/kernel/sched.c
- linux-2.6.38.2.org/kernel/sched.c 2011-04-12 08:25:47.756313137 -0700
+++ linux-2.6.38.2/kernel/sched.c 2011-04-12 08:33:38.564317495 -0700
@@ -3996,6 +3996,8 @@
 rq- curr = next;
 ++*switch_count;
+ next- ctx++;
 context_switch(rq, prev, next); /* unlocks the rq */
 * The context switch have flipped the stack from under us
diff -ruNa linux-2.6.38.2.org/kernel/sys.c linux-2.6.38.2/kernel/sys.c
- linux-2.6.38.2.org/kernel/sys.c 2011-04-12 08:25:47.744313137 -0700
+++ linux-2.6.38.2/kernel/sys.c 2011-04-12 08:28:19.656312264 -0700
@@ -1762,6 +1762,13 @@
 argv_free(info- argv);
+asmlinkage int sys_mycall(int num)
+ printk(KERN_INFO "success!");
+ return num;
 * orderly_poweroff - Trigger an orderly system poweroff
 * @force: force poweroff if command execution fails

,最终总结一下这两个试验:
1》模块编程,无须从头编译内核,必定要写Makefile,要有进口和出口函数;
2》体系调用,3个过程正确,且有必要从头编译内核

************关于,许多人在运用/proc/plist来列出当时进程信息的时分,或许呈现显现列表不全的问题,可运用下面的程序(首要运用了seq_file接口)来处理该问题:
#include linux/init.h 
#include linux/module.h 
#include linux/proc_fs.h //proc_fs 
#include linux/seq_file.h //seq_file 
#include linux/fs.h //struct file,struct inode 
#include linux/sched.h //next_task() 
MODULE_AUTHOR("vampire"); 
MODULE_LICENSE("GPL"); 
MODULE_DESCRIPTION("a test module utilise the seq_file mechanism"); 
static void *ps_seq_start(struct seq_file *s,loff_t *pos){ 
struct task_struct *task; 
seq_printf(s,"%s\t%s\t%s\t%s\t%s\t%s\n","pid","ppid","uid","gid","comm","ctx"); 
if(*pos 0) 
return NULL; 
else{ 
task=next_task(current); 
return task; 
static void *ps_seq_next(struct seq_file *s,void *v,loff_t *pos){ 
struct task_struct *task=(struct task_struct *)v; 
++*pos; 
if(task- pid current- pid){ 
return NULL; 
}else{ 
task=next_task(task); 
return task; 
static void ps_seq_stop(struct seq_file *s,void *v){} 
static int ps_seq_show(struct seq_file *s,void *v){ 
rwlock_t lock = RW_LOCK_UNLOCKED; 
struct task_struct *task=(struct task_struct *)v; 
read_lock( lock); 
seq_printf(s,"%d\t%d\t%d\t%d\t%s\t%d\n",task- pid,task- parent- pid,task- uid,task- gid,task- comm,task- ctx); 
read_unlock( lock); 
return 0; 
static struct seq_operations ps_seq_ops = { 
.start = ps_seq_start, 
.next = ps_seq_next, 
.stop = ps_seq_stop, 
.show = ps_seq_show 
static int ps_open(struct inode *inode,struct file *file){ 
return seq_open(file, ps_seq_ops); 
static struct file_operations ps_file_ops = { 
.owner = THIS_MODULE, 
.open = ps_open, 
.read = seq_read, 
.llseek = seq_lseek, 
.release= seq_release                          
			
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表亚美娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章

阅读排行

  • 1
  • 2
  • 3

    Ubuntu,Mac运用比较sohu

    体系,软件,文件
  • 4

    putty常用命令csdn

    文件,目录,运用
  • 5

    Linux网络装备tengxun

    装备,网络,网卡
  • 6

    Linux检查网卡流量环球

    运用,体系,指令
  • 7

    时刻同步huabian

    进程,问题,对方
  • 8
  • 9

    ubuntu一些常用软件装置mingxing

    发动,拼音输入,装置
  • 10

    linux shelldongfang

    指令,循环,条件