2 * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
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"
27 * Do the usual stuff to become a daemon.
29 * Fork, become session leader, dup fd 0, 1, 2 to /dev/null.
31 * \sa fork(2), setsid(2), dup(2).
33 void daemon_init(void)
38 DSS_INFO_LOG("daemonizing\n");
43 exit(EXIT_SUCCESS); /* parent exits */
44 /* become session leader */
50 null = open("/dev/null", O_RDONLY);
53 if (dup2(null, STDIN_FILENO) < 0)
55 if (dup2(null, STDOUT_FILENO) < 0)
57 if (dup2(null, STDERR_FILENO) < 0)
62 DSS_EMERG_LOG("fatal: %s\n", strerror(errno));
67 * fopen() the given file in append mode.
69 * \param logfile_name The name of the file to open.
71 * \return Either calls exit() or returns a valid file handle.
73 FILE *open_log(const char *logfile_name)
78 logfile = fopen(logfile_name, "a");
80 DSS_EMERG_LOG("can not open %s: %s\n", logfile_name,
89 * Close the log file of the daemon.
91 * \param logfile The log file handle.
93 * It's OK to call this with logfile == \p NULL.
95 void close_log(FILE* logfile)
99 DSS_INFO_LOG("closing logfile\n");
104 * Log the startup message.
106 void log_welcome(int loglevel)
108 DSS_INFO_LOG("***** welcome to dss ******\n");
109 DSS_DEBUG_LOG("using loglevel %d\n", loglevel);