c042e3de1c2858b3866e40ff3d7984aa2eff057b
[oweals/gnunet.git] / contrib / services / openrc / gnunet.initd
1 #!/sbin/openrc-run
2 # Copyright 1999-2016 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 if [ -d /run ] ; then
6         PIDFILE=${PIDFILE:-/run/gnunet/arm-service.pid}
7 else
8         PIDFILE=${PIDFILE:-/var/run/gnunet/arm-service.pid}
9 fi
10
11 depend() {
12         # FIXME: refine?
13         need net
14 }
15
16 GNUNET_PATH="/usr"
17 GNUNET_HOME="/var/lib/gnunet"
18 SUID_ROOT_HELPERS="exit nat-server nat-client transport-bluetooth transport-wlan vpn"
19
20 chmodown_execbin() {
21         if [ -x $1 ]; then
22                 if [ "$3" ]; then
23                         chown $3 $1 2>/dev/null && chmod $2 $1
24                 else
25                         chmod $2 $1
26                 fi
27         fi
28 }
29
30 checkconfig() {
31         if [ -n "$(find ${GNUNET_HOME}/.local/share/gnunet -maxdepth 1 -name gnunet.conf -perm +0044)" ] ; then
32                 eerror "${conf} must not be world or group readable. Try:"
33                 eerror "    chmod 600 ${conf}"
34                 eerror "    chown gnunet:gnunet ${conf}"
35                 return 1
36         fi
37         mkdir -p ${GNUNET_HOME}/.cache/gnunet
38
39         # taken from dangole's lede config.. thx!
40         local libexec="${GNUNET_PATH}/lib/gnunet/libexec"               # why not /usr/libexec/gnunet ?
41         # not reliable enough:
42         #[ -e ${libexec}/.permfix ] && return
43         for helper in $SUID_ROOT_HELPERS; do
44                 chmodown_execbin ${libexec}/gnunet-helper-$helper u+s
45         done
46         chmodown_execbin ${libexec}/gnunet-helper-dns 4750 root:gnunetdns
47         chmodown_execbin ${libexec}/gnunet-service-dns 2750 gnunet:gnunetdns
48         #touch ${libexec}/.permfix
49 }
50
51 start() {
52         checkconfig || return 1
53
54         local piddir=$(dirname ${PIDFILE})
55         if [ ! -d ${piddir} ] ; then
56                 ebegin "Making ${piddir}"
57                 mkdir -p ${piddir}
58                 eend $?
59                 ebegin "Changing permissions of ${piddir}"
60                 chown gnunet:gnunet ${piddir}
61                 eend $?
62         fi
63
64         ebegin "Starting ${SVCNAME}"
65         # shouldn't be necessary... but
66         start-stop-daemon --start --user gnunet --name gnunet --pidfile ${PIDFILE} \
67              --exec ${GNUNET_PATH}/lib/gnunet/libexec/gnunet-service-arm -- -d
68                 # flags to be passed to the process appear after the double-dash
69         eend $?
70 }
71
72 stop() {
73         ebegin "Stopping ${SVCNAME}"
74         start-stop-daemon --stop --signal QUIT --pidfile ${PIDFILE}
75         sleep 1
76         killall -u gnunet
77         sleep 1
78         rm -rf /tmp/gnunet-gnunet-runtime >/dev/null 2>&1
79         rm -rf /tmp/gnunet-system-runtime >/dev/null 2>&1
80         eend $?
81 }
82