chiark / gitweb /
Further lib/ testing.
[disorder] / server / disorderd.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2004-2008 Richard Kettlewell
460b9539 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>
6d2d327c 22#include "types.h"
460b9539 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>
b12be54a 41#include <gcrypt.h>
460b9539 42
43#include "daemonize.h"
44#include "event.h"
45#include "log.h"
46#include "configuration.h"
5df73aeb 47#include "rights.h"
460b9539 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"
e83d0967 59#include "printf.h"
3fbdc96d 60#include "version.h"
460b9539 61
62static ev_source *ev;
63
460b9539 64static 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' },
18e6d6e6 72 { "wide-open", no_argument, 0, 'w' },
e83d0967 73 { "syslog", no_argument, 0, 's' },
460b9539 74 { 0, 0, 0, 0 }
75};
76
77/* display usage message and terminate */
78static void help(void) {
79 xprintf("Usage:\n"
80 " disorderd [OPTIONS]\n"
81 "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"
e83d0967 87 " --syslog, -s Log to syslog even with -f\n"
460b9539 88 " --pidfile PATH, -P PATH Leave a pidfile\n");
89 xfclose(stdout);
90 exit(0);
91}
92
49a773eb
RK
93/* signals ------------------------------------------------------------------ */
94
460b9539 95/* SIGHUP callback */
96static int handle_sighup(ev_source attribute((unused)) *ev_,
97 int attribute((unused)) sig,
98 void attribute((unused)) *u) {
99 info("received SIGHUP");
100 reconfigure(ev, 1);
101 return 0;
102}
103
104/* fatal signals */
105
106static int handle_sigint(ev_source attribute((unused)) *ev_,
107 int attribute((unused)) sig,
108 void attribute((unused)) *u) {
109 info("received SIGINT");
110 quit(ev);
111}
112
113static int handle_sigterm(ev_source attribute((unused)) *ev_,
114 int attribute((unused)) sig,
115 void attribute((unused)) *u) {
116 info("received SIGTERM");
117 quit(ev);
118}
119
49a773eb
RK
120/* periodic actions --------------------------------------------------------- */
121
122struct periodic_data {
123 void (*callback)(ev_source *);
124 int period;
125};
460b9539 126
49a773eb
RK
127static int periodic_callback(ev_source *ev_,
128 const struct timeval attribute((unused)) *now,
129 void *u) {
460b9539 130 struct timeval w;
49a773eb 131 struct periodic_data *const pd = u;
460b9539 132
49a773eb 133 pd->callback(ev_);
460b9539 134 gettimeofday(&w, 0);
49a773eb
RK
135 w.tv_sec += pd->period;
136 ev_timeout(ev, 0, &w, periodic_callback, pd);
460b9539 137 return 0;
138}
139
49a773eb
RK
140/** @brief Create a periodic action
141 * @param ev Event loop
142 * @param callback Callback function
143 * @param period Interval between calls in seconds
144 * @param immediate If true, call @p callback straight away
145 */
146static void create_periodic(ev_source *ev_,
147 void (*callback)(ev_source *),
148 int period,
149 int immediate) {
460b9539 150 struct timeval w;
49a773eb 151 struct periodic_data *const pd = xmalloc(sizeof *pd);
460b9539 152
49a773eb
RK
153 pd->callback = callback;
154 pd->period = period;
155 if(immediate)
156 callback(ev_);
460b9539 157 gettimeofday(&w, 0);
49a773eb
RK
158 w.tv_sec += period;
159 ev_timeout(ev_, 0, &w, periodic_callback, pd);
460b9539 160}
161
49a773eb
RK
162static void periodic_rescan(ev_source *ev_) {
163 trackdb_rescan(ev_, 1/*check*/);
164}
165
166static void periodic_database_gc(ev_source attribute((unused)) *ev_) {
167 trackdb_gc();
168}
169
170static void periodic_volume_check(ev_source attribute((unused)) *ev_) {
460b9539 171 int l, r;
172 char lb[32], rb[32];
173
174 if(!mixer_control(&l, &r, 0)) {
175 if(l != volume_left || r != volume_right) {
176 volume_left = l;
177 volume_right = r;
178 snprintf(lb, sizeof lb, "%d", l);
179 snprintf(rb, sizeof rb, "%d", r);
180 eventlog("volume", lb, rb, (char *)0);
181 }
182 }
460b9539 183}
184
49a773eb
RK
185static void periodic_play_check(ev_source *ev_) {
186 play(ev_);
187}
460b9539 188
49a773eb
RK
189static void periodic_add_random(ev_source *ev_) {
190 add_random_track(ev_);
460b9539 191}
192
e83d0967
RK
193/* We fix the path to include the bindir and sbindir we were installed into */
194static void fix_path(void) {
195 char *path = getenv("PATH");
e99d42b1 196 static char *newpath;
197 /* static or libgc collects it! */
e83d0967
RK
198
199 if(!path)
200 error(0, "PATH is not set at all!");
201
31780899 202 if(*finkbindir && strcmp(finkbindir, "/"))
e83d0967
RK
203 /* We appear to be a finkized mac; include fink on the path in case the
204 * tools we need are there. */
205 byte_xasprintf(&newpath, "PATH=%s:%s:%s:%s",
206 path, bindir, sbindir, finkbindir);
207 else
208 byte_xasprintf(&newpath, "PATH=%s:%s:%s", path, bindir, sbindir);
209 putenv(newpath);
210 info("%s", newpath);
211}
212
213int main(int argc, char **argv) {
214 int n, background = 1, logsyslog = 0;
460b9539 215 const char *pidfile = 0;
460b9539 216
217 set_progname(argv);
320598d4 218 mem_init();
460b9539 219 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
220 /* garbage-collect PCRE's memory */
221 pcre_malloc = xmalloc;
222 pcre_free = xfree;
e83d0967 223 while((n = getopt_long(argc, argv, "hVc:dfP:Ns", options, 0)) >= 0) {
460b9539 224 switch(n) {
225 case 'h': help();
3fbdc96d 226 case 'V': version("disorderd");
460b9539 227 case 'c': configfile = optarg; break;
228 case 'd': debugging = 1; break;
229 case 'f': background = 0; break;
230 case 'P': pidfile = optarg; break;
e83d0967 231 case 's': logsyslog = 1; break;
18e6d6e6 232 case 'w': wideopen = 1; break;
460b9539 233 default: fatal(0, "invalid option");
234 }
235 }
236 /* go into background if necessary */
237 if(background)
238 daemonize(progname, LOG_DAEMON, pidfile);
e83d0967
RK
239 else if(logsyslog) {
240 /* If we're running under some kind of daemon supervisor then we may want
241 * to log to syslog but not to go into background */
242 openlog(progname, LOG_PID, LOG_DAEMON);
243 log_default = &log_syslog;
244 }
460b9539 245 info("process ID %lu", (unsigned long)getpid());
e83d0967 246 fix_path();
460b9539 247 srand(time(0)); /* don't start the same every time */
b12be54a
RK
248 /* gcrypt initialization */
249 gcry_control(GCRYCTL_INIT_SECMEM, 1);
460b9539 250 /* create event loop */
251 ev = ev_new();
252 if(ev_child_setup(ev)) fatal(0, "ev_child_setup failed");
253 /* read config */
c00fce3a 254 if(config_read(1))
460b9539 255 fatal(0, "cannot read configuration");
659d87e8
RK
256 /* make sure the home directory exists and has suitable permissions */
257 make_home();
460b9539 258 /* Start the speaker process (as root! - so it can choose its nice value) */
259 speaker_setup(ev);
260 /* set server nice value _after_ starting the speaker, so that they
261 * are independently niceable */
262 xnice(config->nice_server);
263 /* change user */
264 become_mortal();
265 /* make sure we're not root, whatever the config says */
266 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
267 /* open a lockfile - we only want one copy of the server to run at once. */
268 if(config->lock) {
269 const char *lockfile;
270 int lockfd;
9ba0617a
RK
271 struct flock lock;
272
460b9539 273 lockfile = config_get_file("lock");
274 if((lockfd = open(lockfile, O_RDWR|O_CREAT, 0600)) < 0)
275 fatal(errno, "error opening %s", lockfile);
276 cloexec(lockfd);
277 memset(&lock, 0, sizeof lock);
278 lock.l_type = F_WRLCK;
279 lock.l_whence = SEEK_SET;
280 if(fcntl(lockfd, F_SETLK, &lock) < 0)
281 fatal(errno, "error locking %s", lockfile);
282 }
283 /* initialize database environment */
a745dd43 284 trackdb_init(TRACKDB_NORMAL_RECOVER|TRACKDB_MAY_CREATE);
460b9539 285 trackdb_master(ev);
f0feb22e 286 /* install new config (calls trackdb_open()) */
460b9539 287 reconfigure(ev, 0);
f0feb22e
RK
288 /* pull in old users */
289 trackdb_old_users();
290 /* create a root login */
291 trackdb_create_root();
460b9539 292 /* re-read config if we receive a SIGHUP */
293 if(ev_signal(ev, SIGHUP, handle_sighup, 0)) fatal(0, "ev_signal failed");
294 /* exit on SIGINT/SIGTERM */
295 if(ev_signal(ev, SIGINT, handle_sigint, 0)) fatal(0, "ev_signal failed");
296 if(ev_signal(ev, SIGTERM, handle_sigterm, 0)) fatal(0, "ev_signal failed");
297 /* ignore SIGPIPE */
298 signal(SIGPIPE, SIG_IGN);
49a773eb
RK
299 /* Rescan immediately and then daily */
300 create_periodic(ev, periodic_rescan, 86400, 1/*immediate*/);
301 /* Tidy up the database once a minute */
302 create_periodic(ev, periodic_database_gc, 60, 0);
303 /* Check the volume immediately and then once a minute */
304 create_periodic(ev, periodic_volume_check, 60, 1);
305 /* Check for a playable track once a second */
306 create_periodic(ev, periodic_play_check, 1, 0);
1ef295b3
RK
307 /* Try adding a random track immediately and once every two seconds */
308 create_periodic(ev, periodic_add_random, 2, 1);
460b9539 309 /* enter the event loop */
310 n = ev_run(ev);
311 /* if we exit the event loop, something must have gone wrong */
312 fatal(errno, "ev_run returned %d", n);
313}
314
315/*
316Local Variables:
317c-basic-offset:2
318comment-column:40
e83d0967 319fill-column:79
460b9539 320End:
321*/