/** * Configures the standard I/O file descriptors for subsequent execution of * child processes. The standard input, output, and error file descriptors are * redirected to `/dev/null` if they are closed to prevent child processes that * mistakenly write to them from misbehaving. * * @retval 0 Success * @retval -1 Failure. log_add() called. */ static int configure_stdio_file_descriptors(void) { int status = open_on_dev_null_if_closed(STDIN_FILENO, O_RDONLY); if (status == 0) { status = open_on_dev_null_if_closed(STDOUT_FILENO, O_WRONLY); if (status == 0) status = open_on_dev_null_if_closed(STDERR_FILENO, O_RDWR); } return status; }
static void test_open_on_dev_null_if_closed( void) { int status = close(STDERR_FILENO); CU_ASSERT_EQUAL_FATAL(status, 0); status = open_on_dev_null_if_closed(STDERR_FILENO, O_RDWR); CU_ASSERT_EQUAL_FATAL(status, 0); CU_ASSERT_FALSE_FATAL(log_is_stderr_useful()); CU_ASSERT_TRUE_FATAL(fcntl(STDERR_FILENO, F_GETFD) >= 0); }