2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2012 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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file server/disorderd.c
19 * @brief Main DisOrder server
21 #include "disorder-server.h"
25 static 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' },
33 { "wide-open", no_argument, 0, 'w' },
34 { "wait-for-root", no_argument, 0, 'W' },
35 { "syslog", no_argument, 0, 's' },
39 /* display usage message and terminate */
40 static void help(void) {
42 " disorderd [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"
49 " --syslog, -s Log to syslog even with -f\n"
50 " --pidfile PATH, -P PATH Leave a pidfile\n");
55 /* signals ------------------------------------------------------------------ */
58 static int handle_sighup(ev_source attribute((unused)) *ev_,
59 int attribute((unused)) sig,
60 void attribute((unused)) *u) {
61 disorder_info("received SIGHUP");
62 reconfigure(ev, RECONFIGURE_RELOADING);
68 static int handle_sigint(ev_source attribute((unused)) *ev_,
69 int attribute((unused)) sig,
70 void attribute((unused)) *u) {
71 disorder_info("received SIGINT");
75 static int handle_sigterm(ev_source attribute((unused)) *ev_,
76 int attribute((unused)) sig,
77 void attribute((unused)) *u) {
78 disorder_info("received SIGTERM");
82 /* periodic actions --------------------------------------------------------- */
84 /** @brief A job executed periodically by the server */
85 struct periodic_data {
86 /** @brief Callback to process job */
87 void (*callback)(ev_source *);
89 /** @brief Period of job in seconds */
93 static int periodic_callback(ev_source *ev_,
94 const struct timeval attribute((unused)) *now,
97 struct periodic_data *const pd = u;
101 w.tv_sec += pd->period;
102 ev_timeout(ev, 0, &w, periodic_callback, pd);
106 /** @brief Create a periodic action
107 * @param ev_ Event loop
108 * @param callback Callback function
109 * @param period Interval between calls in seconds
110 * @param immediate If true, call @p callback straight away
112 static void create_periodic(ev_source *ev_,
113 void (*callback)(ev_source *),
117 struct periodic_data *const pd = xmalloc(sizeof *pd);
119 pd->callback = callback;
125 ev_timeout(ev_, 0, &w, periodic_callback, pd);
128 static void periodic_rescan(ev_source *ev_) {
129 trackdb_rescan(ev_, 1/*check*/, 0, 0);
132 static void periodic_database_gc(ev_source attribute((unused)) *ev_) {
136 static void periodic_volume_check(ev_source attribute((unused)) *ev_) {
140 if(api && api->get_volume) {
141 api->get_volume(&l, &r);
142 if(l != volume_left || r != volume_right) {
145 snprintf(lb, sizeof lb, "%d", l);
146 snprintf(rb, sizeof rb, "%d", r);
147 eventlog("volume", lb, rb, (char *)0);
152 static void periodic_play_check(ev_source *ev_) {
156 static void periodic_add_random(ev_source *ev_) {
157 add_random_track(ev_);
160 /* We fix the path to include the bindir and sbindir we were installed into */
161 static void fix_path(void) {
162 char *path = getenv("PATH");
163 static char *newpath;
164 /* static or libgc collects it! */
167 disorder_error(0, "PATH is not set at all!");
169 if(*finkbindir && strcmp(finkbindir, "/"))
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);
175 byte_xasprintf(&newpath, "PATH=%s:%s:%s", path, bindir, sbindir);
177 disorder_info("%s", newpath);
180 /* Used by test scripts to wait for things to get ready */
181 static void wait_for_root(void) {
182 const char *password;
184 while(!trackdb_readable()) {
185 disorder_info("waiting for trackdb...");
188 trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE);
190 trackdb_open(TRACKDB_READ_ONLY);
191 password = trackdb_get_password("root");
195 disorder_info("waiting for root user to be created...");
198 trackdb_deinit(NULL);
201 int main(int argc, char **argv) {
202 int n, background = 1, logsyslog = 0, wfr = 0;
203 const char *pidfile = 0;
208 if(!setlocale(LC_CTYPE, ""))
209 disorder_fatal(errno, "error calling setlocale");
210 /* garbage-collect PCRE's memory */
212 while((n = getopt_long(argc, argv, "hVc:dfP:NsW", options, 0)) >= 0) {
215 case 'V': version("disorderd");
216 case 'c': configfile = optarg; break;
217 case 'd': debugging = 1; break;
218 case 'f': background = 0; break;
219 case 'P': pidfile = optarg; break;
220 case 's': logsyslog = 1; break;
221 case 'w': wideopen = 1; break;
222 case 'W': wfr = 1; break;
223 default: disorder_fatal(0, "invalid option");
227 if(config_read(1, NULL))
228 disorder_fatal(0, "cannot read configuration");
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 disorder_info("version "VERSION" process ID %lu", (unsigned long)getpid());
243 srand(xtime(0)); /* don't start the same every time */
244 /* gcrypt initialization */
245 if(!gcry_check_version(NULL))
246 disorder_fatal(0, "gcry_check_version failed");
247 gcry_control(GCRYCTL_INIT_SECMEM, 1);
248 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
249 /* make sure we can't have more than FD_SETSIZE files open (event.c does
250 * check but this provides an additional line of defence) */
251 if(getrlimit(RLIMIT_NOFILE, rl) < 0)
252 disorder_fatal(errno, "getrlimit RLIMIT_NOFILE");
253 if(rl->rlim_cur > FD_SETSIZE) {
254 rl->rlim_cur = FD_SETSIZE;
255 if(setrlimit(RLIMIT_NOFILE, rl) < 0)
256 disorder_fatal(errno, "setrlimit to reduce RLIMIT_NOFILE to %lu",
257 (unsigned long)rl->rlim_cur);
258 disorder_info("set RLIM_NOFILE to %lu", (unsigned long)rl->rlim_cur);
260 disorder_info("RLIM_NOFILE is %lu", (unsigned long)rl->rlim_cur);
261 /* create event loop */
263 if(ev_child_setup(ev)) disorder_fatal(0, "ev_child_setup failed");
265 config_uaudio_apis = uaudio_apis;
266 if(config_read(1, NULL))
267 disorder_fatal(0, "cannot read configuration");
268 /* make sure the home directory exists and has suitable permissions */
270 /* Start the speaker process (as root! - so it can choose its nice value) */
272 /* set server nice value _after_ starting the speaker, so that they
273 * are independently niceable */
274 xnice(config->nice_server);
277 /* make sure we're not root, whatever the config says */
278 if(getuid() == 0 || geteuid() == 0)
279 disorder_fatal(0, "do not run as root");
280 /* open a lockfile - we only want one copy of the server to run at once. */
282 const char *lockfile;
286 lockfile = config_get_file("lock");
287 if((lockfd = open(lockfile, O_RDWR|O_CREAT, 0600)) < 0)
288 disorder_fatal(errno, "error opening %s", lockfile);
290 memset(&lock, 0, sizeof lock);
291 lock.l_type = F_WRLCK;
292 lock.l_whence = SEEK_SET;
293 if(fcntl(lockfd, F_SETLK, &lock) < 0)
294 disorder_fatal(errno, "error locking %s", lockfile);
296 /* initialize database environment */
297 trackdb_init(TRACKDB_NORMAL_RECOVER|TRACKDB_MAY_CREATE);
299 /* install new config; don't create socket */
300 if(reconfigure(ev, RECONFIGURE_FIRST))
301 disorder_fatal(0, "failed to read configuration");
302 /* Open the database */
303 trackdb_open(TRACKDB_CAN_UPGRADE);
304 /* load the queue and recently-played list */
307 /* Arrange timeouts for schedule actions */
309 /* create a root login */
310 trackdb_create_root();
313 /* check for change to database parameters */
315 /* re-read config if we receive a SIGHUP */
316 if(ev_signal(ev, SIGHUP, handle_sighup, 0))
317 disorder_fatal(0, "ev_signal failed");
318 /* exit on SIGINT/SIGTERM */
319 if(ev_signal(ev, SIGINT, handle_sigint, 0))
320 disorder_fatal(0, "ev_signal failed");
321 if(ev_signal(ev, SIGTERM, handle_sigterm, 0))
322 disorder_fatal(0, "ev_signal failed");
324 signal(SIGPIPE, SIG_IGN);
325 /* Rescan immediately and then daily */
326 create_periodic(ev, periodic_rescan, 86400, 1/*immediate*/);
327 /* Tidy up the database once a minute */
328 create_periodic(ev, periodic_database_gc, 60, 0);
329 /* Check the volume immediately and then once a minute */
330 create_periodic(ev, periodic_volume_check, 60, 1);
331 /* Check for a playable track once a second */
332 create_periodic(ev, periodic_play_check, 1, 0);
333 /* Try adding a random track immediately and once every two seconds */
334 create_periodic(ev, periodic_add_random, 2, 1);
335 /* Issue a rescan when devices are mounted or unmouted */
336 create_periodic(ev, periodic_mount_check, MOUNT_CHECK_INTERVAL, 1);
337 /* enter the event loop */
339 /* if we exit the event loop, something must have gone wrong */
340 disorder_fatal(errno, "ev_run returned %d", n);