--- /dev/null
+#! /bin/sh
+### BEGIN INIT INFO
+# Provides: ursaryd
+# Required-Start: $local_fs $remote_fs pulseaudio
+# Required-Stop: $local_fs $remote_fs
+# Default-Start: 2 3 4 5
+# Default-Stop: 0 1 6
+# Short-Description: Ursary control panel daemon
+### END INIT INFO
+
+# Authors: Martin Mares <mj@ucw.cz>
+
+# Do NOT "set -e"
+
+# PATH should only include /usr/* if it runs after the mountnfs.sh script
+PATH=/sbin:/usr/sbin:/bin:/usr/bin:/opt/sbin:/opt/bin
+DESC="Ursary control panel daemon"
+NAME=ursaryd
+DAEMON=/usr/local/sbin/ursaryd
+DAEMON_ARGS=""
+PIDFILE=/run/ursaryd.pid
+SCRIPTNAME=/etc/init.d/$NAME
+
+# Exit if the package is not installed
+[ -x "$DAEMON" ] || exit 0
+
+# Read configuration variable file if it is present
+[ -r /etc/default/$NAME ] && . /etc/default/$NAME
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
+. /lib/lsb/init-functions
+
+do_start()
+{
+ ulimit -c unlimited
+ ucw-daemon-control --start --pid-file $PIDFILE -- $DAEMON $DAEMON_ARGS
+}
+
+do_stop()
+{
+ ucw-daemon-control --stop --pid-file $PIDFILE
+}
+
+do_reload()
+{
+ ucw-daemon-control --reload --pid-file $PIDFILE
+}
+
+do_status()
+{
+ ucw-daemon-control --check --pid-file $PIDFILE
+}
+
+case "$1" in
+ start)
+ [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+ do_start
+ case "$?" in
+ 0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+ 100) [ "$VERBOSE" != no ] && log_progress_msg "already running" && log_end_msg 0 ;;
+ 103) [ "$VERBOSE" != no ] && log_progress_msg "found dead, restarting" && log_end_msg 0 ;;
+ *) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+ esac
+ ;;
+ stop)
+ [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+ do_stop
+ case "$?" in
+ 0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+ 100) [ "$VERBOSE" != no ] && log_progress_msg "already stopped" && log_end_msg 0 ;;
+ 103) [ "$VERBOSE" != no ] && log_progress_msg "already dead" && log_end_msg 0 ;;
+ *) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+ esac
+ ;;
+ reload|force-reload)
+ log_daemon_msg "Reloading $DESC" "$NAME"
+ do_reload
+ case "$?" in
+ 0) log_end_msg 0 ;;
+ 101|103) log_progress_msg "is not running" && log_end_msg 1 ;;
+ *) log_end_msg 1 ;;
+ esac
+ ;;
+ restart)
+ log_daemon_msg "Restarting $DESC" "$NAME"
+ do_stop
+ case "$?" in
+ 0|100|103)
+ do_start
+ case "$?" in
+ 0|100) log_end_msg 0 ;;
+ 103) log_progress_msg "was found dead" && log_end_msg 0 ;;
+ *) log_end_msg 1 ;;
+ esac
+ ;;
+ *)
+ log_end_msg 1
+ ;;
+ esac
+ ;;
+ status)
+ do_status
+ case "$?" in
+ 0) exit 0 ;;
+ 101) exit 3 ;;
+ 103) exit 1 ;;
+ *) exit 4 ;;
+ esac
+ ;;
+ *)
+ echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload|status}" >&2
+ exit 3
+ ;;
+esac
+
+:
#include <ucw/opt.h>
#include <ucw/stkstring.h>
+#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*** Main entry point ***/
-static void daemon_body(struct daemon_params *dp UNUSED)
-{
- main_init();
- update_timer.handler = do_update;
-
- noct_init();
- pulse_init();
- mpd_init();
-
- msg(L_INFO, "Ursary daemon starting");
- main_loop();
-}
-
static int debug;
static struct opt_section options = {
}
};
+static void sigterm_handler(struct main_signal *ms UNUSED)
+{
+ main_shut_down();
+}
+
+static void daemon_body(struct daemon_params *dp)
+{
+ main_init();
+ update_timer.handler = do_update;
+
+ noct_init();
+ pulse_init();
+ mpd_init();
+
+ static struct main_signal term_sig = {
+ .signum = SIGTERM,
+ .handler = sigterm_handler,
+ };
+ signal_add(&term_sig);
+
+ msg(L_INFO, "Ursary daemon starting");
+ main_loop();
+ msg(L_INFO, "Ursary daemon shut down");
+ daemon_exit(dp);
+}
+
int main(int argc UNUSED, char **argv)
{
opt_parse(&options, argv+1);
};
daemon_init(&dp);
- if (debug)
- {
- log_init(argv[0]);
- }
- else
+ log_init(argv[0]);
+ if (!debug)
{
struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
log_set_default_stream(ls);