From 5d13535fb4dd7d2ddb4cdc5e42c667d83734ea7a Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 27 Feb 2011 20:27:12 -0800 Subject: [PATCH] Working LSB startup script. --- guacd/Makefile.am | 13 +++++ guacd/configure.in | 7 +++ guacd/init.d/guacd.in | 132 ++++++++++++++++++++++++++++++++++++++++++ guacd/src/daemon.c | 29 +++++++++- 4 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 guacd/init.d/guacd.in diff --git a/guacd/Makefile.am b/guacd/Makefile.am index 28ef4b94..4bef6738 100644 --- a/guacd/Makefile.am +++ b/guacd/Makefile.am @@ -1,3 +1,16 @@ + AUTOMAKE_OPTIONS = gnu +initdir = @init_dir@ + sbin_PROGRAMS = guacd +init_SCRIPTS = init.d/guacd + guacd_SOURCES = src/daemon.c + +EXTRA_DIST = init.d/guacd +CLEANFILES = $(init_SCRIPTS) + +init.d/guacd: init.d/guacd.in + sed -e 's,[@]sbindir[@],$(sbindir),g' < init.d/guacd.in > init.d/guacd + chmod +x init.d/guacd + diff --git a/guacd/configure.in b/guacd/configure.in index 05e8f308..a0e2945d 100644 --- a/guacd/configure.in +++ b/guacd/configure.in @@ -19,5 +19,12 @@ AC_CHECK_HEADERS([netinet/in.h stdlib.h string.h sys/socket.h syslog.h unistd.h AC_FUNC_MALLOC AC_CHECK_FUNCS([memset socket strerror fork]) +# Options +AC_ARG_WITH(init_dir, + [AS_HELP_STRING([--with-init-dir=], + [install an init script to the given directory]) + ],init_dir=$withval) +AC_SUBST(init_dir) + AC_CONFIG_FILES([Makefile]) AC_OUTPUT diff --git a/guacd/init.d/guacd.in b/guacd/init.d/guacd.in new file mode 100644 index 00000000..55c6f44d --- /dev/null +++ b/guacd/init.d/guacd.in @@ -0,0 +1,132 @@ +#!/bin/sh + +# ***** BEGIN LICENSE BLOCK ***** +# Version: MPL 1.1/GPL 2.0/LGPL 2.1 +# +# The contents of this file are subject to the Mozilla Public License Version +# 1.1 (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +# for the specific language governing rights and limitations under the +# License. +# +# The Original Code is guacd. +# +# The Initial Developer of the Original Code is +# Michael Jumper. +# Portions created by the Initial Developer are Copyright (C) 2010 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# +# Alternatively, the contents of this file may be used under the terms of +# either the GNU General Public License Version 2 or later (the "GPL"), or +# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), +# in which case the provisions of the GPL or the LGPL are applicable instead +# of those above. If you wish to allow use of your version of this file only +# under the terms of either the GPL or the LGPL, and not to allow others to +# use your version of this file under the terms of the MPL, indicate your +# decision by deleting the provisions above and replace them with the notice +# and other provisions required by the GPL or the LGPL. If you do not delete +# the provisions above, a recipient may use your version of this file under +# the terms of any one of the MPL, the GPL or the LGPL. +# +# ***** END LICENSE BLOCK ***** + +# guacd +# +# chkconfig: - 20 80 +# description: Guacamole proxy daemon + +### BEGIN INIT INFO +# Provides: guacd +# Required-Start: $network $syslog +# Required-Stop: $network $syslog +# Short-Description: Guacamole proxy daemon +# Description: The Guacamole proxy daemon, required to translate remote desktop protocols into the text-based Guacamole protocol used by the JavaScript application. +### END INIT INFO + +# Source function library. +. /lib/lsb/init-functions + +prog="guacd" +exec="@sbindir@/$prog" +pidfile="/var/run/$prog.pid" + +[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog + +start() { + [ -x $exec ] || exit 5 + echo -n "Starting $prog: " + start_daemon -p "$pidfile" $exec -p "$pidfile" + retval=$? + + case "$retval" in + 0) + echo "SUCCESS" + ;; + *) + echo "FAIL" + ;; + esac + + return $retval +} + +stop() { + echo -n "Stopping $prog: " + killproc -p "$pidfile" $prog + retval=$? + + case "$retval" in + 0) + echo "SUCCESS" + ;; + *) + echo "FAIL" + ;; + esac + + return $retval +} + +restart() { + stop + start +} + +force_reload() { + restart +} + +status() { + + PID=`pidofproc -p "$pidfile" $prog` + retval=$? + + case "$retval" in + 0) + echo "$prog is running with PID=$PID." + ;; + *) + echo "$prog is not running." + ;; + esac + + +} + + +case "$1" in + start|stop|status|restart|force-reload) + $1 + ;; + *) + echo "Usage: $0 {start|stop|status|restart|force-reload}" + exit 2 +esac +exit $? + diff --git a/guacd/src/daemon.c b/guacd/src/daemon.c index 2e7b9f59..3029fc5a 100644 --- a/guacd/src/daemon.c +++ b/guacd/src/daemon.c @@ -135,6 +135,8 @@ int main(int argc, char* argv[]) { int listen_port = 4822; /* Default port */ int opt; + char* pidfile = NULL; + /* Daemon Process */ pid_t daemon_pid; @@ -144,7 +146,7 @@ int main(int argc, char* argv[]) { #endif /* Parse arguments */ - while ((opt = getopt(argc, argv, "l:")) != -1) { + while ((opt = getopt(argc, argv, "l:p:")) != -1) { if (opt == 'l') { listen_port = atoi(optarg); if (listen_port <= 0) { @@ -152,8 +154,11 @@ int main(int argc, char* argv[]) { exit(EXIT_FAILURE); } } + else if (opt == 'p') { + pidfile = strdup(optarg); + } else { - fprintf(stderr, "USAGE: %s [-l LISTENPORT]\n", argv[0]); + fprintf(stderr, "USAGE: %s [-l LISTENPORT] [-p PIDFILE]\n", argv[0]); exit(EXIT_FAILURE); } } @@ -200,8 +205,26 @@ int main(int argc, char* argv[]) { exit(EXIT_FAILURE); } - /* If parent, exit */ + /* If parent, write PID file and exit */ else if (daemon_pid != 0) { + + if (pidfile != NULL) { + + /* Attempt to open pidfile and write PID */ + FILE* pidf = fopen(pidfile, "w"); + if (pidf) { + fprintf(pidf, "%d\n", daemon_pid); + fclose(pidf); + } + + /* Warn on failure */ + else { + fprintf(stderr, "WARNING: Could not write PID file: %s\n", lasterror()); + exit(EXIT_FAILURE); + } + + } + exit(EXIT_SUCCESS); } #else