2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 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 void rescan_after(long offset);
65 static void dbgc_after(long offset);
66 static void volumecheck_after(long offset);
68 static const struct option options[] = {
69 { "help", no_argument, 0, 'h' },
70 { "version", no_argument, 0, 'V' },
71 { "config", required_argument, 0, 'c' },
72 { "debug", no_argument, 0, 'd' },
73 { "foreground", no_argument, 0, 'f' },
74 { "log", required_argument, 0, 'l' },
75 { "pidfile", required_argument, 0, 'P' },
76 { "wide-open", no_argument, 0, 'w' },
77 { "syslog", no_argument, 0, 's' },
81 /* display usage message and terminate */
82 static void help(void) {
84 " disorderd [OPTIONS]\n"
86 " --help, -h Display usage message\n"
87 " --version, -V Display version number\n"
88 " --config PATH, -c PATH Set configuration file\n"
89 " --debug, -d Turn on debugging\n"
90 " --foreground, -f Do not become a daemon\n"
91 " --syslog, -s Log to syslog even with -f\n"
92 " --pidfile PATH, -P PATH Leave a pidfile\n");
98 static int handle_sighup(ev_source attribute((unused)) *ev_,
99 int attribute((unused)) sig,
100 void attribute((unused)) *u) {
101 info("received SIGHUP");
108 static int handle_sigint(ev_source attribute((unused)) *ev_,
109 int attribute((unused)) sig,
110 void attribute((unused)) *u) {
111 info("received SIGINT");
115 static int handle_sigterm(ev_source attribute((unused)) *ev_,
116 int attribute((unused)) sig,
117 void attribute((unused)) *u) {
118 info("received SIGTERM");
122 static int rescan_again(ev_source *ev_,
123 const struct timeval attribute((unused)) *now,
124 void attribute((unused)) *u) {
125 trackdb_rescan(ev_, 1/*check*/);
130 static void rescan_after(long offset) {
135 ev_timeout(ev, 0, &w, rescan_again, 0);
138 static int dbgc_again(ev_source attribute((unused)) *ev_,
139 const struct timeval attribute((unused)) *now,
140 void attribute((unused)) *u) {
146 static void dbgc_after(long offset) {
151 ev_timeout(ev, 0, &w, dbgc_again, 0);
154 static int volumecheck_again(ev_source attribute((unused)) *ev_,
155 const struct timeval attribute((unused)) *now,
156 void attribute((unused)) *u) {
160 if(!mixer_control(&l, &r, 0)) {
161 if(l != volume_left || r != volume_right) {
164 snprintf(lb, sizeof lb, "%d", l);
165 snprintf(rb, sizeof rb, "%d", r);
166 eventlog("volume", lb, rb, (char *)0);
169 volumecheck_after(60);
173 static void volumecheck_after(long offset) {
178 ev_timeout(ev, 0, &w, volumecheck_again, 0);
181 /* We fix the path to include the bindir and sbindir we were installed into */
182 static void fix_path(void) {
183 char *path = getenv("PATH");
184 static char *newpath;
185 /* static or libgc collects it! */
188 error(0, "PATH is not set at all!");
190 if(*finkbindir && strcmp(finkbindir, "/"))
191 /* We appear to be a finkized mac; include fink on the path in case the
192 * tools we need are there. */
193 byte_xasprintf(&newpath, "PATH=%s:%s:%s:%s",
194 path, bindir, sbindir, finkbindir);
196 byte_xasprintf(&newpath, "PATH=%s:%s:%s", path, bindir, sbindir);
201 int main(int argc, char **argv) {
202 int n, background = 1, logsyslog = 0;
203 const char *pidfile = 0;
207 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
208 /* garbage-collect PCRE's memory */
209 pcre_malloc = xmalloc;
211 while((n = getopt_long(argc, argv, "hVc:dfP:Ns", options, 0)) >= 0) {
214 case 'V': version("disorderd");
215 case 'c': configfile = optarg; break;
216 case 'd': debugging = 1; break;
217 case 'f': background = 0; break;
218 case 'P': pidfile = optarg; break;
219 case 's': logsyslog = 1; break;
220 case 'w': wideopen = 1; break;
221 default: fatal(0, "invalid option");
224 /* go into background if necessary */
226 daemonize(progname, LOG_DAEMON, pidfile);
228 /* If we're running under some kind of daemon supervisor then we may want
229 * to log to syslog but not to go into background */
230 openlog(progname, LOG_PID, LOG_DAEMON);
231 log_default = &log_syslog;
233 info("process ID %lu", (unsigned long)getpid());
235 srand(time(0)); /* don't start the same every time */
236 /* gcrypt initialization */
237 gcry_control(GCRYCTL_INIT_SECMEM, 1);
238 /* create event loop */
240 if(ev_child_setup(ev)) fatal(0, "ev_child_setup failed");
243 fatal(0, "cannot read configuration");
244 /* make sure the home directory exists and has suitable permissions */
246 /* Start the speaker process (as root! - so it can choose its nice value) */
248 /* set server nice value _after_ starting the speaker, so that they
249 * are independently niceable */
250 xnice(config->nice_server);
253 /* make sure we're not root, whatever the config says */
254 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
255 /* open a lockfile - we only want one copy of the server to run at once. */
257 const char *lockfile;
261 lockfile = config_get_file("lock");
262 if((lockfd = open(lockfile, O_RDWR|O_CREAT, 0600)) < 0)
263 fatal(errno, "error opening %s", lockfile);
265 memset(&lock, 0, sizeof lock);
266 lock.l_type = F_WRLCK;
267 lock.l_whence = SEEK_SET;
268 if(fcntl(lockfd, F_SETLK, &lock) < 0)
269 fatal(errno, "error locking %s", lockfile);
271 /* initialize database environment */
272 trackdb_init(TRACKDB_NORMAL_RECOVER|TRACKDB_MAY_CREATE);
274 /* install new config (calls trackdb_open()) */
276 /* pull in old users */
278 /* create a root login */
279 trackdb_create_root();
280 /* re-read config if we receive a SIGHUP */
281 if(ev_signal(ev, SIGHUP, handle_sighup, 0)) fatal(0, "ev_signal failed");
282 /* exit on SIGINT/SIGTERM */
283 if(ev_signal(ev, SIGINT, handle_sigint, 0)) fatal(0, "ev_signal failed");
284 if(ev_signal(ev, SIGTERM, handle_sigterm, 0)) fatal(0, "ev_signal failed");
286 signal(SIGPIPE, SIG_IGN);
287 /* Start a rescan straight away */
288 trackdb_rescan(ev, 1/*check*/);
289 /* We'll rescan again after a day */
291 /* periodically tidy up the database */
293 /* periodically check the volume */
294 volumecheck_again(0, 0, 0);
295 /* set initial state */
298 /* enter the event loop */
300 /* if we exit the event loop, something must have gone wrong */
301 fatal(errno, "ev_run returned %d", n);