If you are interested in kernel, you should operate on it. Syscall is a good way.Today let us learn how to do it together.
first, do this command under /usr/src/linux-2.6.33.7/arch/x86/kernel.( linux-2.6.33.7 is my kernel edition. you can choose yours. ) sudo vim syscall_table_32.s. Then you can add your syscall option like following.
329 .long sys_signalfd4
330 .long sys_eventfd2
331 .long sys_epoll_create1
332 .long sys_dup3 /* 330 */
333 .long sys_pipe2
334 .long sys_inotify_init1
335 .long sys_preadv
336 .long sys_pwritev
337 .long sys_rt_tgsigqueueinfo /* 335 */
338 .long sys_perf_event_open
339 .long sys_recvmmsg
340 .long sys_sayhello /*338*/
Second, we should add syscall number to /usr/src/linux-2.6.33.7/arch/x86/include/asm/unistd_32.h like following.
1 #define __NR_eventfd 323
332 #define __NR_fallocate 324
333 #define __NR_timerfd_settime 325
334 #define __NR_timerfd_gettime 326
335 #define __NR_signalfd4 327
336 #define __NR_eventfd2 328
337 #define __NR_epoll_create1 329
338 #define __NR_dup3 330
339 #define __NR_pipe2 331
340 #define __NR_inotify_init1 332
341 #define __NR_preadv 333
342 #define __NR_pwritev 334
343 #define __NR_rt_tgsigqueueinfo 335
344 #define __NR_perf_event_open 336
345 #define __NR_recvmmsg 337
346 #define __NR_sayhello 338
Third, we should add our source code to kernel. We can add to any file if they are relative. We often add to /usr/src/linux-2.6.33.7/kernel/sys.c. Following is my code.
1659 /*my_syscall: print every process's infromation.
1660 Programmer name: Harry Wei
1661 */
1662 asmlinkage void sys_sayhello( void )
1663 {
1664 struct task_struct *task;
1665
1666 task = &init_task;
1667
1668 printk( KERN_INFO"call syshello\n" );
1669
1670 for_each_process( task )
1671 {
1672 printk( KERN_ALERT"%d\t%s\n", task->pid, task->comm );
1673 }
1674 }
Remember that you must include linux/sched.h.
Then, we should compile the kernel because system call is related to kernel.
We should care it is different from LKM.
At last, we should write our program to call the syscall. Mine is following.
//first sys_call function.
#include
#include
#include
#define __NR_sayhello 338
int main( void )
{
syscall( __NR_sayhello );
return( 0 );
}
If you run the program, you will see like
following by enter command dmesg in terminal.
[ 136.010108] call syshello
[ 136.010240] 1 init
[ 136.010540] 2 kthreadd
[ 136.010814] 3 migration/0
[ 136.011072] 4 ksoftirqd/0
[ 136.011327] 5 watchdog/0
[ 136.011579] 6 events/0
[ 136.011826] 7 cpuset
[ 136.015303] 8 khelper
[ 136.015610] 9 netns
[ 136.015877] 10 async/mgr
[ 136.018018] 11 pm
[ 136.018019] 12 sync_supers
[ 136.018019] 13 bdi-default
[ 136.018823] 14 kintegrityd/0
[ 136.019063] 15 kblockd/0
[ 136.019294] 16 kacpid
[ 136.019562] 17 kacpi_notify
[ 136.019800] 18 kacpi_hotplug
[ 136.020701] 19 ata/0
[ 136.020937] 20 ata_aux
[ 136.021152] 21 ksuspend_usbd
[ 136.021376] 22 khubd
[ 136.021588] 23 kseriod
[ 136.021834] 24 kmmcd
[ 136.024801] 26 khungtaskd
[ 136.025083] 27 kswapd0
[ 136.025308] 28 ksmd
[ 136.025530] 29 aio/0
[ 136.025754] 30 ecryptfs-kthrea
[ 136.025997] 31 crypto/0
[ 136.026235] 35 scsi_eh_0
[ 136.026521] 36 scsi_eh_1
[ 136.026757] 38 kstriped
[ 136.026988] 39 kmpathd/0
[ 136.027217] 40 kmpath_handlerd
[ 136.027458] 41 ksnapd
[ 136.027689] 42 kondemand/0
[ 136.027929] 43 kconservative/0
[ 136.028355] 45 jbd2/sda1-8
[ 136.028624] 46 ext4-dio-unwrit
[ 136.028864] 53 flush-8:0
[ 136.029124] 79 upstart-udev-br
[ 136.029374] 81 udevd
[ 136.029638] 202 udevd
[ 136.029960] 204 udevd
[ 136.031088] 229 kpsmoused
[ 136.031390] 343 jbd2/sda5-8
[ 136.031662] 344 ext4-dio-unwrit
[ 136.031915] 347 jbd2/sda7-8
[ 136.032318] 348 ext4-dio-unwrit
[ 136.032568] 364 rsyslogd
[ 136.032827] 371 dbus-daemon
[ 136.033082] 381 NetworkManager
[ 136.033321] 383 avahi-daemon
[ 136.033556] 385 avahi-daemon
[ 136.033816] 388 modem-manager
[ 136.034052] 420 wpa_supplicant
[ 136.034289] 425 dhclient
[ 136.034514] 487 recovery-menu
[ 136.034777] 695 netroot
[ 136.035036] 716 dhclient
[ 136.035268] 717 bash
[ 136.035493] 729 su
[ 136.035715] 732 console-kit-dae
[ 136.035956] 803 bash
[ 136.036356] 851 a.out
[ 223.206813] hrtimer: interrupt took 10389977 ns
[ 436.199844] call syshello
[ 436.201170] 1 init
[ 436.201201] 2 kthreadd
[ 436.201226] 3 migration/0
[ 436.201253] 4 ksoftirqd/0
[ 436.201278] 5 watchdog/0
[ 436.201303] 6 events/0
[ 436.201327] 7 cpuset
[ 436.201352] 8 khelper
[ 436.201377] 9 netns
[ 436.201401] 10 async/mgr
[ 436.201426] 11 pm
[ 436.201451] 12 sync_supers
[ 436.201477] 13 bdi-default
[ 436.201502] 14 kintegrityd/0
[ 436.201528] 15 kblockd/0
[ 436.201553] 16 kacpid
[ 436.201577] 17 kacpi_notify
[ 436.201603] 18 kacpi_hotplug
[ 436.201628] 19 ata/0
[ 436.201653] 20 ata_aux
[ 436.201678] 21 ksuspend_usbd
[ 436.201703] 22 khubd
[ 436.201727] 23 kseriod
[ 436.201752] 24 kmmcd
[ 436.201777] 26 khungtaskd
[ 436.201802] 27 kswapd0
[ 436.201827] 28 ksmd
[ 436.201850] 29 aio/0
[ 436.201873] 30 ecryptfs-kthrea
[ 436.201898] 31 crypto/0
[ 436.201921] 35 scsi_eh_0
[ 436.201945] 36 scsi_eh_1
[ 436.201969] 38 kstriped
[ 436.201992] 39 kmpathd/0
[ 436.202016] 40 kmpath_handlerd
[ 436.202189] 41 ksnapd
[ 436.202212] 42 kondemand/0
[ 436.202236] 43 kconservative/0
[ 436.202261] 45 jbd2/sda1-8
[ 436.202285] 46 ext4-dio-unwrit
[ 436.202309] 53 flush-8:0
[ 436.202333] 79 upstart-udev-br
[ 436.202357] 81 udevd
[ 436.202381] 202 udevd
[ 436.202404] 204 udevd
[ 436.202428] 229 kpsmoused
[ 436.202452] 343 jbd2/sda5-8
[ 436.202477] 344 ext4-dio-unwrit
[ 436.202503] 347 jbd2/sda7-8
[ 436.202526] 348 ext4-dio-unwrit
[ 436.202551] 364 rsyslogd
[ 436.202574] 371 dbus-daemon
[ 436.202599] 381 NetworkManager
[ 436.202623] 383 avahi-daemon
[ 436.202647] 385 avahi-daemon
[ 436.202671] 388 modem-manager
[ 436.202695] 420 wpa_supplicant
[ 436.202719] 425 dhclient
[ 436.202743] 487 recovery-menu
[ 436.202767] 695 netroot
[ 436.202791] 716 dhclient
[ 436.202815] 717 bash
[ 436.202838] 729 su
[ 436.202861] 732 console-kit-dae
[ 436.202885] 803 bash
[ 436.202909] 867 startx
[ 436.202932] 884 xinit
[ 436.202957] 885 Xorg
[ 436.202981] 889 ck-launch-sessi
[ 436.203005] 908 ibus-daemon
[ 436.203030] 920 ssh-agent
[ 436.203060] 921 ibus-gconf
[ 436.203086] 923 python
[ 436.203110] 925 ibus-x11
[ 436.203134] 935 dbus-launch
[ 436.203202] 938 dbus-daemon
[ 436.203227] 940 gconfd-2
[ 436.203251] 941 x-session-manag
[ 436.203275] 944 dbus-launch
[ 436.203299] 945 dbus-daemon
[ 436.203323] 946 ibus-engine-m17
[ 436.203348] 949 gconfd-2
[ 436.203372] 956 gnome-keyring-d
[ 436.203396] 959 gnome-settings-
[ 436.203421] 961 gvfsd
[ 436.203444] 966 gvfs-fuse-daemo
[ 436.203468] 970 polkit-gnome-au
[ 436.203493] 971 bluetooth-apple
[ 436.203517] 976 gnome-power-man
[ 436.203542] 977 gnome-panel
[ 436.203566] 978 metacity
[ 436.203589] 980 nautilus
[ 436.203613] 982 nm-applet
[ 436.203637] 985 pulseaudio
[ 436.203661] 987 rtkit-daemon
[ 436.203685] 994 polkitd
[ 436.203709] 1005 gconf-helper
[ 436.203733] 1009 upowerd
[ 436.203757] 1013 gvfsd-trash
[ 436.203781] 1032 bonobo-activati
[ 436.203805] 1053 wnck-applet
[ 436.203830] 1054 trashapplet
[ 436.203854] 1056 gvfs-gdu-volume
[ 436.203878] 1061 gnome-screensav
[ 436.203903] 1062 hald
[ 436.203926] 1064 udisks-daemon
[ 436.203950] 1065 hald-runner
[ 436.203975] 1067 udisks-daemon
[ 436.203995] 1084 gdu-notificatio
[ 436.209907] 1095 gvfs-afc-volume
[ 436.209939] 1098 gvfs-gphoto2-vo
[ 436.209964] 1100 gvfsd-burn
[ 436.210086] 1102 hald-addon-inpu
[ 436.210114] 1117 hald-addon-stor
[ 436.210139] 1123 hald-addon-stor
[ 436.210163] 1125 hald-addon-acpi
[ 436.210188] 1134 indicator-apple
[ 436.210213] 1138 clock-applet
[ 436.210237] 1139 indicator-apple
[ 436.210262] 1141 notification-ar
[ 436.210286] 1151 gvfsd-metadata
[ 436.210310] 1153 indicator-me-se
[ 436.210341] 1156 indicator-sound
[ 436.210366] 1158 indicator-sessi
[ 436.210390] 1162 indicator-messa
[ 436.210414] 1164 indicator-appli
[ 436.210439] 1166 gnome-terminal
[ 436.210463] 1167 gnome-pty-helpe
[ 436.210487] 1168 bash
[ 436.210511] 1178 python
[ 436.210534] 1181 evolution-alarm
[ 436.210559] 1262 update-notifier
[ 436.210583] 1266 system-service-
[ 436.210607] 1271 aptd
[ 436.210630] 1282 a.out
阅读(683) | 评论(0) | 转发(0) |