chiark / gitweb /
Move track sorting to its own function. Only choose_populate() uses
[disorder] / server / disorderd.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2004-2008 Richard Kettlewell
460b9539 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 2 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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
05b75f8d 20#include "disorder-server.h"
460b9539 21
22static ev_source *ev;
23
460b9539 24static const struct option options[] = {
25 { "help", no_argument, 0, 'h' },
26 { "version", no_argument, 0, 'V' },
27 { "config", required_argument, 0, 'c' },
28 { "debug", no_argument, 0, 'd' },
29 { "foreground", no_argument, 0, 'f' },
30 { "log", required_argument, 0, 'l' },
31 { "pidfile", required_argument, 0, 'P' },
18e6d6e6 32 { "wide-open", no_argument, 0, 'w' },
e83d0967 33 { "syslog", no_argument, 0, 's' },
460b9539 34 { 0, 0, 0, 0 }
35};
36
37/* display usage message and terminate */
38static void help(void) {
39 xprintf("Usage:\n"
40 " disorderd [OPTIONS]\n"
41 "Options:\n"
42 " --help, -h Display usage message\n"
43 " --version, -V Display version number\n"
44 " --config PATH, -c PATH Set configuration file\n"
45 " --debug, -d Turn on debugging\n"
46 " --foreground, -f Do not become a daemon\n"
e83d0967 47 " --syslog, -s Log to syslog even with -f\n"
460b9539 48 " --pidfile PATH, -P PATH Leave a pidfile\n");
49 xfclose(stdout);
50 exit(0);
51}
52
49a773eb
RK
53/* signals ------------------------------------------------------------------ */
54
460b9539 55/* SIGHUP callback */
56static int handle_sighup(ev_source attribute((unused)) *ev_,
57 int attribute((unused)) sig,
58 void attribute((unused)) *u) {
59 info("received SIGHUP");
60 reconfigure(ev, 1);
61 return 0;
62}
63
64/* fatal signals */
65
66static int handle_sigint(ev_source attribute((unused)) *ev_,
67 int attribute((unused)) sig,
68 void attribute((unused)) *u) {
69 info("received SIGINT");
70 quit(ev);
71}
72
73static int handle_sigterm(ev_source attribute((unused)) *ev_,
74 int attribute((unused)) sig,
75 void attribute((unused)) *u) {
76 info("received SIGTERM");
77 quit(ev);
78}
79
49a773eb
RK
80/* periodic actions --------------------------------------------------------- */
81
82struct periodic_data {
83 void (*callback)(ev_source *);
84 int period;
85};
460b9539 86
49a773eb
RK
87static int periodic_callback(ev_source *ev_,
88 const struct timeval attribute((unused)) *now,
89 void *u) {
460b9539 90 struct timeval w;
49a773eb 91 struct periodic_data *const pd = u;
460b9539 92
49a773eb 93 pd->callback(ev_);
460b9539 94 gettimeofday(&w, 0);
49a773eb
RK
95 w.tv_sec += pd->period;
96 ev_timeout(ev, 0, &w, periodic_callback, pd);
460b9539 97 return 0;
98}
99
49a773eb
RK
100/** @brief Create a periodic action
101 * @param ev Event loop
102 * @param callback Callback function
103 * @param period Interval between calls in seconds
104 * @param immediate If true, call @p callback straight away
105 */
106static void create_periodic(ev_source *ev_,
107 void (*callback)(ev_source *),
108 int period,
109 int immediate) {
460b9539 110 struct timeval w;
49a773eb 111 struct periodic_data *const pd = xmalloc(sizeof *pd);
460b9539 112
49a773eb
RK
113 pd->callback = callback;
114 pd->period = period;
115 if(immediate)
116 callback(ev_);
460b9539 117 gettimeofday(&w, 0);
49a773eb
RK
118 w.tv_sec += period;
119 ev_timeout(ev_, 0, &w, periodic_callback, pd);
460b9539 120}
121
49a773eb 122static void periodic_rescan(ev_source *ev_) {
dd9af5cb 123 trackdb_rescan(ev_, 1/*check*/, 0, 0);
49a773eb
RK
124}
125
126static void periodic_database_gc(ev_source attribute((unused)) *ev_) {
127 trackdb_gc();
128}
129
130static void periodic_volume_check(ev_source attribute((unused)) *ev_) {
460b9539 131 int l, r;
132 char lb[32], rb[32];
133
3c499fe7 134 if(!mixer_control(-1/*as configured*/, &l, &r, 0)) {
460b9539 135 if(l != volume_left || r != volume_right) {
136 volume_left = l;
137 volume_right = r;
138 snprintf(lb, sizeof lb, "%d", l);
139 snprintf(rb, sizeof rb, "%d", r);
140 eventlog("volume", lb, rb, (char *)0);
141 }
142 }
460b9539 143}
144
49a773eb
RK
145static void periodic_play_check(ev_source *ev_) {
146 play(ev_);
147}
460b9539 148
49a773eb
RK
149static void periodic_add_random(ev_source *ev_) {
150 add_random_track(ev_);
460b9539 151}
152
e83d0967
RK
153/* We fix the path to include the bindir and sbindir we were installed into */
154static void fix_path(void) {
155 char *path = getenv("PATH");
e99d42b1 156 static char *newpath;
157 /* static or libgc collects it! */
e83d0967
RK
158
159 if(!path)
160 error(0, "PATH is not set at all!");
161
31780899 162 if(*finkbindir && strcmp(finkbindir, "/"))
e83d0967
RK
163 /* We appear to be a finkized mac; include fink on the path in case the
164 * tools we need are there. */
165 byte_xasprintf(&newpath, "PATH=%s:%s:%s:%s",
166 path, bindir, sbindir, finkbindir);
167 else
168 byte_xasprintf(&newpath, "PATH=%s:%s:%s", path, bindir, sbindir);
169 putenv(newpath);
170 info("%s", newpath);
171}
172
173int main(int argc, char **argv) {
174 int n, background = 1, logsyslog = 0;
460b9539 175 const char *pidfile = 0;
460b9539 176
177 set_progname(argv);
320598d4 178 mem_init();
460b9539 179 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
180 /* garbage-collect PCRE's memory */
181 pcre_malloc = xmalloc;
182 pcre_free = xfree;
e83d0967 183 while((n = getopt_long(argc, argv, "hVc:dfP:Ns", options, 0)) >= 0) {
460b9539 184 switch(n) {
185 case 'h': help();
3fbdc96d 186 case 'V': version("disorderd");
460b9539 187 case 'c': configfile = optarg; break;
188 case 'd': debugging = 1; break;
189 case 'f': background = 0; break;
190 case 'P': pidfile = optarg; break;
e83d0967 191 case 's': logsyslog = 1; break;
18e6d6e6 192 case 'w': wideopen = 1; break;
460b9539 193 default: fatal(0, "invalid option");
194 }
195 }
196 /* go into background if necessary */
197 if(background)
198 daemonize(progname, LOG_DAEMON, pidfile);
e83d0967
RK
199 else if(logsyslog) {
200 /* If we're running under some kind of daemon supervisor then we may want
201 * to log to syslog but not to go into background */
202 openlog(progname, LOG_PID, LOG_DAEMON);
203 log_default = &log_syslog;
204 }
460b9539 205 info("process ID %lu", (unsigned long)getpid());
e83d0967 206 fix_path();
460b9539 207 srand(time(0)); /* don't start the same every time */
b12be54a
RK
208 /* gcrypt initialization */
209 gcry_control(GCRYCTL_INIT_SECMEM, 1);
460b9539 210 /* create event loop */
211 ev = ev_new();
212 if(ev_child_setup(ev)) fatal(0, "ev_child_setup failed");
213 /* read config */
c00fce3a 214 if(config_read(1))
460b9539 215 fatal(0, "cannot read configuration");
659d87e8
RK
216 /* make sure the home directory exists and has suitable permissions */
217 make_home();
460b9539 218 /* Start the speaker process (as root! - so it can choose its nice value) */
219 speaker_setup(ev);
220 /* set server nice value _after_ starting the speaker, so that they
221 * are independently niceable */
222 xnice(config->nice_server);
223 /* change user */
224 become_mortal();
225 /* make sure we're not root, whatever the config says */
226 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
227 /* open a lockfile - we only want one copy of the server to run at once. */
228 if(config->lock) {
229 const char *lockfile;
230 int lockfd;
9ba0617a
RK
231 struct flock lock;
232
460b9539 233 lockfile = config_get_file("lock");
234 if((lockfd = open(lockfile, O_RDWR|O_CREAT, 0600)) < 0)
235 fatal(errno, "error opening %s", lockfile);
236 cloexec(lockfd);
237 memset(&lock, 0, sizeof lock);
238 lock.l_type = F_WRLCK;
239 lock.l_whence = SEEK_SET;
240 if(fcntl(lockfd, F_SETLK, &lock) < 0)
241 fatal(errno, "error locking %s", lockfile);
242 }
243 /* initialize database environment */
a745dd43 244 trackdb_init(TRACKDB_NORMAL_RECOVER|TRACKDB_MAY_CREATE);
460b9539 245 trackdb_master(ev);
f0feb22e 246 /* install new config (calls trackdb_open()) */
460b9539 247 reconfigure(ev, 0);
f0feb22e
RK
248 /* pull in old users */
249 trackdb_old_users();
250 /* create a root login */
251 trackdb_create_root();
460b9539 252 /* re-read config if we receive a SIGHUP */
253 if(ev_signal(ev, SIGHUP, handle_sighup, 0)) fatal(0, "ev_signal failed");
254 /* exit on SIGINT/SIGTERM */
255 if(ev_signal(ev, SIGINT, handle_sigint, 0)) fatal(0, "ev_signal failed");
256 if(ev_signal(ev, SIGTERM, handle_sigterm, 0)) fatal(0, "ev_signal failed");
257 /* ignore SIGPIPE */
258 signal(SIGPIPE, SIG_IGN);
49a773eb
RK
259 /* Rescan immediately and then daily */
260 create_periodic(ev, periodic_rescan, 86400, 1/*immediate*/);
261 /* Tidy up the database once a minute */
262 create_periodic(ev, periodic_database_gc, 60, 0);
263 /* Check the volume immediately and then once a minute */
264 create_periodic(ev, periodic_volume_check, 60, 1);
265 /* Check for a playable track once a second */
266 create_periodic(ev, periodic_play_check, 1, 0);
1ef295b3
RK
267 /* Try adding a random track immediately and once every two seconds */
268 create_periodic(ev, periodic_add_random, 2, 1);
460b9539 269 /* enter the event loop */
270 n = ev_run(ev);
271 /* if we exit the event loop, something must have gone wrong */
272 fatal(errno, "ev_run returned %d", n);
273}
274
275/*
276Local Variables:
277c-basic-offset:2
278comment-column:40
e83d0967 279fill-column:79
460b9539 280End:
281*/