xref: /freebsd/libexec/rc/rc.d/accounting (revision 1e121c3ef151e74d455f699ac172c9a1cfac20d8)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb#
30696600cSBjoern A. Zeeb# $FreeBSD$
40696600cSBjoern A. Zeeb#
50696600cSBjoern A. Zeeb
60696600cSBjoern A. Zeeb# PROVIDE: accounting
70696600cSBjoern A. Zeeb# REQUIRE: mountcritremote
80696600cSBjoern A. Zeeb# BEFORE: DAEMON
90696600cSBjoern A. Zeeb# KEYWORD: nojail
100696600cSBjoern A. Zeeb
110696600cSBjoern A. Zeeb. /etc/rc.subr
120696600cSBjoern A. Zeeb
130696600cSBjoern A. Zeebname="accounting"
140696600cSBjoern A. Zeebrcvar="accounting_enable"
150696600cSBjoern A. Zeebaccounting_command="/usr/sbin/accton"
160696600cSBjoern A. Zeebaccounting_file="/var/account/acct"
170696600cSBjoern A. Zeeb
180696600cSBjoern A. Zeebextra_commands="rotate_log"
190696600cSBjoern A. Zeeb
200696600cSBjoern A. Zeebstart_cmd="accounting_start"
210696600cSBjoern A. Zeebstop_cmd="accounting_stop"
220696600cSBjoern A. Zeebrotate_log_cmd="accounting_rotate_log"
230696600cSBjoern A. Zeeb
24*1e121c3eSIan Leporecreate_accounting_file()
25*1e121c3eSIan Lepore{
26*1e121c3eSIan Lepore	install -o root -g wheel -m 0640 /dev/null "${accounting_file}"
27*1e121c3eSIan Lepore}
28*1e121c3eSIan Lepore
290696600cSBjoern A. Zeebaccounting_start()
300696600cSBjoern A. Zeeb{
310696600cSBjoern A. Zeeb	local _dir
320696600cSBjoern A. Zeeb
330696600cSBjoern A. Zeeb	_dir="${accounting_file%/*}"
340696600cSBjoern A. Zeeb	if [ ! -d "$_dir" ]; then
35*1e121c3eSIan Lepore		if ! mkdir -p -m 0750 "$_dir"; then
360696600cSBjoern A. Zeeb			err 1 "Could not create $_dir."
370696600cSBjoern A. Zeeb		fi
380696600cSBjoern A. Zeeb	fi
390696600cSBjoern A. Zeeb
400696600cSBjoern A. Zeeb	if [ ! -e "$accounting_file" ]; then
410696600cSBjoern A. Zeeb		echo -n "Creating accounting file ${accounting_file}"
42*1e121c3eSIan Lepore		create_accounting_file
430696600cSBjoern A. Zeeb		echo '.'
440696600cSBjoern A. Zeeb	fi
450696600cSBjoern A. Zeeb
460696600cSBjoern A. Zeeb	echo "Turning on accounting."
470696600cSBjoern A. Zeeb	${accounting_command} ${accounting_file}
480696600cSBjoern A. Zeeb}
490696600cSBjoern A. Zeeb
500696600cSBjoern A. Zeebaccounting_stop()
510696600cSBjoern A. Zeeb{
520696600cSBjoern A. Zeeb	echo "Turning off accounting."
530696600cSBjoern A. Zeeb	${accounting_command}
540696600cSBjoern A. Zeeb}
550696600cSBjoern A. Zeeb
560696600cSBjoern A. Zeebaccounting_rotate_log()
570696600cSBjoern A. Zeeb{
58*1e121c3eSIan Lepore	# Note that this function must handle being called as "onerotate_log"
59*1e121c3eSIan Lepore	# (by the periodic scripts) when accounting is disabled, and handle
60*1e121c3eSIan Lepore	# being called multiple times (by an admin making mistakes) without
61*1e121c3eSIan Lepore	# anything having actually rotated the old .0 file out of the way.
620696600cSBjoern A. Zeeb
63*1e121c3eSIan Lepore	if [ -e "${accounting_file}.0" ]; then
64*1e121c3eSIan Lepore		err 1 "Cannot rotate accounting log, ${accounting_file}.0 already exists."
65*1e121c3eSIan Lepore	fi
660696600cSBjoern A. Zeeb
67*1e121c3eSIan Lepore	if [ ! -e "${accounting_file}" ]; then
68*1e121c3eSIan Lepore		err 1 "Cannot rotate accounting log, ${accounting_file} does not exist."
690696600cSBjoern A. Zeeb	fi
700696600cSBjoern A. Zeeb
710696600cSBjoern A. Zeeb	mv ${accounting_file} ${accounting_file}.0
720696600cSBjoern A. Zeeb
730696600cSBjoern A. Zeeb	if checkyesno accounting_enable; then
74*1e121c3eSIan Lepore		create_accounting_file
75*1e121c3eSIan Lepore		${accounting_command} "${accounting_file}"
760696600cSBjoern A. Zeeb	fi
770696600cSBjoern A. Zeeb}
780696600cSBjoern A. Zeeb
790696600cSBjoern A. Zeebload_rc_config $name
800696600cSBjoern A. Zeebrun_rc_command "$1"
81