Skip to content
Advertisement

Problem doing a system call, the system call is not displaying to kernel

In Ubuntu, I wrote a new system call:

SYSCALL_DEFINE1(print_other, pid_t, targetpid)
{
    struct task_struct *p;

    int found = 0;

    for(p = &init_task; next_task(p) != &init_task; p=next_task(p))
    {
        if(p->pid == targetpid)
        {
            found = 1;
            break;
        }
    }

    if (found)
    {
            for(p = current; p != &init_task; p = p->parent)
            {
                    printk("Task:n");
                    printk("Process ID: %dn", p->pid);
                printk("Running state: %ldn", p->state);
                    printk("Program name: %sn", p->comm);
                printk("Start time: %llun", p->start_time);
                    printk("Virtual runtime: %llunn", p->se.vruntime);
            }

    }

    else
    {
        printk("Your process was not found");
    }

    return 0;
}

This is my testing file:

#include <linux/unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define __NR_print_other 337

int main(int argc, char *argv[])
{
    char search[10];
    char *error;
    pid_t in_pid;
    unsigned long pid;

    while (true)
    {
        printf("Enter PID to search: ");
        scanf("%s", search);
        printf("passed scanfn");
        pid = strtoul(search, &error, 10);
        printf("passed strtouln");

        if (*error || error == argv[1] || ((pid_t)pid != pid ||
                (pid_t)pid <= 0))
        {
            printf("in if statementn");
            printf("nError: Invalid PID enteredn");
            printf("Try againn");
        }

        else
        {
            printf("in else statementn");
            in_pid = pid;
            syscall(__NR_print_other, in_pid);
            printf("about to return, in_pid = %dn", in_pid);

            return 0;
        }
    }
}

But the testing file is good. The system call is not doing anything and I cannot see why. What am I supposed to do that I am doing wrong?

I don’t really have anything left to find. I checked the testing file and it runs properly. It returns that the in_pid is correct and runs the error bounds checks properly. There must be a logical error in the system call but I don’t see what the problem would be.

Advertisement

Answer

Your system call is working and doing something. Just run dmesg and you should see something similar to this:

[ 3755.306897] Task:
[ 3755.306898] Process ID: 1
[ 3755.306899] Running state: 1
[ 3755.306900] Program name: systemd
[ 3755.306902] Start time: 371331827
[ 3755.306903] Virtual runtime: 1757840935
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement