Keep fd 2 open as our log fd, so other things that write there (e.g. assert()) work sanely.

This commit is contained in:
Ian Gulliver
2016-03-09 13:06:26 -08:00
parent fc14de31ff
commit 87f82b0327
2 changed files with 12 additions and 15 deletions

View File

@@ -76,7 +76,6 @@ int main(int argc, char *argv[]) {
reopen(STDIN_FILENO, "/dev/null", O_RDONLY);
reopen(STDOUT_FILENO, "/dev/full", O_WRONLY);
reopen(STDERR_FILENO, "/dev/full", O_WRONLY);
peer_loop();
@@ -104,7 +103,6 @@ int main(int argc, char *argv[]) {
assert(!close(STDIN_FILENO));
assert(!close(STDOUT_FILENO));
assert(!close(STDERR_FILENO));
return EXIT_SUCCESS;
}

View File

@@ -29,21 +29,25 @@ static opts_group log_opts;
static char log_module = 'L';
static void log_rotate() {
static void log_open() {
if (!log_path) {
LOG(log_id, "Asked to rotate logs but not logging to a file; ignoring");
return;
}
int fd = open(log_path, O_WRONLY | O_CREAT | O_APPEND | O_NOCTTY | O_CLOEXEC, S_IRUSR | S_IWUSR);
assert(fd >= 0);
assert(dup3(fd, STDERR_FILENO, O_CLOEXEC) == STDERR_FILENO);
assert(!close(fd));
}
static void log_rotate() {
assert(log_path);
uint8_t old_log_id[UUID_LEN], new_log_id[UUID_LEN];
uuid_gen(new_log_id);
LOG(log_id, "Switching to new log with ID %s at: %s", new_log_id, log_path);
memcpy(old_log_id, log_id, UUID_LEN);
memcpy(log_id, new_log_id, UUID_LEN);
assert(!fclose(log_stream));
log_stream = fopen(log_path, "a");
assert(log_stream);
setlinebuf(log_stream);
log_open();
LOG(log_id, "Log start after switch from log ID %s", old_log_id);
}
@@ -75,13 +79,8 @@ void log_opts_add() {
void log_init() {
opts_call(log_opts);
if (log_path) {
log_stream = fopen(log_path, "a");
} else {
int fd = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 0);
assert(fd >= 0);
log_stream = fdopen(fd, "a");
}
log_open();
log_stream = fdopen(STDERR_FILENO, "a");
assert(log_stream);
setlinebuf(log_stream);