Skip to content

Commit

Permalink
linux-sandbox: improve commenting and debug logging.
Browse files Browse the repository at this point in the history
In particular, include timestamps and make it clear when clone and fork start
and return.

PiperOrigin-RevId: 333198030
  • Loading branch information
Googler authored and copybara-github committed Sep 23, 2020
1 parent 1517af1 commit f5eee73
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/main/tools/linux-sandbox-pid1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ static void SetupSignalHandlers() {
}

static void SpawnChild() {
PRINT_DEBUG("calling fork...");
global_child_pid = fork();

if (global_child_pid < 0) {
Expand Down Expand Up @@ -425,6 +426,8 @@ static void SpawnChild() {
if (execvp(opt.args[0], opt.args.data()) < 0) {
DIE("execvp(%s, %p)", opt.args[0], opt.args.data());
}
} else {
PRINT_DEBUG("child started with PID %d", global_child_pid);
}
}

Expand Down Expand Up @@ -465,6 +468,8 @@ static int WaitForChild() {
}

int Pid1Main(void *sync_pipe_param) {
PRINT_DEBUG("Pid1Main started");

if (getpid() != 1) {
DIE("Using PID namespaces, but we are not PID 1");
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/tools/linux-sandbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <string>
#include <vector>

Expand Down Expand Up @@ -130,6 +131,8 @@ static void SpawnPid1() {
const int kStackSize = 1024 * 1024;
std::vector<char> child_stack(kStackSize);

PRINT_DEBUG("calling pipe(2)...");

int sync_pipe[2];
if (pipe(sync_pipe) < 0) {
DIE("pipe");
Expand All @@ -147,6 +150,8 @@ static void SpawnPid1() {
// We use clone instead of unshare, because unshare sometimes fails with
// EINVAL due to a race condition in the Linux kernel (see
// https://lkml.org/lkml/2015/7/28/833).
PRINT_DEBUG("calling clone(2)...");

global_child_pid =
clone(Pid1Main, child_stack.data() + kStackSize, clone_flags, sync_pipe);
if (global_child_pid < 0) {
Expand All @@ -169,6 +174,8 @@ static void SpawnPid1() {
if (close(sync_pipe[0]) < 0) {
DIE("close");
}

PRINT_DEBUG("done manipulating pipes");
}

static int WaitForPid1() {
Expand Down Expand Up @@ -277,14 +284,21 @@ int main(int argc, char *argv[]) {

CloseFds();

// Spawn the child that will fork the sandboxed progam with fresh namespaces
// etc.
SpawnPid1();

// Set up signal handlers to manipulate the child as desired.
SetupSignalHandlers();

// If we've been asked to automatically time out after a certain amount of
// time, arrange for SIGALRM to handle the timeout and then set up an interval
// timer that will raise SIGALRM.
if (opt.timeout_secs > 0) {
InstallSignalHandler(SIGALRM, OnTimeoutOrTerm);
SetTimeout(opt.timeout_secs);
}

// Wait for the child to finish.
return WaitForPid1();
}
22 changes: 16 additions & 6 deletions src/main/tools/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
#ifndef SRC_MAIN_TOOLS_LOGGING_H_
#define SRC_MAIN_TOOLS_LOGGING_H_

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// see
// http://stackoverflow.com/questions/5641427/how-to-make-preprocessor-generate-a-string-for-line-keyword
#define S(x) #x
Expand All @@ -29,12 +35,16 @@
exit(EXIT_FAILURE); \
}

#define PRINT_DEBUG(...) \
do { \
if (global_debug) { \
fprintf(stderr, __FILE__ ":" S__LINE__ ": " __VA_ARGS__); \
fprintf(stderr, "\n"); \
} \
#define PRINT_DEBUG(fmt, ...) \
do { \
if (global_debug) { \
struct timespec ts; \
clock_gettime(CLOCK_REALTIME, &ts); \
\
fprintf(stderr, "%" PRId64 ".%09ld: %s:%d: " fmt "\n", \
((int64_t)ts.tv_sec), ts.tv_nsec, __FILE__, __LINE__, \
##__VA_ARGS__); \
} \
} while (0)

// Set to `true` to let PRINT_DEBUG() print messages.
Expand Down

0 comments on commit f5eee73

Please sign in to comment.