From f854583872ea416e214bf13d9e15c3bfd4839c69 Mon Sep 17 00:00:00 2001 From: Chris Kuehl Date: Mon, 1 Aug 2016 10:24:00 -0700 Subject: [PATCH] Fixes for FreeBSD kernel --- dumb-init.c | 11 +++++++++++ testing/__init__.py | 18 +++++++++++------- tests/tty_test.py | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/dumb-init.c b/dumb-init.c index 6d713e3..cb0cbec 100644 --- a/dumb-init.c +++ b/dumb-init.c @@ -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"); diff --git a/testing/__init__.py b/testing/__init__.py index 3582c88..ef2d543 100644 --- a/testing/__init__.py +++ b/testing/__init__.py @@ -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): diff --git a/tests/tty_test.py b/tests/tty_test.py index 16fccf8..23f6a5c 100644 --- a/tests/tty_test.py +++ b/tests/tty_test.py @@ -29,7 +29,7 @@ def readall(fd): return result else: raise - if chunk == '': + if chunk == b'': return result else: result += chunk