globals 2.83 KB
Newer Older
Jouke Witteveen's avatar
Jouke Witteveen committed
1
NETCTL_VERSION="notpackaged"
2
PROFILE_DIR="/etc/netctl"
Jouke Witteveen's avatar
Jouke Witteveen committed
3
SUBR_DIR="/usr/lib/network"
Jouke Witteveen's avatar
Jouke Witteveen committed
4
CONN_DIR="$SUBR_DIR/connections"
Jouke Witteveen's avatar
Jouke Witteveen committed
5
STATE_DIR="/run/network"
6
STATE_FILE="${NETCTL_STATE_FILE:-/var/lib/netctl/netctl.state}"
7

Jim Pryor's avatar
Jim Pryor committed
8

9
10
### Logging/Error reporting

11
report_notice() {
12
13
14
    echo "$*"
}

15
report_error() {
16
17
18
    echo "$*"
}

19
20
report_debug() {
    is_yes "${NETCTL_DEBUG:-no}" && echo "DEBUG: $*" >&2
21
22
}

23
24
25
exit_error() {
    report_error "$@" >&2
    exit 1
26
27
28
}


29
30
31
32
33
34
35
36
37
38
39
40
### Variable management

## Check if a variable occurs in an array
# $1: the variable to find
# $2...: the array elements
in_array() {
    local hay needle=$1
    shift
    for hay; do
        [[ $hay == "$needle" ]] && return 0
    done
    return 1
41
42
}

43
44
45
46
47
## Check if a variable denotes a positive truth value
# $1: the variable to check, use is_yes ${VAR:-yes} to set a default
is_yes() {
    case ${1,,} in
      yes|true|on|1)
48
        return 0
Jouke Witteveen's avatar
Jouke Witteveen committed
49
      ;;
50
      no|false|off|0)
51
        return 1
Jouke Witteveen's avatar
Jouke Witteveen committed
52
      ;;
53
54
      *)
        report_error "Not a valid truth value: '$1'"
Jouke Witteveen's avatar
Jouke Witteveen committed
55
        return 2
Jouke Witteveen's avatar
Jouke Witteveen committed
56
      ;;
57
58
59
    esac
}

60
61
62
63
64

### Control flow

## Show what we evaluate when debugging, but always evaluate
do_debug() {
Jouke Witteveen's avatar
Jouke Witteveen committed
65
66
    report_debug "${FUNCNAME[1]}:" "$@"
    "$@"
67
68
69
70
71
72
}

## Exit if we are not effectively root
# $1: program name (optional)
ensure_root() {
    (( EUID == 0 )) || exit_error "${1-$0} needs root privileges"
Jouke Witteveen's avatar
Jouke Witteveen committed
73
}
74

75
76
77
## Waits until a statement succeeds or a timeout occurs
# $1: timeout in seconds
# $2...: condition command
78
79
80
timeout_wait() {
    local timeout=$1
    (( timeout *= 5 ))
81
    shift
Jouke Witteveen's avatar
Jouke Witteveen committed
82
    until eval "$*"; do
83
        (( timeout-- > 0 )) || return 1
84
        sleep 0.2
85
86
87
88
89
    done
    return 0
}


90
91
92
93
94
95
### Profile management

## List all acceptable profiles names (assume they don't contain newlines)
list_profiles() {
    # JP: follow aliases with -L, also skip profiles that end with '.conf' (so
    #     profile.conf can be the wpa.conf file for profile)
96
    find -L "$PROFILE_DIR/" -maxdepth 1 -type f -not -name '.*' -not -name '*~' -not -name '*.action' -not -name '*.conf' -not -name '*.service' -printf "%f\n"
97
98
99
100
101
}

## Sources all hooks, a profile and any interface hook
# $1: profile name
load_profile() {
Jim Pryor's avatar
Jim Pryor committed
102
    local hook
103
104
105
106
    if [[ ! -r "$PROFILE_DIR/$1" ]]; then
        exit_error "Profile '$1' does not exist or is not readable"
    fi
    while read -r hook; do
107
        source "$hook"
108
109
110
111
112
    done < <(find -L "$PROFILE_DIR/hooks" -maxdepth 1 -type f -executable -not -name '.*' -not -name '*~' | sort -u)
    source "$PROFILE_DIR/$1"
    if [[ -z $Interface ]]; then
        exit_error "Profile '$1' does not specify an interface"
    fi
113
    if [[ ! -r "${Connection:+$CONN_DIR/$Connection}" ]]; then
114
115
116
117
118
        exit_error "Profile '$1' does not specify a valid connection"
    fi
    if [[ -x "$PROFILE_DIR/interfaces/$Interface" ]]; then
        source "$PROFILE_DIR/interfaces/$Interface"
    fi
119
    source "$CONN_DIR/$Connection"
120
121
122
}


Jim Pryor's avatar
Jim Pryor committed
123
# vim: ft=sh ts=4 et sw=4: