lib/configuration.c, lib/uaudio-rtp.c: Allow configuring payload size.
[disorder] / server / disorderd.c
1 /*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2012 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 3 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,
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 *
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/>.
17 */
18 /** @file server/disorderd.c
19 * @brief Main DisOrder server
20 */
21 #include "disorder-server.h"
22
23 static ev_source *ev;
24
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' },
36 { 0, 0, 0, 0 }
37 };
38
39 /* display usage message and terminate */
40 static void attribute((noreturn)) 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"
49 " --syslog, -s Log to syslog even with -f\n"
50 " --pidfile PATH, -P PATH Leave a pidfile\n");
51 xfclose(stdout);
52 exit(0);
53 }
54
55 /* signals ------------------------------------------------------------------ */
56
57 /* SIGHUP callback */
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);
63 return 0;
64 }
65
66 /* fatal signals */
67
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");
72 quit(ev);
73 }
74
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");
79 quit(ev);
80 }
81
82 /* periodic actions --------------------------------------------------------- */
83
84 /** @brief A job executed periodically by the server */
85 struct periodic_data {
86 /** @brief Callback to process job */
87 void (*callback)(ev_source *);
88
89 /** @brief Period of job in seconds */
90 int period;
91 };
92
93 static int periodic_callback(ev_source *ev_,
94 const struct timeval attribute((unused)) *now,
95 void *u) {
96 struct timeval w;
97 struct periodic_data *const pd = u;
98
99 pd->callback(ev_);
100 gettimeofday(&w, 0);
101 w.tv_sec += pd->period;
102 ev_timeout(ev, 0, &w, periodic_callback, pd);
103 return 0;
104 }
105
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
111 */
112 static void create_periodic(ev_source *ev_,
113 void (*callback)(ev_source *),
114 int period,
115 int immediate) {
116 struct timeval w;
117 struct periodic_data *const pd = xmalloc(sizeof *pd);
118
119 pd->callback = callback;
120 pd->period = period;
121 if(immediate)
122 callback(ev_);
123 gettimeofday(&w, 0);
124 w.tv_sec += period;
125 ev_timeout(ev_, 0, &w, periodic_callback, pd);
126 }
127
128 static void periodic_rescan(ev_source *ev_) {
129 trackdb_rescan(ev_, 1/*check*/, 0, 0);
130 }
131
132 static void periodic_database_gc(ev_source attribute((unused)) *ev_) {
133 trackdb_gc();
134 }
135
136 static void periodic_volume_check(ev_source attribute((unused)) *ev_) {
137 int l, r;
138 char lb[32], rb[32];
139
140 if(api && api->get_volume) {
141 api->get_volume(&l, &r);
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 }
150 }
151
152 static void periodic_play_check(ev_source *ev_) {
153 play(ev_);
154 }
155
156 static void periodic_add_random(ev_source *ev_) {
157 add_random_track(ev_);
158 }
159
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! */
165
166 if(!path)
167 disorder_error(0, "PATH is not set at all!");
168
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);
174 else
175 byte_xasprintf(&newpath, "PATH=%s:%s:%s", path, bindir, sbindir);
176 putenv(newpath);
177 disorder_info("%s", newpath);
178 }
179
180 /* Used by test scripts to wait for things to get ready */
181 static 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
201 int main(int argc, char **argv) {
202 int n, background = 1, logsyslog = 0, wfr = 0;
203 const char *pidfile = 0;
204 struct rlimit rl[1];
205
206 set_progname(argv);
207 mem_init();
208 if(!setlocale(LC_CTYPE, ""))
209 disorder_fatal(errno, "error calling setlocale");
210 /* garbage-collect PCRE's memory */
211 regexp_setup();
212 while((n = getopt_long(argc, argv, "hVc:dfP:NsW", options, 0)) >= 0) {
213 switch(n) {
214 case 'h': help();
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");
224 }
225 }
226 if(wfr) {
227 if(config_read(1, NULL))
228 disorder_fatal(0, "cannot read configuration");
229 wait_for_root();
230 return 0;
231 }
232 /* go into background if necessary */
233 if(background)
234 daemonize(progname, LOG_DAEMON, pidfile);
235 else if(logsyslog) {
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;
240 }
241 disorder_info("version "VERSION" process ID %lu", (unsigned long)getpid());
242 fix_path();
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);
259 } else
260 disorder_info("RLIM_NOFILE is %lu", (unsigned long)rl->rlim_cur);
261 /* create event loop */
262 ev = ev_new();
263 if(ev_child_setup(ev)) disorder_fatal(0, "ev_child_setup failed");
264 /* read config */
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 */
269 make_home();
270 /* Start the speaker process (as root! - so it can choose its nice value) */
271 speaker_setup(ev);
272 /* set server nice value _after_ starting the speaker, so that they
273 * are independently niceable */
274 xnice(config->nice_server);
275 /* change user */
276 become_mortal();
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. */
281 if(1) {
282 const char *lockfile;
283 int lockfd;
284 struct flock lock;
285
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);
289 cloexec(lockfd);
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);
295 }
296 /* initialize database environment */
297 trackdb_init(TRACKDB_NORMAL_RECOVER|TRACKDB_MAY_CREATE);
298 trackdb_master(ev);
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 */
305 queue_read();
306 recent_read();
307 /* Arrange timeouts for schedule actions */
308 schedule_init(ev);
309 /* create a root login */
310 trackdb_create_root();
311 /* create sockets */
312 reset_sockets(ev);
313 /* check for change to database parameters */
314 dbparams_check();
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");
323 /* ignore SIGPIPE */
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 */
338 n = ev_run(ev);
339 /* if we exit the event loop, something must have gone wrong */
340 disorder_fatal(errno, "ev_run returned %d", n);
341 }
342
343 /*
344 Local Variables:
345 c-basic-offset:2
346 comment-column:40
347 fill-column:79
348 End:
349 */