寫了乙個核心模組 實現簡單的類似ps命令

2021-06-16 16:13:47 字數 4933 閱讀 4642

核心模組建立在proc檔案系統上建立_ps檔案.遍例程序核心鍊錶task_struct.將遍例結果存入緩衝區.影射到/proc/_ps檔案中.使用者態的程式去讀取 這個檔案.列印顯示 當前程序的的pid,ppid 和程序名.

[code]

#include

#include

#include

#include

#include

module_license("gpl");

module_author("mq110");

static int ps_read(char *page, char **start, off_t offset,int count, int *eof, void *data)  

; char tmp[128];

struct task_struct *p;

if(offset >0)

return 0;

memset(buf,0,sizeof(buf));

read_lock(&tasklist_lock);

for_each_process(p)   //遍例核心程序鍊錶.

read_unlock(&tasklist_lock);

*start=buf;

return strlen(buf);

} static __init int ps_init(void)

entry->mode = s_ifreg | 0444;

entry->size = 0;

entry->read_proc = ps_read;

return 0;

} static __exit void ps_cleanup(void)

module_init(ps_init);

module_exit(ps_cleanup);

[/code]

以下是makefile. target改名成程式名就ok了.

[code]

target = 006

obj-m := $(target).o

kerneldir=/lib/modules/`uname -r`/build

pwd=`pwd`

default :

$(make) -c $(kerneldir) m=$(pwd) modules

install :

insmod $(target).ko

uninstall :

rmmod $(target).ko

clean :

rm -rf *.o *.mod.c *.ko

[/code]

以下是使用者態程式:

[code]

#include

#include

int main()

printf("pid\t\tppid\t\t\tcommand\n");

while(!feof(fp))

fclose(fp);

return 0;

} [/code]

make ;make install 之後 編譯使用者態程式.

執行結果:

[root@firewall 006]# ./likeps

pid             ppid                    command

1               0                       init

2               1                       migration/0

3               1                       ksoftirqd/0

4               1                       watchdog/0

5               1                       migration/1

6               1                       ksoftirqd/1

7               1                       watchdog/1

8               1                       migration/2

9               1                       ksoftirqd/2

10              1                       watchdog/2

11              1                       migration/3

12              1                       ksoftirqd/3

13              1                       watchdog/3

14              1                       events/0

15              1                       events/1

16              1                       events/2

17              1                       events/3

18              1                       khelper

19              1                       kthread

24              19                      kacpid

100             19                      kblockd/0

101             19                      kblockd/1

102             19                      kblockd/2

103             19                      kblockd/3

106             19                      khubd

197             19                      pdflush

198             19                      pdflush

199             1                       kswapd0

200             19                      aio/0

201             19                      aio/1

202             19                      aio/2

203             19                      aio/3

288             19                      kseriod

392             1                       kjournald

1224            1                       udevd

1941            1                       kjournald

1944            1                       kjournald

1967            1                       kjournald

2820            1                       syslogd

2824            1                       klogd

2840            1                       sshd

2852            1                       vsftpd

2862            1                       gpm

2882            1                       atd

3184            1                       mingetty

3185            1                       mingetty

3186            1                       mingetty

3187            1                       mingetty

3188            1                       mingetty

3189            1                       mingetty

6819            1                       mysqld_safe

6846            6819                    mysqld

7475            1                       smbd

7476            7475                    smbd

7480            1                       nmbd

19400           2840                    sshd

19404           19400                   bash

乙個最簡單的Linux核心模組

include include static int init hello init void static void exit hello exit module init hello init module exit hello exit module license gpl 這個最簡單的核心模...

編譯乙個核心模組

1,編寫hello.c include include module license gpl static int init hello init void static void exit hello exit void module init hello init module exit hel...

簡單的寫了乙個列印Lua Table的函式

簡單的寫了乙個將lua table 列印成json的格式,當然這個函式還是有點bug的,就是不能區分 同時存在陣列和key vlaue,如 local test 會被當做陣列處理,由於專案中沒有匯入json庫,不能使用json.encode 所以自己隨便了乙個,基本上滿足需求,後面也就沒有管了,如果...