[packages] dhcp: use network.sh to find device names
[openwrt/svn-archive/archive.git] / net / dhcp / files / dhcpd.init
1 #!/bin/sh /etc/rc.common
2 START=60
3
4 lease_file=/tmp/dhcpd.leases
5 config_file=/etc/dhcpd.conf
6 pid_file=/var/run/dhcpd.pid
7 hosts_file=/tmp/dhcpd.hosts
8 dhcp_ifs=""
9
10 parse_dhcp() {
11 local cfg="$1"
12 config_get net "$cfg" interface
13 [ -n "$net" ] || return 0
14
15 config_get_bool ignore "$1" ignore 0
16 if [ "$ignore" -eq 1 ]; then return 0; fi
17
18 local dev
19 network_get_device dev "$net" && append dhcp_ifs "$dev"
20 }
21
22 parse_host_entry() {
23 local cfg="$1"
24
25 config_get name "$cfg" name
26
27 config_get ip "$cfg" ip
28 [ -n "$ip" ] || return 0
29
30 local i=0
31 config_get mac "$cfg" mac
32 for m in $mac; do
33 local hostid="$name"
34 if [ $i -ne 0 ]; then hostid="$name"_"$i"; fi
35
36 echo "host $hostid {" >> "$hosts_file"
37 echo "hardware ethernet $m;" >> "$hosts_file"
38 echo "fixed-address $ip;" >> "$hosts_file"
39 echo "}" >> $hosts_file
40
41 i=$((i+1))
42 done
43 }
44
45 init_config() {
46 echo " #Automatically generated by dhcpd initscript, any modifications will be overwritten" > "$hosts_file"
47
48 . /lib/functions/network.sh
49
50 config_load dhcp
51 config_foreach parse_host_entry host
52 config_foreach parse_dhcp dhcp
53 }
54
55 start() {
56 if [ -e $pid_file ] ; then
57 echo " dhcpd already running with PID `cat $pid_file`"
58 return 1
59 fi
60
61 echo Starting isc-dhcpd
62
63 if [ ! -e $lease_file ]; then
64 echo " Creating $lease_file"
65 touch $lease_file
66 fi
67
68 /usr/sbin/dhcpd -q -cf $config_file -lf $lease_file
69
70 if [ $? -ne 0 ]; then
71 echo " isc-dhcpd failed to start"
72 fi
73 }
74
75 stop() {
76 echo "Stopping isc-dhcpd"
77 if [ -e $pid_file ]; then
78 kill -KILL `cat $pid_file`
79
80 if [ $? -ne 0 ]; then
81 echo " PID " `cat $pid_file` not found
82 echo " Is the DHCP server running?"
83 fi
84
85 rm -f $pid_file
86 else
87 echo " $pid_file not found"
88 fi
89 }