Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <string.h>
#include <time.h>
#include <ctype.h>
#ifdef FLB_HAVE_FORK
#include <fcntl.h>
#include <unistd.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -177,6 +181,7 @@ void flb_utils_warn_c(const char *msg)
/* Run current process in background mode */
int flb_utils_set_daemon(struct flb_config *config)
{
int fd;
pid_t pid;

if ((pid = fork()) < 0){
Expand All @@ -202,8 +207,24 @@ int flb_utils_set_daemon(struct flb_config *config)
/* Our last STDOUT messages */
flb_info("switching to background mode (PID=%ld)", (long) getpid());

fclose(stderr);
fclose(stdout);
/* Redirect stdin, stdout, stderr to `/dev/null`. */
fd = open("/dev/null", O_RDWR);
if (fd == -1) {
flb_error("Failed to open /dev/null for daemonization");
return -1;
}

if (dup2(fd, STDIN_FILENO) == -1 ||
dup2(fd, STDOUT_FILENO) == -1 ||
dup2(fd, STDERR_FILENO) == -1) {
close(fd);
flb_error("Failed to redirect standard file descriptors to /dev/null");
return -1;
}

if (fd > 2) {
close(fd);
}

return 0;
}
Expand Down