Quantcast
Channel: Active questions tagged kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 6335

SystemCall to process and show a table of PID's, names and their states

$
0
0

So i have an assignment that I am having some difficulty wrapping my head around. The assignment is to create a system call called "mysyscall3" that takes no input and produces

./a.outPID COMM    STATE1   systemd INT2   kthreadd    INT3   rcu_gp  OTHER4   rcu_par_gp  OTHER6   kworker/0:0H    OTHER7   kworker/u4:0    OTHER8   mm_percpu_wq    OTHER9   ksoftirqd/0 INT10  rcu_sched   OTHER11  migration/0 INT13  cpuhp/0 INT14  cpuhp/1 INT15  migration/1 INT16  ksoftirqd/1 INT18  kworker/1:0H    OTHER20  kdevtmpfs   INT21  netns   OTHER22  kauditd INT24  khungtaskd  INT25  oom_reaper  INT26  writeback   OTHER27  kcompactd0  INT28  ksmd    INT29  khugepaged  INT49  cryptd  OTHER126 kintegrityd OTHER127 kblockd OTHER128 blkcg_punt_bio  OTHER129 tpm_dev_wq  OTHER130 md  OTHER131 edac-poller OTHER132 watchdogd   INT158 kswapd0 INT161 kthrotld    OTHER162 acpi_thermal_pm OTHER163 kmpath_rdacd    OTHER164 kaluad  OTHER166 ipv6_addrconf   OTHER226 zswap-shrink    OTHER242 kworker/u5:0    OTHER456 nvme-wq OTHER461 nvme-reset-wq   OTHER464 nvme-delete-wq  OTHER484 ena OTHER507 xfsalloc    OTHER513 xfs_mru_cache   OTHER517 xfs-buf/nvme0n1 OTHER518 xfs-conv/nvme0n OTHER519 xfs-cil/nvme0n1 OTHER520 xfs-reclaim/nvm OTHER521 xfs-eofblocks/n OTHER522 xfs-log/nvme0n1 OTHER523 xfsaild/nvme0n1 INT524 kworker/0:1H    OTHER609 systemd-journal INT639 systemd-udevd   INT657 auditd  INT701 dbus-daemon INT702 irqbalance  INT705 rngd    INT709 chronyd INT710 polkitd INT721 sssd    INT746 kworker/1:1H    OTHER756 sssd_be INT765 sssd_nss    INT775 systemd-logind  INT858 NetworkManager  INT867 tuned   INT1010    systemd-resolve INT1063    rsyslogd    INT1072    sshd    INT1074    agetty  INT1075    crond   INT1076    agetty  INT1188    kworker/0:2 OTHER1535    kworker/0:3 OTHER1546    sshd    INT1550    systemd INT1554    (sd-pam)    INT1560    sshd    INT1561    bash    INT1691    kworker/u4:1    OTHER1734    kworker/1:1 OTHER1746    kworker/1:2 OTHER1748    a.out   RUNNING86 PIDS 1 RUNNING 43 INT 0 UNINT 0 STOPPED 0 TRACED 42 OTHER

The following is the test code that will be used to test out our system call

    /* test.c  * * test syscall3 442 * * Compile: *     gcc test.c * Run: *     ./a.out */#define _GNU_SOURCE#include <unistd.h>#include <sys/syscall.h>#include <stdio.h>#define SYS_mysyscall3 442/* test.c * * gcc test.c  # compile * * Use dmesg to verify mysyscall3 was received by the kernel, i.e. *  * dmesg | grep mysyscall3 * */#define _GNU_SOURCE#include <unistd.h>#include <sys/syscall.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#define SYS_mysyscall3 442int main(int argc, char **argv){    int maxp;    long ret;    char *buf;    buf = (char*) malloc(512*2048);    if ( buf == NULL ) {        fprintf(stderr, "Error malloc buf\n");        return 1;    }    if (argc != 1) {        printf("mysyscall3 requires no input.\n");        return -1;    }    ret = syscall(SYS_mysyscall3, buf);    if ( ret ) { // non-zero return value is an error        fprintf(stderr, "Error: mysyscall3 returned %ld.\n", ret);        return 1;    }    printf("%s\n", buf);    free(buf);    return 0;}

And below is what i have so far in my /kernel/sys.c file

SYSCALL_DEFINE(mysyscall3) {        struct task_struct *task;        int numPid = 0;        char *buf;        buf = (char *)kmalloc(*buf,GFP_KERNEL);        if (buf == NULL) {                printk(KERN_INFO "mysyscall3 kmalloc\n");                return -EFAULT;        }        for_each_process(task) {                buf = ("%d\t%s\t%ld\n", task->pid,task->comm,task->state);                numPid++;        }        pr_info("%d PIDS\n",numPid);        return 0;}

So i want to make sure of a few things... since in the test code (2nd snippet) my professor is passing the buf parameter to the system call would it be correct to assume that my actual definition should be SYSCALL_DEFINE1?

Also, i have made the appropriate changes to my systables_64 file to add the system call , and yet on a fresh kernel rebuild and restart if i run my test code nothing is printed to the screen. Should i be using something like copy_to_user() , and if so, should i do that in the for_each_process(3rd code document). I would like to just get something printed to my screen but thats where i keep getting stuck, maybe someone could point me in the right direction ?


Viewing all articles
Browse latest Browse all 6335

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>