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