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