Skip to content

Commit 3c34fb8

Browse files
log.c: apply O_CLOEXEC to avoid leaking file descriptors
the embedded dnsmasq already does a fd cleanup on startup, so this is not strictly necessary, but still good practice Signed-off-by: darkexplosiveqwx <101737077+darkexplosiveqwx@users.noreply.github.com>
1 parent 40a1104 commit 3c34fb8

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/log.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "database/query-table.h"
2929
// runGC()
3030
#include "gc.h"
31-
// open(), O_WRONLY, O_CREAT, O_APPEND
31+
// open(), O_WRONLY, O_CREAT, O_APPEND, O_CLOEXEC
3232
#include <fcntl.h>
3333

3434
static bool print_log = true, print_stdout = true;
@@ -67,7 +67,7 @@ static bool write_log_line(struct log_fd *log, const char *line, size_t len)
6767
{
6868
log->reopen_needed = 0;
6969
close(log->fd);
70-
log->fd = open(log->path, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP);
70+
log->fd = open(log->path, O_WRONLY|O_CREAT|O_APPEND|O_CLOEXEC, S_IRUSR|S_IWUSR|S_IRGRP);
7171
}
7272

7373
ssize_t written = 0;
@@ -111,7 +111,7 @@ void open_log_fds(bool ftl)
111111
if(config.files.log.ftl.v.s != NULL)
112112
{
113113
ftl_log.path = config.files.log.ftl.v.s;
114-
ftl_log.fd = open(ftl_log.path, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP);
114+
ftl_log.fd = open(ftl_log.path, O_WRONLY|O_CREAT|O_APPEND|O_CLOEXEC, S_IRUSR|S_IWUSR|S_IRGRP);
115115
if(ftl_log.fd == -1)
116116
{
117117
printf("ERROR: Opening of FTL log (%s) failed: %s\nUsing syslog instead!\n",
@@ -126,14 +126,14 @@ void open_log_fds(bool ftl)
126126
if(config.files.log.webserver.v.s != NULL)
127127
{
128128
webserver_log.path = config.files.log.webserver.v.s;
129-
webserver_log.fd = open(webserver_log.path, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP);
129+
webserver_log.fd = open(webserver_log.path, O_WRONLY|O_CREAT|O_APPEND|O_CLOEXEC, S_IRUSR|S_IWUSR|S_IRGRP);
130130
}
131131

132132
// pihole.log (dnsmasq) — FTL owns this file from now on
133133
if(config.files.log.dnsmasq.v.s != NULL)
134134
{
135135
dnsmasq_log.path = config.files.log.dnsmasq.v.s;
136-
dnsmasq_log.fd = open(dnsmasq_log.path, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP);
136+
dnsmasq_log.fd = open(dnsmasq_log.path, O_WRONLY|O_CREAT|O_APPEND|O_CLOEXEC, S_IRUSR|S_IWUSR|S_IRGRP);
137137
}
138138
}
139139

@@ -897,7 +897,7 @@ bool flush_dnsmasq_log(void)
897897
pthread_mutex_lock(&dnsmasq_log.lock);
898898
if(dnsmasq_log.fd != -1)
899899
close(dnsmasq_log.fd);
900-
dnsmasq_log.fd = open(dnsmasq_log.path, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP);
900+
dnsmasq_log.fd = open(dnsmasq_log.path, O_WRONLY|O_CREAT|O_APPEND|O_CLOEXEC, S_IRUSR|S_IWUSR|S_IRGRP);
901901
pthread_mutex_unlock(&dnsmasq_log.lock);
902902

903903
// Flush dnsmasq FIFO logs

0 commit comments

Comments
 (0)