aboutsummarylogtreecommitdiffstats
path: root/helper
blob: 7128a9c267bb1b98e93ed778d45aaa0b7bfe879d (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/bash
PROG="$(basename $0)"
usage(){
	local m w x
	if [ -n "$1" ]; then
		w="2>&1"
		x="-1"
		m="!!!!! $PROG: error $1

"
	else
		w=""
		x="0"
	fi
	m="$m$PROG assists the alsabeep module
it takes a single argument help|install|remove|status|start|stop

help give this message

install is used to set up and start any service scripts should normally be
called from pre_install.

remove is the opposite of install, shuts down and removes any service scripts
normally used in pre_remove script.

status reports on the status of the alsabeep module and alsabeep-daemon.

start insmods the alsabeep module and starts alsabeep-daemon. Will normally
be invoked from a service script. A -L<div> argument can be used to modify
stdout/err diversion. The -D<int> argument can control the alsabeep module's
debug value. The a s f t v l n arguments see /etc/alsabeep-helper.conf can
also be overridden.

stop rmmods the alsabeep module and kills alsabeep-daemon. Will normally
be invoked from a service script. The stdout/err diversion can also be
altered using -L<div>.
"
	if [ "$x" = "0" ]; then
		echo "$m"
	else
		echo "$m" 1>&2
	fi
	exit $x
}

getArg(){
	declare -g arg
	local a
	for a in "$@"; do
		case "$a" in
			(help|install|remove|status|start|stop)
				[ -n "$arg" ] && usage "additional argument '$a' unwanted after '$arg'"
				arg="$a";;
			(*);;
		esac
	done
	[ -z "$arg" ] && usage "no positional argument found"
}

getArg "$@"

checkRoot(){
	[ "`id -un`" != "root" ] && usage "needs to be run as root"
}

parseArgs(){
	declare -g alsabeep_exe alsabeep_debug alsabeep_log
	local opt opts="$1" nargs=0
	shift
	while [ $OPTIND -le "$#" ]; do
		if getopts "$opts" opt; then
			case $opt in
				(a|s|f|t|v|l|n) eval "alsabeep_$opt='$OPTARG'";;
				(L) alsabeep_log="$OPTARG";;
				(D) alsabeep_debug="$OPTARG";;
				(\?) usage "unexpected -$OPTARG";;
				(*) usage "option -$OPTARG requires an argument.";;
			esac
		else
			[ "$nargs" != 0 ] && usage "unexpected positional argument '${!OPTIND}'"
			((OPTIND++))
			((nargs++))
		fi
	done
	source /etc/alsabeep.conf
}

divert(){
	if [ -n "$alsabeep_log" ]; then
		eval "exec &>> $alsabeep_log"
		echo "###### $PROG $@"
	fi
}

header(){
	echo "##### alsabeep-helper $1 @ `date -u '+%Y%m%d%H%M%S'`"
}

stop(){
	header "stop"
	if lsmod | grep -qs alsabeep; then rmmod alsabeep && echo "unloaded module alsabeep"; fi
	killall -q alsabeep-daemon && echo "killed alsabeep-daemon"
	rm -rf /dev/input/by-path/platform-pcspkr-event-spkr && echo "removed platform-pcspkr-event-spkr"
}

start(){
	header "start"
	if modprobe alsabeep debug="${alsabeep_debug:0}"; then
		echo "modprobe alsabeep debug=${alsabeep_debug:0}"
	fi

	if lsmod | grep -sq alsabeep; then
		for x in $(find /sys/devices/virtual/input -name name); do
			if [ "$(cat $x)" = "alsabeep" ]; then
				#we found our device
				d="$(dirname $x)"
				d=$(ls -d $d/event*)
				[ -n "$d" ] && r="-r/dev/input/$(basename $d)"
				break
			fi
		done
		if [ -z "$r" ]; then
			usage "cannot find alsabeep /dev/input/ device"
		fi
	else
		usage "failed to load alsabeep"
	fi

	echo "# about to exec
${alsabeep_exe} $r $LFLAG"
	eval "exec $alsabeep_exe $r $LFLAG"
}

unknown_init(){
	usage "unknown init '`readlink /usr/bin/init`'"
}

install(){
	header "install"
	rmmod pcspkr 2>/dev/null
	rmmod fancybeep 2>/dev/null
	#set up service files
	case "`readlink /usr/bin/init`" in
		(*systemd)
			cat > /lib/systemd/system/alsabeep.service <<\EOF
[Unit]
Description=Alsa based beep replacement for systems without a PC Speaker

[Service]
ExecStart=/usr/bin/alsabeep-helper start
ExecStop=/usr/bin/alsabeep-helper stop

[Install]
WantedBy=default.target
EOF
			systemctl enable alsabeep
			systemctl start alsabeep
			systemctl daemon-reload
			;;
		(runit-init)
			mkdir /etc/runit/sv/alsabeep
			cat >/etc/runit/sv/alsabeep/run <<\EOF
#!/usr/bin/sh
exec /usr/bin/alsabeep-helper start
EOF
			cat >/etc/runit/sv/alsabeep/finish <<\EOF
#!/usr/bin/sh
exec /usr/bin/alsabeep-helper stop
EOF
			chmod a+x /etc/runit/sv/alsabeep/*
			ln -s ../../sv/alsabeep /run/runit/service/
			;;
		(openrc-init)
			cat >/etc/init.d/alsabeep <<\EOF
#!/sbin/openrc-run
name=alsabeep
description="Alsa based beep replacement for systems without a PC Speaker"
start() {
		ebegin "Starting alsabeep"
		/usr/bin/alsabeep-helper -n0 start
		eend 0
}
stop() {
		ebegin "Stopping alsabeep"
		/usr/bin/alsabeep-helper stop
		eend 0
}
status() {
		p="`pidof alsabeep-daemon`"
		if [ -d /sys/module/alsabeep ]; then
			m='is running'
			[ -n "$p" ] && m="$m with alsabeep-daemon[$p]" || m="$m without alsdabeep-daemon"
		else
			m='is stopped'
			[ -n "$p" ] && m="$m, but alsabeep-daemon[$p] runs" || true
		fi
		einfo "Status alsabeep $m"
}
EOF
			chmod a+x /etc/init.d/alsabeep
			rc-update add alsabeep default
			rc-service alsabeep start
			rc-update -u
			;;
		(dinit-init)
			cat > /etc/dinit.d/alsabeep <<\EOF
type         = scripted
command      = /bin/alsabeep-helper -n0 -L/var/log/dinit/alsabeep.log start
stop-command = /bin/alsabeep-helper stop
restart      = false
waits-for    = loginready
EOF
			dinitctl enable alsabeep
			dinitctl start alsabeep
			dinitcheck
			;;
		(s6-init)
			mkdir /etc/s6/sv/alsabeep
			echo "longrun" > /etc/s6/sv/alsabeep/type
			cat >/etc/s6/sv/alsabeep/run <<\EOF
#!/usr/bin/sh
exec /usr/bin/alsabeep-helper start
EOF
			cat >/etc/s6/sv/alsabeep/finish <<\EOF
#!/usr/bin/sh
exec /usr/bin/alsabeep-helper stop
EOF
			chmod a+x /etc/s6/sv/alsabeep/*
			touch /etc/s6/adminsv/default/contents.d/alsabeep
			s6-db-reload
			s6-rc -u change /run/service/alsabeep
			;;
		(*) unknown_init;;
	esac
	echo "users need to be in the input group for alsabeep to work properly
use groups command to check and then sudo usermod -a -G input <username> if 
needed"
}

remove(){
	header "remove"
	case "`readlink /usr/bin/init`" in
		(*systemd)
			systemctl stop alsabeep
			systemctl disable alsabeep
			systemctl daemon-reload
			;;
		(runit-init)
			rm -f /run/runit/service/alsabeep
			;;
		(openrc-init)
			rc-service alsabeep stop
			rc-update delete alsabeep default
			rc-update -u
			;;
		(dinit-init)
			dinitctl stop alsabeep
			dinitctl disable alsabeep
			dinitcheck
			;;
		(s6-init)
			s6-rc -d change /run/service/alsabeep
			rm -f /etc/s6/adminsv/default/contents.d/alsabeep
			s6-db-reload
			;;
		(*) unknown_init;;
	esac
	stop
	rm -rf	/lib/systemd/system/alsabeep.service \
			/etc/runit/sv/alsabeep \
			/etc/init.d/alsabeep \
			/etc/dinit.d/alsabeep \
			/etc/s6/*/*/*/alsabeep \
			|| true
}

case "$arg" in
	(help)
		usage
		;;
	(install)
		checkRoot
		install
		exit
		;;
	(remove)
		checkRoot
		remove
		exit
		;;
	(start)
		checkRoot
		parseArgs ":a:d:f:l:n:t:v:L:D:" "$@"
		divert
		stop
		start
		;;
	(stop)
		checkRoot
		parseArgs ":L:" "$@"
		divert
		stop
		;;
	(status)
		if [ -d "/sys/module/alsabeep" ]; then
			echo "module alsabeep is loaded"
		else
			echo "module alsabeep is not loaded"
		fi
		pidof -q alsabeep-daemon && echo "alsabeep-daemon is running" || echo "alsabeep-daemon is not running"
		;;
esac