Home | History | Annotate | Download | only in dhcpcd-hooks
      1 # Start, reconfigure and stop wpa_supplicant per wireless interface.
      2 # This is needed because wpa_supplicant lacks hotplugging of any kind
      3 # and the user should not be expected to have to wire it into their system
      4 # if the base system doesn't do this itself.
      5 
      6 if [ -z "$wpa_supplicant_conf" ]; then
      7 	for x in \
      8 		/etc/wpa_supplicant/wpa_supplicant-"$interface".conf \
      9 		/etc/wpa_supplicant/wpa_supplicant.conf \
     10 		/etc/wpa_supplicant-"$interface".conf \
     11 		/etc/wpa_supplicant.conf \
     12 	; do
     13 		if [ -s "$x" ]; then
     14 			wpa_supplicant_conf="$x"
     15 			break
     16 		fi
     17 	done
     18 fi
     19 : ${wpa_supplicant_conf:=/etc/wpa_supplicant.conf}
     20 
     21 wpa_supplicant_ctrldir()
     22 {
     23 	local dir
     24 
     25 	dir=$(key_get_value "[[:space:]]*ctrl_interface=" \
     26 		"$wpa_supplicant_conf")
     27 	dir=$(trim "$dir")
     28 	case "$dir" in
     29 	DIR=*)
     30 		dir=${dir##DIR=}
     31 		dir=${dir%%[[:space:]]GROUP=*}
     32 		dir=$(trim "$dir")
     33 		;;
     34 	esac
     35 	printf %s "$dir"
     36 }
     37 
     38 wpa_supplicant_start()
     39 {
     40 	local dir err errn
     41 
     42 	# If the carrier is up, don't bother checking anything
     43 	[ "$ifcarrier" = "up" ] && return 0
     44 
     45 	# Pre flight checks
     46 	if [ ! -s "$wpa_supplicant_conf" ]; then
     47 		syslog warn \
     48 			"$wpa_supplicant_conf does not exist"
     49 		syslog warn "not interacting with wpa_supplicant(8)"
     50 		return 1
     51 	fi
     52 	dir=$(wpa_supplicant_ctrldir)
     53 	if [ -z "$dir" ]; then
     54 		syslog warn \
     55 			"ctrl_interface not defined in $wpa_supplicant_conf"
     56 		syslog warn "not interacting with wpa_supplicant(8)"
     57 		return 1
     58 	fi
     59 
     60 	wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1 && return 0
     61 	syslog info "starting wpa_supplicant"
     62 	driver=${wpa_supplicant_driver:+-D}$wpa_supplicant_driver
     63 	err=$(wpa_supplicant -B -c"$wpa_supplicant_conf" -i"$interface" \
     64 	    "$driver" 2>&1)
     65 	errn=$?
     66 	if [ $errn != 0 ]; then
     67 		syslog err "failed to start wpa_supplicant"
     68 		syslog err "$err"
     69 	fi
     70 	return $errn
     71 }
     72 
     73 wpa_supplicant_reconfigure()
     74 {
     75 	local dir err errn
     76 
     77 	dir=$(wpa_supplicant_ctrldir)
     78 	[ -z "$dir" ] && return 1
     79 	if ! wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1; then
     80 		wpa_supplicant_start
     81 		return $?
     82 	fi
     83 	syslog info "reconfiguring wpa_supplicant"
     84 	err=$(wpa_cli -p "$dir" -i "$interface" reconfigure 2>&1)
     85 	errn=$?
     86 	if [ $errn != 0 ]; then
     87 		syslog err "failed to reconfigure wpa_supplicant"
     88 		syslog err "$err"
     89 	fi
     90 	return $errn
     91 }
     92 
     93 wpa_supplicant_stop()
     94 {
     95 	local dir err errn
     96 
     97 	dir=$(wpa_supplicant_ctrldir)
     98 	[ -z "$dir" ] && return 1
     99 	wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1 || return 0
    100 	syslog info "stopping wpa_supplicant"
    101 	err=$(wpa_cli -i"$interface" terminate 2>&1)
    102 	errn=$?
    103 	if [ $errn != 0 ]; then
    104 		syslog err "failed to start wpa_supplicant"
    105 		syslog err "$err"
    106 	fi
    107 	return $errn
    108 }
    109 
    110 if [ "$ifwireless" = "1" ] && \
    111     type wpa_supplicant >/dev/null 2>&1 && \
    112     type wpa_cli >/dev/null 2>&1
    113 then
    114 	case "$reason" in
    115 	PREINIT)	wpa_supplicant_start;;
    116 	RECONFIGURE)	wpa_supplicant_reconfigure;;
    117 	DEPARTED)	wpa_supplicant_stop;;
    118 	esac
    119 fi
    120