chiark / gitweb /
Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/disorder
[disorder] / server / disorderd.c
... / ...
CommitLineData
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
23static ev_source *ev;
24
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' },
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 */
40static 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 */
58static 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
68static 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
75static 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 */
85struct 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
93static 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 */
112static 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
128static void periodic_rescan(ev_source *ev_) {
129 trackdb_rescan(ev_, 1/*check*/, 0, 0);
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_) {
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
152static void periodic_play_check(ev_source *ev_) {
153 play(ev_);
154}
155
156static 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 */
161static 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 */
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
201int 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 config_per_user = 0;
227 if(wfr) {
228 if(config_read(1, NULL))
229 disorder_fatal(0, "cannot read configuration");
230 wait_for_root();
231 return 0;
232 }
233 /* go into background if necessary */
234 if(background)
235 daemonize(progname, LOG_DAEMON, pidfile);
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 }
242 disorder_info("version "VERSION" process ID %lu", (unsigned long)getpid());
243 fix_path();
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);
260 } else
261 disorder_info("RLIM_NOFILE is %lu", (unsigned long)rl->rlim_cur);
262 /* create event loop */
263 ev = ev_new();
264 if(ev_child_setup(ev)) disorder_fatal(0, "ev_child_setup failed");
265 /* read config */
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 */
270 make_home();
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 */
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. */
282 if(1) {
283 const char *lockfile;
284 int lockfd;
285 struct flock lock;
286
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);
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)
295 disorder_fatal(errno, "error locking %s", lockfile);
296 }
297 /* initialize database environment */
298 trackdb_init(TRACKDB_NORMAL_RECOVER|TRACKDB_MAY_CREATE);
299 trackdb_master(ev);
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 */
306 queue_read();
307 recent_read();
308 /* Arrange timeouts for schedule actions */
309 schedule_init(ev);
310 /* create a root login */
311 trackdb_create_root();
312 /* create sockets */
313 reset_sockets(ev);
314 /* check for change to database parameters */
315 dbparams_check();
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");
324 /* ignore SIGPIPE */
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 */
339 n = ev_run(ev);
340 /* if we exit the event loop, something must have gone wrong */
341 disorder_fatal(errno, "ev_run returned %d", n);
342}
343
344/*
345Local Variables:
346c-basic-offset:2
347comment-column:40
348fill-column:79
349End:
350*/