I think a process count of 0 in procstat_getprocs() is not necessarily an error and this should not trigger a message to stderr. The code below demonstrates the issue, assuming 1000 is not part of a process group. Tweak the value if necessary. If procstat_getprocs() does not find any processes in the group, it returns 0 via the *count argument as expected, but also prints a warning to stderr using warnx(): kinfo_proc structure size mismatch (len = 0) The warnx() call can be found in /usr/src/lib/libprocstat/libprocstat.c. It seems to be that the action to be taken when count == 0 should be left to the caller. #include <stdio.h> #include <sysexits.h> #include <sys/param.h> #include <sys/queue.h> #include <sys/socket.h> #include <sys/user.h> #include <sys/sysctl.h> #include <libprocstat.h> int main(int argc,char *argv[]) { unsigned pgid = 1000, pid_count; struct procstat *proc_info; struct kinfo_proc *proc_list; // FILE *procstat_err; proc_info = procstat_open_sysctl(); // Silence warnx() // procstat_err = fopen("/dev/null", "w+"); // err_set_file(procstat_err); proc_list = procstat_getprocs(proc_info, KERN_PROC_PGRP, pgid, (unsigned *)&pid_count); // fclose(procstat_err); procstat_freeprocs(proc_info, proc_list); procstat_close(proc_info); return EX_OK; }
Another issue with this is that pid_count is not set to 0 when no matching processes are found. I think this would be good to do even if the case is considered an error.