pid, parent, owner, command name, state, threads, real and virtual memory in bytes, CPU time in ms. NetBSD: sysctl KERN_PROC2 the way libkvm's kvm_getproc2 asks (size, then the buffer by count, ENOMEM -> again), CPU time from p_rtime_* as ps(1) does (print.c:1059), state from p_realstat and the LWP's p_stat. Linux: /proc/PID/stat read after the LAST ')', memory from statm. Measured: test_processes (this process, init, a zombie until waited for, a sleeping child named 'a b)c', SIGSTOP as T, SIGCONT back) and the three other McdeSys tests PASS, mcde-linux exit=0. Against ps -eo for every process in both lists: 631 compared, parent, owner, threads, RSS, state and CPU time all agree. First version used stat's rss: on this kernel it is larger than VmRSS/statm (126 of 632 processes more than 10 % off), so statm is read. The NetBSD code is compiled against the NetBSD sysroot with x86_64--netbsd-gcc -Wall -Wextra (all of netbsd.c, exit 0), not run there. Also <ctype.h> in netbsd.c and linux.c: isspace() was used undeclared (an error with GCC 14, masked in the build by -Wno-error=implicit-function-declaration). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
172 lines
4.4 KiB
C
172 lines
4.4 KiB
C
/*
|
|
* test_processes.c -- McdeSys: the list of processes (make check)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* mcde_sys_processes is what dtactivity lists (tasksd t0091). What is checked
|
|
* is what a person sees: this process is there with its own pid, parent, owner
|
|
* and a name, it uses memory, and children in known states show them -- one
|
|
* asleep, one stopped by SIGSTOP, one a zombie until it is waited for. The
|
|
* sleeping child is started through a link named "a b)c": a name with a space
|
|
* and a ')' is what breaks a reader of /proc/PID/stat that looks for the first
|
|
* ')'. The same test runs on every system McdeSys has a backend for.
|
|
*/
|
|
|
|
#include <errno.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static const McdeSysProcess *
|
|
find(const McdeSysProcess *v, size_t n, pid_t pid)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; i++)
|
|
if (v[i].pid == (long) pid)
|
|
return &v[i];
|
|
return NULL;
|
|
}
|
|
|
|
static void
|
|
nap(int ms)
|
|
{
|
|
struct timespec t;
|
|
|
|
t.tv_sec = ms / 1000;
|
|
t.tv_nsec = (long) (ms % 1000) * 1000000L;
|
|
(void) nanosleep(&t, NULL);
|
|
}
|
|
|
|
/* the state of PID in a fresh list, '\0' when it is not there; waits up to
|
|
* about 2 s for WANT, since a child needs a moment to exec and to sleep */
|
|
static char
|
|
state_of(pid_t pid, char want, McdeSysProcess *copy)
|
|
{
|
|
McdeSysProcess *v;
|
|
size_t n;
|
|
int tries;
|
|
char st = '\0';
|
|
|
|
for (tries = 0; tries < 40; tries++) {
|
|
const McdeSysProcess *p;
|
|
|
|
if (mcde_sys_processes(&v, &n) != MCDE_SYS_OK)
|
|
return '\0';
|
|
p = find(v, n, pid);
|
|
st = p != NULL ? p->state : '\0';
|
|
if (p != NULL && copy != NULL)
|
|
*copy = *p;
|
|
free(v);
|
|
if (st == want)
|
|
return st;
|
|
nap(50);
|
|
}
|
|
return st;
|
|
}
|
|
|
|
static pid_t
|
|
spawn(const char *path, const char *arg)
|
|
{
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
execl(path, path, arg, (char *) NULL);
|
|
_exit(127);
|
|
}
|
|
return pid;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
McdeSysProcess *v, me, ch;
|
|
size_t n;
|
|
const McdeSysProcess *p;
|
|
char dir[] = "/tmp/test-processes.XXXXXX", link[200];
|
|
const char *sleep_bin = NULL;
|
|
pid_t kid, zomb;
|
|
int status;
|
|
|
|
/* the argument checks */
|
|
CHECK(mcde_sys_processes(NULL, &n) == MCDE_SYS_EINVAL);
|
|
CHECK(mcde_sys_processes(&v, NULL) == MCDE_SYS_EINVAL);
|
|
|
|
/* this process */
|
|
CHECK(mcde_sys_processes(&v, &n) == MCDE_SYS_OK);
|
|
CHECK(n > 0 && v != NULL);
|
|
p = find(v, n, getpid());
|
|
CHECK(p != NULL);
|
|
if (p != NULL) {
|
|
me = *p;
|
|
CHECK(me.ppid == (long) getppid());
|
|
CHECK(me.uid == (unsigned long) getuid());
|
|
CHECK(me.user[0] != '\0');
|
|
CHECK(me.comm[0] != '\0');
|
|
/* the kernel keeps 15 characters on Linux, and libtool may
|
|
* run the program as "lt-test_processes" */
|
|
CHECK(strstr(me.comm, "test_proc") != NULL);
|
|
CHECK(me.state == 'R' || me.state == 'S');
|
|
CHECK(me.nlwp >= 1);
|
|
CHECK(me.rss > 0);
|
|
CHECK(me.vsz >= me.rss);
|
|
}
|
|
/* init is there, and it is root's */
|
|
p = find(v, n, 1);
|
|
CHECK(p != NULL && p->uid == 0);
|
|
free(v);
|
|
|
|
/* a zombie shows until it is waited for */
|
|
zomb = spawn("/bin/true", "");
|
|
CHECK(zomb > 0);
|
|
CHECK(state_of(zomb, 'Z', NULL) == 'Z');
|
|
CHECK(waitpid(zomb, &status, 0) == zomb);
|
|
CHECK(state_of(zomb, '\0', NULL) == '\0'); /* gone once waited for */
|
|
|
|
/* a sleeping child with a name that has a space and a ')' */
|
|
if (access("/bin/sleep", X_OK) == 0)
|
|
sleep_bin = "/bin/sleep";
|
|
else if (access("/usr/bin/sleep", X_OK) == 0)
|
|
sleep_bin = "/usr/bin/sleep";
|
|
if (sleep_bin != NULL && mkdtemp(dir) != NULL) {
|
|
snprintf(link, sizeof link, "%s/a b)c", dir);
|
|
CHECK(symlink(sleep_bin, link) == 0);
|
|
kid = spawn(link, "30");
|
|
CHECK(kid > 0);
|
|
memset(&ch, 0, sizeof ch);
|
|
CHECK(state_of(kid, 'S', &ch) == 'S');
|
|
CHECK(strcmp(ch.comm, "a b)c") == 0);
|
|
CHECK(ch.ppid == (long) getpid());
|
|
CHECK(ch.uid == (unsigned long) getuid());
|
|
CHECK(ch.nlwp >= 1);
|
|
/* stopped: SIGSTOP shows as T */
|
|
CHECK(kill(kid, SIGSTOP) == 0);
|
|
CHECK(state_of(kid, 'T', NULL) == 'T');
|
|
CHECK(kill(kid, SIGCONT) == 0);
|
|
CHECK(state_of(kid, 'S', NULL) == 'S');
|
|
(void) kill(kid, SIGKILL);
|
|
(void) waitpid(kid, &status, 0);
|
|
(void) unlink(link);
|
|
(void) rmdir(dir);
|
|
}
|
|
printf("%s: %d failures\n", failures ? "FAIL" : "PASS", failures);
|
|
return failures != 0;
|
|
}
|