在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是正确的,并正确运行错误界限检查。系统调用中一定有逻辑错误,但我不知道会出现什么问题。

最佳答案

您的系统调用正在工作并正在执行某些操作。只要运行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

关于c - 执行系统调用时出现问题,系统调用未显示给内核,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58016201/

10-12 05:31