/* * 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 #include #include #include #include #include #include #include #include #include #include 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; }