File: ifplugd.udev.agent

package info (click to toggle)
ifplugd 0.28-19
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,152 kB
  • ctags: 772
  • sloc: sh: 2,535; ansic: 2,023; makefile: 176
file content (90 lines) | stat: -rw-r--r-- 2,214 bytes parent folder | download | duplicates (2)
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
#!/bin/sh
# udev agent script

HOTPLUGFUNCS=/lib/udev/hotplug.functions
[ -f $HOTPLUGFUNCS ] || exit 1
. $HOTPLUGFUNCS

if [ -z "$INTERFACE" ]; then
    mesg Bad invocation: \$INTERFACE is not set
    exit 1
fi

DAEMON_NAME=ifplugd
DAEMON=/usr/sbin/$DAEMON_NAME
if [ ! -x $DAEMON ]; then
    mesg No $DAEMON_NAME executable: $DAEMON
    exit 1
fi

CFG=/etc/default/$DAEMON_NAME
if [ -f $CFG ]; then
    . $CFG
else
    mesg No $DAEMON_NAME configuration file
    exit 1
fi

# return true (0) if searchifc ($1) is in argument list ($@)
# return false (1) otherwise
search_interfaces () {
    searchifc=$1
    shift

    for i in $@; do
        if [ "$i" = "$searchifc" ] || [ "$i" = "all" ]; then
            return 0
        fi
    done

    return 1
}

# wait for networking to be available, taken from net.agent (ifupdown)
wait_for_interface () {
    waitifc=$1

    while :; do
        ifcstate="$(cat /sys/class/net/${waitifc}/operstate 2>/dev/null || true)"
        if [ "$ifcstate" != down ]; then
                return 0
        fi
        sleep 1
    done
}

ifplugd_daemon () {
    search_interfaces "$INTERFACE" $INTERFACES
    if [ $? -gt 0 ]; then
        # Interface isn't statically managed by ifplugd
        search_interfaces "$INTERFACE" $HOTPLUG_INTERFACES
        if [ $? -eq 0 ]; then
            # Interface is in hotplug allowed list
            case "$ACTION" in
            add|register)
                debug_mesg Invoking $DAEMON_NAME for $INTERFACE

                # check for interface specific arguments
                IF1=$(echo $INTERFACE | sed "s/-/_/")
                A=$(eval echo \$\{ARGS_${IF1}\})
                [ -z "$A" ] && A="$ARGS"

                # wait for loopback interface to exist, we may have
                # been invoked very early in boot sequence
                wait_for_interface lo

                # spawn ifplugd daemon
                exec $DAEMON -i $INTERFACE $A
                ;;
            remove|unregister)
                debug_mesg Stopping $DAEMON_NAME for $INTERFACE

                # kill a running ifplugd daemon
                exec $DAEMON -k -i $INTERFACE
                ;;
            esac
        fi
    fi
}

ifplugd_daemon &