From 6e39a87debe45c3a85dd8922566fe14042d49a7b Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 29 Nov 2014 17:22:07 +0100 Subject: [PATCH] Daemonization --- Makefile | 16 +++++++++------- ursaryd.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 5d8d44c..f2849b1 100644 --- 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) diff --git a/ursaryd.c b/ursaryd.c index 0315c34..fb6f04b 100644 --- a/ursaryd.c +++ b/ursaryd.c @@ -4,16 +4,20 @@ * (c) 2014 Martin Mares */ -#define LOCAL_DEBUG +#undef LOCAL_DEBUG #include #include +#include +#include #include +#include #include #include #include #include +#include #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; } -- 2.39.2