2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
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.
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.
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
27 #include <sys/types.h>
33 #include <sys/socket.h>
41 #include "daemonize.h"
44 #include "configuration.h"
59 static void rescan_after(long offset);
60 static void dbgc_after(long offset);
61 static void volumecheck_after(long offset);
63 static const struct option options[] = {
64 { "help", no_argument, 0, 'h' },
65 { "version", no_argument, 0, 'V' },
66 { "config", required_argument, 0, 'c' },
67 { "debug", no_argument, 0, 'd' },
68 { "foreground", no_argument, 0, 'f' },
69 { "log", required_argument, 0, 'l' },
70 { "pidfile", required_argument, 0, 'P' },
71 { "no-initial-rescan", no_argument, 0, 'N' },
75 /* display usage message and terminate */
76 static void help(void) {
78 " disorderd [OPTIONS]\n"
80 " --help, -h Display usage message\n"
81 " --version, -V Display version number\n"
82 " --config PATH, -c PATH Set configuration file\n"
83 " --debug, -d Turn on debugging\n"
84 " --foreground, -f Do not become a daemon\n"
85 " --pidfile PATH, -P PATH Leave a pidfile\n");
90 /* display version number and terminate */
91 static void version(void) {
92 xprintf("disorderd version %s\n", disorder_version_string);
98 static int handle_sighup(ev_source attribute((unused)) *ev_,
99 int attribute((unused)) sig,
100 void attribute((unused)) *u) {
101 info("received SIGHUP");
108 static int handle_sigint(ev_source attribute((unused)) *ev_,
109 int attribute((unused)) sig,
110 void attribute((unused)) *u) {
111 info("received SIGINT");
115 static int handle_sigterm(ev_source attribute((unused)) *ev_,
116 int attribute((unused)) sig,
117 void attribute((unused)) *u) {
118 info("received SIGTERM");
122 static int rescan_again(ev_source *ev_,
123 const struct timeval attribute((unused)) *now,
124 void attribute((unused)) *u) {
130 static void rescan_after(long offset) {
135 ev_timeout(ev, 0, &w, rescan_again, 0);
138 static int dbgc_again(ev_source attribute((unused)) *ev_,
139 const struct timeval attribute((unused)) *now,
140 void attribute((unused)) *u) {
146 static void dbgc_after(long offset) {
151 ev_timeout(ev, 0, &w, dbgc_again, 0);
154 static int volumecheck_again(ev_source attribute((unused)) *ev_,
155 const struct timeval attribute((unused)) *now,
156 void attribute((unused)) *u) {
160 if(!mixer_control(&l, &r, 0)) {
161 if(l != volume_left || r != volume_right) {
164 snprintf(lb, sizeof lb, "%d", l);
165 snprintf(rb, sizeof rb, "%d", r);
166 eventlog("volume", lb, rb, (char *)0);
169 volumecheck_after(60);
173 static void volumecheck_after(long offset) {
178 ev_timeout(ev, 0, &w, volumecheck_again, 0);
181 int main(int argc, char **argv) {
182 int n, background = 1;
183 const char *pidfile = 0;
184 int initial_rescan = 1;
188 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
189 /* garbage-collect PCRE's memory */
190 pcre_malloc = xmalloc;
192 while((n = getopt_long(argc, argv, "hVc:dfP:N", options, 0)) >= 0) {
196 case 'c': configfile = optarg; break;
197 case 'd': debugging = 1; break;
198 case 'f': background = 0; break;
199 case 'P': pidfile = optarg; break;
200 case 'N': initial_rescan = 0; break;
201 default: fatal(0, "invalid option");
204 /* go into background if necessary */
206 daemonize(progname, LOG_DAEMON, pidfile);
207 info("process ID %lu", (unsigned long)getpid());
208 srand(time(0)); /* don't start the same every time */
209 /* create event loop */
211 if(ev_child_setup(ev)) fatal(0, "ev_child_setup failed");
214 fatal(0, "cannot read configuration");
215 /* Start the speaker process (as root! - so it can choose its nice value) */
217 /* set server nice value _after_ starting the speaker, so that they
218 * are independently niceable */
219 xnice(config->nice_server);
222 /* make sure we're not root, whatever the config says */
223 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
224 /* open a lockfile - we only want one copy of the server to run at once. */
226 const char *lockfile;
230 lockfile = config_get_file("lock");
231 if((lockfd = open(lockfile, O_RDWR|O_CREAT, 0600)) < 0)
232 fatal(errno, "error opening %s", lockfile);
234 memset(&lock, 0, sizeof lock);
235 lock.l_type = F_WRLCK;
236 lock.l_whence = SEEK_SET;
237 if(fcntl(lockfd, F_SETLK, &lock) < 0)
238 fatal(errno, "error locking %s", lockfile);
240 /* initialize database environment */
241 trackdb_init(TRACKDB_NORMAL_RECOVER);
243 /* install new config */
245 /* re-read config if we receive a SIGHUP */
246 if(ev_signal(ev, SIGHUP, handle_sighup, 0)) fatal(0, "ev_signal failed");
247 /* exit on SIGINT/SIGTERM */
248 if(ev_signal(ev, SIGINT, handle_sigint, 0)) fatal(0, "ev_signal failed");
249 if(ev_signal(ev, SIGTERM, handle_sigterm, 0)) fatal(0, "ev_signal failed");
251 signal(SIGPIPE, SIG_IGN);
252 /* start a rescan straight away */
256 /* periodically tidy up the database */
258 /* periodically check the volume */
259 volumecheck_after(60);
260 /* set initial state */
263 /* enter the event loop */
265 /* if we exit the event loop, something must have gone wrong */
266 fatal(errno, "ev_run returned %d", n);
275 /* arch-tag:eaf909880c8fd3d1fef94ca8f12efe78 */