【问题标题】:How can I return something else than 0 from a FreeBSD system call如何从 FreeBSD 系统调用返回 0 以外的值
【发布时间】:2011-12-22 13:06:47
【问题描述】:

我已经编写了一个系统调用,它在我之前添加的 td_sched 中设置了一个变量

#include <sys/param.h>
#include <sys/proc.h>
#include <sys/module.h>
#include <sys/sysproto.h>
#include <sys/sysent.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/sched.h>
#include <sys/lock.h>
#include <sys/mutex.h>


struct set_proc_args{
    pid_t pid;
    struct timeval WCET;
    struct timeval deadline;
};

static int set_process_slack(struct thread *tda ,struct set_proc_args * arg){

    struct proc * process = pfind(arg->pid);
    struct thread* td = FIRST_THREAD_IN_PROC(process);

    if(process == NULL)
    {
        tda->td_retval[0] = -1;
        return -1;
    }


    if(td == NULL)
    {
        tda->td_retval[0] = -1;
        return -1;
    }
    PROC_LOCK_ASSERT(process, MA_OWNED);
    td->td_sched->WCET = (1000000 * arg->WCET.tv_sec + arg->WCET.tv_usec);
    td->td_sched->deadline =(uint64_t)( 1000000 * arg->deadline.tv_sec+arg->deadline.tv_usec);
    td->td_sched->slack_mode = 1;
    PROC_UNLOCK(process);
    return 0;
}

所以我想在没有找到具有此 ID 的进程时返回 -1。 我已经测试并看到找到进程时代码正在运行 但如果没有找到 FreeBSD 重新启动 问题出在哪里? 其实我不知道如何正确返回-1。

【问题讨论】:

    标签: kernel return-value freebsd system-calls


    【解决方案1】:

    我愿意打赌我的血汗钱是因为这个:

    struct proc * process = pfind(arg->pid);
    struct thread* td = FIRST_THREAD_IN_PROC(process);
    if(process == NULL) {
        tda->td_retval[0] = -1;
        return -1;
    }
    

    在不存在此类进程的情况下,pfind 将根据manpage 返回 NULL:

    pfind() and zpfind() return a pointer to a proc structure on success and a NULL on failure.

    FIRST_THREAD_IN_PROC 函数或宏几乎肯定会尝试取消对 process 的引用以找到它的第一个线程。

    因为process 为NULL,取消引用将导致核心转储。或者,更准确地说,如果您只是作为内核可以丢弃的正常进程运行,它导致核心转储。

    这是在系统调用中的事实要严重得多,因此需要重新启动。您必须在内核级代码中比用户级代码更没有错误。

    尝试重新排列上面的代码,以便在尝试使用它之前检查process 的NULL 值,例如:

    struct proc * process = pfind(arg->pid);
    struct thread* td;
    if(process == NULL) {
        tda->td_retval[0] = -1;
        return -1;
    }
    td = FIRST_THREAD_IN_PROC(process);
    

    【讨论】:

      猜你喜欢
      • 2012-09-14
      • 1970-01-01
      • 2012-02-23
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 2012-10-07
      • 2012-03-10
      相关资源
      最近更新 更多