Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Need help writing init script for a program for an ebuild
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
StifflerStealth
Retired Dev
Retired Dev


Joined: 03 Jul 2002
Posts: 968

PostPosted: Sun Nov 16, 2014 10:06 am    Post subject: Need help writing init script for a program for an ebuild Reply with quote

Hi all,

Hope you can help me on this one. I'm trying to write an ebuild VyprVPN Linux CLI program written for the Debian/Ubuntu family -- mainly good for getting the Chameleon protocol which is nice for those in countries that block OpenVPN. Anyways, the init script that is included with the .deb package is, well, very confusing. I will post the init script with my comments between three sets of curly braces as such {{{ comment }}}. I have tried writing my own init script, but when I start it, it won't give me the input line back and need to press ctrl + c to kill it. It's not running as a daemon in the background.

Here's the init script:
/etc/init.d/vyprvpn:

#!/bin/sh

### BEGIN INIT INFO
# Provides:       vyprvpn
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the vyprvpn management service
# Description:       starts vyprvpn_service using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/vyprvpn_service
NAME=vyprvpn
DESC=vyprvpn

# Include nginx defaults if available
if [ -r /etc/default/nginx ]; then
        . /etc/default/nginx
fi

test -x $DAEMON || exit 0

. /lib/init/vars.sh
. /lib/lsb/init-functions

{{{ This is setting the pid file to use the same one as nginx, so both pids will be stored in this file. Why? The only thing I can think of is that it's the only way it knows nginx is running and it's pid to find it? I have no clue. Is this really needed and I can use my own pid file? However, I have tried my own pid file and nginx not installed and it vyprvpn doesn't work right. Does this logic, and the one above, mean that it really, absolutely needs nginx running? Read in the restart section for more comments on this issue. }}}

PID=$(awk -F'[ \t;]+' '/[^#]pid/ {print $2}' /etc/nginx/nginx.conf)
if [ -z "$PID" ]
then
        PID=/run/nginx.pid
fi

#
# Function that starts the daemon/service
#
do_start()
{


{{{ I understand that in OpenRC, you don't need logic like this as it's smart enough to know if something is running or not. I just need a line that will work and give me back my
console when started by starting the daemon in the background. }}}


        # 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 $PID --exec $DAEMON --test > /dev/null \
                || return 1
        start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \
                $DAEMON_OPTS 2>/dev/null \
                || return 2
}

#
# 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 $PID --name $NAME
        RETVAL="$?"

        sleep 1
        return "$RETVAL"
}

case "$1" in
        start)
                [ "$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)
                [ "$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
                ;;
        restart)
                log_daemon_msg "Restarting $DESC" "$NAME"

{{{ I have looked in the Gentoo nginx init script and it does check the config as well. So, this logic is the same across distros. A big however though. However, why does this init script need to care about nginx's config file? I know the below code will restart nginx and needs to take care of it. This still makes no sense though. I understand that since there are two pids in this pid file and killing based on this pid file will kill both programs. Hence, why I want to use a different pid file, but not 100% sure that would work since vyprvpn seems to be a giant leech onto nginx. Is there a better logic to doing this, like using the init script of nginx to shut down and restart, and then once that has happened, starting vyprvpn up and using the same pid? Can't I just kill vyprvpn without killing nginx even using the same pid file, but some kind of logic as in the second pid number in that file to kill and removing it? Restarting vyprvpn should not take out nginx even if it does fully need it and the only way to use it is via some kind of pid hackery that must be going on. }}}

                # Check configuration before stopping nginx
                if ! test_nginx_config; then
                        log_end_msg 1 # Configuration error
                        exit 0
                fi

                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
                ;;
        status)
                status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?
                ;;
        *)
                echo "Usage: $NAME {start|stop|restart|status}" >&2
                exit 3
                ;;
esac

exit 0


I want the init script to be as condense and clean as possible, but as you can see from my comments, it's really confusing what's going on in the above script. If you want to play around with the actual files the link is here:
http://www.goldenfrog.com/community/beta/vyprvpn-linux-cli

Thanks.
_________________
Nothing to read in this sig. Move along.
Back to top
View user's profile Send private message
gf-gregh
n00b
n00b


Joined: 16 Nov 2014
Posts: 1
Location: Austin

PostPosted: Sun Nov 16, 2014 4:35 pm    Post subject: VyrpVPN Gentoo ebuild support Reply with quote

Greetings,
I have been involved with the VyprVPN CLI project here at Golden Frog. I apologize for the confusion created by the sys-5 init script that was included in this build of the package. With the current versions of Ubuntu utilizing the upstart init system we decided that in this beta to focus on this as our primary service management tool. This script can be found at the following location "/etc/init/vyprvpn.conf". Currently the system service only uses backgrounding provided by the upstart tooling. We are also aware of an issue that causes the service to hang intermittently when using the daemonize utility in linux and have this issue tracked in our system. I hope that this gives you some of the information that you need, please feel free to reach out to us further in regards to this project.
Back to top
View user's profile Send private message
Ant P.
Watchman
Watchman


Joined: 18 Apr 2009
Posts: 6920

PostPosted: Sun Nov 16, 2014 5:54 pm    Post subject: Re: Need help writing init script for a program for an ebuil Reply with quote

StifflerStealth wrote:
[...] but when I start it, it won't give me the input line back and need to press ctrl + c to kill it. It's not running as a daemon in the background.

It may be a badly behaved daemon that tries to grab stdin from the controlling TTY, I've come across other things with the same problem. See if adding "< /dev/null" to the start-stop-daemon command helps.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum