]> mj.ucw.cz Git - ursary.git/commitdiff
Daemonization
authorMartin Mares <mj@ucw.cz>
Sat, 29 Nov 2014 16:22:07 +0000 (17:22 +0100)
committerMartin Mares <mj@ucw.cz>
Sat, 29 Nov 2014 16:22:07 +0000 (17:22 +0100)
Makefile
ursaryd.c

index 5d8d44cbd59632080bfda92721ec82eb312197d7..f2849b1c97b5ed942a0c20ebe216e654aa1d5677 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,14 @@
-LIBUSB_CFLAGS := $(shell pkg-config --cflags libusb-1.0)
-LIBUSB_LIBS := $(shell pkg-config --libs libusb-1.0)
+# PC=PKG_CONFIG_PATH=/home/mj/src/libucw/run/lib/pkgconfig pkg-config
+PC=PKG_CONFIG_PATH=/opt/lib/pkgconfig pkg-config
 
-LIBPULSE_CFLAGS := $(shell pkg-config --cflags libpulse)
-LIBPULSE_LIBS := $(shell pkg-config --libs libpulse)
+LIBUSB_CFLAGS := $(shell $(PC) --cflags libusb-1.0)
+LIBUSB_LIBS := $(shell $(PC) --libs libusb-1.0)
 
-LIBUCW_PKG := /home/mj/src/libucw/run/lib/pkgconfig
-LIBUCW_CFLAGS := $(shell PKG_CONFIG_PATH=$(LIBUCW_PKG) pkg-config --cflags libucw)
-LIBUCW_LIBS := $(shell PKG_CONFIG_PATH=$(LIBUCW_PKG) pkg-config --libs libucw)
+LIBPULSE_CFLAGS := $(shell $(PC) --cflags libpulse)
+LIBPULSE_LIBS := $(shell $(PC) --libs libpulse)
+
+LIBUCW_CFLAGS := $(shell $(PC) --cflags libucw)
+LIBUCW_LIBS := $(shell $(PC) --libs libucw)
 
 CFLAGS=-O2 -Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Wundef -Wredundant-decls -std=gnu99 $(LIBUCW_CFLAGS) $(LIBUSB_CFLAGS) $(LIBPULSE_CFLAGS) -g2
 LDLIBS=$(LIBUCW_LIBS) $(LIBUSB_LIBS) $(LIBPULSE_LIBS)
index 0315c344efce35272315f0a80db54c1dee4c5a28..fb6f04bfd25e87887488743c5402b11be2f4fc28 100644 (file)
--- a/ursaryd.c
+++ b/ursaryd.c
@@ -4,16 +4,20 @@
  *     (c) 2014 Martin Mares <mj@ucw.cz>
  */
 
-#define LOCAL_DEBUG
+#undef LOCAL_DEBUG
 
 #include <ucw/lib.h>
 #include <ucw/clists.h>
+#include <ucw/daemon.h>
+#include <ucw/log.h>
 #include <ucw/mainloop.h>
+#include <ucw/opt.h>
 #include <ucw/stkstring.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <syslog.h>
 
 #include "ursaryd.h"
 
@@ -640,9 +644,8 @@ void notify_touch(int rotary, int on UNUSED)
 
 /*** Main entry point ***/
 
-int main(int argc UNUSED, char **argv)
+static void daemon_body(struct daemon_params *dp UNUSED)
 {
-  log_init(argv[0]);
   main_init();
   update_timer.handler = do_update;
 
@@ -650,8 +653,46 @@ int main(int argc UNUSED, char **argv)
   pulse_init();
   mpd_init();
 
-  msg(L_DEBUG, "Entering main loop");
+  msg(L_INFO, "Ursary daemon starting");
   main_loop();
+}
+
+static int debug;
+
+static struct opt_section options = {
+  OPT_ITEMS {
+    OPT_HELP("Control console for the Ursary"),
+    OPT_HELP(""),
+    OPT_HELP("Options:"),
+    OPT_HELP_OPTION,
+    OPT_BOOL('d', "debug", debug, 0, "\tEnable debugging mode (no fork etc.)"),
+    OPT_END
+  }
+};
+
+int main(int argc UNUSED, char **argv)
+{
+  opt_parse(&options, argv+1);
+  unsetenv("DISPLAY");
+  unsetenv("HOME");
+
+  struct daemon_params dp = {
+    .flags = (debug ? DAEMON_FLAG_SIMULATE : 0),
+    .pid_file = "/run/ursaryd.pid",
+    .run_as_user = "ursary",
+  };
+  daemon_init(&dp);
+
+  if (debug)
+    {
+      log_init(argv[0]);
+    }
+  else
+    {
+      struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
+      log_set_default_stream(ls);
+    }
 
+  daemon_run(&dp, daemon_body);
   return 0;
 }