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
27 #include <sys/types.h>
33 #include <sys/socket.h>
41 #include "daemonize.h"
44 #include "configuration.h"
60 static void rescan_after(long offset
);
61 static void dbgc_after(long offset
);
62 static void volumecheck_after(long offset
);
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 { "no-initial-rescan", no_argument
, 0, 'N' },
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 /* display version number and terminate */
94 static void version(void) {
95 xprintf("disorderd version %s\n", disorder_version_string
);
100 /* SIGHUP callback */
101 static int handle_sighup(ev_source
attribute((unused
)) *ev_
,
102 int attribute((unused
)) sig
,
103 void attribute((unused
)) *u
) {
104 info("received SIGHUP");
111 static int handle_sigint(ev_source
attribute((unused
)) *ev_
,
112 int attribute((unused
)) sig
,
113 void attribute((unused
)) *u
) {
114 info("received SIGINT");
118 static int handle_sigterm(ev_source
attribute((unused
)) *ev_
,
119 int attribute((unused
)) sig
,
120 void attribute((unused
)) *u
) {
121 info("received SIGTERM");
125 static int rescan_again(ev_source
*ev_
,
126 const struct timeval
attribute((unused
)) *now
,
127 void attribute((unused
)) *u
) {
133 static void rescan_after(long offset
) {
138 ev_timeout(ev
, 0, &w
, rescan_again
, 0);
141 static int dbgc_again(ev_source
attribute((unused
)) *ev_
,
142 const struct timeval
attribute((unused
)) *now
,
143 void attribute((unused
)) *u
) {
149 static void dbgc_after(long offset
) {
154 ev_timeout(ev
, 0, &w
, dbgc_again
, 0);
157 static int volumecheck_again(ev_source
attribute((unused
)) *ev_
,
158 const struct timeval
attribute((unused
)) *now
,
159 void attribute((unused
)) *u
) {
163 if(!mixer_control(&l
, &r
, 0)) {
164 if(l
!= volume_left
|| r
!= volume_right
) {
167 snprintf(lb
, sizeof lb
, "%d", l
);
168 snprintf(rb
, sizeof rb
, "%d", r
);
169 eventlog("volume", lb
, rb
, (char *)0);
172 volumecheck_after(60);
176 static void volumecheck_after(long offset
) {
181 ev_timeout(ev
, 0, &w
, volumecheck_again
, 0);
184 /* We fix the path to include the bindir and sbindir we were installed into */
185 static void fix_path(void) {
186 char *path
= getenv("PATH");
190 error(0, "PATH is not set at all!");
193 /* We appear to be a finkized mac; include fink on the path in case the
194 * tools we need are there. */
195 byte_xasprintf(&newpath
, "PATH=%s:%s:%s:%s",
196 path
, bindir
, sbindir
, finkbindir
);
198 byte_xasprintf(&newpath
, "PATH=%s:%s:%s", path
, bindir
, sbindir
);
203 int main(int argc
, char **argv
) {
204 int n
, background
= 1, logsyslog
= 0;
205 const char *pidfile
= 0;
206 int initial_rescan
= 1;
210 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
211 /* garbage-collect PCRE's memory */
212 pcre_malloc
= xmalloc
;
214 while((n
= getopt_long(argc
, argv
, "hVc:dfP:Ns", options
, 0)) >= 0) {
218 case 'c': configfile
= optarg
; break;
219 case 'd': debugging
= 1; break;
220 case 'f': background
= 0; break;
221 case 'P': pidfile
= optarg
; break;
222 case 'N': initial_rescan
= 0; break;
223 case 's': logsyslog
= 1; break;
224 default: fatal(0, "invalid option");
227 /* go into background if necessary */
229 daemonize(progname
, LOG_DAEMON
, pidfile
);
231 /* If we're running under some kind of daemon supervisor then we may want
232 * to log to syslog but not to go into background */
233 openlog(progname
, LOG_PID
, LOG_DAEMON
);
234 log_default
= &log_syslog
;
236 info("process ID %lu", (unsigned long)getpid());
238 srand(time(0)); /* don't start the same every time */
239 /* create event loop */
241 if(ev_child_setup(ev
)) fatal(0, "ev_child_setup failed");
244 fatal(0, "cannot read configuration");
245 /* Start the speaker process (as root! - so it can choose its nice value) */
247 /* set server nice value _after_ starting the speaker, so that they
248 * are independently niceable */
249 xnice(config
->nice_server
);
252 /* make sure we're not root, whatever the config says */
253 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
254 /* open a lockfile - we only want one copy of the server to run at once. */
256 const char *lockfile
;
260 lockfile
= config_get_file("lock");
261 if((lockfd
= open(lockfile
, O_RDWR
|O_CREAT
, 0600)) < 0)
262 fatal(errno
, "error opening %s", lockfile
);
264 memset(&lock
, 0, sizeof lock
);
265 lock
.l_type
= F_WRLCK
;
266 lock
.l_whence
= SEEK_SET
;
267 if(fcntl(lockfd
, F_SETLK
, &lock
) < 0)
268 fatal(errno
, "error locking %s", lockfile
);
270 /* initialize database environment */
271 trackdb_init(TRACKDB_NORMAL_RECOVER
);
273 /* install new config */
275 /* re-read config if we receive a SIGHUP */
276 if(ev_signal(ev
, SIGHUP
, handle_sighup
, 0)) fatal(0, "ev_signal failed");
277 /* exit on SIGINT/SIGTERM */
278 if(ev_signal(ev
, SIGINT
, handle_sigint
, 0)) fatal(0, "ev_signal failed");
279 if(ev_signal(ev
, SIGTERM
, handle_sigterm
, 0)) fatal(0, "ev_signal failed");
281 signal(SIGPIPE
, SIG_IGN
);
282 /* start a rescan straight away */
286 /* periodically tidy up the database */
288 /* periodically check the volume */
289 volumecheck_after(60);
290 /* set initial state */
293 /* enter the event loop */
295 /* if we exit the event loop, something must have gone wrong */
296 fatal(errno
, "ev_run returned %d", n
);