source: freewrt/package/base-files/default/etc/functions.sh@ 9e3d21f

freewrt_1_0 freewrt_2_0
Last change on this file since 9e3d21f was 9e3d21f, checked in by Thorsten Glaser <tg@…>, 19 years ago

I have a bad feeling about this code

git-svn-id: svn://www.freewrt.org/trunk/freewrt@286 afb5a338-a214-0410-bd46-81f09a774fd1

  • Property mode set to 100755
File size: 3.6 KB
Line 
1#!/bin/ash
2
3alias debug=${DEBUG:-:}
4
5# valid interface?
6if_valid() (
7 ifconfig "$1" >&- 2>&- ||
8 [ "${1%%[0-9]}" = br ] || {
9 [ "${1%%[0-9]}" = vlan ] && (
10 i=${1#vlan}
11 hwname=$(nvram get vlan${i}hwname)
12 hwaddr=$(nvram get ${hwname}macaddr)
13 [ -z "$hwaddr" ] && return 1
14
15 vif=$(ifconfig -a | awk \
16 '/^eth.*'$hwaddr'/ {print $1; exit}' IGNORECASE=1)
17 debug "# vlan$i => $vif"
18
19 $DEBUG ifconfig $vif up
20 $DEBUG vconfig add $vif $i 2>&-
21 )
22 } || { debug "# missing interface '$1' ignored"; false; }
23)
24
25do_ifup() {
26 if_proto=$(nvram get ${2}_proto)
27 if=$(nvram get ${2}_ifname)
28 [ "${if%%[0-9]}" = ppp ] && if=$(nvram get ${2}_device)
29
30 pidfile=/var/run/${if}.pid
31 [ -f $pidfile ] && $DEBUG kill $(<$pidfile)
32
33 case $1 in
34 static)
35 ip=$(nvram get ${2}_ipaddr)
36 netmask=$(nvram get ${2}_netmask)
37 gateway=$(nvram get ${2}_gateway)
38 mtu=$(nvram get ${2}_mtu)
39 static_route=$(nvram get ${2}_static_route)
40
41 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} ${mtu:+mtu $(($mtu))} broadcast + up
42 ${gateway:+$DEBUG route add default gw $gateway}
43
44 [ -n "$static_route" ] && for route in $static_route; do
45 eval "set $(echo $route | sed 's/:/ /g')"
46 [ "$2" = "255.255.255.255" ] && opt=-host
47 $DEBUG route add ${opt:-"-net"} $1 netmask $2 gw $3 metric $4
48 done
49
50 [ -f /etc/resolv.conf ] || {
51 debug "# --- creating /etc/resolv.conf ---"
52 for dns in $(nvram get ${2}_dns); do
53 echo "nameserver $dns" >>/etc/resolv.conf
54 done
55 }
56
57 env -i ACTION=ifup INTERFACE="${2}" PROTO=static /sbin/hotplug iface &
58 ;;
59 dhcp)
60 DHCP_IP=$(nvram get ${2}_ipaddr)
61 DHCP_NETMASK=$(nvram get ${2}_netmask)
62 mtu=$(nvram get ${2}_mtu)
63 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} ${mtu:+mtu $(($mtu))} broadcast + up
64
65 DHCP_ARGS="-i $if ${DHCP_IP:+-r $DHCP_IP} -b -p $pidfile"
66 DHCP_HOSTNAME=$(nvram get ${2}_hostname)
67 DHCP_HOSTNAME=${DHCP_HOSTNAME%%.*}
68 [ -z $DHCP_HOSTNAME ] || DHCP_ARGS="$DHCP_ARGS -H $DHCP_HOSTNAME"
69 [ "$if_proto" = pptp ] && DHCP_ARGS="$DHCP_ARGS -n -q" || DHCP_ARGS="$DHCP_ARGS -R &"
70 [ -r $pidfile ] && oldpid=$(<$pidfile)
71 ${DEBUG:-eval} "udhcpc $DHCP_ARGS"
72 [ -n "$oldpid" ] && pidof udhcpc | grep "$oldpid" >&- 2>&- && {
73 sleep 1
74 kill -9 $oldpid
75 }
76 # hotplug events are handled by /usr/share/udhcpc/default.script
77 ;;
78 none|"")
79 ;;
80 *)
81 [ -x "/sbin/ifup.$1" ] && { $DEBUG /sbin/ifup.$1 ${2}; exit; }
82 echo "### ifup ${2}: ignored ${2}_proto=\"$1\" (not supported)"
83 ;;
84 esac
85}
86
87load_modules() {
88 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
89}
90
91user_exists() {
92 grep -q "^$1:" $IPKG_INSTROOT/etc/passwd 2>&-
93 return $?
94}
95
96group_exists() {
97 grep -q "^$1:" $IPKG_INSTROOT/etc/group 2>&-
98 return $?
99}
100
101service_exists() {
102 grep -q "^$1[[:space:]]*$2" $IPKG_INSTROOT/etc/services 2>&-
103 return $?
104}
105
106rcconf_exists() {
107 grep -q "$1" $IPKG_INSTROOT/etc/rc.conf 2>&-
108 return $?
109}
110
111add_user() {
112 user_exists $1 || {
113 echo "adding user $1 to /etc/passwd"
114 echo "$1:x:$2:${3:-$2}:$1:${4:-/tmp}:${5:-/bin/false}" \
115 >>$IPKG_INSTROOT/etc/passwd
116 }
117}
118
119add_group() {
120 group_exists $1 || {
121 echo "adding group $1 to /etc/group"
122 echo "$1:x:$2:$3" >>$IPKG_INSTROOT/etc/group
123 }
124}
125
126add_service() {
127 service_exists $1 $2 || {
128 echo "adding service $1 to /etc/services"
129 echo -e "$1\t$2" >>$IPKG_INSTROOT/etc/services
130 }
131}
132
133add_rcconf() {
134 rcconf_exists $1 $2 || {
135 echo "adding service $1 to /etc/rc.conf"
136 echo -e "$2" >>$IPKG_INSTROOT/etc/rc.conf
137 }
138}
139
140get_next_uid() {
141 uid=$(grep -v -e ^nobody: -e ^admin: $IPKG_INSTROOT/etc/passwd | \
142 tail -n 1 | awk -F : '{ print $3 }')
143 echo $((uid+1))
144}
145
146get_next_gid() {
147 gid=$(grep -v -e ^nogroup: -e ^admin: $IPKG_INSTROOT/etc/group | \
148 tail -n 1 | awk -F : '{ print $3 }')
149 echo $((gid+1))
150}
Note: See TracBrowser for help on using the repository browser.