summaryrefslogtreecommitdiffstats
path: root/src/netctl-auto
blob: ea692ca8cd63121a6469e3f44a6ebd5bc0c339b3 (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
#! /bin/bash

. /usr/lib/network/globals
. "$SUBR_DIR/wpa"
. "$SUBR_DIR/rfkill"

: ${ACTIOND:=wpa_actiond -p /run/wpa_supplicant}
: ${ACTION_SCRIPT:=$SUBR_DIR/auto.action}

if [[ $# != 2 || $1 != @(start|stop) ]]; then
    exit_error "Usage: netctl-auto [start|stop] <interface>"
fi

STARTSTOP=$1
INTERFACE=$2
PIDFILE="$STATE_DIR/wpa_actiond_$INTERFACE.pid"
shift 2

case $STARTSTOP in
  start)
    if wpa_is_active "$INTERFACE"; then
        exit_error "The interface ($INTERFACE) is already in use"
    fi
    if [[ -x "$PROFILE_DIR/interfaces/$INTERFACE" ]]; then
        source "$PROFILE_DIR/interfaces/$INTERFACE"
    fi
    if [[ $RFKill ]]; then
        enable_rf "$INTERFACE" "$RFKill" || exit 1
    fi

    if ! WPA_CONF=$(wpa_make_config_file "$INTERFACE"); then
        exit_error "Could not create the configuration file for interface '$INTERFACE'"
    fi
    list_profiles | while read -r profile; do
        report_debug "Examining profile '$profile'"
        (
          source "$PROFILE_DIR/$profile"
          [[ $Interface == "$INTERFACE" ]] || continue
          is_yes "${ExcludeAuto:-no}" && exit 1
          [[ $Connection != "wireless" ]] && exit 1
          : ${Security:=none}
          # Exclude wpa-config, the wpa_conf is 'complete' and doesn't fit in this scheme
          [[ $Security == "wpa-config" ]] && exit 1

          printf "%s\n" "network={" "$(wpa_make_config_block)" "id_str=\"$profile\"" "}" >> "$WPA_CONF"
          report_notice "Included profile '$profile'"
        )
    done

    # Start the WPA supplicant
    : ${WPADriver:=nl80211,wext}
    WPAOptions+=" -W"
    if wpa_start "$INTERFACE" "$WPADriver" "$WPA_CONF"; then
        if $ACTIOND -i "$INTERFACE" -P "$PIDFILE" -a "$ACTION_SCRIPT" "$@"; then
            exit 0
        fi
        wpa_stop "$INTERFACE"
    fi
    exit 1
  ;;
  stop)
    kill "$(< "$PIDFILE")"
    if [[ -x "$PROFILE_DIR/interfaces/$INTERFACE" ]]; then
        source "$PROFILE_DIR/interfaces/$INTERFACE"
    fi
    timeout_wait 1 '! wpa_is_active "$INTERFACE"' || wpa_stop "$INTERFACE"
    ip link set dev "$INTERFACE" down
    [[ $RFKill ]] && disable_rf "$INTERFACE" "$RFKill"
    exit 0
  ;;
esac


# vim: ft=sh ts=4 et sw=4: