(chibi process): fix process-running? on OpenBSD, NetBSD and DragonFly

tl;dr process-running? would always return #f on OpenBSD and
NetBSD, and in the one-argument case it would always return #t
on DragonFly.

To get the process information from the process table on OpenBSD
and NetBSD, we need to pass 6 level names to sysctl instead of 4.
Passing the wrong number of level names to sysctl has caused it
to always fail, which in turn caused process-running? to always
return #f:

  (process-running? 1)                        =>  #f
  (process-running? (current-process-id))     =>  #f

and so on.

After the above fix, we also need to check the amount of data
actually filled in by sysctl.  It appears that on OpenBSD, NetBSD
and DragonFly, if the requested process doesn't exist then sysctl
will return with a return value of 0 and just not actually fill in
the given structure.  This caused process-running? to return #t
when no process with the given PID existed:

  (process-running? -1)                       =>  #t
  (process-running? <other nonexistent pid>)  =>  #t

and so on.

I have tested on OpenBSD, NetBSD, DragonFly BSD and FreeBSD, and
process-running? now behaves as expected on all of them.
This commit is contained in:
Kris Katterjohn 2019-08-30 13:05:32 -05:00
parent 0bfc31a1e5
commit 3da3f3cab3
2 changed files with 8 additions and 1 deletions

View file

@ -6,6 +6,7 @@
(test-begin "processes") (test-begin "processes")
(test #t (process-running? (current-process-id))) (test #t (process-running? (current-process-id)))
(test #t (process-running? (parent-process-id))) (test #t (process-running? (parent-process-id)))
(test #f (process-running? -1))
(test #f (signal-set-contains? (current-signal-mask) signal/alarm)) (test #f (signal-set-contains? (current-signal-mask) signal/alarm))
(test #t (signal-set? (make-signal-set))) (test #t (signal-set? (make-signal-set)))
(test #t (signal-set? (current-signal-mask))) (test #t (signal-set? (current-signal-mask)))

View file

@ -83,8 +83,14 @@ static sexp sexp_pid_cmdline (sexp ctx, int pid) {
int id = KERN_PROC; int id = KERN_PROC;
#endif #endif
size_t reslen = sizeof(res); size_t reslen = sizeof(res);
#if defined(__NetBSD__) || defined(__OpenBSD__)
int name[6] = {CTL_KERN, id, KERN_PROC_PID, pid, reslen, 1};
unsigned namelen = 6;
#else
int name[4] = {CTL_KERN, id, KERN_PROC_PID, pid}; int name[4] = {CTL_KERN, id, KERN_PROC_PID, pid};
if (sysctl(name, 4, &res, &reslen, NULL, 0) >= 0) { unsigned namelen = 4;
#endif
if (sysctl(name, namelen, &res, &reslen, NULL, 0) >= 0 && reslen > 0) {
#if defined(__APPLE__) #if defined(__APPLE__)
return sexp_c_string(ctx, res.kp_proc.p_comm, -1); return sexp_c_string(ctx, res.kp_proc.p_comm, -1);
#elif defined(__NetBSD__) || defined(__OpenBSD__) #elif defined(__NetBSD__) || defined(__OpenBSD__)