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>
42 #include "daemonize.h"
45 #include "configuration.h"
62 static void rescan_after(long offset
);
63 static void dbgc_after(long offset
);
64 static void volumecheck_after(long offset
);
66 static const struct option options
[] = {
67 { "help", no_argument
, 0, 'h' },
68 { "version", no_argument
, 0, 'V' },
69 { "config", required_argument
, 0, 'c' },
70 { "debug", no_argument
, 0, 'd' },
71 { "foreground", no_argument
, 0, 'f' },
72 { "log", required_argument
, 0, 'l' },
73 { "pidfile", required_argument
, 0, 'P' },
74 { "no-initial-rescan", no_argument
, 0, 'N' },
75 { "wide-open", no_argument
, 0, 'w' },
76 { "syslog", no_argument
, 0, 's' },
80 /* display usage message and terminate */
81 static void help(void) {
83 " disorderd [OPTIONS]\n"
85 " --help, -h Display usage message\n"
86 " --version, -V Display version number\n"
87 " --config PATH, -c PATH Set configuration file\n"
88 " --debug, -d Turn on debugging\n"
89 " --foreground, -f Do not become a daemon\n"
90 " --syslog, -s Log to syslog even with -f\n"
91 " --pidfile PATH, -P PATH Leave a pidfile\n");
96 /* display version number and terminate */
97 static void version(void) {
98 xprintf("disorderd version %s\n", disorder_version_string
);
103 /* SIGHUP callback */
104 static int handle_sighup(ev_source
attribute((unused
)) *ev_
,
105 int attribute((unused
)) sig
,
106 void attribute((unused
)) *u
) {
107 info("received SIGHUP");
114 static int handle_sigint(ev_source
attribute((unused
)) *ev_
,
115 int attribute((unused
)) sig
,
116 void attribute((unused
)) *u
) {
117 info("received SIGINT");
121 static int handle_sigterm(ev_source
attribute((unused
)) *ev_
,
122 int attribute((unused
)) sig
,
123 void attribute((unused
)) *u
) {
124 info("received SIGTERM");
128 static int rescan_again(ev_source
*ev_
,
129 const struct timeval
attribute((unused
)) *now
,
130 void attribute((unused
)) *u
) {
136 static void rescan_after(long offset
) {
141 ev_timeout(ev
, 0, &w
, rescan_again
, 0);
144 static int dbgc_again(ev_source
attribute((unused
)) *ev_
,
145 const struct timeval
attribute((unused
)) *now
,
146 void attribute((unused
)) *u
) {
152 static void dbgc_after(long offset
) {
157 ev_timeout(ev
, 0, &w
, dbgc_again
, 0);
160 static int volumecheck_again(ev_source
attribute((unused
)) *ev_
,
161 const struct timeval
attribute((unused
)) *now
,
162 void attribute((unused
)) *u
) {
166 if(!mixer_control(&l
, &r
, 0)) {
167 if(l
!= volume_left
|| r
!= volume_right
) {
170 snprintf(lb
, sizeof lb
, "%d", l
);
171 snprintf(rb
, sizeof rb
, "%d", r
);
172 eventlog("volume", lb
, rb
, (char *)0);
175 volumecheck_after(60);
179 static void volumecheck_after(long offset
) {
184 ev_timeout(ev
, 0, &w
, volumecheck_again
, 0);
187 /* We fix the path to include the bindir and sbindir we were installed into */
188 static void fix_path(void) {
189 char *path
= getenv("PATH");
190 static char *newpath
;
191 /* static or libgc collects it! */
194 error(0, "PATH is not set at all!");
196 if(*finkbindir
&& strcmp(finkbindir
, "/"))
197 /* We appear to be a finkized mac; include fink on the path in case the
198 * tools we need are there. */
199 byte_xasprintf(&newpath
, "PATH=%s:%s:%s:%s",
200 path
, bindir
, sbindir
, finkbindir
);
202 byte_xasprintf(&newpath
, "PATH=%s:%s:%s", path
, bindir
, sbindir
);
207 int main(int argc
, char **argv
) {
208 int n
, background
= 1, logsyslog
= 0;
209 const char *pidfile
= 0;
210 int initial_rescan
= 1;
214 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
215 /* garbage-collect PCRE's memory */
216 pcre_malloc
= xmalloc
;
218 while((n
= getopt_long(argc
, argv
, "hVc:dfP:Ns", options
, 0)) >= 0) {
222 case 'c': configfile
= optarg
; break;
223 case 'd': debugging
= 1; break;
224 case 'f': background
= 0; break;
225 case 'P': pidfile
= optarg
; break;
226 case 'N': initial_rescan
= 0; break;
227 case 's': logsyslog
= 1; break;
228 case 'w': wideopen
= 1; break;
229 default: fatal(0, "invalid option");
232 /* go into background if necessary */
234 daemonize(progname
, LOG_DAEMON
, pidfile
);
236 /* If we're running under some kind of daemon supervisor then we may want
237 * to log to syslog but not to go into background */
238 openlog(progname
, LOG_PID
, LOG_DAEMON
);
239 log_default
= &log_syslog
;
241 info("process ID %lu", (unsigned long)getpid());
243 srand(time(0)); /* don't start the same every time */
244 /* create event loop */
246 if(ev_child_setup(ev
)) fatal(0, "ev_child_setup failed");
249 fatal(0, "cannot read configuration");
250 /* make sure the home directory exists and has suitable permissions */
252 /* create the default login */
254 /* Start the speaker process (as root! - so it can choose its nice value) */
256 /* set server nice value _after_ starting the speaker, so that they
257 * are independently niceable */
258 xnice(config
->nice_server
);
261 /* make sure we're not root, whatever the config says */
262 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
263 /* open a lockfile - we only want one copy of the server to run at once. */
265 const char *lockfile
;
269 lockfile
= config_get_file("lock");
270 if((lockfd
= open(lockfile
, O_RDWR
|O_CREAT
, 0600)) < 0)
271 fatal(errno
, "error opening %s", lockfile
);
273 memset(&lock
, 0, sizeof lock
);
274 lock
.l_type
= F_WRLCK
;
275 lock
.l_whence
= SEEK_SET
;
276 if(fcntl(lockfd
, F_SETLK
, &lock
) < 0)
277 fatal(errno
, "error locking %s", lockfile
);
279 /* initialize database environment */
280 trackdb_init(TRACKDB_NORMAL_RECOVER
);
282 /* install new config */
284 /* re-read config if we receive a SIGHUP */
285 if(ev_signal(ev
, SIGHUP
, handle_sighup
, 0)) fatal(0, "ev_signal failed");
286 /* exit on SIGINT/SIGTERM */
287 if(ev_signal(ev
, SIGINT
, handle_sigint
, 0)) fatal(0, "ev_signal failed");
288 if(ev_signal(ev
, SIGTERM
, handle_sigterm
, 0)) fatal(0, "ev_signal failed");
290 signal(SIGPIPE
, SIG_IGN
);
291 /* start a rescan straight away */
295 /* periodically tidy up the database */
297 /* periodically check the volume */
298 volumecheck_again(0, 0, 0);
299 /* set initial state */
302 /* enter the event loop */
304 /* if we exit the event loop, something must have gone wrong */
305 fatal(errno
, "ev_run returned %d", n
);