xref: /netbsd-src/etc/rc.d/ntpd (revision 08ffba536b8f9a45c1aa93a29d85a2b2e22bc4b2)
1#!/bin/sh
2#
3# $NetBSD: ntpd,v 1.17 2020/07/04 06:24:53 skrll Exp $
4#
5
6# PROVIDE: ntpd
7# REQUIRE: DAEMON
8# BEFORE:  LOGIN
9# KEYWORD: chrootdir
10
11$_rc_subr_loaded . /etc/rc.subr
12
13name="ntpd"
14rcvar=$name
15command="/usr/sbin/${name}"
16pidfile="/var/run/${name}.pid"
17start_precmd="ntpd_precmd"
18required_files="/etc/ntp.conf"
19
20ntpd_precmd()
21{
22	rc_flags="-p ${pidfile} $rc_flags"
23	if [ -z "$ntpd_chrootdir" ]; then
24		return 0;
25	fi
26
27	# If running in a chroot cage, ensure that the appropriate files
28	# exist inside the cage, as well as helper symlinks into the cage
29	# from outside.
30	#
31	# As this is called after the is_running and required_dir checks
32	# are made in run_rc_command(), we can safely assume ${ntpd_chrootdir}
33	# exists and ntpd isn't running at this point (unless forcestart
34	# is used).
35	#
36	# Before enabling ntpd_chrootdir, ensure that:
37	#	- The kernel has "pseudo-device clockctl" compiled in
38	#	- /dev/clockctl is present
39	#
40	if (: </dev/clockctl) 2>/dev/null; then
41		: exists and works
42	else
43		logger -s -p daemon.warning -t ntpd \
44		    "WARNING: not chrooting -- no working /dev/clockctl device"
45		return 0
46	fi
47	if [ ! -c "${ntpd_chrootdir}/dev/clockctl" ]; then
48		rm -f "${ntpd_chrootdir}/dev/clockctl"
49		( cd /dev ; /bin/pax -rw -pe clockctl "${ntpd_chrootdir}/dev" )
50	fi
51	ln -fs "${ntpd_chrootdir}/var/db/ntp.drift" /var/db/ntp.drift
52
53	if [ ! -d "${ntpd_chrootdir}/etc" ]; then
54		mkdir "${ntpd_chrootdir}/etc"
55	fi
56
57	for f in resolv.conf; do
58		if ! cmp -s "/etc/$f" "${ntpd_chrootdir}/etc/$f"; then
59			cp -p "/etc/$f" "${ntpd_chrootdir}/etc/$f"
60		fi
61	done
62
63	if [ ! -f "${ntpd_chrootdir}/etc/services" ]; then
64		getent services ntp/udp ntp/tcp \
65			> "${ntpd_chrootdir}/etc/services"
66	fi
67	if [ ! -d "${ntpd_chrootdir}/var/db" ]; then
68		mkdir -p "${ntpd_chrootdir}/var/db"
69	fi
70	if [ ! -f "${ntpd_chrootdir}/var/db/services.cdb" ]; then
71		services_mkdb -o "${ntpd_chrootdir}/var/db/services.cdb" \
72			"${ntpd_chrootdir}/etc/services"
73	fi
74
75	#	Change run_rc_commands()'s internal copy of $ntpd_flags
76	#
77	rc_flags="-u ntpd:ntpd -i ${ntpd_chrootdir} $rc_flags"
78}
79
80load_rc_config $name
81run_rc_command "$1"
82