summaryrefslogtreecommitdiff
path: root/security/zeronet/files/zeronet-service.in
blob: 1ec6afe3d478dd0517524cf4397497e7e0d2376b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh

# Copyright (C) 2016 by Yuri Victorovich. All rights reserved.

# PROVIDE: zeronet
# REQUIRE: NETWORKING SERVERS tor
# KEYWORD: shutdown

# zeronet is disabled by default, if you have configuration file
#
# Add the following line to /etc/rc.conf to enable zeronet:
#
#zeronet_enable="YES"

. /etc/rc.subr


name="zeronet"
rcvar=zeronet_enable
start_cmd="zeronet_start"
stop_cmd="zeronet_stop"
status_cmd="zeronet_status"

load_rc_config ${name}

: ${zeronet_enable="NO"}
: ${zeronet_args=""}

is_process_running() {
  local pidfile=$1
  [ -f $pidfile ] && procstat `cat $pidfile` >/dev/null 2>&1
}

stop_daemon() {
  # assume PID is also PGID (daemon(8) PID is always PGID)
  [ -f "$1" ] && kill -- -$(cat $1)
}

zeronet_start() {
  local logfile=/var/log/zeronet.log
  local pidfile=/var/run/zeronet.pid
  # already running?
  if is_process_running $pidfile; then
    echo "zeronet is already running (pid=$(cat $pidfile))"
    return 1
  fi
  # log file
  touch $logfile
  chmod 640 $logfile
  # user depends on the port option, so better force it on directories to avoid user confusion
  chown -R %%USER%%:%%GROUP%% /var/db/zeronet /var/log/zeronet
  # workaround for https://github.com/HelloZeroNet/ZeroNet/issues/477: ZeroNet shouldn't be re-running coffee on the pre-installed files.
  (cd %%LOCALBASE%%/share/zeronet && touch `find . -name all.js`)
  # create /var/db/zeronet/zeronet.conf because it is written to by ZeroNet when the 'tor' option isn't used
  touch /var/db/zeronet/zeronet.conf
  chown -R %%USER%%:%%GROUP%% /var/db/zeronet/zeronet.conf
  # run
  cd %%LOCALBASE%%/share/zeronet
  /usr/sbin/daemon -P $pidfile -u %%USER%% %%LOCALBASE%%/share/zeronet/zeronet.py ${zeronet_args} >>$logfile 2>&1
  # make sure it runs
  if is_process_running $pidfile; then
    echo "started zeronet (pid=$(cat $pidfile))"
  else
    echo "failed to start zeronet"
  fi
}

zeronet_stop() {
  local pidfile=/var/run/zeronet.pid
  if is_process_running $pidfile; then
    echo "stopping zeronet (pid=$(cat $pidfile))"
    stop_daemon $pidfile
  else
    echo "zeronet isn't running"
  fi
}

zeronet_status() {
  local pidfile=/var/run/zeronet.pid
  if is_process_running $pidfile; then
    echo "zeronet is running, pid=$(cat $pidfile)"
  else
    echo "zeronet isn't running"
  fi
}

command="/usr/bin/true"

run_rc_command "$1"