往期内容:
(一)Linux内核输出Helloworld
上期内容:
(二)命令行输入模块变量&分模块编写
下期内容:
(四)判断读入进程的合理性并打印进程状态及关系
课程任务:开发一个内核模块或组件,完成如下功能:
- 读取
∼/targets
文件。格式如下,
pid: 100, 234
prog: program, another program
file: a filename; another filename
- 记录
pid
或prog
进程访问了哪些文件和 IP 地址,记录file
文件被哪些进程所访问。记录内容至少包括进程 pid 和程序名、日期时间、访问模式。 - 当进程或文件数量不大于 5 个,展示它们的关系,例如,进程之间的父子关系,某个文件被哪些进程并发访问。
- 支持记录最多 20 个进程,给出进程分别为 5,10,20 个情况下的模块性能,包括 CPU 和内存使用情况。
第四周 判断进程关系+打印进程树
本周主要认识是实现了给定pid
序列判断其中进程关系和打印Linux进程树两个功能,具体介绍如下。
一、给定pid序列判断进程关系
1.函数说明
功能写在process_relations
模块中,该模块的主函数声明如下。其中,pid_list 是一个整型数组,表示需要查询的进程 PID;pid_count 表示 pid_list 中进程的数量。
void process_relationship(int *pid_list, int pid_count)
2. 实现过程
该内核模块的实现过程如下:
- 遍历 pid_list 中的每个进程。
- 对于每个进程,查询其父进程和子进程,以及兄弟进程。
- 在查询过程中,将父进程、子进程和兄弟进程的pid和pid_list中的值进行对照,如果有相等的,则说明这样的关系存在于pid_list中的两个进程之间,将这种关系输出。
3. 具体代码
代码如下。在实现过程中,通过pid_task()函数将pid转为进程实体(为task_struck类)进行操作。
void process_relationship(int *pid_list, int pid_count)
{
int i, j;
// 遍历 pid_list 中的进程
for (i = 0; i < pid_count; i++) {
struct task_struct *task = pid_task(find_vpid(pid_list[i]), PIDTYPE_PID);
if (task == NULL) {
printk(KERN_INFO "Process with PID %d not found\n", pid_list[i]);
continue;
}
// 输出进程的父子关系
if (task->real_parent != NULL) {
for (j = 0; j < pid_count; j++) {
// Judge if the process in pid_list
struct task_struct *another_task = pid_task(find_vpid(pid_list[j]), PIDTYPE_PID);
if (task->real_parent->pid == another_task->pid){
printk(KERN_INFO "Process with PID %d has parent with PID %d\n", task->pid, task->real_parent->pid);
break;
}
}
}
if (!list_empty(&task->children)) {
struct task_struct *child;
list_for_each_entry(child, &task->children, sibling) {
// Judge if the process in pid_list
for (j = 0; j < pid_count; j++){
struct task_struct *another_task = pid_task(find_vpid(pid_list[j]), PIDTYPE_PID);
if (child->pid == another_task->pid){
printk(KERN_INFO "Process with PID %d has child with PID %d\n", task->pid, child->pid);
break;
}
}
}
}
// 输出进程之间的兄弟关系
if (i > 0) {
for (j = 0; j < i; j++) {
struct task_struct *sibling_task = pid_task(find_vpid(pid_list[j]), PIDTYPE_PID);
if (sibling_task != NULL && sibling_task->real_parent == task->real_parent) {
printk(KERN_INFO "Process with PID %d and process with PID %d are siblings\n", task->pid, sibling_task->pid);
}
}
}
}
}
4. 测试用例
int pid_list[] = {1, 2, 3, 4}; // 需要查询的进程 PID
int pid_count = 4; // 进程数量
调用方法为在命令行中加载和卸载模块并查看日志。
usr@user1:~/develop/kernel/Ptree$ sudo insmod process_relations.ko
usr@user1:~/develop/kernel/Ptree$ sudo rmmod process_relations
usr@user1:~/develop/kernel/Ptree$ sudo dmesg
5. 输出结果
由下列结果可知四个节点之间的关系为:
- 1和2互为兄弟进程;
- 3和4为2的子进程;
- 3和4互为兄弟进城。
[ 4790.512217] Process with PID 2 has child with PID 3
[ 4790.512218] Process with PID 2 has child with PID 4
[ 4790.512248] Process with PID 2 and process with PID 1 are siblings
[ 4790.512250] Process with PID 3 has parent with PID 2
[ 4790.512250] Process with PID 4 has parent with PID 2
[ 4790.512251] Process with PID 4 and process with PID 3 are siblings
二、打印Linux进程树
1. 函数说明
该代码主要是实现了打印以当前进程为根节点的Linux进程树。
2. 实现过程
该内核模块的实现过程如下:
- 获取当前进程的所有子进程;
- 利用深度优先遍历这些子进程和它们的子进程;
- 在遍历过程中按照代际关系输出在日志中。
3. 具体代码
递归函数如下。
void print_pstree(struct task_struct* task, int indent) {
struct task_struct* child;
// Every Generation add a "| "
char buf[100]={0};
int i;
for (i = 0; i < indent; i++) {
sprintf(buf + strlen(buf), "| ");
}
printk(KERN_INFO "%s|-- %s\n", buf, task->comm);
list_for_each_entry(child, &task->children, sibling) {
print_pstree(child, indent + 1);
}
}
在初始化函数中调用,测试用例是当前进程的子进程,只需要修改task
就可以打印其他的进程树。
for (task = current; task != &init_task; task = task->parent) {
;
}
print_pstree(task, 0);
4. 测试方法
调用方法为在命令行中加载和卸载模块并查看日志。
usr@user1:~/develop/kernel/Ptree$ sudo insmod process_tree.ko
usr@user1:~/develop/kernel/Ptree$ sudo rmmod process_tree.ko
usr@user1:~/develop/kernel/Ptree$ sudo dmesg
5. 输出结果
[ 4890.608975] ************* Hello, Linux Kernel *************
[ 4890.608980] |-- swapper/0
[ 4890.608982] | |-- systemd
[ 4890.608982] | | |-- systemd-journal
[ 4890.608983] | | |-- vmware-vmblock-
[ 4890.608984] | | |-- systemd-udevd
[ 4890.608985] | | |-- systemd-oomd
[ 4890.608986] | | |-- systemd-resolve
[ 4890.608986] | | |-- systemd-timesyn
[ 4890.608987] | | |-- VGAuthService
[ 4890.608987] | | |-- vmtoolsd
[ 4890.608988] | | |-- accounts-daemon
[ 4890.608989] | | |-- acpid
[ 4890.608990] | | |-- avahi-daemon
[ 4890.608991] | | | |-- avahi-daemon
[ 4890.608992] | | |-- bluetoothd
[ 4890.608992] | | |-- cron
[ 4890.608993] | | |-- dbus-daemon
[ 4890.608994] | | |-- NetworkManager
[ 4890.608994] | | |-- irqbalance
[ 4890.608995] | | |-- networkd-dispat
[ 4890.608996] | | |-- polkitd
[ 4890.608997] | | |-- power-profiles-
[ 4890.608997] | | |-- rsyslogd
[ 4890.608998] | | |-- snapd
[ 4890.608999] | | |-- switcheroo-cont
[ 4890.608999] | | |-- systemd-logind
[ 4890.609000] | | |-- udisksd
[ 4890.609001] | | |-- wpa_supplicant
[ 4890.609002] | | |-- ModemManager
[ 4890.609003] | | |-- cupsd
[ 4890.609003] | | | |-- dbus
[ 4890.609004] | | | |-- dbus
[ 4890.609005] | | |-- unattended-upgr
[ 4890.609006] | | |-- sshd
[ 4890.609006] | | |-- gdm3
[ 4890.609007] | | | |-- gdm-session-wor
[ 4890.609008] | | | | |-- gdm-wayland-ses
[ 4890.609009] | | | | | |-- gnome-session-b
[ 4890.609010] | | |-- cups-browsed
[ 4890.609011] | | |-- kerneloops
[ 4890.609011] | | |-- kerneloops
[ 4890.609012] | | |-- rtkit-daemon
[ 4890.609013] | | |-- upowerd
[ 4890.609014] | | |-- geoclue
[ 4890.609014] | | |-- packagekitd
[ 4890.609015] | | |-- colord
[ 4890.609016] | | |-- systemd
[ 4890.609016] | | | |-- (sd-pam)
[ 4890.609017] | | | |-- pipewire
[ 4890.609018] | | | |-- pipewire-media-
[ 4890.609018] | | | |-- pulseaudio
[ 4890.609019] | | | |-- dbus-daemon
[ 4890.609020] | | | |-- gvfsd
[ 4890.609021] | | | | |-- gvfsd-trash
[ 4890.609021] | | | |-- xdg-document-po
[ 4890.609022] | | | |-- gvfsd-fuse
[ 4890.609023] | | | |-- xdg-permission-
[ 4890.609024] | | | |-- tracker-miner-f
[ 4890.609024] | | | |-- gnome-session-c
[ 4890.609025] | | | |-- gvfs-udisks2-vo
[ 4890.609026] | | | |-- gnome-session-b
[ 4890.609026] | | | | |-- at-spi-bus-laun
[ 4890.609027] | | | | | |-- dbus-daemon
[ 4890.609028] | | | | |-- gsd-disk-utilit
[ 4890.609029] | | | | |-- evolution-alarm
[ 4890.609030] | | | | |-- update-notifier
[ 4890.609031] | | | |-- gvfs-gphoto2-vo
[ 4890.609031] | | | |-- gvfs-mtp-volume
[ 4890.609032] | | | |-- gvfs-afc-volume
[ 4890.609033] | | | |-- gvfs-goa-volume
[ 4890.609033] | | | |-- gnome-shell
[ 4890.609034] | | | | |-- Xwayland
[ 4890.609035] | | | | |-- gjs
[ 4890.609036] | | | |-- goa-daemon
[ 4890.609037] | | | |-- goa-identity-se
[ 4890.609037] | | | |-- evolution-sourc
[ 4890.609038] | | | |-- dconf-service
[ 4890.609039] | | | |-- evolution-calen
[ 4890.609060] | | | |-- gnome-shell-cal
[ 4890.609060] | | | |-- evolution-addre
[ 4890.609061] | | | |-- at-spi2-registr
[ 4890.609062] | | | |-- sh
[ 4890.609062] | | | | |-- ibus-daemon
[ 4890.609063] | | | | | |-- ibus-memconf
[ 4890.609064] | | | | | |-- ibus-extension-
[ 4890.609065] | | | | | |-- ibus-engine-sim
[ 4890.609066] | | | |-- gsd-a11y-settin
[ 4890.609066] | | | |-- gsd-color
[ 4890.609067] | | | |-- gsd-datetime
[ 4890.609068] | | | |-- gsd-housekeepin
[ 4890.609068] | | | |-- gsd-keyboard
[ 4890.609069] | | | |-- gsd-media-keys
[ 4890.609070] | | | |-- gsd-power
[ 4890.609070] | | | |-- gsd-print-notif
[ 4890.609071] | | | |-- gsd-rfkill
[ 4890.609072] | | | |-- gsd-screensaver
[ 4890.609072] | | | |-- gsd-sharing
[ 4890.609073] | | | |-- gsd-smartcard
[ 4890.609074] | | | |-- gsd-sound
[ 4890.609074] | | | |-- gsd-wacom
[ 4890.609075] | | | |-- ibus-portal
[ 4890.609076] | | | |-- vmtoolsd
[ 4890.609077] | | | |-- gjs
[ 4890.609077] | | | |-- snapd-desktop-i
[ 4890.609078] | | | | |-- snapd-desktop-i
[ 4890.609079] | | | |-- gsd-printer
[ 4890.609080] | | | |-- gvfsd-metadata
[ 4890.609080] | | | |-- xdg-desktop-por
[ 4890.609081] | | | |-- xdg-desktop-por
[ 4890.609082] | | | |-- gjs
[ 4890.609082] | | | |-- xdg-desktop-por
[ 4890.609083] | | | |-- gsd-xsettings
[ 4890.609084] | | | |-- ibus-x11
[ 4890.609084] | | | |-- nautilus
[ 4890.609085] | | | |-- gnome-terminal-
[ 4890.609085] | | | | |-- bash
[ 4890.609086] | | | | |-- bash
[ 4890.609087] | | | | | |-- sudo
[ 4890.609088] | | | | | | |-- sudo
[ 4890.609088] | | | | | | | |-- insmod
[ 4890.609089] | | |-- gnome-keyring-d
[ 4890.609090] | |-- kthreadd
[ 4890.609091] | | |-- rcu_gp
[ 4890.609091] | | |-- rcu_par_gp
[ 4890.609092] | | |-- slub_flushwq
[ 4890.609093] | | |-- netns
[ 4890.609093] | | |-- kworker/0:0H
[ 4890.609094] | | |-- mm_percpu_wq
[ 4890.609095] | | |-- rcu_tasks_kthre
[ 4890.609095] | | |-- rcu_tasks_rude_
[ 4890.609096] | | |-- rcu_tasks_trace
[ 4890.609097] | | |-- ksoftirqd/0
[ 4890.609097] | | |-- rcu_preempt
[ 4890.609098] | | |-- migration/0
[ 4890.609099] | | |-- idle_inject/0
[ 4890.609099] | | |-- cpuhp/0
[ 4890.609100] | | |-- cpuhp/1
[ 4890.609101] | | |-- idle_inject/1
[ 4890.609101] | | |-- migration/1
[ 4890.609102] | | |-- ksoftirqd/1
[ 4890.609103] | | |-- kworker/1:0H
[ 4890.609103] | | |-- kdevtmpfs
[ 4890.609104] | | |-- inet_frag_wq
[ 4890.609105] | | |-- kauditd
[ 4890.609105] | | |-- khungtaskd
[ 4890.609106] | | |-- oom_reaper
[ 4890.609107] | | |-- writeback
[ 4890.609107] | | |-- kcompactd0
[ 4890.609108] | | |-- ksmd
[ 4890.609109] | | |-- khugepaged
[ 4890.609109] | | |-- kintegrityd
[ 4890.609110] | | |-- kblockd
[ 4890.609111] | | |-- blkcg_punt_bio
[ 4890.609111] | | |-- tpm_dev_wq
[ 4890.609112] | | |-- ata_sff
[ 4890.609112] | | |-- md
[ 4890.609113] | | |-- edac-poller
[ 4890.609114] | | |-- devfreq_wq
[ 4890.609114] | | |-- watchdogd
[ 4890.609115] | | |-- kworker/1:1H
[ 4890.609116] | | |-- kswapd0
[ 4890.609116] | | |-- ecryptfs-kthrea
[ 4890.609117] | | |-- kthrotld
[ 4890.609118] | | |-- irq/24-pciehp
[ 4890.609119] | | |-- irq/25-pciehp
[ 4890.609119] | | |-- irq/26-pciehp
[ 4890.609120] | | |-- irq/27-pciehp
[ 4890.609120] | | |-- irq/28-pciehp
[ 4890.609121] | | |-- irq/29-pciehp
[ 4890.609122] | | |-- irq/30-pciehp
[ 4890.609123] | | |-- irq/31-pciehp
[ 4890.609123] | | |-- irq/32-pciehp
[ 4890.609124] | | |-- irq/33-pciehp
[ 4890.609124] | | |-- irq/34-pciehp
[ 4890.609125] | | |-- irq/35-pciehp
[ 4890.609126] | | |-- irq/36-pciehp
[ 4890.609126] | | |-- irq/37-pciehp
[ 4890.609127] | | |-- irq/38-pciehp
[ 4890.609128] | | |-- irq/39-pciehp
[ 4890.609128] | | |-- irq/40-pciehp
[ 4890.609129] | | |-- irq/41-pciehp
[ 4890.609130] | | |-- irq/42-pciehp
[ 4890.609130] | | |-- irq/43-pciehp
[ 4890.609131] | | |-- irq/44-pciehp
[ 4890.609131] | | |-- irq/45-pciehp
[ 4890.609132] | | |-- irq/46-pciehp
[ 4890.609133] | | |-- irq/47-pciehp
[ 4890.609133] | | |-- irq/48-pciehp
[ 4890.609134] | | |-- irq/49-pciehp
[ 4890.609135] | | |-- irq/50-pciehp
[ 4890.609135] | | |-- irq/51-pciehp
[ 4890.609136] | | |-- irq/52-pciehp
[ 4890.609136] | | |-- irq/53-pciehp
[ 4890.609137] | | |-- irq/54-pciehp
[ 4890.609138] | | |-- irq/55-pciehp
[ 4890.609138] | | |-- acpi_thermal_pm
[ 4890.609139] | | |-- xenbus_probe
[ 4890.609140] | | |-- scsi_eh_0
[ 4890.609140] | | |-- scsi_tmf_0
[ 4890.609141] | | |-- scsi_eh_1
[ 4890.609142] | | |-- scsi_tmf_1
[ 4890.609142] | | |-- vfio-irqfd-clea
[ 4890.609143] | | |-- mld
[ 4890.609144] | | |-- ipv6_addrconf
[ 4890.609144] | | |-- kstrp
[ 4890.609145] | | |-- zswap-shrink
[ 4890.609146] | | |-- kworker/u257:0
[ 4890.609146] | | |-- charger_manager
[ 4890.609147] | | |-- kworker/0:1H
[ 4890.609148] | | |-- mpt_poll_0
[ 4890.609148] | | |-- mpt/0
[ 4890.609149] | | |-- scsi_eh_2
[ 4890.609149] | | |-- scsi_tmf_2
[ 4890.609150] | | |-- scsi_eh_3
[ 4890.609151] | | |-- scsi_tmf_3
[ 4890.609151] | | |-- scsi_eh_4
[ 4890.609152] | | |-- scsi_tmf_4
[ 4890.609153] | | |-- scsi_eh_5
[ 4890.609153] | | |-- scsi_tmf_5
[ 4890.609154] | | |-- scsi_eh_6
[ 4890.609155] | | |-- scsi_tmf_6
[ 4890.609155] | | |-- scsi_eh_7
[ 4890.609156] | | |-- scsi_tmf_7
[ 4890.609157] | | |-- scsi_eh_8
[ 4890.609157] | | |-- scsi_tmf_8
[ 4890.609158] | | |-- scsi_eh_9
[ 4890.609159] | | |-- scsi_tmf_9
[ 4890.609159] | | |-- scsi_eh_10
[ 4890.609160] | | |-- scsi_tmf_10
[ 4890.609161] | | |-- scsi_eh_11
[ 4890.609162] | | |-- scsi_tmf_11
[ 4890.609162] | | |-- scsi_eh_12
[ 4890.609163] | | |-- scsi_tmf_12
[ 4890.609163] | | |-- scsi_eh_13
[ 4890.609164] | | |-- scsi_tmf_13
[ 4890.609165] | | |-- scsi_eh_14
[ 4890.609166] | | |-- scsi_tmf_14
[ 4890.609166] | | |-- scsi_eh_15
[ 4890.609167] | | |-- scsi_tmf_15
[ 4890.609168] | | |-- scsi_eh_16
[ 4890.609168] | | |-- scsi_tmf_16
[ 4890.609169] | | |-- scsi_eh_17
[ 4890.609169] | | |-- scsi_tmf_17
[ 4890.609170] | | |-- scsi_eh_18
[ 4890.609171] | | |-- scsi_tmf_18
[ 4890.609171] | | |-- scsi_eh_19
[ 4890.609172] | | |-- scsi_tmf_19
[ 4890.609173] | | |-- scsi_eh_20
[ 4890.609173] | | |-- scsi_tmf_20
[ 4890.609174] | | |-- scsi_eh_21
[ 4890.609175] | | |-- scsi_tmf_21
[ 4890.609175] | | |-- scsi_eh_22
[ 4890.609176] | | |-- scsi_tmf_22
[ 4890.609177] | | |-- scsi_eh_23
[ 4890.609177] | | |-- scsi_tmf_23
[ 4890.609178] | | |-- scsi_eh_24
[ 4890.609179] | | |-- scsi_tmf_24
[ 4890.609179] | | |-- scsi_eh_25
[ 4890.609180] | | |-- scsi_tmf_25
[ 4890.609181] | | |-- scsi_eh_26
[ 4890.609181] | | |-- scsi_tmf_26
[ 4890.609182] | | |-- scsi_eh_27
[ 4890.609183] | | |-- scsi_tmf_27
[ 4890.609183] | | |-- scsi_eh_28
[ 4890.609184] | | |-- scsi_tmf_28
[ 4890.609185] | | |-- scsi_eh_29
[ 4890.609185] | | |-- scsi_tmf_29
[ 4890.609186] | | |-- scsi_eh_30
[ 4890.609187] | | |-- scsi_tmf_30
[ 4890.609187] | | |-- scsi_eh_31
[ 4890.609188] | | |-- scsi_tmf_31
[ 4890.609189] | | |-- scsi_eh_32
[ 4890.609189] | | |-- scsi_tmf_32
[ 4890.609190] | | |-- jbd2/sda3-8
[ 4890.609191] | | |-- ext4-rsv-conver
[ 4890.609192] | | |-- ipmi-msghandler
[ 4890.609192] | | |-- irq/16-vmwgfx
[ 4890.609193] | | |-- card0-crtc0
[ 4890.609194] | | |-- card0-crtc1
[ 4890.609194] | | |-- card0-crtc2
[ 4890.609195] | | |-- card0-crtc3
[ 4890.609195] | | |-- card0-crtc4
[ 4890.609196] | | |-- card0-crtc5
[ 4890.609197] | | |-- card0-crtc6
[ 4890.609197] | | |-- card0-crtc7
[ 4890.609198] | | |-- kworker/u257:2
[ 4890.609199] | | |-- cryptd
[ 4890.609199] | | |-- kworker/u256:3
[ 4890.609200] | | |-- kworker/1:0
[ 4890.609201] | | |-- kworker/u256:1
[ 4890.609202] | | |-- kworker/0:1
[ 4890.609202] | | |-- kworker/1:3
[ 4890.609203] | | |-- kworker/0:0
[ 4890.609204] | | |-- kworker/u256:2
[ 4890.609205] | | |-- kworker/1:1
[ 4903.615497] ************* Hello, Linux Kernel *************