1#!/bin/sh
2#
3# $FreeBSD$
4
5#
6# Copyright (C) 1994-2016 The FreeBSD Project. All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30#
31# PROVIDE: ntpa
32# REQUIRE: networking
33# KEYWORD: shutdown
34#
35# Add these lines to /etc/rc.conf to enable ntpa:
36#
37# ntpa_enable (bool):   Set to NO by default.
38#                       Set it to YES to enable ntpa.
39# ntpa_config (path):   Set to %%PREFIX%%/etc/ntpa/ntpa.conf
40#                       by default.
41# ntpa_tempdir (path):  Set to /tmp by default.
42# ntpa_user (user):     Set to ntpa by default.
43#
44# Run additional instances of ntpa with:
45# ln -s ntpa ntpa_name
46#
47
48. /etc/rc.subr
49
50# taken from security/openvpn.
51name="$file" ;
52
53case "$0" in
54/etc/rc*)
55        # during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
56        # so get the name of the script from $_file
57        name="$_file"
58        ;;
59*/service)
60        # do not use this as $0
61        ;;
62*)
63        name="$0"
64        ;;
65esac
66
67# default name to "ntpa" if guessing failed
68# Trailing semicolon for service(8)'s benefit:
69name="${name:-ntpa}" ;
70name="${name##*/}"
71desc="Monitors NTP daemon"
72rcvar=${name}_enable
73
74start_cmd=ntpa_start
75stop_cmd=ntpa_stop
76reload_cmd=ntpa_reload
77configtest_cmd=ntpa_configtest
78
79extra_commands="reload configtest"
80
81load_rc_config ${name}
82
83eval ": \${${name}_enable:=\"NO\"}"
84eval ": \${${name}_config:=\"%%PREFIX%%/etc/ntpa/${name}.conf\"}"
85eval ": \${${name}_tempdir:=\"/tmp/\"}"
86eval ": \${${name}_user:=\"ntpa\"}"
87
88config="$(eval echo \${${name}_config})"
89tempdir="$(eval echo \${${name}_tempdir})"
90ntpauser="$(eval echo \${${name}_user})"
91
92pid_dir=/var/run/ntpa
93pidfile="$pid_dir/${name}.pid"
94
95ntpa_start()
96{
97    if [ ! -d "$pid_dir" ]; then
98        install -m 0775 -g $ntpauser -o $ntpauser -d "$pid_dir"
99    fi
100
101    if [ -f ${pidfile} ]; then
102        rc_pid=`cat ${pidfile}`
103        echo 1>&2 "${name} already running? (pid=$rc_pid)."
104        return 1
105    else
106        echo "Starting ${name}."
107        su -m ${ntpauser} -c "sh -c '%%PREFIX%%/sbin/ntpa --config ${config} --writepid ${pidfile} --temp ${tempdir} --daemon ${name} &'"
108    fi
109}
110
111ntpa_configtest()
112{
113    su -m ${ntpauser} -c "sh -c '%%PREFIX%%/sbin/ntpav -v ${config}'"
114}
115
116ntpa_reload()
117{
118    if [ ! -f ${pidfile} ]; then
119        _run_rc_notrunning
120        return 1
121    else
122        echo "Reloading ${name} configuration."
123        rc_pid=`cat ${pidfile}`
124        kill -USR1 $rc_pid
125    fi
126}
127
128ntpa_stop()
129{
130    if [ ! -f ${pidfile} ]; then
131        _run_rc_notrunning
132        return 1
133    else
134        echo "Stopping ${name}."
135        rc_pid=`cat ${pidfile}`
136        kill -TERM $rc_pid
137        wait_for_pids ${rc_pid}
138    fi
139}
140
141run_rc_command "$1"
142