1 #include "capsicum-test.h"
7 #include <sys/socket.h>
8 #include <sys/sysctl.h>
10 #include <libprocstat.h>
22 bool tmpdir_on_tmpfs = false;
23 bool force_mt = false;
24 bool force_nofork = false;
28 std::map<std::string, std::string> tmp_paths;
31 const char *TmpFile(const char *p) {
32 std::string pathname(p);
33 if (tmp_paths.find(pathname) == tmp_paths.end()) {
34 std::string fullname = tmpdir + "/" + pathname;
35 tmp_paths[pathname] = fullname;
37 return tmp_paths[pathname].c_str();
40 char ProcessState(int pid) {
42 // Open the process status file.
44 snprintf(s, sizeof(s), "/proc/%d/status", pid);
45 FILE *f = fopen(s, "r");
46 if (f == NULL) return '\0';
48 // Read the file line by line looking for the state line.
49 const char *prompt = "State:\t";
51 fgets(s, sizeof(s), f);
52 if (!strncmp(s, prompt, strlen(prompt))) {
54 return s[strlen(prompt)];
61 // First check if the process exists/we have permission to see it. This
62 // Avoids warning messages being printed to stderr by libprocstat.
67 name[2] = KERN_PROC_PID;
69 if (sysctl(name, nitems(name), NULL, &len, NULL, 0) < 0 && errno == ESRCH) {
70 if (verbose) fprintf(stderr, "Process %d does not exist\n", pid);
71 return '\0'; // No such process.
73 unsigned int count = 0;
74 struct procstat *prstat = procstat_open_sysctl();
75 EXPECT_NE(nullptr, prstat) << "procstat_open_sysctl failed.";
77 struct kinfo_proc *p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &count);
78 if (p == NULL || count == 0) {
80 fprintf(stderr, "procstat_getprocs failed with %p/%d: %s\n", (void *)p,
81 count, strerror(errno));
83 procstat_close(prstat);
87 // See state() in bin/ps/print.c
93 if (p->ki_tdflags & TDF_SINTR) /* interruptable (long) */
104 // We treat SWAIT/SLOCK as 'S' here (instead of 'W'/'L').
114 procstat_freeprocs(prstat, p);
115 procstat_close(prstat);
116 if (verbose) fprintf(stderr, "Process %d in state '%c'\n", pid, result);