diff --git a/data/init/guhd b/data/init/guhd index a7305e8c..997ce1ac 100644 --- a/data/init/guhd +++ b/data/init/guhd @@ -1,8 +1,4 @@ -#!/bin/sh -# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing. -if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then - set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script -fi +#! /bin/sh ### BEGIN INIT INFO # Provides: skeleton # Required-Start: $remote_fs $syslog @@ -11,53 +7,123 @@ fi # Default-Stop: 0 1 6 # Short-Description: Example initscript # Description: This file should be used to construct scripts to be -# placed in /etc/init.d. This example start a -# single forking daemon capable of writing a pid -# file. To get other behavoirs, implemend -# do_start(), do_stop() or other functions to -# override the defaults in /lib/init/init-d-script. +# placed in /etc/init.d. ### END INIT INFO -# Author: Michael Zanetti +# Author: Simon Stuerz +# Do NOT "set -e" + +# PATH should only include /usr/* if it runs after the mountnfs.sh script +PATH=/sbin:/usr/sbin:/bin:/usr/bin DESC="guh daemon" +NAME=guhd +DAEMON=/usr/bin/$NAME +DAEMON_ARGS="" +PIDFILE=/var/run/$NAME.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.2-14) to ensure that this file is present +# and status_of_proc is working. +. /lib/lsb/init-functions + +# +# Function that starts the daemon/service +# +do_start() +{ + # Return + # 0 if daemon has been started + # 1 if daemon was already running + # 2 if daemon could not be started + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ + || return 1 + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \ + $DAEMON_ARGS \ + || return 2 + # Add code here, if necessary, that waits for the process to be ready + # to handle requests from services started subsequently which depend + # on this one. As a last resort, sleep for some time. +} + +# +# Function that stops the daemon/service +# +do_stop() +{ + # Return + # 0 if daemon has been stopped + # 1 if daemon was already stopped + # 2 if daemon could not be stopped + # other if a failure occurred + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME + RETVAL="$?" + [ "$RETVAL" = 2 ] && return 2 + # Wait for children to finish too if this is a daemon that forks + # and if the daemon is only ever run from this initscript. + # If the above conditions are not satisfied then add some other code + # that waits for the process to drop all resources that could be + # needed by services started subsequently. A last resort is to + # sleep for some time. + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON + [ "$?" = 2 ] && return 2 + # Many daemons don't delete their pidfiles when they exit. + rm -f $PIDFILE + return "$RETVAL" +} case "$1" in start) - log_daemon_msg "Starting guh daemon" "guh" || true - if start-stop-daemon --start --background --quiet --oknodo -m --pidfile /var/run/guh.pid --exec /usr/bin/guhd -- $GUH_OPTS; then - log_end_msg 0 || true - else - log_end_msg 1 || true - fi + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" + do_start + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac ;; stop) - log_daemon_msg "Stopping guh daemon" "guh" || true - if start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/guhd.pid; then - log_end_msg 0 || true - else - log_end_msg 1 || true - fi + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" + do_stop + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + status) + status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? ;; restart) - check_for_upstart 1 - check_privsep_dir - check_config - log_daemon_msg "Restarting guh daemon" "guh" || true - start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile /var/run/guhd.pid - check_for_no_start log_end_msg - check_dev_null log_end_msg - if start-stop-daemon --start --background --quiet --oknodo -m --pidfile /var/run/guhd.pid --exec /usr/bin/guhd -- $GUH_OPTS; then - log_end_msg 0 || true - else - log_end_msg 1 || true - fi + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac ;; - *) - log_action_msg "Usage: /etc/init.d/guhd {start|stop|restart}" || true - exit 1 + echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2 + exit 3 + ;; esac -exit 0 - +: diff --git a/server/guhservice.cpp b/server/guhservice.cpp new file mode 100644 index 00000000..7f02a351 --- /dev/null +++ b/server/guhservice.cpp @@ -0,0 +1,17 @@ +#include "guhservice.h" + +GuhService::GuhService(int argc, char **argv): + QtService(argc, argv, "guh daemon") +{ + setServiceDescription("guh daemon"); + setServiceFlags(QtServiceBase::CanBeSuspended); +} + +GuhService::~GuhService() +{ +} + +void GuhService::start() +{ + GuhCore::instance(); +} diff --git a/server/guhservice.h b/server/guhservice.h new file mode 100644 index 00000000..ba369a1b --- /dev/null +++ b/server/guhservice.h @@ -0,0 +1,20 @@ +#ifndef GUHSERVICE_H +#define GUHSERVICE_H + +#include +#include "qtservice/qtservice.h" + +#include "guhcore.h" + +class GuhService : public QtService +{ + +public: + explicit GuhService(int argc, char **argv); + ~GuhService(); + +protected: + void start(); +}; + +#endif // GUHSERVICE_H diff --git a/server/main.cpp b/server/main.cpp index a1f40b90..b4e1dcec 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -17,81 +17,18 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include -#include - #include -#include - -#include -#include -#include -#include -#include -#include #include -#include -#include - -class GuhService : public QtService -{ - -public: - GuhService(int argc, char **argv): - QtService(argc, argv, "guh daemon") - { - setServiceDescription("guh daemon"); - setServiceFlags(QtServiceBase::CanBeSuspended); - } - -protected: - void start() - { - GuhCore::instance(); - } - -}; - - -//void daemonizeGuh() { -// // Our process ID and Session ID -// pid_t pid, sid; - -// // Fork off the parent process -// pid = fork(); -// if (pid < 0) { -// exit(EXIT_FAILURE); -// } -// // If we got a good PID, then we can exit the parent process. -// if (pid > 0) { -// exit(EXIT_SUCCESS); -// } - -// // Change the file mode mask -// umask(0); -// // Create a new SID for the child process -// sid = setsid(); -// if (sid < 0) { -// // Log the failure -// exit(EXIT_FAILURE); -// } - -// // Change the current working directory -// if ((chdir("/")) < 0) { -// /* Log the failure */ -// exit(EXIT_FAILURE); -// } - -// // Close out the standard file descriptors -// close(STDIN_FILENO); -// close(STDOUT_FILENO); -// close(STDERR_FILENO); -//} +#include "guhcore.h" +#include "guhservice.h" int main(int argc, char *argv[]) { - QCoreApplication a(argc, argv); - QStringList arguments = a.arguments(); + QStringList arguments; + for (int i = 0; i < argc; ++i) { + arguments.append(QString(argv[i])); + } if (arguments.contains("-h") || arguments.contains("--help")) { qDebug() << "guhd" << GUH_VERSION_STRING << "(C) 2014-2015 guh" ; @@ -110,18 +47,21 @@ int main(int argc, char *argv[]) } if (arguments.contains("-v") || arguments.contains("--version")) { - qDebug() << GUH_VERSION_STRING; + qDebug() << "guhd" << GUH_VERSION_STRING; exit(0); } if (!arguments.contains("-e") && !arguments.contains("--executable")) { - qDebug() << "guhd is starting as daemon."; + qDebug() << "guhd started as daemon."; GuhService service(argc, argv); + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); return service.exec(); - //daemonizeGuh(); - } else { - qDebug() << "guhd is starting as executable."; } + + QCoreApplication a(argc, argv); + qDebug() << "guhd started as executable."; a.setOrganizationName("guh"); a.setApplicationName("guhd"); GuhCore::instance(); diff --git a/server/qtservice/qtservice.pri b/server/qtservice/qtservice.pri index 09452981..bd71f44a 100644 --- a/server/qtservice/qtservice.pri +++ b/server/qtservice/qtservice.pri @@ -1,4 +1,4 @@ -include(../common.pri) +#include(../common.pri) INCLUDEPATH += $$PWD DEPENDPATH += $$PWD !win32:QT += network diff --git a/server/server.pro b/server/server.pro index ced1166d..3d3e809a 100644 --- a/server/server.pro +++ b/server/server.pro @@ -17,7 +17,8 @@ LIBS += -L$$top_builddir/libguh/ -lguh include(server.pri) include(qtservice/qtservice.pri) -SOURCES += main.cpp +SOURCES += main.cpp \ + guhservice.cpp boblight { xcompile { @@ -27,3 +28,6 @@ boblight { } DEFINES += USE_BOBLIGHT } + +HEADERS += \ + guhservice.h