aboutsummaryrefslogtreecommitdiffstats
path: root/net/ntpa/files/ntpa.in
blob: adaba01d0028261cf0d149849143f15b1ad35792 (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
#!/bin/sh
#
# $FreeBSD$
#
# PROVIDE: ntpa
# REQUIRE: networking
# KEYWORD: shutdown
#
# Add these lines to /etc/rc.conf to enable ntpa:
#
# ntpa_enable (bool):   Set to NO by default.
#                       Set it to YES to enable ntpa.
# ntpa_config (path):   Set to %%PREFIX%%/etc/ntpa/ntpa.conf
#                       by default.
# ntpa_tempdir (path):  Set to /tmp by default.
# ntpa_user (user):     Set to ntpa by default.
#
# Run additional instances of ntpa with:
# ln -s ntpa ntpa_name
#

. /etc/rc.subr

# taken from security/openvpn.
name="$file" ;

case "$0" in
/etc/rc*)
        # during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
        # so get the name of the script from $_file
        name="$_file"
        ;;
*/service)
        # do not use this as $0
        ;;
*)
        name="$0"
        ;;
esac

# default name to "ntpa" if guessing failed
# Trailing semicolon for service(8)'s benefit:
name="${name:-ntpa}" ;
name="${name##*/}"
desc="Monitors NTP daemon"
rcvar=${name}_enable

start_cmd=ntpa_start
stop_cmd=ntpa_stop
reload_cmd=ntpa_reload
configtest_cmd=ntpa_configtest

extra_commands="reload configtest"

load_rc_config ${name}

eval ": \${${name}_enable:=\"NO\"}"
eval ": \${${name}_config:=\"%%PREFIX%%/etc/ntpa/${name}.conf\"}"
eval ": \${${name}_tempdir:=\"/tmp/\"}"
eval ": \${${name}_user:=\"ntpa\"}"

config="$(eval echo \${${name}_config})"
tempdir="$(eval echo \${${name}_tempdir})"
ntpauser="$(eval echo \${${name}_user})"

pid_dir=/var/run/ntpa
pidfile="$pid_dir/${name}.pid"

ntpa_start()
{
    if [ ! -d "$pid_dir" ]; then
        install -m 0775 -g $ntpauser -o $ntpauser -d "$pid_dir"
    fi

    if [ -f ${pidfile} ]; then
        rc_pid=`cat ${pidfile}`
        echo 1>&2 "${name} already running? (pid=$rc_pid)."
        return 1
    else
        echo "Starting ${name}."
        su -m ${ntpauser} -c "sh -c '%%PREFIX%%/sbin/ntpa --config ${config} --writepid ${pidfile} --temp ${tempdir} --daemon ${name} &'"
    fi
}

ntpa_configtest()
{
    su -m ${ntpauser} -c "sh -c '%%PREFIX%%/sbin/ntpav -v ${config}'"
}

ntpa_reload()
{
    if [ ! -f ${pidfile} ]; then
        _run_rc_notrunning
        return 1
    else
        echo "Reloading ${name} configuration."
        rc_pid=`cat ${pidfile}`
        kill -USR1 $rc_pid
    fi
}

ntpa_stop()
{
    if [ ! -f ${pidfile} ]; then
        _run_rc_notrunning
        return 1
    else
        echo "Stopping ${name}."
        rc_pid=`cat ${pidfile}`
        kill -TERM $rc_pid
        wait_for_pids ${rc_pid}
    fi
}

run_rc_command "$1"