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