aboutsummaryrefslogtreecommitdiff
path: root/contrib/capsicum-test/capsicum-test.cc
blob: 6adb222ec0550a6543c539038de02f5bbdf54e39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "capsicum-test.h"

#include <stdio.h>
#include <string.h>
#include <signal.h>

#include <map>
#include <vector>
#include <string>

bool verbose = false;
bool tmpdir_on_tmpfs = false;
bool force_mt = false;
bool force_nofork = false;
uid_t other_uid = 0;

namespace {
std::map<std::string, std::string> tmp_paths;
}

const char *TmpFile(const char *p) {
  std::string pathname(p);
  if (tmp_paths.find(pathname) == tmp_paths.end()) {
    std::string fullname = tmpdir + "/" + pathname;
    tmp_paths[pathname] = fullname;
  }
  return tmp_paths[pathname].c_str();
}

char ProcessState(int pid) {
#ifdef __linux__
  // Open the process status file.
  char s[1024];
  snprintf(s, sizeof(s), "/proc/%d/status", pid);
  FILE *f = fopen(s, "r");
  if (f == NULL) return '\0';

  // Read the file line by line looking for the state line.
  const char *prompt = "State:\t";
  while (!feof(f)) {
    fgets(s, sizeof(s), f);
    if (!strncmp(s, prompt, strlen(prompt))) {
      fclose(f);
      return s[strlen(prompt)];
    }
  }
  fclose(f);
  return '?';
#endif
#ifdef __FreeBSD__
  char buffer[1024];
  snprintf(buffer, sizeof(buffer), "ps -p %d -o state | grep -v STAT", pid);
  sig_t original = signal(SIGCHLD, SIG_IGN);
  FILE* cmd = popen(buffer, "r");
  usleep(50000);  // allow any pending SIGCHLD signals to arrive
  signal(SIGCHLD, original);
  int result = fgetc(cmd);
  fclose(cmd);
  // Map FreeBSD codes to Linux codes.
  switch (result) {
    case EOF:
      return '\0';
    case 'D': // disk wait
    case 'R': // runnable
    case 'S': // sleeping
    case 'T': // stopped
    case 'Z': // zombie
      return result;
    case 'W': // idle interrupt thread
      return 'S';
    case 'I': // idle
      return 'S';
    case 'L': // waiting to acquire lock
    default:
      return '?';
  }
#endif
}