Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
3 | * Copyright (C) 2004, 2005, 2006 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 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 | */ | |
20 | ||
21 | #include <config.h> | |
6d2d327c | 22 | #include "types.h" |
460b9539 | 23 | |
24 | #include <stdio.h> | |
25 | #include <getopt.h> | |
26 | #include <pwd.h> | |
27 | #include <grp.h> | |
28 | #include <sys/types.h> | |
29 | #include <errno.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | #include <unistd.h> | |
33 | #include <signal.h> | |
34 | #include <sys/socket.h> | |
35 | #include <time.h> | |
36 | #include <locale.h> | |
37 | #include <syslog.h> | |
38 | #include <sys/time.h> | |
39 | #include <pcre.h> | |
40 | #include <fcntl.h> | |
41 | ||
42 | #include "daemonize.h" | |
43 | #include "event.h" | |
44 | #include "log.h" | |
45 | #include "configuration.h" | |
46 | #include "trackdb.h" | |
47 | #include "queue.h" | |
48 | #include "mem.h" | |
49 | #include "play.h" | |
50 | #include "server.h" | |
51 | #include "state.h" | |
52 | #include "syscalls.h" | |
53 | #include "defs.h" | |
54 | #include "user.h" | |
55 | #include "mixer.h" | |
56 | #include "eventlog.h" | |
e83d0967 | 57 | #include "printf.h" |
460b9539 | 58 | |
59 | static ev_source *ev; | |
60 | ||
61 | static void rescan_after(long offset); | |
62 | static void dbgc_after(long offset); | |
63 | static void volumecheck_after(long offset); | |
64 | ||
65 | static const struct option options[] = { | |
66 | { "help", no_argument, 0, 'h' }, | |
67 | { "version", no_argument, 0, 'V' }, | |
68 | { "config", required_argument, 0, 'c' }, | |
69 | { "debug", no_argument, 0, 'd' }, | |
70 | { "foreground", no_argument, 0, 'f' }, | |
71 | { "log", required_argument, 0, 'l' }, | |
72 | { "pidfile", required_argument, 0, 'P' }, | |
73 | { "no-initial-rescan", no_argument, 0, 'N' }, | |
18e6d6e6 | 74 | { "wide-open", no_argument, 0, 'w' }, |
e83d0967 | 75 | { "syslog", no_argument, 0, 's' }, |
460b9539 | 76 | { 0, 0, 0, 0 } |
77 | }; | |
78 | ||
79 | /* display usage message and terminate */ | |
80 | static void help(void) { | |
81 | xprintf("Usage:\n" | |
82 | " disorderd [OPTIONS]\n" | |
83 | "Options:\n" | |
84 | " --help, -h Display usage message\n" | |
85 | " --version, -V Display version number\n" | |
86 | " --config PATH, -c PATH Set configuration file\n" | |
87 | " --debug, -d Turn on debugging\n" | |
88 | " --foreground, -f Do not become a daemon\n" | |
e83d0967 | 89 | " --syslog, -s Log to syslog even with -f\n" |
460b9539 | 90 | " --pidfile PATH, -P PATH Leave a pidfile\n"); |
91 | xfclose(stdout); | |
92 | exit(0); | |
93 | } | |
94 | ||
95 | /* display version number and terminate */ | |
96 | static void version(void) { | |
97 | xprintf("disorderd version %s\n", disorder_version_string); | |
98 | xfclose(stdout); | |
99 | exit(0); | |
100 | } | |
101 | ||
102 | /* SIGHUP callback */ | |
103 | static int handle_sighup(ev_source attribute((unused)) *ev_, | |
104 | int attribute((unused)) sig, | |
105 | void attribute((unused)) *u) { | |
106 | info("received SIGHUP"); | |
107 | reconfigure(ev, 1); | |
108 | return 0; | |
109 | } | |
110 | ||
111 | /* fatal signals */ | |
112 | ||
113 | static int handle_sigint(ev_source attribute((unused)) *ev_, | |
114 | int attribute((unused)) sig, | |
115 | void attribute((unused)) *u) { | |
116 | info("received SIGINT"); | |
117 | quit(ev); | |
118 | } | |
119 | ||
120 | static int handle_sigterm(ev_source attribute((unused)) *ev_, | |
121 | int attribute((unused)) sig, | |
122 | void attribute((unused)) *u) { | |
123 | info("received SIGTERM"); | |
124 | quit(ev); | |
125 | } | |
126 | ||
127 | static int rescan_again(ev_source *ev_, | |
128 | const struct timeval attribute((unused)) *now, | |
129 | void attribute((unused)) *u) { | |
130 | trackdb_rescan(ev_); | |
131 | rescan_after(86400); | |
132 | return 0; | |
133 | } | |
134 | ||
135 | static void rescan_after(long offset) { | |
136 | struct timeval w; | |
137 | ||
138 | gettimeofday(&w, 0); | |
139 | w.tv_sec += offset; | |
140 | ev_timeout(ev, 0, &w, rescan_again, 0); | |
141 | } | |
142 | ||
143 | static int dbgc_again(ev_source attribute((unused)) *ev_, | |
144 | const struct timeval attribute((unused)) *now, | |
145 | void attribute((unused)) *u) { | |
146 | trackdb_gc(); | |
147 | dbgc_after(60); | |
148 | return 0; | |
149 | } | |
150 | ||
151 | static void dbgc_after(long offset) { | |
152 | struct timeval w; | |
153 | ||
154 | gettimeofday(&w, 0); | |
155 | w.tv_sec += offset; | |
156 | ev_timeout(ev, 0, &w, dbgc_again, 0); | |
157 | } | |
158 | ||
159 | static int volumecheck_again(ev_source attribute((unused)) *ev_, | |
160 | const struct timeval attribute((unused)) *now, | |
161 | void attribute((unused)) *u) { | |
162 | int l, r; | |
163 | char lb[32], rb[32]; | |
164 | ||
165 | if(!mixer_control(&l, &r, 0)) { | |
166 | if(l != volume_left || r != volume_right) { | |
167 | volume_left = l; | |
168 | volume_right = r; | |
169 | snprintf(lb, sizeof lb, "%d", l); | |
170 | snprintf(rb, sizeof rb, "%d", r); | |
171 | eventlog("volume", lb, rb, (char *)0); | |
172 | } | |
173 | } | |
174 | volumecheck_after(60); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | static void volumecheck_after(long offset) { | |
179 | struct timeval w; | |
180 | ||
181 | gettimeofday(&w, 0); | |
182 | w.tv_sec += offset; | |
183 | ev_timeout(ev, 0, &w, volumecheck_again, 0); | |
184 | } | |
185 | ||
e83d0967 RK |
186 | /* We fix the path to include the bindir and sbindir we were installed into */ |
187 | static void fix_path(void) { | |
188 | char *path = getenv("PATH"); | |
e99d42b1 | 189 | static char *newpath; |
190 | /* static or libgc collects it! */ | |
e83d0967 RK |
191 | |
192 | if(!path) | |
193 | error(0, "PATH is not set at all!"); | |
194 | ||
31780899 | 195 | if(*finkbindir && strcmp(finkbindir, "/")) |
e83d0967 RK |
196 | /* We appear to be a finkized mac; include fink on the path in case the |
197 | * tools we need are there. */ | |
198 | byte_xasprintf(&newpath, "PATH=%s:%s:%s:%s", | |
199 | path, bindir, sbindir, finkbindir); | |
200 | else | |
201 | byte_xasprintf(&newpath, "PATH=%s:%s:%s", path, bindir, sbindir); | |
202 | putenv(newpath); | |
203 | info("%s", newpath); | |
204 | } | |
205 | ||
206 | int main(int argc, char **argv) { | |
207 | int n, background = 1, logsyslog = 0; | |
460b9539 | 208 | const char *pidfile = 0; |
209 | int initial_rescan = 1; | |
210 | ||
211 | set_progname(argv); | |
320598d4 | 212 | mem_init(); |
460b9539 | 213 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
214 | /* garbage-collect PCRE's memory */ | |
215 | pcre_malloc = xmalloc; | |
216 | pcre_free = xfree; | |
e83d0967 | 217 | while((n = getopt_long(argc, argv, "hVc:dfP:Ns", options, 0)) >= 0) { |
460b9539 | 218 | switch(n) { |
219 | case 'h': help(); | |
220 | case 'V': version(); | |
221 | case 'c': configfile = optarg; break; | |
222 | case 'd': debugging = 1; break; | |
223 | case 'f': background = 0; break; | |
224 | case 'P': pidfile = optarg; break; | |
225 | case 'N': initial_rescan = 0; break; | |
e83d0967 | 226 | case 's': logsyslog = 1; break; |
18e6d6e6 | 227 | case 'w': wideopen = 1; break; |
460b9539 | 228 | default: fatal(0, "invalid option"); |
229 | } | |
230 | } | |
231 | /* go into background if necessary */ | |
232 | if(background) | |
233 | daemonize(progname, LOG_DAEMON, pidfile); | |
e83d0967 RK |
234 | else if(logsyslog) { |
235 | /* If we're running under some kind of daemon supervisor then we may want | |
236 | * to log to syslog but not to go into background */ | |
237 | openlog(progname, LOG_PID, LOG_DAEMON); | |
238 | log_default = &log_syslog; | |
239 | } | |
460b9539 | 240 | info("process ID %lu", (unsigned long)getpid()); |
e83d0967 | 241 | fix_path(); |
460b9539 | 242 | srand(time(0)); /* don't start the same every time */ |
243 | /* create event loop */ | |
244 | ev = ev_new(); | |
245 | if(ev_child_setup(ev)) fatal(0, "ev_child_setup failed"); | |
246 | /* read config */ | |
c00fce3a | 247 | if(config_read(1)) |
460b9539 | 248 | fatal(0, "cannot read configuration"); |
249 | /* Start the speaker process (as root! - so it can choose its nice value) */ | |
250 | speaker_setup(ev); | |
251 | /* set server nice value _after_ starting the speaker, so that they | |
252 | * are independently niceable */ | |
253 | xnice(config->nice_server); | |
254 | /* change user */ | |
255 | become_mortal(); | |
256 | /* make sure we're not root, whatever the config says */ | |
257 | if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root"); | |
258 | /* open a lockfile - we only want one copy of the server to run at once. */ | |
259 | if(config->lock) { | |
260 | const char *lockfile; | |
261 | int lockfd; | |
262 | struct flock lock; | |
263 | ||
264 | lockfile = config_get_file("lock"); | |
265 | if((lockfd = open(lockfile, O_RDWR|O_CREAT, 0600)) < 0) | |
266 | fatal(errno, "error opening %s", lockfile); | |
267 | cloexec(lockfd); | |
268 | memset(&lock, 0, sizeof lock); | |
269 | lock.l_type = F_WRLCK; | |
270 | lock.l_whence = SEEK_SET; | |
271 | if(fcntl(lockfd, F_SETLK, &lock) < 0) | |
272 | fatal(errno, "error locking %s", lockfile); | |
273 | } | |
274 | /* initialize database environment */ | |
275 | trackdb_init(TRACKDB_NORMAL_RECOVER); | |
276 | trackdb_master(ev); | |
277 | /* install new config */ | |
278 | reconfigure(ev, 0); | |
279 | /* re-read config if we receive a SIGHUP */ | |
280 | if(ev_signal(ev, SIGHUP, handle_sighup, 0)) fatal(0, "ev_signal failed"); | |
281 | /* exit on SIGINT/SIGTERM */ | |
282 | if(ev_signal(ev, SIGINT, handle_sigint, 0)) fatal(0, "ev_signal failed"); | |
283 | if(ev_signal(ev, SIGTERM, handle_sigterm, 0)) fatal(0, "ev_signal failed"); | |
284 | /* ignore SIGPIPE */ | |
285 | signal(SIGPIPE, SIG_IGN); | |
286 | /* start a rescan straight away */ | |
287 | if(initial_rescan) | |
288 | trackdb_rescan(ev); | |
289 | rescan_after(86400); | |
290 | /* periodically tidy up the database */ | |
291 | dbgc_after(60); | |
292 | /* periodically check the volume */ | |
104ae93a | 293 | volumecheck_again(0, 0, 0); |
460b9539 | 294 | /* set initial state */ |
295 | add_random_track(); | |
296 | play(ev); | |
297 | /* enter the event loop */ | |
298 | n = ev_run(ev); | |
299 | /* if we exit the event loop, something must have gone wrong */ | |
300 | fatal(errno, "ev_run returned %d", n); | |
301 | } | |
302 | ||
303 | /* | |
304 | Local Variables: | |
305 | c-basic-offset:2 | |
306 | comment-column:40 | |
e83d0967 | 307 | fill-column:79 |
460b9539 | 308 | End: |
309 | */ |