chiark / gitweb /
debian/control: db4.8 is obsolete in wheezy.
[disorder] / server / disorderd.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
bcef8d6f 3 * Copyright (C) 2004-2012 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
9 *
e7eb3a27
RK
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
132a5a4a
RK
18/** @file server/disorderd.c
19 * @brief Main DisOrder server
20 */
05b75f8d 21#include "disorder-server.h"
460b9539 22
23static ev_source *ev;
24
460b9539 25static const struct option options[] = {
26 { "help", no_argument, 0, 'h' },
27 { "version", no_argument, 0, 'V' },
28 { "config", required_argument, 0, 'c' },
29 { "debug", no_argument, 0, 'd' },
30 { "foreground", no_argument, 0, 'f' },
31 { "log", required_argument, 0, 'l' },
32 { "pidfile", required_argument, 0, 'P' },
18e6d6e6 33 { "wide-open", no_argument, 0, 'w' },
bcef8d6f 34 { "wait-for-root", no_argument, 0, 'W' },
e83d0967 35 { "syslog", no_argument, 0, 's' },
460b9539 36 { 0, 0, 0, 0 }
37};
38
39/* display usage message and terminate */
40static void help(void) {
41 xprintf("Usage:\n"
42 " disorderd [OPTIONS]\n"
43 "Options:\n"
44 " --help, -h Display usage message\n"
45 " --version, -V Display version number\n"
46 " --config PATH, -c PATH Set configuration file\n"
47 " --debug, -d Turn on debugging\n"
48 " --foreground, -f Do not become a daemon\n"
e83d0967 49 " --syslog, -s Log to syslog even with -f\n"
460b9539 50 " --pidfile PATH, -P PATH Leave a pidfile\n");
51 xfclose(stdout);
52 exit(0);
53}
54
49a773eb
RK
55/* signals ------------------------------------------------------------------ */
56
460b9539 57/* SIGHUP callback */
58static int handle_sighup(ev_source attribute((unused)) *ev_,
59 int attribute((unused)) sig,
60 void attribute((unused)) *u) {
2e9ba080 61 disorder_info("received SIGHUP");
37f29ce9 62 reconfigure(ev, RECONFIGURE_RELOADING);
460b9539 63 return 0;
64}
65
66/* fatal signals */
67
68static int handle_sigint(ev_source attribute((unused)) *ev_,
69 int attribute((unused)) sig,
70 void attribute((unused)) *u) {
2e9ba080 71 disorder_info("received SIGINT");
460b9539 72 quit(ev);
73}
74
75static int handle_sigterm(ev_source attribute((unused)) *ev_,
76 int attribute((unused)) sig,
77 void attribute((unused)) *u) {
2e9ba080 78 disorder_info("received SIGTERM");
460b9539 79 quit(ev);
80}
81
49a773eb
RK
82/* periodic actions --------------------------------------------------------- */
83
2c6ee627 84/** @brief A job executed periodically by the server */
49a773eb 85struct periodic_data {
2c6ee627 86 /** @brief Callback to process job */
49a773eb 87 void (*callback)(ev_source *);
2c6ee627
RK
88
89 /** @brief Period of job in seconds */
49a773eb
RK
90 int period;
91};
460b9539 92
49a773eb
RK
93static int periodic_callback(ev_source *ev_,
94 const struct timeval attribute((unused)) *now,
95 void *u) {
460b9539 96 struct timeval w;
49a773eb 97 struct periodic_data *const pd = u;
460b9539 98
49a773eb 99 pd->callback(ev_);
460b9539 100 gettimeofday(&w, 0);
49a773eb
RK
101 w.tv_sec += pd->period;
102 ev_timeout(ev, 0, &w, periodic_callback, pd);
460b9539 103 return 0;
104}
105
49a773eb 106/** @brief Create a periodic action
59cf25c4 107 * @param ev_ Event loop
49a773eb
RK
108 * @param callback Callback function
109 * @param period Interval between calls in seconds
110 * @param immediate If true, call @p callback straight away
111 */
112static void create_periodic(ev_source *ev_,
113 void (*callback)(ev_source *),
114 int period,
115 int immediate) {
460b9539 116 struct timeval w;
49a773eb 117 struct periodic_data *const pd = xmalloc(sizeof *pd);
460b9539 118
49a773eb
RK
119 pd->callback = callback;
120 pd->period = period;
121 if(immediate)
122 callback(ev_);
460b9539 123 gettimeofday(&w, 0);
49a773eb
RK
124 w.tv_sec += period;
125 ev_timeout(ev_, 0, &w, periodic_callback, pd);
460b9539 126}
127
49a773eb 128static void periodic_rescan(ev_source *ev_) {
dd9af5cb 129 trackdb_rescan(ev_, 1/*check*/, 0, 0);
49a773eb
RK
130}
131
132static void periodic_database_gc(ev_source attribute((unused)) *ev_) {
133 trackdb_gc();
134}
135
136static void periodic_volume_check(ev_source attribute((unused)) *ev_) {
460b9539 137 int l, r;
138 char lb[32], rb[32];
139
b50cfb8a
RK
140 if(api && api->get_volume) {
141 api->get_volume(&l, &r);
460b9539 142 if(l != volume_left || r != volume_right) {
143 volume_left = l;
144 volume_right = r;
145 snprintf(lb, sizeof lb, "%d", l);
146 snprintf(rb, sizeof rb, "%d", r);
147 eventlog("volume", lb, rb, (char *)0);
148 }
149 }
460b9539 150}
151
49a773eb
RK
152static void periodic_play_check(ev_source *ev_) {
153 play(ev_);
154}
460b9539 155
49a773eb
RK
156static void periodic_add_random(ev_source *ev_) {
157 add_random_track(ev_);
460b9539 158}
159
e83d0967
RK
160/* We fix the path to include the bindir and sbindir we were installed into */
161static void fix_path(void) {
162 char *path = getenv("PATH");
e99d42b1 163 static char *newpath;
164 /* static or libgc collects it! */
e83d0967
RK
165
166 if(!path)
2e9ba080 167 disorder_error(0, "PATH is not set at all!");
e83d0967 168
31780899 169 if(*finkbindir && strcmp(finkbindir, "/"))
e83d0967
RK
170 /* We appear to be a finkized mac; include fink on the path in case the
171 * tools we need are there. */
172 byte_xasprintf(&newpath, "PATH=%s:%s:%s:%s",
173 path, bindir, sbindir, finkbindir);
174 else
175 byte_xasprintf(&newpath, "PATH=%s:%s:%s", path, bindir, sbindir);
176 putenv(newpath);
2e9ba080 177 disorder_info("%s", newpath);
e83d0967
RK
178}
179
bcef8d6f
RK
180/* Used by test scripts to wait for things to get ready */
181static void wait_for_root(void) {
182 const char *password;
183
184 while(!trackdb_readable()) {
185 disorder_info("waiting for trackdb...");
186 sleep(1);
187 }
188 trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE);
189 for(;;) {
190 trackdb_open(TRACKDB_READ_ONLY);
191 password = trackdb_get_password("root");
192 trackdb_close();
193 if(password)
194 break;
195 disorder_info("waiting for root user to be created...");
196 sleep(1);
197 }
198 trackdb_deinit(NULL);
199}
200
e83d0967 201int main(int argc, char **argv) {
bcef8d6f 202 int n, background = 1, logsyslog = 0, wfr = 0;
460b9539 203 const char *pidfile = 0;
31e2a93e 204 struct rlimit rl[1];
460b9539 205
206 set_progname(argv);
320598d4 207 mem_init();
2e9ba080
RK
208 if(!setlocale(LC_CTYPE, ""))
209 disorder_fatal(errno, "error calling setlocale");
460b9539 210 /* garbage-collect PCRE's memory */
211 pcre_malloc = xmalloc;
212 pcre_free = xfree;
bcef8d6f 213 while((n = getopt_long(argc, argv, "hVc:dfP:NsW", options, 0)) >= 0) {
460b9539 214 switch(n) {
215 case 'h': help();
3fbdc96d 216 case 'V': version("disorderd");
460b9539 217 case 'c': configfile = optarg; break;
218 case 'd': debugging = 1; break;
219 case 'f': background = 0; break;
220 case 'P': pidfile = optarg; break;
e83d0967 221 case 's': logsyslog = 1; break;
18e6d6e6 222 case 'w': wideopen = 1; break;
bcef8d6f 223 case 'W': wfr = 1; break;
2e9ba080 224 default: disorder_fatal(0, "invalid option");
460b9539 225 }
226 }
bcef8d6f
RK
227 if(wfr) {
228 if(config_read(1, NULL))
229 disorder_fatal(0, "cannot read configuration");
230 wait_for_root();
231 return 0;
232 }
460b9539 233 /* go into background if necessary */
234 if(background)
235 daemonize(progname, LOG_DAEMON, pidfile);
e83d0967
RK
236 else if(logsyslog) {
237 /* If we're running under some kind of daemon supervisor then we may want
238 * to log to syslog but not to go into background */
239 openlog(progname, LOG_PID, LOG_DAEMON);
240 log_default = &log_syslog;
241 }
2e9ba080 242 disorder_info("process ID %lu", (unsigned long)getpid());
e83d0967 243 fix_path();
4265e5d3 244 srand(xtime(0)); /* don't start the same every time */
b12be54a 245 /* gcrypt initialization */
5bf7c546
RK
246 if(!gcry_check_version(NULL))
247 disorder_fatal(0, "gcry_check_version failed");
b12be54a 248 gcry_control(GCRYCTL_INIT_SECMEM, 1);
5bf7c546 249 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
31e2a93e
RK
250 /* make sure we can't have more than FD_SETSIZE files open (event.c does
251 * check but this provides an additional line of defence) */
252 if(getrlimit(RLIMIT_NOFILE, rl) < 0)
2e9ba080 253 disorder_fatal(errno, "getrlimit RLIMIT_NOFILE");
31e2a93e
RK
254 if(rl->rlim_cur > FD_SETSIZE) {
255 rl->rlim_cur = FD_SETSIZE;
256 if(setrlimit(RLIMIT_NOFILE, rl) < 0)
2e9ba080
RK
257 disorder_fatal(errno, "setrlimit to reduce RLIMIT_NOFILE to %lu",
258 (unsigned long)rl->rlim_cur);
259 disorder_info("set RLIM_NOFILE to %lu", (unsigned long)rl->rlim_cur);
31e2a93e 260 } else
2e9ba080 261 disorder_info("RLIM_NOFILE is %lu", (unsigned long)rl->rlim_cur);
460b9539 262 /* create event loop */
263 ev = ev_new();
2e9ba080 264 if(ev_child_setup(ev)) disorder_fatal(0, "ev_child_setup failed");
460b9539 265 /* read config */
b50cfb8a 266 config_uaudio_apis = uaudio_apis;
02ba7921 267 if(config_read(1, NULL))
2e9ba080 268 disorder_fatal(0, "cannot read configuration");
659d87e8
RK
269 /* make sure the home directory exists and has suitable permissions */
270 make_home();
460b9539 271 /* Start the speaker process (as root! - so it can choose its nice value) */
272 speaker_setup(ev);
273 /* set server nice value _after_ starting the speaker, so that they
274 * are independently niceable */
275 xnice(config->nice_server);
276 /* change user */
277 become_mortal();
278 /* make sure we're not root, whatever the config says */
2e9ba080
RK
279 if(getuid() == 0 || geteuid() == 0)
280 disorder_fatal(0, "do not run as root");
460b9539 281 /* open a lockfile - we only want one copy of the server to run at once. */
b4a8d0ff 282 if(1) {
460b9539 283 const char *lockfile;
284 int lockfd;
9ba0617a
RK
285 struct flock lock;
286
460b9539 287 lockfile = config_get_file("lock");
288 if((lockfd = open(lockfile, O_RDWR|O_CREAT, 0600)) < 0)
2e9ba080 289 disorder_fatal(errno, "error opening %s", lockfile);
460b9539 290 cloexec(lockfd);
291 memset(&lock, 0, sizeof lock);
292 lock.l_type = F_WRLCK;
293 lock.l_whence = SEEK_SET;
294 if(fcntl(lockfd, F_SETLK, &lock) < 0)
2e9ba080 295 disorder_fatal(errno, "error locking %s", lockfile);
460b9539 296 }
297 /* initialize database environment */
a745dd43 298 trackdb_init(TRACKDB_NORMAL_RECOVER|TRACKDB_MAY_CREATE);
460b9539 299 trackdb_master(ev);
37f29ce9
RK
300 /* install new config; don't create socket */
301 if(reconfigure(ev, RECONFIGURE_FIRST))
2e9ba080 302 disorder_fatal(0, "failed to read configuration");
36c5e137
RK
303 /* Open the database */
304 trackdb_open(TRACKDB_CAN_UPGRADE);
305 /* load the queue and recently-played list */
306 queue_read();
307 recent_read();
308 /* Arrange timeouts for schedule actions */
309 schedule_init(ev);
f0feb22e
RK
310 /* create a root login */
311 trackdb_create_root();
37f29ce9
RK
312 /* create sockets */
313 reset_sockets(ev);
74f77840
RK
314 /* check for change to database parameters */
315 dbparams_check();
460b9539 316 /* re-read config if we receive a SIGHUP */
2e9ba080
RK
317 if(ev_signal(ev, SIGHUP, handle_sighup, 0))
318 disorder_fatal(0, "ev_signal failed");
460b9539 319 /* exit on SIGINT/SIGTERM */
2e9ba080
RK
320 if(ev_signal(ev, SIGINT, handle_sigint, 0))
321 disorder_fatal(0, "ev_signal failed");
322 if(ev_signal(ev, SIGTERM, handle_sigterm, 0))
323 disorder_fatal(0, "ev_signal failed");
460b9539 324 /* ignore SIGPIPE */
325 signal(SIGPIPE, SIG_IGN);
49a773eb
RK
326 /* Rescan immediately and then daily */
327 create_periodic(ev, periodic_rescan, 86400, 1/*immediate*/);
328 /* Tidy up the database once a minute */
329 create_periodic(ev, periodic_database_gc, 60, 0);
330 /* Check the volume immediately and then once a minute */
331 create_periodic(ev, periodic_volume_check, 60, 1);
332 /* Check for a playable track once a second */
333 create_periodic(ev, periodic_play_check, 1, 0);
1ef295b3
RK
334 /* Try adding a random track immediately and once every two seconds */
335 create_periodic(ev, periodic_add_random, 2, 1);
18f94073
RK
336 /* Issue a rescan when devices are mounted or unmouted */
337 create_periodic(ev, periodic_mount_check, MOUNT_CHECK_INTERVAL, 1);
460b9539 338 /* enter the event loop */
339 n = ev_run(ev);
340 /* if we exit the event loop, something must have gone wrong */
2e9ba080 341 disorder_fatal(errno, "ev_run returned %d", n);
460b9539 342}
343
344/*
345Local Variables:
346c-basic-offset:2
347comment-column:40
e83d0967 348fill-column:79
460b9539 349End:
350*/