1#!/bin/sh
2#
3#
4
5# PROVIDE: accounting
6# REQUIRE: mountcritremote
7# BEFORE: DAEMON
8# KEYWORD: nojail
9
10. /etc/rc.subr
11
12name="accounting"
13rcvar="accounting_enable"
14accounting_command="/usr/sbin/accton"
15accounting_file="/var/account/acct"
16
17extra_commands="rotate_log"
18
19start_cmd="accounting_start"
20stop_cmd="accounting_stop"
21rotate_log_cmd="accounting_rotate_log"
22
23create_accounting_file()
24{
25	install -o root -g wheel -m 0640 /dev/null "${accounting_file}"
26}
27
28accounting_start()
29{
30	local _dir
31
32	_dir="${accounting_file%/*}"
33	if [ ! -d "$_dir" ]; then
34		if ! mkdir -p -m 0750 "$_dir"; then
35			err 1 "Could not create $_dir."
36		fi
37	fi
38
39	if [ ! -e "$accounting_file" ]; then
40		echo -n "Creating accounting file ${accounting_file}"
41		create_accounting_file
42		echo '.'
43	fi
44
45	echo "Turning on accounting."
46	${accounting_command} ${accounting_file}
47}
48
49accounting_stop()
50{
51	echo "Turning off accounting."
52	${accounting_command}
53}
54
55accounting_rotate_log()
56{
57	# Note that this function must handle being called as "onerotate_log"
58	# (by the periodic scripts) when accounting is disabled, and handle
59	# being called multiple times (by an admin making mistakes) without
60	# anything having actually rotated the old .0 file out of the way.
61
62	if [ -e "${accounting_file}.0" ]; then
63		err 1 "Cannot rotate accounting log, ${accounting_file}.0 already exists."
64	fi
65
66	if [ ! -e "${accounting_file}" ]; then
67		err 1 "Cannot rotate accounting log, ${accounting_file} does not exist."
68	fi
69
70	mv ${accounting_file} ${accounting_file}.0
71
72	if checkyesno accounting_enable; then
73		create_accounting_file
74		${accounting_command} "${accounting_file}"
75	fi
76}
77
78load_rc_config $name
79run_rc_command "$1"
80