2 * Copyright (C) 1997-2010 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file daemon.c Some helpers for programs that detach from the console. */
13 #include <sys/types.h>
21 #include "gcc-compat.h"
28 * Do the usual stuff to become a daemon.
30 * Fork, become session leader, dup fd 0, 1, 2 to /dev/null.
32 * \sa fork(2), setsid(2), dup(2).
34 void daemon_init(void)
39 DSS_INFO_LOG(("daemonizing\n"));
44 exit(EXIT_SUCCESS); /* parent exits */
45 /* become session leader */
49 null = open("/dev/null", O_RDWR);
52 if (dup2(null, STDIN_FILENO) < 0)
54 if (dup2(null, STDOUT_FILENO) < 0)
56 if (dup2(null, STDERR_FILENO) < 0)
61 DSS_EMERG_LOG(("fatal: %s\n", strerror(errno)));
66 * fopen() the given file in append mode.
68 * \param logfile_name The name of the file to open.
70 * \return Either calls exit() or returns a valid file handle.
72 FILE *open_log(const char *logfile_name)
77 logfile = fopen(logfile_name, "a");
79 DSS_EMERG_LOG(("can not open %s: %s\n", logfile_name,
88 * Close the log file of the daemon.
90 * \param logfile The log file handle.
92 * It's OK to call this with logfile == \p NULL.
94 void close_log(FILE* logfile)
98 DSS_INFO_LOG(("closing logfile\n"));
103 * Log the startup message.
105 void log_welcome(int loglevel)
107 DSS_INFO_LOG(("***** welcome to dss ******\n"));
108 DSS_DEBUG_LOG(("using loglevel %d\n", loglevel));