summaryrefslogtreecommitdiff
path: root/arch-tmpfiles
blob: 3b9488586cb2a47d68c62f5bf4f57359acd4772b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/bin/bash
#
# /usr/lib/initscripts/arch-tmpfiles
#
# Control creation, deletion, and cleaning of volatile and temporary files
#

warninvalid() {
	printf "arch-tmpfiles: ignoring invalid entry on line %d of \`%s'\n" "$LINENUM" "$FILE"
	(( ++error ))
} >&2

checkparams() {
	local parmreq=$1; shift
	local path=$1 mode=$2 uid=$3 gid=$4

	# parmcount must be >= parmreq
	if (( $# < parmreq )); then
		return 1
	fi

	# mode must be valid octal and 3 or 4 digits
	if [[ $mode && ! $mode =~ ^[0-7]{3,4}$ ]]; then
		return 1
	fi

	# uid must be numeric or a valid user name
	if [[ $uid ]] && ! getent passwd "$uid" >/dev/null; then
		return 1
	fi

	# gid must be numeric or a valid group name
	if [[ $gid ]] && ! getent group "$gid" >/dev/null; then
		return 1
	fi

	return 0
}

relabel() {
	local -a paths=($1)
	local mode=$2 uid=$3 gid=$4

	if ! checkparams 4 "$@"; then
		warninvalid
		return
	fi

	for path in "${paths[@]}"; do
		if [[ -e $path ]]; then
			[[ $uid != '-' ]] && chown $CHOPTS "$uid" "$path"
			[[ $gid != '-' ]] && chgrp $CHOPTS "$gid" "$path"
			[[ $mode != '-' ]] && chmod $CHOPTS "$mode" "$path"
		fi
	done
}

_f() {
	# Create a file if it doesn't exist yet
	local path=$1 mode=$2 uid=$3 gid=$4

	(( CREATE )) || return 0

	if ! checkparams 4 "$@"; then
		warninvalid
		return
	fi

	if [[ ! -e $path ]]; then
		install -m"$mode" -o"$uid" -g"$gid" /dev/null "$path"
	fi
}

_F() {
	# Create or truncate a file
	local path=$1 mode=$2 uid=$3 gid=$4

	(( CREATE )) || return 0

	if ! checkparams 4 "$@"; then
		warninvalid
		return
	fi

	install -m"$mode" -o"$uid" -g"$gid" /dev/null "$path"
}

_d() {
	# Create a directory if it doesn't exist yet
	local path=$1 mode=$2 uid=$3 gid=$4

	(( CREATE )) || return 0

	if ! checkparams 4 "$@"; then
		warninvalid
		return
	fi

	if [[ ! -d "$path" ]]; then
		install -d -m"$mode" -o"$uid" -g"$gid" "$path"
	fi
}

_D() {
	# Create or empty a directory
	local path=$1 mode=$2 uid=$3 gid=$4

	if ! checkparams 4 "$@"; then
		warninvalid
		return
	fi

	if [[ -d $path ]] && (( REMOVE )); then
		find "$path" -mindepth 1 -maxdepth 1 -xdev -exec rm -rf {} +
	fi

	if (( CREATE )); then
		install -d -m"$mode" -o"$uid" -g"$gid" "$path"
	fi
}

_p() {
	# Create a named pipe (FIFO) if it doesn't exist yet
	local path=$1 mode=$2 uid=$3 gid=$4

	(( CREATE )) || return 0

	if ! checkparams 4 "$@"; then
		warninvalid
		return
	fi

	if [[ ! -p "$path" ]]; then
		mkfifo -m$mode "$path"
		chown "$uid:$gid" "$path"
	fi
}

_x() {
	# Ignore a path during cleaning. Use this type to exclude paths from clean-up as
	# controlled with the Age parameter. Note that lines of this type do not
	# influence the effect of r or R lines. Lines of this type accept shell-style
	# globs in place of of normal path names.
	:
	# XXX: we don't implement this
}

_r() {
	# Remove a file or directory if it exists. This may not be used to remove
	# non-empty directories, use R for that. Lines of this type accept shell-style
	# globs in place of normal path names.
	local path
	local -a paths=($1)

	(( REMOVE )) || return 0

	if ! checkparams 1 "$@"; then
		warninvalid
		return
	fi

	for path in "${paths[@]}"; do
		if [[ -f $path ]]; then
			rm -f "$path"
		elif [[ -d $path ]]; then
			rmdir "$path"
		fi
	done
}

_R() {
	# Recursively remove a path and all its subdirectories (if it is a directory).
	# Lines of this type accept shell-style globs in place of normal path names.
	local path
	local -a paths=($1)

	(( REMOVE )) || return 0

	if ! checkparams 1 "$@"; then
		warninvalid
		return
	fi

	for path in "${paths[@]}"; do
		[[ -d $path ]] && rm -rf --one-file-system "$path"
	done
}

_z() {
	# Set ownership, access mode and relabel security context of a file or
	# directory if it exists. Lines of this type accept shell-style globs in
	# place of normal path names.
	local -a paths=($1)
	local mode=$2 uid=$3 gid=$4

	(( CREATE )) || return 0

	relabel "$@"
}

_Z() {
	# Recursively set ownership, access mode and relabel security context of a
	# path and all its subdirectories (if it is a directory). Lines of this type
	# accept shell-style globs in place of normal path names.

	(( CREATE )) || return 0

	CHOPTS=-R relabel "$@"
}

shopt -s nullglob

declare -i CREATE=0 REMOVE=0 CLEAN=0 error=0 LINENO=0
declare FILE=
declare -A fragments
declare -a tmpfiles_d=(
	/usr/lib/tmpfiles.d/*.conf
	/etc/tmpfiles.d/*.conf
	/run/tmpfiles.d/*.conf
)

while (( $# )); do
	case $1 in
		--create) CREATE=1 ;;
		--remove) REMOVE=1 ;;
	esac
	shift
done

if (( !(CREATE + REMOVE) )); then
	printf 'usage: %s [--create] [--remove]\n' "${0##*/}"
	exit 1
fi

# directories declared later in the tmpfiles_d array will override earlier
# directories, on a per file basis.
# Example: `/etc/tmpfiles.d/foo.conf' supersedes `/usr/lib/tmpfiles.d/foo.conf'.
for path in "${tmpfiles_d[@]}"; do
	[[ -f $path ]] && fragments[${path##*/}]=${path%/*}
done

# catch errors in functions so we can exit with something meaningful
set -E
trap '(( ++error ))' ERR

# loop through the gathered fragments, sorted globally by filename.
# `/run/tmpfiles/foo.conf' will always be read after `/etc/tmpfiles.d/bar.conf'
while read -d '' fragment; do
	LINENUM=0

	printf -v FILE '%s/%s' "${fragments[$fragment]}" "$fragment"

	### FILE FORMAT ###
	# XXX: We ignore the final 'Age' parameter
	# 0    1              2    3    4    5
	# Type Path           Mode UID  GID  Age
	# d    /run/user      0755 root root 10d

	# omit read's -r flag to honor escapes here, so that whitespace can be
	# escaped for paths. We will _not_ honor quoted paths.
	while read -a line; do
		(( ++LINENUM ))

		# skip over comments and empty lines
		if (( ! ${#line[*]} )) || [[ ${line[0]:0:1} = '#' ]]; then
			continue
		fi

		# whine about invalid entries
		if ! type -t _${line[0]} >/dev/null; then
			warninvalid
			continue
		fi

		# fall back on defaults when parameters are passed as '-'
		if [[ ${line[2]} = '-' ]]; then
			case ${line[0]} in
				p|f|F) line[2]=0644 ;;
				d|D) line[2]=0755 ;;
			esac
		fi
		[[ ${line[3]} = '-' ]] && line[3]=0
		[[ ${line[4]} = '-' ]] && line[4]=0

		"_${line[@]}"
	done <"$FILE"
done < <(printf '%s\0' "${!fragments[@]}" | sort -z)

exit $error

# vim: set ts=2 sw=2 noet: