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