chiark / gitweb /
More reliable gapless play.
[disorder] / server / disorderd.c
index 913721bdecc87915a24943a755306c9e76004c5d..74fc07eb4a7d3c49059b3d54d5383f56d6f0d944 100644 (file)
@@ -1,70 +1,27 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
+ * Copyright (C) 2004-2009 Richard Kettlewell
  *
- * This program is free software; you can redistribute it and/or modify
+ * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-
-#include <config.h>
-#include "types.h"
-
-#include <stdio.h>
-#include <getopt.h>
-#include <pwd.h>
-#include <grp.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <signal.h>
-#include <sys/socket.h>
-#include <time.h>
-#include <locale.h>
-#include <syslog.h>
-#include <sys/time.h>
-#include <pcre.h>
-#include <fcntl.h>
-#include <gcrypt.h>
-
-#include "daemonize.h"
-#include "event.h"
-#include "log.h"
-#include "configuration.h"
-#include "rights.h"
-#include "trackdb.h"
-#include "queue.h"
-#include "mem.h"
-#include "play.h"
-#include "server.h"
-#include "state.h"
-#include "syscalls.h"
-#include "defs.h"
-#include "user.h"
-#include "mixer.h"
-#include "eventlog.h"
-#include "printf.h"
-#include "version.h"
+/** @file server/disorderd.c
+ * @brief Main DisOrder server
+ */
+#include "disorder-server.h"
 
 static ev_source *ev;
 
