Add wireless client for RED to startup process.
[ipfire-2.x.git] / src / initscripts / init.d / networking / functions.network
1 #!/bin/sh
2 #
3 ########################################################################
4 # Begin
5 #
6 # Description : A collection of functions for the IPFire network scripts
7 #
8 # Authors     : IPFire Development Team <developers@ipfire.org>
9 #
10 # Version     : 01.00
11 #
12 # Notes       : 
13 #
14 ########################################################################
15
16 . /etc/sysconfig/rc
17 . $rc_functions
18
19
20 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
21 eval $(/usr/local/bin/readhash /var/ipfire/dns/settings)
22
23 dhcpcd_get_pid() {
24                 # This function returns the pid of a dhcpcd by a given
25                 # network device, if a pidfile exists.
26
27                 local device="$1"
28                 local pidfile="/var/run/dhcpcd-${device}.pid"
29
30                 # Check if a pid file exists.
31                 if [ -f "${pidfile}" ] ; then
32
33                         # Get the pid from the file.
34                         local pid="$(<"${pidfile}")"
35
36                         echo "${pid}"
37                 fi
38 }
39
40 dhcpcd_is_running() {
41         # This functions checks if a dhcpcd is running by a given pid.
42
43         local pid="$1"
44
45         # Check if a dhcpcd is running.
46         if [ -n "${pid}" -a -d "/proc/${pid}" ]; then
47                 # Return "0" (True) if a dhcpcd is running.
48                 return 0
49         fi
50
51         # Return 1 (False) no dhcpcd is running.
52         return 1
53 }
54
55 dhcpcd_start() {
56         # This function will start a dhcpcd on a speciefied device.
57
58         local device="$1"
59         local dhcp_start=""
60
61         boot_mesg -n "Starting dhcpcd on the ${device} interface..."
62
63         # Check if a dhcpcd is already running.
64         local pid="$(dhcpcd_get_pid "${device}")"
65
66         if  dhcpcd_is_running "${pid}"; then
67                 boot_mesg "dhcpcd already running!" ${WARNING}
68                 echo_warning
69                 exit 2
70         fi
71
72         # Check if a DHCP hostname has been set.
73         if [ -n "${RED_DHCP_HOSTNAME}" ]; then
74                 dhcp_start+="-h ${RED_DHCP_HOSTNAME}"
75         fi
76
77         # Start dhcpcd.
78         /sbin/dhcpcd "${device}" "${dhcp_start}" >/dev/null 2>&1
79         ret="$?"
80
81         if [ "${ret}" -eq 0 ]; then
82                 . /var/ipfire/dhcpc/dhcpcd-"${device}".info
83                 echo ""
84                 echo_ok
85                 boot_mesg "           DHCP Assigned Settings for ${device}:"
86                 boot_mesg_flush
87                 boot_mesg "           IP Address:      $ip_address"
88                 boot_mesg_flush
89
90                 if [ -n "${RED_DHCP_HOSTNAME}" ]; then
91                         boot_mesg "           Hostname:        $RED_DHCP_HOSTNAME"
92                         boot_mesg_flush
93                 fi
94
95                 boot_mesg "           Subnet Mask:     $subnet_mask"
96                 boot_mesg_flush
97                 boot_mesg "           Default Gateway: $routers"
98                 boot_mesg_flush
99                 boot_mesg "           DNS Server:      $domain_name_servers"
100                 boot_mesg_flush
101         else
102                 echo ""
103                 $(exit "${ret}")
104                 evaluate_retval
105         fi
106 }
107
108 dhcpcd_stop() {
109         # This function stops a previously started dhcpcd on a given device.
110
111         local device="$1"
112         local dhcp_stop="-k"
113         local leaseinfo="/var/ipfire/dhcpc/dhcpcd-${device}.info"
114
115         boot_mesg -n "Stopping dhcpcd on the ${device} interface..."
116
117         # Check if a dhcpcd is running.
118         local pid="$(dhcpcd_get_pid "${device}")"
119
120         if ! dhcpcd_is_running "${pid}"; then
121                 boot_mesg "dhcpcd not running!" ${WARNING}
122                 echo_warning
123                 exit 1
124         fi
125
126         # Check if we got a valid lease.
127         if [ -e $leaseinfo ]; then
128                 . $leaseinfo
129                 if [ "$dchp_lease_time" = "4294967295" ]; then
130                         # do nothing, just echo ok
131                         echo_ok
132                 else
133                         # Stop dhcpcd.
134                         /sbin/dhcpcd "${device}" "${dhcp_stop}" &> /dev/null
135                         ret="$?"
136
137                         # Wait until dhcpd has stopped.
138                         while [ -d "/proc/${pid}" ]; do
139                                 sleep 1
140                         done
141
142                         # Display console message, depended on the exit code
143                         # of the stopped dhcpcd.
144                         if [ "${ret}" -eq 0 ]; then
145                                 echo_ok
146                         elif [ "${ret}" -eq 1 ]; then
147                                 boot_mesg "failed to stop dhcpcd!" ${WARNING}
148                                 echo_warning
149                         else
150                                 echo_failure
151                         fi
152                 fi
153         fi
154 }