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