-static void rescan_after(long offset);
-static void dbgc_after(long offset);
-static void volumecheck_after(long offset);
-
 static const struct option options[] = {
   { "help", no_argument, 0, 'h' },
   { "version", no_argument, 0, 'V' },
@@ -94,6 +51,8 @@ static void help(void) {
   exit(0);
 }
 
+/* signals ------------------------------------------------------------------ */
+
 /* SIGHUP callback */
 static int handle_sighup(ev_source attribute((unused)) *ev_,
                         int attribute((unused)) sig,
@@ -119,45 +78,62 @@ static int handle_sigterm(ev_source attribute((unused)) *ev_,
   quit(ev);
 }
 
-static int rescan_again(ev_source *ev_,
-                       const struct timeval attribute((unused)) *now,
-                       void attribute((unused)) *u) {
-  trackdb_rescan(ev_, 1/*check*/);
-  rescan_after(86400);
-  return 0;
-}
+/* periodic actions --------------------------------------------------------- */
+
+struct periodic_data {
+  void (*callback)(ev_source *);
+  int period;
+};
 
-static void rescan_after(long offset) {
+static int periodic_callback(ev_source *ev_,
+                            const struct timeval attribute((unused)) *now,
+                            void *u) {
   struct timeval w;
+  struct periodic_data *const pd = u;
 
+  pd->callback(ev_);
   gettimeofday(&w, 0);
-  w.tv_sec += offset;
-  ev_timeout(ev, 0, &w, rescan_again, 0);
-}
-
-static int dbgc_again(ev_source attribute((unused)) *ev_,
-                     const struct timeval attribute((unused)) *now,
-                     void attribute((unused)) *u) {
-  trackdb_gc();
-  dbgc_after(60);
+  w.tv_sec += pd->period;
+  ev_timeout(ev, 0, &w, periodic_callback, pd);
   return 0;
 }
 
-static void dbgc_after(long offset) {
+/** @brief Create a periodic action
+ * @param ev_ Event loop
+ * @param callback Callback function
+ * @param period Interval between calls in seconds
+ * @param immediate If true, call @p callback straight away
+ */
+static void create_periodic(ev_source *ev_,
+                           void (*callback)(ev_source *),
+                           int period,
+                           int immediate) {
   struct timeval w;
+  struct periodic_data *const pd = xmalloc(sizeof *pd);
 
+  pd->callback = callback;
+  pd->period = period;
+  if(immediate)
+    callback(ev_);
   gettimeofday(&w, 0);
-  w.tv_sec += offset;
-  ev_timeout(ev, 0, &w, dbgc_again, 0);
+  w.tv_sec += period;
+  ev_timeout(ev_, 0, &w, periodic_callback, pd);
 }
 
-static int volumecheck_again(ev_source attribute((unused)) *ev_,
-                            const struct timeval attribute((unused)) *now,
-                            void attribute((unused)) *u) {
+static void periodic_rescan(ev_source *ev_) {
+  trackdb_rescan(ev_, 1/*check*/, 0, 0);
+}
+
+static void periodic_database_gc(ev_source attribute((unused)) *ev_) {
+  trackdb_gc();
+}
+
+static void periodic_volume_check(ev_source attribute((unused)) *ev_) {
   int l, r;
   char lb[32], rb[32];
 
-  if(!mixer_control(&l, &r, 0)) {
+  if(api && api->get_volume) {
+    api->get_volume(&l, &r);
     if(l != volume_left || r != volume_right) {
       volume_left = l;
       volume_right = r;
@@ -166,16 +142,14 @@ static int volumecheck_again(ev_source attribute((unused)) *ev_,
       eventlog("volume", lb, rb, (char *)0);
     }
   }
-  volumecheck_after(60);
-  return 0;
 }
 
-static void volumecheck_after(long offset) {
-  struct timeval w;
+static void periodic_play_check(ev_source *ev_) {
+  play(ev_);
+}
 
-  gettimeofday(&w, 0);
-  w.tv_sec += offset;
-  ev_timeout(ev, 0, &w, volumecheck_again, 0);
+static void periodic_add_random(ev_source *ev_) {
+  add_random_track(ev_);
 }
 
 /* We fix the path to include the bindir and sbindir we were installed into */
@@ -201,6 +175,7 @@ static void fix_path(void) {
 int main(int argc, char **argv) {
   int n, background = 1, logsyslog = 0;
   const char *pidfile = 0;
+  struct rlimit rl[1];
 
   set_progname(argv);
   mem_init();
@@ -234,11 +209,27 @@ int main(int argc, char **argv) {
   fix_path();
   srand(time(0));                      /* don't start the same every time */
   /* gcrypt initialization */
+  if(!gcry_check_version(NULL))
+    disorder_fatal(0, "gcry_check_version failed");
   gcry_control(GCRYCTL_INIT_SECMEM, 1);
+  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
+  /* make sure we can't have more than FD_SETSIZE files open (event.c does
+   * check but this provides an additional line of defence) */
+  if(getrlimit(RLIMIT_NOFILE, rl) < 0)
+    fatal(errno, "getrlimit RLIMIT_NOFILE");
+  if(rl->rlim_cur > FD_SETSIZE) {
+    rl->rlim_cur = FD_SETSIZE;
+    if(setrlimit(RLIMIT_NOFILE, rl) < 0)
+      fatal(errno, "setrlimit to reduce RLIMIT_NOFILE to %lu",
+            (unsigned long)rl->rlim_cur);
+    info("set RLIM_NOFILE to %lu", (unsigned long)rl->rlim_cur);
+  } else
+    info("RLIM_NOFILE is %lu", (unsigned long)rl->rlim_cur);
   /* create event loop */
   ev = ev_new();
   if(ev_child_setup(ev)) fatal(0, "ev_child_setup failed");
   /* read config */
+  config_uaudio_apis = uaudio_apis;
   if(config_read(1))
     fatal(0, "cannot read configuration");
   /* make sure the home directory exists and has suitable permissions */
@@ -284,17 +275,16 @@ int main(int argc, char **argv) {
   if(ev_signal(ev, SIGTERM, handle_sigterm, 0)) fatal(0, "ev_signal failed");
   /* ignore SIGPIPE */
   signal(SIGPIPE, SIG_IGN);
-  /* Start a rescan straight away */
-  trackdb_rescan(ev, 1/*check*/);
-  /* We'll rescan again after a day */
-  rescan_after(86400);
-  /* periodically tidy up the database */
-  dbgc_after(60);
-  /* periodically check the volume */
-  volumecheck_again(0, 0, 0);
-  /* set initial state */
-  add_random_track();
-  play(ev);
+  /* Rescan immediately and then daily */
+  create_periodic(ev, periodic_rescan, 86400, 1/*immediate*/);
+  /* Tidy up the database once a minute */
+  create_periodic(ev, periodic_database_gc, 60, 0);
+  /* Check the volume immediately and then once a minute */
+  create_periodic(ev, periodic_volume_check, 60, 1);
+  /* Check for a playable track once a second */
+  create_periodic(ev, periodic_play_check, 1, 0);
+  /* Try adding a random track immediately and once every two seconds */
+  create_periodic(ev, periodic_add_random, 2, 1);
   /* enter the event loop */
   n = ev_run(ev);
   /* if we exit the event loop, something must have gone wrong */