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 */
211 pcre_malloc
= xmalloc
;
213 while((n
= getopt_long(argc
, argv
, "hVc:dfP:NsW", options
, 0)) >= 0) {
216 case 'V': version("disorderd");
217 case 'c': configfile
= optarg
; break;
218 case 'd': debugging
= 1; break;
219 case 'f': background
= 0; break;
220 case 'P': pidfile
= optarg
; break;
221 case 's': logsyslog
= 1; break;
222 case 'w': wideopen
= 1; break;
223 case 'W': wfr
= 1; break;
224 default: disorder_fatal(0, "invalid option");
228 if(config_read(1, NULL
))
229 disorder_fatal(0, "cannot read configuration");
233 /* go into background if necessary */
235 daemonize(progname
, LOG_DAEMON
, pidfile
);
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
;
242 disorder_info("version "VERSION
" process ID %lu", (unsigned long)getpid());
244 srand(xtime(0)); /* don't start the same every time */
245 /* gcrypt initialization */
246 if(!gcry_check_version(NULL
))
247 disorder_fatal(0, "gcry_check_version failed");
248 gcry_control(GCRYCTL_INIT_SECMEM
, 1);
249 gcry_control (GCRYCTL_INITIALIZATION_FINISHED
, 0);
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)
253 disorder_fatal(errno
, "getrlimit RLIMIT_NOFILE");
254 if(rl
->rlim_cur
> FD_SETSIZE
) {
255 rl
->rlim_cur
= FD_SETSIZE
;
256 if(setrlimit(RLIMIT_NOFILE
, rl
) < 0)
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
);
261 disorder_info("RLIM_NOFILE is %lu", (unsigned long)rl
->rlim_cur
);
262 /* create event loop */
264 if(ev_child_setup(ev
)) disorder_fatal(0, "ev_child_setup failed");
266 config_uaudio_apis
= uaudio_apis
;
267 if(config_read(1, NULL
))
268 disorder_fatal(0, "cannot read configuration");
269 /* make sure the home directory exists and has suitable permissions */
271 /* Start the speaker process (as root! - so it can choose its nice value) */
273 /* set server nice value _after_ starting the speaker, so that they
274 * are independently niceable */
275 xnice(config
->nice_server
);
278 /* make sure we're not root, whatever the config says */
279 if(getuid() == 0 || geteuid() == 0)
280 disorder_fatal(0, "do not run as root");
281 /* open a lockfile - we only want one copy of the server to run at once. */
283 const char *lockfile
;
287 lockfile
= config_get_file("lock");
288 if((lockfd
= open(lockfile
, O_RDWR
|O_CREAT
, 0600)) < 0)
289 disorder_fatal(errno
, "error opening %s", lockfile
);
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)
295 disorder_fatal(errno
, "error locking %s", lockfile
);
297 /* initialize database environment */
298 trackdb_init(TRACKDB_NORMAL_RECOVER
|TRACKDB_MAY_CREATE
);
300 /* install new config; don't create socket */
301 if(reconfigure(ev
, RECONFIGURE_FIRST
))
302 disorder_fatal(0, "failed to read configuration");
303 /* Open the database */
304 trackdb_open(TRACKDB_CAN_UPGRADE
);
305 /* load the queue and recently-played list */
308 /* Arrange timeouts for schedule actions */
310 /* create a root login */
311 trackdb_create_root();
314 /* check for change to database parameters */
316 /* re-read config if we receive a SIGHUP */
317 if(ev_signal(ev
, SIGHUP
, handle_sighup
, 0))
318 disorder_fatal(0, "ev_signal failed");
319 /* exit on SIGINT/SIGTERM */
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");
325 signal(SIGPIPE
, SIG_IGN
);
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);
334 /* Try adding a random track immediately and once every two seconds */
335 create_periodic(ev
, periodic_add_random
, 2, 1);
336 /* Issue a rescan when devices are mounted or unmouted */
337 create_periodic(ev
, periodic_mount_check
, MOUNT_CHECK_INTERVAL
, 1);
338 /* enter the event loop */
340 /* if we exit the event loop, something must have gone wrong */
341 disorder_fatal(errno
, "ev_run returned %d", n
);