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