Merge pull request #109 from chriskuehl/fixes-for-freebsd

Fixes for FreeBSD kernel
This commit is contained in:
Chris Kuehl 2016-08-01 18:47:40 -07:00 committed by GitHub
commit 301eeddce7
3 changed files with 23 additions and 8 deletions

View file

@ -32,6 +32,7 @@
// Signals we care about are numbered from 1 to 31, inclusive. // Signals we care about are numbered from 1 to 31, inclusive.
// (32 and above are real-time signals.) // (32 and above are real-time signals.)
// TODO: this is likely not portable outside of Linux, or on strange architectures
#define MAXSIG 31 #define MAXSIG 31
// Indices are one-indexed (signal 1 is at index 1). Index zero is unused. // Indices are one-indexed (signal 1 is at index 1). Index zero is unused.
@ -233,12 +234,22 @@ char **parse_command(int argc, char *argv[]) {
return &argv[optind]; return &argv[optind];
} }
// A dummy signal handler used for signals we care about.
// On the FreeBSD kernel, ignored signals cannot be waited on by `sigwait` (but
// they can be on Linux). We must provide a dummy handler.
// https://lists.freebsd.org/pipermail/freebsd-ports/2009-October/057340.html
void dummy(int signum) {}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char **cmd = parse_command(argc, argv); char **cmd = parse_command(argc, argv);
sigset_t all_signals; sigset_t all_signals;
sigfillset(&all_signals); sigfillset(&all_signals);
sigprocmask(SIG_BLOCK, &all_signals, NULL); sigprocmask(SIG_BLOCK, &all_signals, NULL);
int i = 0;
for (i = 1; i <= MAXSIG; i++)
signal(i, dummy);
child_pid = fork(); child_pid = fork();
if (child_pid < 0) { if (child_pid < 0) {
PRINTERR("Unable to fork. Exiting.\n"); PRINTERR("Unable to fork. Exiting.\n");

View file

@ -48,13 +48,17 @@ def print_signals(args=()):
def child_pids(pid): def child_pids(pid):
"""Return a list of direct child PIDs for the given PID.""" """Return a list of direct child PIDs for the given PID."""
pid = str(pid) children = set()
tasks = LocalPath('/proc').join(pid, 'task').listdir() for p in LocalPath('/proc').listdir():
return set( stat = p.join('stat')
int(child_pid) if stat.isfile():
for task in tasks stat = stat.open().read()
for child_pid in task.join('children').read().split() m = re.match('^\d+ \([^\)]+\) [a-zA-Z] (\d+) ', stat)
) assert m, stat
ppid = int(m.group(1))
if ppid == pid:
children.add(int(p.basename))
return children
def pid_tree(pid): def pid_tree(pid):

View file

@ -29,7 +29,7 @@ def readall(fd):
return result return result
else: else:
raise raise
if chunk == '': if chunk == b'':
return result return result
else: else:
result += chunk result += chunk