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.
// (32 and above are real-time signals.)
// TODO: this is likely not portable outside of Linux, or on strange architectures
#define MAXSIG 31
// 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];
}
// 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[]) {
char **cmd = parse_command(argc, argv);
sigset_t all_signals;
sigfillset(&all_signals);
sigprocmask(SIG_BLOCK, &all_signals, NULL);
int i = 0;
for (i = 1; i <= MAXSIG; i++)
signal(i, dummy);
child_pid = fork();
if (child_pid < 0) {
PRINTERR("Unable to fork. Exiting.\n");

View file

@ -48,13 +48,17 @@ def print_signals(args=()):
def child_pids(pid):
"""Return a list of direct child PIDs for the given PID."""
pid = str(pid)
tasks = LocalPath('/proc').join(pid, 'task').listdir()
return set(
int(child_pid)
for task in tasks
for child_pid in task.join('children').read().split()
)
children = set()
for p in LocalPath('/proc').listdir():
stat = p.join('stat')
if stat.isfile():
stat = stat.open().read()
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):

View file

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