summaryrefslogtreecommitdiffstats
path: root/dhcpcd/dhcpcd-hooks/10-wpa_supplicant
blob: a0dc018e1751d4c06f96f7e3613153be7b3ac1f1 (plain) (blame)
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
# Start, reconfigure and stop wpa_supplicant per wireless interface.
# This is needed because wpa_supplicant lacks hotplugging of any kind
# and the user should not be expected to have to wire it into their system
# if the base system doesn't do this itself.

if [ -z "$wpa_supplicant_conf" ]; then
	for x in \
		/etc/wpa_supplicant/wpa_supplicant-"$interface".conf \
		/etc/wpa_supplicant/wpa_supplicant.conf \
		/etc/wpa_supplicant-"$interface".conf \
		/etc/wpa_supplicant.conf \
	; do
		if [ -s "$x" ]; then
			wpa_supplicant_conf="$x"
			break
		fi
	done
fi
: ${wpa_supplicant_conf:=/etc/wpa_supplicant.conf}

wpa_supplcant_ctrldir() {
	local dir

	dir=$(key_get_value "[[:space:]]*ctrl_interface=" \
		"$wpa_supplicant_conf")
	dir=$(trim "$dir")
	case "$dir" in
	DIR=*)
		dir=${dir##DIR=}
		dir=${dir%%[[:space:]]GROUP=*}
		dir=$(trim "$dir")
		;;
	esac
	printf %s "$dir"
}

wpa_supplicant_start()
{
	local dir err errn

	# Pre flight checks
	if [ ! -s "$wpa_supplicant_conf" ]; then
		syslog warn \
			"$wpa_supplicant_conf does not exist"
		syslog warn "not interacting with wpa_supplicant(8)"
		return 1
	fi
	dir=$(wpa_supplcant_ctrldir)
	if [ -z "$dir" ]; then
		syslog warn \
			"ctrl_interface not defined in $wpa_supplicant_conf"
		syslog warn "not interacting with wpa_supplicant(8)"
		return 1
	fi

	wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1 && return 0
	syslog info "starting wpa_supplicant"
	err=$(wpa_supplicant -B -c"$wpa_supplicant_conf" -i"$interface" 2>&1)
	errn=$?
	if [ $errn != 0 ]; then
		syslog err "failed to start wpa_supplicant"
		syslog err "$err"
	fi
	return $errn
}

wpa_supplicant_reconfigure()
{
	local dir err errn

	dir=$(wpa_supplcant_ctrldir)
	[ -z "$dir" ] && return 1
	if ! wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1; then
		wpa_supplicant_start
		return $?
	fi
	syslog info "reconfiguring wpa_supplicant"
	err=$(wpa_cli -p "$dir" -i "$interface" reconfigure 2>&1)
	errn=$?
	if [ $errn != 0 ]; then
		syslog err "failed to reconfigure wpa_supplicant"
		syslog err "$err"
	fi
	return $errn
}

wpa_supplicant_stop()
{
	local dir err errn

	dir=$(wpa_supplcant_ctrldir)
	[ -z "$dir" ] && return 1
	wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1 || return 0
	syslog info "stopping wpa_supplicant"
	err=$(wpa_cli -i"$interface" terminate 2>&1)
	errn=$?
	if [ $errn != 0 ]; then
		syslog err "failed to start wpa_supplicant"
		syslog err "$err"
	fi
	return $errn
}

if [ "$ifwireless" = "1" ] && \
    type wpa_supplicant >/dev/null 2>&1 && \
    type wpa_cli >/dev/null 2>&1
then
	case "$reason" in
	PREINIT)	wpa_supplicant_start;;
	RECONFIGURE)	wpa_supplicant_reconfigure;;
	DEPARTED)	wpa_supplicant_stop;;
	esac
fi