2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006 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>
42 #include "daemonize.h"
45 #include "configuration.h"
61 static void rescan_after(long offset);
62 static void dbgc_after(long offset);
63 static void volumecheck_after(long offset);
65 static const struct option options[] = {
66 { "help", no_argument, 0, 'h' },
67 { "version", no_argument, 0, 'V' },
68 { "config", required_argument, 0, 'c' },
69 { "debug", no_argument, 0, 'd' },
70 { "foreground", no_argument, 0, 'f' },
71 { "log", required_argument, 0, 'l' },
72 { "pidfile", required_argument, 0, 'P' },
73 { "no-initial-rescan", no_argument, 0, 'N' },
74 { "wide-open", no_argument, 0, 'w' },
75 { "syslog", no_argument, 0, 's' },
79 /* display usage message and terminate */
80 static void help(void) {
82 " disorderd [OPTIONS]\n"
84 " --help, -h Display usage message\n"
85 " --version, -V Display version number\n"
86 " --config PATH, -c PATH Set configuration file\n"
87 " --debug, -d Turn on debugging\n"
88 " --foreground, -f Do not become a daemon\n"
89 " --syslog, -s Log to syslog even with -f\n"
90 " --pidfile PATH, -P PATH Leave a pidfile\n");
95 /* display version number and terminate */
96 static void version(void) {
97 xprintf("disorderd version %s\n", disorder_version_string);
102 /* SIGHUP callback */
103 static int handle_sighup(ev_source attribute((unused)) *ev_,
104 int attribute((unused)) sig,
105 void attribute((unused)) *u) {
106 info("received SIGHUP");
113 static int handle_sigint(ev_source attribute((unused)) *ev_,
114 int attribute((unused)) sig,
115 void attribute((unused)) *u) {
116 info("received SIGINT");
120 static int handle_sigterm(ev_source attribute((unused)) *ev_,
121 int attribute((unused)) sig,
122 void attribute((unused)) *u) {
123 info("received SIGTERM");
127 static int rescan_again(ev_source *ev_,
128 const struct timeval attribute((unused)) *now,
129 void attribute((unused)) *u) {
135 static void rescan_after(long offset) {
140 ev_timeout(ev, 0, &w, rescan_again, 0);
143 static int dbgc_again(ev_source attribute((unused)) *ev_,
144 const struct timeval attribute((unused)) *now,
145 void attribute((unused)) *u) {
151 static void dbgc_after(long offset) {
156 ev_timeout(ev, 0, &w, dbgc_again, 0);
159 static int volumecheck_again(ev_source attribute((unused)) *ev_,
160 const struct timeval attribute((unused)) *now,
161 void attribute((unused)) *u) {
165 if(!mixer_control(&l, &r, 0)) {
166 if(l != volume_left || r != volume_right) {
169 snprintf(lb, sizeof lb, "%d", l);
170 snprintf(rb, sizeof rb, "%d", r);
171 eventlog("volume", lb, rb, (char *)0);
174 volumecheck_after(60);
178 static void volumecheck_after(long offset) {
183 ev_timeout(ev, 0, &w, volumecheck_again, 0);
186 /* We fix the path to include the bindir and sbindir we were installed into */
187 static void fix_path(void) {
188 char *path = getenv("PATH");
192 error(0, "PATH is not set at all!");
194 if(*finkbindir && strcmp(finkbindir, "/"))
195 /* We appear to be a finkized mac; include fink on the path in case the
196 * tools we need are there. */
197 byte_xasprintf(&newpath, "PATH=%s:%s:%s:%s",
198 path, bindir, sbindir, finkbindir);
200 byte_xasprintf(&newpath, "PATH=%s:%s:%s", path, bindir, sbindir);
205 int main(int argc, char **argv) {
206 int n, background = 1, logsyslog = 0;
207 const char *pidfile = 0;
208 int initial_rescan = 1;
212 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
213 /* garbage-collect PCRE's memory */
214 pcre_malloc = xmalloc;
216 while((n = getopt_long(argc, argv, "hVc:dfP:Ns", options, 0)) >= 0) {
220 case 'c': configfile = optarg; break;
221 case 'd': debugging = 1; break;
222 case 'f': background = 0; break;
223 case 'P': pidfile = optarg; break;
224 case 'N': initial_rescan = 0; break;
225 case 's': logsyslog = 1; break;
226 case 'w': wideopen = 1; break;
227 default: fatal(0, "invalid option");
230 /* go into background if necessary */
232 daemonize(progname, LOG_DAEMON, pidfile);
234 /* If we're running under some kind of daemon supervisor then we may want
235 * to log to syslog but not to go into background */
236 openlog(progname, LOG_PID, LOG_DAEMON);
237 log_default = &log_syslog;
239 info("process ID %lu", (unsigned long)getpid());
241 srand(time(0)); /* don't start the same every time */
242 /* create event loop */
244 if(ev_child_setup(ev)) fatal(0, "ev_child_setup failed");
247 fatal(0, "cannot read configuration");
248 /* Start the speaker process (as root! - so it can choose its nice value) */
250 /* set server nice value _after_ starting the speaker, so that they
251 * are independently niceable */
252 xnice(config->nice_server);
255 /* make sure we're not root, whatever the config says */
256 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
257 /* open a lockfile - we only want one copy of the server to run at once. */
259 const char *lockfile;
263 lockfile = config_get_file("lock");
264 if((lockfd = open(lockfile, O_RDWR|O_CREAT, 0600)) < 0)
265 fatal(errno, "error opening %s", lockfile);
267 memset(&lock, 0, sizeof lock);
268 lock.l_type = F_WRLCK;
269 lock.l_whence = SEEK_SET;
270 if(fcntl(lockfd, F_SETLK, &lock) < 0)
271 fatal(errno, "error locking %s", lockfile);
273 /* initialize database environment */
274 trackdb_init(TRACKDB_NORMAL_RECOVER);
276 /* install new config */
278 /* re-read config if we receive a SIGHUP */
279 if(ev_signal(ev, SIGHUP, handle_sighup, 0)) fatal(0, "ev_signal failed");
280 /* exit on SIGINT/SIGTERM */
281 if(ev_signal(ev, SIGINT, handle_sigint, 0)) fatal(0, "ev_signal failed");
282 if(ev_signal(ev, SIGTERM, handle_sigterm, 0)) fatal(0, "ev_signal failed");
284 signal(SIGPIPE, SIG_IGN);
285 /* start a rescan straight away */
289 /* periodically tidy up the database */
291 /* periodically check the volume */
292 volumecheck_again(0, 0, 0);
293 /* set initial state */
296 /* enter the event loop */
298 /* if we exit the event loop, something must have gone wrong */
299 fatal(errno, "ev_run returned %d", n);