e03fa42dd71200337f98e41f085cbaa4e721e4bd
2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file server/disorderd.c
19 * @brief Main DisOrder server
21 #include "disorder-server.h"
25 static const struct option options
[] = {
26 { "help", no_argument
, 0, 'h' },
27 { "version", no_argument
, 0, 'V' },
28 { "config", required_argument
, 0, 'c' },
29 { "debug", no_argument
, 0, 'd' },
30 { "foreground", no_argument
, 0, 'f' },
31 { "log", required_argument
, 0, 'l' },
32 { "pidfile", required_argument
, 0, 'P' },
33 { "wide-open", no_argument
, 0, 'w' },
34 { "syslog", no_argument
, 0, 's' },
38 /* display usage message and terminate */
39 static void help(void) {
41 " disorderd [OPTIONS]\n"
43 " --help, -h Display usage message\n"
44 " --version, -V Display version number\n"
45 " --config PATH, -c PATH Set configuration file\n"
46 " --debug, -d Turn on debugging\n"
47 " --foreground, -f Do not become a daemon\n"
48 " --syslog, -s Log to syslog even with -f\n"
49 " --pidfile PATH, -P PATH Leave a pidfile\n");
54 /* signals ------------------------------------------------------------------ */
57 static int handle_sighup(ev_source
attribute((unused
)) *ev_
,
58 int attribute((unused
)) sig
,
59 void attribute((unused
)) *u
) {
60 info("received SIGHUP");
67 static int handle_sigint(ev_source
attribute((unused
)) *ev_
,
68 int attribute((unused
)) sig
,
69 void attribute((unused
)) *u
) {
70 info("received SIGINT");
74 static int handle_sigterm(ev_source
attribute((unused
)) *ev_
,
75 int attribute((unused
)) sig
,
76 void attribute((unused
)) *u
) {
77 info("received SIGTERM");
81 /* periodic actions --------------------------------------------------------- */
83 struct periodic_data
{
84 void (*callback
)(ev_source
*);
88 static int periodic_callback(ev_source
*ev_
,
89 const struct timeval
attribute((unused
)) *now
,
92 struct periodic_data
*const pd
= u
;
96 w
.tv_sec
+= pd
->period
;
97 ev_timeout(ev
, 0, &w
, periodic_callback
, pd
);
101 /** @brief Create a periodic action
102 * @param ev_ Event loop
103 * @param callback Callback function
104 * @param period Interval between calls in seconds
105 * @param immediate If true, call @p callback straight away
107 static void create_periodic(ev_source
*ev_
,
108 void (*callback
)(ev_source
*),
112 struct periodic_data
*const pd
= xmalloc(sizeof *pd
);
114 pd
->callback
= callback
;
120 ev_timeout(ev_
, 0, &w
, periodic_callback
, pd
);
123 static void periodic_rescan(ev_source
*ev_
) {
124 trackdb_rescan(ev_
, 1/*check*/, 0, 0);
127 static void periodic_database_gc(ev_source
attribute((unused
)) *ev_
) {
131 static void periodic_volume_check(ev_source
attribute((unused
)) *ev_
) {
135 if(!mixer_control(-1/*as configured*/, &l
, &r
, 0)) {
136 if(l
!= volume_left
|| r
!= volume_right
) {
139 snprintf(lb
, sizeof lb
, "%d", l
);
140 snprintf(rb
, sizeof rb
, "%d", r
);
141 eventlog("volume", lb
, rb
, (char *)0);
146 static void periodic_play_check(ev_source
*ev_
) {
150 static void periodic_add_random(ev_source
*ev_
) {
151 add_random_track(ev_
);
154 /* We fix the path to include the bindir and sbindir we were installed into */
155 static void fix_path(void) {
156 char *path
= getenv("PATH");
157 static char *newpath
;
158 /* static or libgc collects it! */
161 error(0, "PATH is not set at all!");
163 if(*finkbindir
&& strcmp(finkbindir
, "/"))
164 /* We appear to be a finkized mac; include fink on the path in case the
165 * tools we need are there. */
166 byte_xasprintf(&newpath
, "PATH=%s:%s:%s:%s",
167 path
, bindir
, sbindir
, finkbindir
);
169 byte_xasprintf(&newpath
, "PATH=%s:%s:%s", path
, bindir
, sbindir
);
174 int main(int argc
, char **argv
) {
175 int n
, background
= 1, logsyslog
= 0;
176 const char *pidfile
= 0;
180 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
181 /* garbage-collect PCRE's memory */
182 pcre_malloc
= xmalloc
;
184 while((n
= getopt_long(argc
, argv
, "hVc:dfP:Ns", options
, 0)) >= 0) {
187 case 'V': version("disorderd");
188 case 'c': configfile
= optarg
; break;
189 case 'd': debugging
= 1; break;
190 case 'f': background
= 0; break;
191 case 'P': pidfile
= optarg
; break;
192 case 's': logsyslog
= 1; break;
193 case 'w': wideopen
= 1; break;
194 default: fatal(0, "invalid option");
197 /* go into background if necessary */
199 daemonize(progname
, LOG_DAEMON
, pidfile
);
201 /* If we're running under some kind of daemon supervisor then we may want
202 * to log to syslog but not to go into background */
203 openlog(progname
, LOG_PID
, LOG_DAEMON
);
204 log_default
= &log_syslog
;
206 info("process ID %lu", (unsigned long)getpid());
208 srand(time(0)); /* don't start the same every time */
209 /* gcrypt initialization */
210 gcry_control(GCRYCTL_INIT_SECMEM
, 1);
211 /* create event loop */
213 if(ev_child_setup(ev
)) fatal(0, "ev_child_setup failed");
216 fatal(0, "cannot read configuration");
217 /* make sure the home directory exists and has suitable permissions */
219 /* Start the speaker process (as root! - so it can choose its nice value) */
221 /* set server nice value _after_ starting the speaker, so that they
222 * are independently niceable */
223 xnice(config
->nice_server
);
226 /* make sure we're not root, whatever the config says */
227 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
228 /* open a lockfile - we only want one copy of the server to run at once. */
230 const char *lockfile
;
234 lockfile
= config_get_file("lock");
235 if((lockfd
= open(lockfile
, O_RDWR
|O_CREAT
, 0600)) < 0)
236 fatal(errno
, "error opening %s", lockfile
);
238 memset(&lock
, 0, sizeof lock
);
239 lock
.l_type
= F_WRLCK
;
240 lock
.l_whence
= SEEK_SET
;
241 if(fcntl(lockfd
, F_SETLK
, &lock
) < 0)
242 fatal(errno
, "error locking %s", lockfile
);
244 /* initialize database environment */
245 trackdb_init(TRACKDB_NORMAL_RECOVER
|TRACKDB_MAY_CREATE
);
247 /* install new config (calls trackdb_open()) */
249 /* pull in old users */
251 /* create a root login */
252 trackdb_create_root();
253 /* re-read config if we receive a SIGHUP */
254 if(ev_signal(ev
, SIGHUP
, handle_sighup
, 0)) fatal(0, "ev_signal failed");
255 /* exit on SIGINT/SIGTERM */
256 if(ev_signal(ev
, SIGINT
, handle_sigint
, 0)) fatal(0, "ev_signal failed");
257 if(ev_signal(ev
, SIGTERM
, handle_sigterm
, 0)) fatal(0, "ev_signal failed");
259 signal(SIGPIPE
, SIG_IGN
);
260 /* Rescan immediately and then daily */
261 create_periodic(ev
, periodic_rescan
, 86400, 1/*immediate*/);
262 /* Tidy up the database once a minute */
263 create_periodic(ev
, periodic_database_gc
, 60, 0);
264 /* Check the volume immediately and then once a minute */
265 create_periodic(ev
, periodic_volume_check
, 60, 1);
266 /* Check for a playable track once a second */
267 create_periodic(ev
, periodic_play_check
, 1, 0);
268 /* Try adding a random track immediately and once every two seconds */
269 create_periodic(ev
, periodic_add_random
, 2, 1);
270 /* enter the event loop */
272 /* if we exit the event loop, something must have gone wrong */
273 fatal(errno
, "ev_run returned %d", n
);