Git init
[external/ifupdown.git] / debian / ifupdown.init
1 #!/bin/sh -e
2 ### BEGIN INIT INFO
3 # Provides:          ifupdown
4 # Required-Start:    ifupdown-clean
5 # Required-Stop:     $local_fs
6 # Default-Start:     S
7 # Default-Stop:      0 6
8 # Short-Description: Prepare the system for taking up interfaces.
9 ### END INIT INFO
10
11 [ -x /sbin/ifup ] || exit 0
12 [ -x /sbin/ifdown ] || exit 0
13
14 MYNAME="${0##*/}"
15 report() { echo "${MYNAME}: $*" ; }
16 report_err() { report "Error: $*" >&2 ; }
17 RUN_DIR=/etc/network/run
18 [ -r /etc/default/ifupdown ] && . /etc/default/ifupdown
19
20 # Note: The state file location is hardcoded in ifup|ifdown
21 IFSTATE=/etc/network/run/ifstate
22
23 myreadlink () {
24   dest="${1%/}"
25   extras=""
26
27   while [ "$dest" != "" ]; do
28     if [ -d "$dest" ]; then
29       cd "$dest"
30       dest=$(/bin/pwd)
31       break
32     fi
33
34     if [ -L "$dest" ]; then
35       d2=$(readlink "$dest")
36       if [ "${d2#/}" = "$d2" ]; then
37         dest="${dest%/*}/$d2"
38       else
39         dest="$d2"
40       fi
41     fi
42
43     while [ ! -e "$dest" ]; do
44       extras="${dest##*/}/$extras"
45       if [ "${extras%%/*}" = ".." ]; then return 1; fi
46       destx="${dest%/*}"
47       if [ "$destx" = "$dest" ]; then destx=""; fi
48       dest="$destx"
49     done
50   done
51   dest="$dest/$extras"
52   echo "${dest%/}"
53 }
54
55 case "$1" in
56   start|restart)
57     if [ "$2" ]; then
58       report_err "Arguments to '$1' command not accepted"
59       exit 3
60     fi
61     echo -n "Setting up networking..."
62
63     # if /etc/network/run is a symlink to a directory that doesn't exist,
64     # create it.
65
66     if [ -L "$RUN_DIR" ] && [ ! -d "$RUN_DIR" ] ; then
67       runmkdir="$(myreadlink "$RUN_DIR")"
68       if [ ! "$runmkdir" ] ; then
69         echo "failed."
70         report_err "Cannot create target of /etc/network/run"
71         exit 1
72       fi
73       if ! mkdir -p "$runmkdir"; then
74         echo "failed."
75         report_err "Failure creating directory $runmkdir"
76         exit 1
77       fi
78     fi
79
80     # Create the state file
81     # Doing this also signals that ifupdown is available for use
82     if [ ! -r "$IFSTATE" ]; then
83       if ! : > "$IFSTATE" ; then
84         echo "failed."
85         report_err "Failure initializing $IFSTATE"
86         exit 1
87       fi 
88     fi
89
90     echo "done."
91     exit 0
92     ;;
93
94   stop)
95     if [ "$2" ]; then
96       report_err "Arguments to '$1' command not accepted"
97       exit 3
98     fi
99     if [ -x /etc/init.d/ifupdown-clean ]; then
100       /etc/init.d/ifupdown-clean start
101     fi
102     ;;
103
104   force-reload)
105     ;;
106
107   *)
108     echo "Usage: $0 {start|stop|restart|force-reload}" >&2
109     exit 3
110     ;;
111 esac
112
113 exit 0