File tree Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Original file line number Diff line number Diff line change 2222#include <string.h>
2323#include <time.h>
2424#include <ctype.h>
25+ #ifdef FLB_HAVE_FORK
26+ #include <fcntl.h>
27+ #include <unistd.h>
28+ #endif
2529
2630#include <sys/types.h>
2731#include <sys/stat.h>
@@ -177,6 +181,7 @@ void flb_utils_warn_c(const char *msg)
177181/* Run current process in background mode */
178182int flb_utils_set_daemon (struct flb_config * config )
179183{
184+ int fd ;
180185 pid_t pid ;
181186
182187 if ((pid = fork ()) < 0 ){
@@ -202,8 +207,15 @@ int flb_utils_set_daemon(struct flb_config *config)
202207 /* Our last STDOUT messages */
203208 flb_info ("switching to background mode (PID=%ld)" , (long ) getpid ());
204209
205- fclose (stderr );
206- fclose (stdout );
210+ /* Redirect stdin, stdout, stderr to `/dev/null`. */
211+ fd = open ("/dev/null" , O_RDWR );
212+ if (fd != -1 ) {
213+ dup2 (fd , STDIN_FILENO );
214+ dup2 (fd , STDOUT_FILENO );
215+ dup2 (fd , STDERR_FILENO );
216+ if (fd > 2 )
217+ close (fd );
218+ }
207219
208220 return 0 ;
209221}
You can’t perform that action at this time.
0 commit comments