]> mj.ucw.cz Git - ursary.git/commitdiff
Init script and related fixes
authorMartin Mares <mj@ucw.cz>
Sat, 29 Nov 2014 16:37:44 +0000 (17:37 +0100)
committerMartin Mares <mj@ucw.cz>
Sat, 29 Nov 2014 16:37:44 +0000 (17:37 +0100)
Makefile
debian-init [new file with mode: 0644]
ursaryd.c

index f2849b1c97b5ed942a0c20ebe216e654aa1d5677..1b41fea395a7a966ee9134624ddc2237a9c86941 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -26,3 +26,7 @@ pulse-ucw.o: pulse-ucw.c ursaryd.h
 clean:
        rm -f `find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core -or -name .depend -or -name .#*`
        rm -f ursaryd
+
+install: all
+       install -m 755 ursaryd /usr/local/sbin/
+       install -m 755 debian-init /etc/init.d/ursaryd
diff --git a/debian-init b/debian-init
new file mode 100644 (file)
index 0000000..30e25d4
--- /dev/null
@@ -0,0 +1,120 @@
+#! /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
+
+:
index fb6f04bfd25e87887488743c5402b11be2f4fc28..dda13680f53329d721f890fbbb1329bf458fbc05 100644 (file)
--- a/ursaryd.c
+++ b/ursaryd.c
@@ -14,6 +14,7 @@
 #include <ucw/opt.h>
 #include <ucw/stkstring.h>
 
+#include <signal.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -644,19 +645,6 @@ void notify_touch(int rotary, int on UNUSED)
 
 /*** 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 = {
@@ -670,6 +658,32 @@ 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);
@@ -683,11 +697,8 @@ int main(int argc UNUSED, char **argv)
   };
   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);