]>
Commit | Line | Data |
---|---|---|
71ea0d68 SS |
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. | |
2e28ecea | 78 | /sbin/dhcpcd ${dhcp_start} ${device} >/dev/null 2>&1 |
71ea0d68 SS |
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 | |
02d67e75 | 121 | boot_mesg " Not running." ${WARNING} |
71ea0d68 SS |
122 | echo_warning |
123 | exit 1 | |
124 | fi | |
125 | ||
d43bb759 | 126 | # Stop dhcpcd. |
2e28ecea | 127 | /sbin/dhcpcd ${dhcp_stop} ${device} &> /dev/null |
d43bb759 AF |
128 | ret="$?" |
129 | ||
130 | # Wait until dhcpd has stopped. | |
131 | while [ -d "/proc/${pid}" ]; do | |
132 | sleep 1 | |
133 | done | |
134 | ||
135 | # Display console message, depended on the exit code | |
136 | # of the stopped dhcpcd. | |
137 | if [ "${ret}" -eq 0 ]; then | |
138 | boot_mesg | |
139 | echo_ok | |
140 | elif [ "${ret}" -eq 1 ]; then | |
141 | boot_mesg "failed to stop dhcpcd!" ${WARNING} | |
142 | echo_warning | |
143 | else | |
144 | boot_mesg | |
145 | echo_failure | |
71ea0d68 SS |
146 | fi |
147 | } |