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

來源:互聯網
上載者:User

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

[CODE]
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/sched.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("mq110");

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

{
    static char buf[1024*8]={0};
    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)   //遍例核心進程鏈表.
    {
        sprintf(tmp,"%d\t\t%d\t\t\t%s\n",p->pid,p->parent->pid,p->comm);
        strcat(buf,tmp);
        memset(tmp,0,sizeof(tmp));
    }
    read_unlock(&tasklist_lock);
    *start=buf;
    return strlen(buf);
}

static __init int ps_init(void)
{
    struct proc_dir_entry *entry;

    entry = create_proc_entry("_ps", 0444, &proc_root);  //建立/proc/_ps檔案.
    if(entry == 0)
    {
        printk(KERN_ERR "create_proc_entry failed!\n");
        return -1;
    }
    entry->mode = S_IFREG | 0444;
    entry->size = 0;
    entry->read_proc = ps_read;
    return 0;
}
static __exit void ps_cleanup(void)
{
    remove_proc_entry("_ps", &proc_root);
}

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 <stdio.h>
#include <error.h>

int main()
{
    FILE *fp;
    char buf[1024];

    fp=fopen("/proc/_ps","r");
    if(fp==NULL)
    {
        perror("fopen");
        return -1;
    }
    printf("pid\t\tppid\t\t\tcommand\n");
    while(!feof(fp))
    {
        if(fgets(buf,sizeof(buf),fp)!=NULL)
            printf("%s",buf);
    }
    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

來源:http://www.chinaunix.net/old_jh/4/653764.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.