2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <sys/types.h>
34 #include <sys/socket.h>
43 #include "daemonize.h"
46 #include "configuration.h"
64 static const struct option options[] = {
65 { "help", no_argument, 0, 'h' },
66 { "version", no_argument, 0, 'V' },
67 { "config", required_argument, 0, 'c' },
68 { "debug", no_argument, 0, 'd' },
69 { "foreground", no_argument, 0, 'f' },
70 { "log", required_argument, 0, 'l' },
71 { "pidfile", required_argument, 0, 'P' },
72 { "wide-open", no_argument, 0, 'w' },
73 { "syslog", no_argument, 0, 's' },
77 /* display usage message and terminate */
78 static void help(void) {
80 " disorderd [OPTIONS]\n"
82 " --help, -h Display usage message\n"
83 " --version, -V Display version number\n"
84 " --config PATH, -c PATH Set configuration file\n"
85 " --debug, -d Turn on debugging\n"
86 " --foreground, -f Do not become a daemon\n"
87 " --syslog, -s Log to syslog even with -f\n"
88 " --pidfile PATH, -P PATH Leave a pidfile\n");
93 /* signals ------------------------------------------------------------------ */
96 static int handle_sighup(ev_source attribute((unused)) *ev_,
97 int attribute((unused)) sig,
98 void attribute((unused)) *u) {
99 info("received SIGHUP");
106 static int handle_sigint(ev_source attribute((unused)) *ev_,
107 int attribute((unused)) sig,
108 void attribute((unused)) *u) {
109 info("received SIGINT");
113 static int handle_sigterm(ev_source attribute((unused)) *ev_,
114 int attribute((unused)) sig,
115 void attribute((unused)) *u) {
116 info("received SIGTERM");
120 /* periodic actions --------------------------------------------------------- */
122 struct periodic_data {
123 void (*callback)(ev_source *);
127 static int periodic_callback(ev_source *ev_,
128 const struct timeval attribute((unused)) *now,
131 struct periodic_data *const pd = u;
135 w.tv_sec += pd->period;
136 ev_timeout(ev, 0, &w, periodic_callback, pd);
140 /** @brief Create a periodic action
141 * @param ev Event loop
142 * @param callback Callback function
143 * @param period Interval between calls in seconds
144 * @param immediate If true, call @p callback straight away
146 static void create_periodic(ev_source *ev_,
147 void (*callback)(ev_source *),
151 struct periodic_data *const pd = xmalloc(sizeof *pd);
153 pd->callback = callback;
159 ev_timeout(ev_, 0, &w, periodic_callback, pd);
162 static void periodic_rescan(ev_source *ev_) {
163 trackdb_rescan(ev_, 1/*check*/);
166 static void periodic_database_gc(ev_source attribute((unused)) *ev_) {
170 static void periodic_volume_check(ev_source attribute((unused)) *ev_) {
174 if(!mixer_control(-1/*as configured*/, &l, &r, 0)) {
175 if(l != volume_left || r != volume_right) {
178 snprintf(lb, sizeof lb, "%d", l);
179 snprintf(rb, sizeof rb, "%d", r);
180 eventlog("volume", lb, rb, (char *)0);
185 static void periodic_play_check(ev_source *ev_) {
189 static void periodic_add_random(ev_source *ev_) {
190 add_random_track(ev_);
193 /* We fix the path to include the bindir and sbindir we were installed into */
194 static void fix_path(void) {
195 char *path = getenv("PATH");
196 static char *newpath;
197 /* static or libgc collects it! */
200 error(0, "PATH is not set at all!");
202 if(*finkbindir && strcmp(finkbindir, "/"))
203 /* We appear to be a finkized mac; include fink on the path in case the
204 * tools we need are there. */
205 byte_xasprintf(&newpath, "PATH=%s:%s:%s:%s",
206 path, bindir, sbindir, finkbindir);
208 byte_xasprintf(&newpath, "PATH=%s:%s:%s", path, bindir, sbindir);
213 int main(int argc, char **argv) {
214 int n, background = 1, logsyslog = 0;
215 const char *pidfile = 0;
219 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
220 /* garbage-collect PCRE's memory */
221 pcre_malloc = xmalloc;
223 while((n = getopt_long(argc, argv, "hVc:dfP:Ns", options, 0)) >= 0) {
226 case 'V': version("disorderd");
227 case 'c': configfile = optarg; break;
228 case 'd': debugging = 1; break;
229 case 'f': background = 0; break;
230 case 'P': pidfile = optarg; break;
231 case 's': logsyslog = 1; break;
232 case 'w': wideopen = 1; break;
233 default: fatal(0, "invalid option");
236 /* go into background if necessary */
238 daemonize(progname, LOG_DAEMON, pidfile);
240 /* If we're running under some kind of daemon supervisor then we may want
241 * to log to syslog but not to go into background */
242 openlog(progname, LOG_PID, LOG_DAEMON);
243 log_default = &log_syslog;
245 info("process ID %lu", (unsigned long)getpid());
247 srand(time(0)); /* don't start the same every time */
248 /* gcrypt initialization */
249 gcry_control(GCRYCTL_INIT_SECMEM, 1);
250 /* create event loop */
252 if(ev_child_setup(ev)) fatal(0, "ev_child_setup failed");
255 fatal(0, "cannot read configuration");
256 /* make sure the home directory exists and has suitable permissions */
258 /* Start the speaker process (as root! - so it can choose its nice value) */
260 /* set server nice value _after_ starting the speaker, so that they
261 * are independently niceable */
262 xnice(config->nice_server);
265 /* make sure we're not root, whatever the config says */
266 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
267 /* open a lockfile - we only want one copy of the server to run at once. */
269 const char *lockfile;
273 lockfile = config_get_file("lock");
274 if((lockfd = open(lockfile, O_RDWR|O_CREAT, 0600)) < 0)
275 fatal(errno, "error opening %s", lockfile);
277 memset(&lock, 0, sizeof lock);
278 lock.l_type = F_WRLCK;
279 lock.l_whence = SEEK_SET;
280 if(fcntl(lockfd, F_SETLK, &lock) < 0)
281 fatal(errno, "error locking %s", lockfile);
283 /* initialize database environment */
284 trackdb_init(TRACKDB_NORMAL_RECOVER|TRACKDB_MAY_CREATE);
286 /* install new config (calls trackdb_open()) */
288 /* pull in old users */
290 /* create a root login */
291 trackdb_create_root();
292 /* re-read config if we receive a SIGHUP */
293 if(ev_signal(ev, SIGHUP, handle_sighup, 0)) fatal(0, "ev_signal failed");
294 /* exit on SIGINT/SIGTERM */
295 if(ev_signal(ev, SIGINT, handle_sigint, 0)) fatal(0, "ev_signal failed");
296 if(ev_signal(ev, SIGTERM, handle_sigterm, 0)) fatal(0, "ev_signal failed");
298 signal(SIGPIPE, SIG_IGN);
299 /* Rescan immediately and then daily */
300 create_periodic(ev, periodic_rescan, 86400, 1/*immediate*/);
301 /* Tidy up the database once a minute */
302 create_periodic(ev, periodic_database_gc, 60, 0);
303 /* Check the volume immediately and then once a minute */
304 create_periodic(ev, periodic_volume_check, 60, 1);
305 /* Check for a playable track once a second */
306 create_periodic(ev, periodic_play_check, 1, 0);
307 /* Try adding a random track immediately and once every two seconds */
308 create_periodic(ev, periodic_add_random, 2, 1);
309 /* enter the event loop */
311 /* if we exit the event loop, something must have gone wrong */
312 fatal(errno, "ev_run returned %d", n);