执行系统调用时出现问题,系统调用未显示给内核

时间:2019-09-19 17:28:01

标签: c linux system-calls

在Ubuntu中,我编写了一个新的系统调用:

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: %d\n", p->pid);
                printk("Running state: %ld\n", p->state);
                    printk("Program name: %s\n", p->comm);
                printk("Start time: %llu\n", p->start_time);
                    printk("Virtual runtime: %llu\n\n", p->se.vruntime);
            }

    }

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

    return 0;
}

这是我的测试文件:

#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 scanf\n");
        pid = strtoul(search, &error, 10);
        printf("passed strtoul\n");

        if (*error || error == argv[1] || ((pid_t)pid != pid ||
                (pid_t)pid <= 0))
        {
            printf("in if statement\n");
            printf("\nError: Invalid PID entered\n");
            printf("Try again\n");
        }

        else
        {
            printf("in else statement\n");
            in_pid = pid;
            syscall(__NR_print_other, in_pid);
            printf("about to return, in_pid = %d\n", in_pid);

            return 0;
        }
    }
}

但是测试文件很好。系统调用没有执行任何操作,我看不出原因。我该怎么做,我做错了?

我真的没有什么可找的。我检查了测试文件,它可以正常运行。它返回in_pid是正确的,并正确运行错误范围检查。系统调用中一定有一个逻辑错误,但是我看不出会有什么问题。

1 个答案:

答案 0 :(得分:0)

您的系统调用正在运行并且正在执行某些操作。只需运行dmesg,您应该会看到类似的内容:

[ 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