I have written a syscall that sets a variable in td_sched that I added earlier
#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;
}
So I want to return -1 when no process with this ID is found.
I’ve tested and saw that the code is working when the process is found
but if it’s not found FreeBSD reboots
Where is the problem?
Actually I don’t know how to return -1 correctly.
I’d be willing to bet my hard-earned money that it’s because of this:
In the case where no such process exists,
pfindwill have returned NULL as per the manpage:The
FIRST_THREAD_IN_PROCfunction or macro then almost certainly tries to dereferenceprocessto find the first thread for it.Because
processis NULL, the dereference will cause a core dump. Or, more correctly, it would cause a core dump if you were simply running as a normal process that the kernel could just toss out.The fact that this is in a syscall is far more serious, hence the reboot. You have to be far more bug-free in kernel-level code than user-level code.
Try to re-arrange that code above so that you check
processfor a NULL value before trying to use it, something like: