chiark / gitweb /
Only report failure to find OSS devices once, not every time we think
[disorder] / server / disorderd.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
4  *
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.
9  *
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.
14  *
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
18  * USA
19  */
20
21 #include <config.h>
22 #include "types.h"
23
24 #include <stdio.h>
25 #include <getopt.h>
26 #include <pwd.h>
27 #include <grp.h>
28 #include <sys/types.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <sys/socket.h>
35 #include <time.h>
36 #include <locale.h>
37 #include <syslog.h>
38 #include <sys/time.h>
39 #include <pcre.h>
40 #include <fcntl.h>
41 #include <gcrypt.h>
42
43 #include "daemonize.h"
44 #include "event.h"
45 #include "log.h"
46 #include "configuration.h"
47 #include "rights.h"
48 #include "trackdb.h"
49 #include "queue.h"
50 #include "mem.h"
51 #include "play.h"
52 #include "server.h"
53 #include "state.h"
54 #include "syscalls.h"
55 #include "defs.h"
56 #include "user.h"
57 #include "mixer.h"
58 #include "eventlog.h"
59 #include "printf.h"
60
61 static ev_source *ev;
62
63 static void rescan_after(long offset);
64 static void dbgc_after(long offset);
65 static void volumecheck_after(long offset);
66
67 static const struct option options[] = {
68   { "help", no_argument, 0, 'h' },
69   { "version", no_argument, 0, 'V' },
70   { "config", required_argument, 0, 'c' },
71   { "debug", no_argument, 0, 'd' },
72   { "foreground", no_argument, 0, 'f' },
73   { "log", required_argument, 0, 'l' },
74   { "pidfile", required_argument, 0, 'P' },
75   { "wide-open", no_argument, 0, 'w' },
76   { "syslog", no_argument, 0, 's' },
77   { 0, 0, 0, 0 }
78 };
79
80 /* display usage message and terminate */
81 static void help(void) {
82   xprintf("Usage:\n"
83           "  disorderd [OPTIONS]\n"
84           "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");
92   xfclose(stdout);
93   exit(0);
94 }
95
96 /* display version number and terminate */
97 static void version(void) {
98   xprintf("%s", disorder_version_string);
99   xfclose(stdout);
100   exit(0);
101 }
102
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");
108   reconfigure(ev, 1);
109   return 0;
110 }
111
112 /* fatal signals */
113
114 static int handle_sigint(ev_source attribute((unused)) *ev_,
115                          int attribute((unused)) sig,
116                          void attribute((unused)) *u) {
117   info("received SIGINT");
118   quit(ev);
119 }
120
121 static int handle_sigterm(ev_source attribute((unused)) *ev_,
122                           int attribute((unused)) sig,
123                           void attribute((unused)) *u) {
124   info("received SIGTERM");
125   quit(ev);
126 }
127
128 static int rescan_again(ev_source *ev_,
129                         const struct timeval attribute((unused)) *now,
130                         void attribute((unused)) *u) {
131   trackdb_rescan(ev_, 1/*check*/);
132   rescan_after(86400);
133   return 0;
134 }
135
136 static void rescan_after(long offset) {
137   struct timeval w;
138
139   gettimeofday(&w, 0);
140   w.tv_sec += offset;
141   ev_timeout(ev, 0, &w, rescan_again, 0);
142 }
143
144 static int dbgc_again(ev_source attribute((unused)) *ev_,
145                       const struct timeval attribute((unused)) *now,
146                       void attribute((unused)) *u) {
147   trackdb_gc();
148   dbgc_after(60);
149   return 0;
150 }
151
152 static void dbgc_after(long offset) {
153   struct timeval w;
154
155   gettimeofday(&w, 0);
156   w.tv_sec += offset;
157   ev_timeout(ev, 0, &w, dbgc_again, 0);
158 }
159
160 static int volumecheck_again(ev_source attribute((unused)) *ev_,
161                              const struct timeval attribute((unused)) *now,
162                              void attribute((unused)) *u) {
163   int l, r;
164   char lb[32], rb[32];
165
166   if(!mixer_control(&l, &r, 0)) {
167     if(l != volume_left || r != volume_right) {
168       volume_left = l;
169       volume_right = r;
170       snprintf(lb, sizeof lb, "%d", l);
171       snprintf(rb, sizeof rb, "%d", r);
172       eventlog("volume", lb, rb, (char *)0);
173     }
174   }
175   volumecheck_after(60);
176   return 0;
177 }
178
179 static void volumecheck_after(long offset) {
180   struct timeval w;
181
182   gettimeofday(&w, 0);
183   w.tv_sec += offset;
184   ev_timeout(ev, 0, &w, volumecheck_again, 0);
185 }
186
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! */
192
193   if(!path)
194     error(0, "PATH is not set at all!");
195
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);
201   else
202     byte_xasprintf(&newpath, "PATH=%s:%s:%s", path, bindir, sbindir);
203   putenv(newpath);
204   info("%s", newpath); 
205 }
206
207 int main(int argc, char **argv) {
208   int n, background = 1, logsyslog = 0;
209   const char *pidfile = 0;
210
211   set_progname(argv);
212   mem_init();
213   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
214   /* garbage-collect PCRE's memory */
215   pcre_malloc = xmalloc;
216   pcre_free = xfree;
217   while((n = getopt_long(argc, argv, "hVc:dfP:Ns", options, 0)) >= 0) {
218     switch(n) {
219     case 'h': help();
220     case 'V': version();
221     case 'c': configfile = optarg; break;
222     case 'd': debugging = 1; break;
223     case 'f': background = 0; break;
224     case 'P': pidfile = optarg; break;
225     case 's': logsyslog = 1; break;
226     case 'w': wideopen = 1; break;
227     default: fatal(0, "invalid option");
228     }
229   }
230   /* go into background if necessary */
231   if(background)
232     daemonize(progname, LOG_DAEMON, pidfile);
233   else if(logsyslog) {
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;
238   }
239   info("process ID %lu", (unsigned long)getpid());
240   fix_path();
241   srand(time(0));                       /* don't start the same every time */
242   /* gcrypt initialization */
243   gcry_control(GCRYCTL_INIT_SECMEM, 1);
244   /* create event loop */
245   ev = ev_new();
246   if(ev_child_setup(ev)) fatal(0, "ev_child_setup failed");
247   /* read config */
248   if(config_read(1))
249     fatal(0, "cannot read configuration");
250   /* make sure the home directory exists and has suitable permissions */
251   make_home();
252   /* Start the speaker process (as root! - so it can choose its nice value) */
253   speaker_setup(ev);
254   /* set server nice value _after_ starting the speaker, so that they
255    * are independently niceable */
256   xnice(config->nice_server);
257   /* change user */
258   become_mortal();
259   /* make sure we're not root, whatever the config says */
260   if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
261   /* open a lockfile - we only want one copy of the server to run at once. */
262   if(config->lock) {
263     const char *lockfile;
264     int lockfd;
265     struct flock lock;
266
267     lockfile = config_get_file("lock");
268     if((lockfd = open(lockfile, O_RDWR|O_CREAT, 0600)) < 0)
269       fatal(errno, "error opening %s", lockfile);
270     cloexec(lockfd);
271     memset(&lock, 0, sizeof lock);
272     lock.l_type = F_WRLCK;
273     lock.l_whence = SEEK_SET;
274     if(fcntl(lockfd, F_SETLK, &lock) < 0)
275       fatal(errno, "error locking %s", lockfile);
276   }
277   /* initialize database environment */
278   trackdb_init(TRACKDB_NORMAL_RECOVER|TRACKDB_MAY_CREATE);
279   trackdb_master(ev);
280   /* install new config (calls trackdb_open()) */
281   reconfigure(ev, 0);
282   /* pull in old users */
283   trackdb_old_users();
284   /* create a root login */
285   trackdb_create_root();
286   /* re-read config if we receive a SIGHUP */
287   if(ev_signal(ev, SIGHUP, handle_sighup, 0)) fatal(0, "ev_signal failed");
288   /* exit on SIGINT/SIGTERM */
289   if(ev_signal(ev, SIGINT, handle_sigint, 0)) fatal(0, "ev_signal failed");
290   if(ev_signal(ev, SIGTERM, handle_sigterm, 0)) fatal(0, "ev_signal failed");
291   /* ignore SIGPIPE */
292   signal(SIGPIPE, SIG_IGN);
293   /* Start a rescan straight away */
294   trackdb_rescan(ev, 1/*check*/);
295   /* We'll rescan again after a day */
296   rescan_after(86400);
297   /* periodically tidy up the database */
298   dbgc_after(60);
299   /* periodically check the volume */
300   volumecheck_again(0, 0, 0);
301   /* set initial state */
302   add_random_track();
303   play(ev);
304   /* enter the event loop */
305   n = ev_run(ev);
306   /* if we exit the event loop, something must have gone wrong */
307   fatal(errno, "ev_run returned %d", n);
308 }
309
310 /*
311 Local Variables:
312 c-basic-offset:2
313 comment-column:40
314 fill-column:79
315 End:
316 */