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