summaryrefslogtreecommitdiff
path: root/network-online
blob: e04eee904f6b6f2e12b621b4bae3912383a7ff4c (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
#!/usr/bin/env bash
# Copyright © 2015-2016 Luke Shumaker <lukeshu@sbcglobal.net>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See the COPYING file for more details.

# Usage: network-online [start|stop] NETWORK_NAME
mode="$1"
lock=/run/network-online/lock
fifo=/run/network-online/"$2"
PS4="$mode: "

# Use a named pipe (fifo) as an inter-process synchronization
# mechanism; a call to block_{a,b} blocks until its counterpart is
# run.
block_a() {
	cat "$fifo" >/dev/null
}
block_b() {
	echo foo >> "$fifo"
}

# Because of restrictions put in place by systemd, the ExecStart
# process can't run `systemctl stop network-online.target`, but the
# ExecStop process can.
case "$mode" in
	start)
		trap 'rm -f -- "$fifo"' EXIT
		set -x
		mkdir -p /run/network-online || exit $?
		exec 8>"$lock" || exit $?
		flock -s 8 || exit $?
		systemctl start network-online.target || exit $?
		mkfifo "$fifo" || exit $?

		block_a # wait for 'stop' to be called
		exec 8>&-
		block_a # signal to 'stop' that we've unlocked
		;;
	stop)
		set -x
		exec 8>"$lock" || exit $?
		flock -s 8 || exit $?
		block_b # signal 'start' to stop
		block_b # wait for 'start' to unlock
		if flock -x -n 8; then
			systemctl stop network-online.target
			rm -rf /run/network-online
		fi
		;;
esac