Bug 245318 - procstat_getprocs() noise on stderr
Summary: procstat_getprocs() noise on stderr
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 12.1-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-04-03 15:41 UTC by Jason W. Bacon
Modified: 2020-04-10 13:21 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jason W. Bacon freebsd_committer freebsd_triage 2020-04-03 15:41:23 UTC
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;
}
Comment 1 Jason W. Bacon freebsd_committer freebsd_triage 2020-04-10 13:21:41 UTC
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.