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