Home | History | Annotate | Line # | Download | only in hooks
10-wpa_supplicant revision 1.1.1.1.8.1
      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 	dir=$(key_get_value "[[:space:]]*ctrl_interface=" \
     24 		"$wpa_supplicant_conf")
     25 	dir=$(trim "$dir")
     26 	case "$dir" in
     27 	DIR=*)
     28 		dir=${dir##DIR=}
     29 		dir=${dir%%[[:space:]]GROUP=*}
     30 		dir=$(trim "$dir")
     31 		;;
     32 	esac
     33 	printf %s "$dir"
     34 }
     35 
     36 wpa_supplicant_start()
     37 {
     38 	# If the carrier is up, don't bother checking anything
     39 	[ "$ifcarrier" = "up" ] && return 0
     40 
     41 	# Pre flight checks
     42 	if [ ! -s "$wpa_supplicant_conf" ]; then
     43 		syslog warn \
     44 			"$wpa_supplicant_conf does not exist"
     45 		syslog warn "not interacting with wpa_supplicant(8)"
     46 		return 1
     47 	fi
     48 	dir=$(wpa_supplicant_ctrldir)
     49 	if [ -z "$dir" ]; then
     50 		syslog warn \
     51 			"ctrl_interface not defined in $wpa_supplicant_conf"
     52 		syslog warn "not interacting with wpa_supplicant(8)"
     53 		return 1
     54 	fi
     55 
     56 	wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1 && return 0
     57 	syslog info "starting wpa_supplicant"
     58 	driver=${wpa_supplicant_driver:+-D}$wpa_supplicant_driver
     59 	err=$(wpa_supplicant -B -c"$wpa_supplicant_conf" -i"$interface" \
     60 	    "$driver" 2>&1)
     61 	errn=$?
     62 	if [ $errn != 0 ]; then
     63 		syslog err "failed to start wpa_supplicant"
     64 		syslog err "$err"
     65 	fi
     66 	return $errn
     67 }
     68 
     69 wpa_supplicant_reconfigure()
     70 {
     71 	dir=$(wpa_supplicant_ctrldir)
     72 	[ -z "$dir" ] && return 1
     73 	if ! wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1; then
     74 		wpa_supplicant_start
     75 		return $?
     76 	fi
     77 	syslog info "reconfiguring wpa_supplicant"
     78 	err=$(wpa_cli -p "$dir" -i "$interface" reconfigure 2>&1)
     79 	errn=$?
     80 	if [ $errn != 0 ]; then
     81 		syslog err "failed to reconfigure wpa_supplicant"
     82 		syslog err "$err"
     83 	fi
     84 	return $errn
     85 }
     86 
     87 wpa_supplicant_stop()
     88 {
     89 	dir=$(wpa_supplicant_ctrldir)
     90 	[ -z "$dir" ] && return 1
     91 	wpa_cli -p "$dir" -i "$interface" status >/dev/null 2>&1 || return 0
     92 	syslog info "stopping wpa_supplicant"
     93 	err=$(wpa_cli -i"$interface" terminate 2>&1)
     94 	errn=$?
     95 	if [ $errn != 0 ]; then
     96 		syslog err "failed to start wpa_supplicant"
     97 		syslog err "$err"
     98 	fi
     99 	return $errn
    100 }
    101 
    102 if [ "$ifwireless" = "1" ] && \
    103     type wpa_supplicant >/dev/null 2>&1 && \
    104     type wpa_cli >/dev/null 2>&1
    105 then
    106 	case "$reason" in
    107 	PREINIT)	wpa_supplicant_start;;
    108 	RECONFIGURE)	wpa_supplicant_reconfigure;;
    109 	DEPARTED)	wpa_supplicant_stop;;
    110 	esac
    111 fi
    112