aboutsummaryrefslogtreecommitdiffstats
path: root/net/nbdkit/files/nbdkit.in
blob: 3adcbb0e772f089272d9307feac3e8e0dda2797d (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/sh

# PROVIDE: nbdkit
# REQUIRE: NETWORKING
#
# Configuration settings for nbdkit in /etc/rc.conf:
#
# nbdkit_enable (bool):                Enable nbdkit. (default=NO)
# nbdkit_flags (str):                  Arguments used by all profiles.
# nbdkit_profiles (list):              Profiles.
# nbdkit_<profile>_flags (str):        Per profile arguments.

. /etc/rc.subr

name="nbdkit"
desc="Network Block Device server toolkit with stable ABI and permissive license"
rcvar="${name}_enable"
start_precmd="nbdkit_prestart"
start_cmd="nbdkit_start"
stop_cmd="nbdkit_stop"
restart_cmd="nbdkit_restart"
status_cmd="nbdkit_status"
nbdkit_bin="%%PREFIX%%/sbin/${name}"
sig_stop=SIGTERM
pid_directory="/var/run/${name}"

load_rc_config $name

: ${nbdkit_enable:="NO"}

nbdkit_check_pidfile()
{
    local profile
    profile="$1"

    local pidfile
    pidfile="${pid_directory}/${profile}.pid"

    local rc_pid
    rc_pid=$(check_pidfile "${pidfile}" "${nbdkit_bin}")

    echo "${rc_pid}"
}

nbdkit_prestart()
{
    if [ ! -d "${pid_directory}" ]; then
        mkdir -p -- "${pid_directory}"
    fi
}

nbdkit_start()
{
    local profile
    profile="$1"

    local rc_pid
    rc_pid=$(nbdkit_check_pidfile "${profile}")

    if [ -n "${rc_pid}" ]; then
        echo 1>&2 "nbdkit profile '${profile}' already running? (pid=${rc_pid})"
        return 1
    fi

    startmsg "Starting nbdkit profile '${profile}'."

    local flags

    eval flags="\${nbdkit_${profile}_flags}"

    local pidfile
    pidfile="${pid_directory}/${profile}.pid"

    eval "${nbdkit_bin}" --pidfile "${pidfile}" ${nbdkit_flags} ${flags}
}

nbdkit_stop()
{
    local profile
    profile="$1"

    local rc_pid
    rc_pid=$(nbdkit_check_pidfile "${profile}")

    local pidfile
    pidfile="${pid_directory}/${profile}.pid"

    if [ -z "${rc_pid}" ]; then
        echo 1>&2 "nbdkit profile '${profile}' not running? (check ${pidfile})"
        return 1
    fi

    echo "Stopping nbdkit profile '${profile}'."

    kill -${sig_stop} "${rc_pid}"
    wait_for_pids "${rc_pid}"
}

nbdkit_restart()
{
    nbdkit_stop "$1"
    nbdkit_start "$1"
}

nbdkit_status()
{
    local profile
    profile="$1"

    local rc_pid
    rc_pid=$(nbdkit_check_pidfile "${profile}")

    if [ -n "${rc_pid}" ]; then
        echo "nbdkit profile '${profile}' is running as pid ${rc_pid}"
    else
        echo "nbdkit profile '${profile}' is not running."
    fi
}

cmd="$1"

if [ $# -gt 0 ]; then
    shift
fi

if [ -n "$1" ]; then
    nbdkit_profiles="$1"
fi

if [ -z "${nbdkit_profiles}" ]; then
    warn "No profiles are configured, configure one to make this rc script useful!"
fi

for profile in ${nbdkit_profiles}; do
    run_rc_command "${cmd}" "${profile}"
done