1 #include "capsicum-test.h"
12 bool tmpdir_on_tmpfs = false;
13 bool force_mt = false;
14 bool force_nofork = false;
18 std::map<std::string, std::string> tmp_paths;
21 const char *TmpFile(const char *p) {
22 std::string pathname(p);
23 if (tmp_paths.find(pathname) == tmp_paths.end()) {
24 std::string fullname = tmpdir + "/" + pathname;
25 tmp_paths[pathname] = fullname;
27 return tmp_paths[pathname].c_str();
30 char ProcessState(int pid) {
32 // Open the process status file.
34 snprintf(s, sizeof(s), "/proc/%d/status", pid);
35 FILE *f = fopen(s, "r");
36 if (f == NULL) return '\0';
38 // Read the file line by line looking for the state line.
39 const char *prompt = "State:\t";
41 fgets(s, sizeof(s), f);
42 if (!strncmp(s, prompt, strlen(prompt))) {
44 return s[strlen(prompt)];
52 snprintf(buffer, sizeof(buffer), "ps -p %d -o state | grep -v STAT", pid);
53 sig_t original = signal(SIGCHLD, SIG_IGN);
54 FILE* cmd = popen(buffer, "r");
55 usleep(50000); // allow any pending SIGCHLD signals to arrive
56 signal(SIGCHLD, original);
57 int result = fgetc(cmd);
59 // Map FreeBSD codes to Linux codes.
63 case 'D': // disk wait
69 case 'W': // idle interrupt thread
73 case 'L': // waiting to acquire lock