/[baselayout]/trunk/net-scripts/net.modules.d/helpers.d/functions
Gentoo

Contents of /trunk/net-scripts/net.modules.d/helpers.d/functions

Parent Directory Parent Directory | Revision Log Revision Log


Revision 872 - (show annotations) (download)
Thu Jan 27 12:37:06 2005 UTC (14 years, 3 months ago) by uberlord
File size: 3273 byte(s)
removed udhcpc-* and dhclient-* helper modules and replaced them with a
generic dhcp module which caters for all interfaces and dhcp clients that
need it.
We now prefer iproute2 over ifconfig if both are installed.

1 # Copyright (c) 2004-2005 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header$
4
5 # Contributed by Roy Marples (uberlord@gentoo.org)
6
7 [[ -z ${devnull} ]] && devnull=/dev/null
8
9 # int netmask2cidr(char *netmask)
10 #
11 # Returns the CIDR of a given netmask
12 netmask2cidr() {
13 local binary="" i
14
15 for i in ${1//./ }; do
16 while [[ ${i} != 0 ]]; do
17 binary=$[${i}%2]${binary}
18 (( i=i>>1 ))
19 done
20 done
21 binary=${binary%%0*}
22 echo ${#binary}
23 }
24
25 # char* netmask2cidr(int cidr)
26 #
27 # Returns the netmask of a given CIDR
28 cidr2netmask() {
29 local cidr=${1} netmask="" done=0 i sum=0 cur=128
30 local octets frac
31
32 (( octets=cidr/8 ))
33 (( frac=cidr%8 ))
34 while [[ octets -gt 0 ]]; do
35 netmask="${netmask}.255"
36 (( octets-- ))
37 (( done++ ))
38 done
39
40 if [[ ${done} -lt 4 ]]; then
41 for (( i=0; i<${frac}; i++ )); do
42 (( sum+=cur ))
43 (( cur/=2 ))
44 done
45 netmask="${netmask}.${sum}"
46 (( done++ ))
47
48 while [[ ${done} -lt 4 ]]; do
49 netmask="${netmask}.0"
50 (( done++ ))
51 done
52 fi
53
54 echo ${netmask:1}
55 }
56
57 # char* interface_device(char *iface)
58 #
59 # Gets the base device of the interface
60 # Can handle eth0:1 and eth0.1
61 # Which returns eth0 in this case
62 interface_device() {
63 local dev=${1%%.*}
64 [[ ${dev} == ${1} ]] && dev=${1%%:*}
65 echo ${dev}
66 }
67
68 # char* interface_type(char* iface)
69 #
70 # Returns the base type of the interface
71 # eth, ippp, etc
72 interface_type() {
73 echo ${1%%[0-9]*}
74 }
75
76 # bool clean_pidfile(char *file)
77 #
78 # Removes the given pidfile if the process is not running
79 # Returns 1 if the process is still running otherwise 0
80 clean_pidfile() {
81 local pidfile=${1}
82
83 [[ ! -f ${pidfile} ]] && return 0
84 local pid=$( cat ${pidfile} )
85
86 if [[ -n ${pid} ]]; then
87 local cmd=${pidfile##*/}
88 cmd=${cmd%%-*}
89 ps -p ${pid} 2>/dev/null | grep -q ${cmd} && return 1
90 fi
91
92 rm -f ${pidfile}
93 return 0
94 }
95
96 # bool process_finished(int pid, char* cmd)
97 #
98 # We wait for 10 seconds until the command ${cmd}
99 # stops running on the process ${pid}
100 process_finished() {
101 local i pid=$1 cmd=$2 secs=${3:-9}
102
103 for (( i=0; i<secs; i++ )); do
104 ps -p ${pid} | grep -q ${cmd} || return 0
105 sleep 1
106 done
107
108 return 1
109 }
110
111 # void function_wrap(char* source, char* target)
112 #
113 # wraps function calls - for example function_wrap(this, that)
114 # maps function names this_* to that_*
115 function_wrap() {
116 local i
117
118 [[ $( type -t ${2}_provides ) == function ]] && return
119
120 for i in $( typeset -f | grep -o ^${1}_'[^ ]*' ); do
121 eval "${2}${i#${1}}() { ${i} \"\$@\"; }"
122 done
123 }
124
125 # Currently sorucing rc-services.sh causes problems, so we replicate
126 # code here when needed
127 if [[ $( type -t mark_service_inactive ) != "function" ]]; then
128 svcdir=/var/lib/init.d
129 mark_service_inactive() {
130 [[ -z $1 ]] && return 1
131 ln -snf /etc/init.d/$1 ${svcdir}/inactive/$1
132 return $?
133 }
134 mark_service_stopped() {
135 [[ -z $1 ]] && return 1
136 [[ -f ${svcdir}/starting/$1 ]] && rm -f ${svcdir}/starting/$1
137 [[ -f ${svcdir}/started/$1 ]] && rm -f ${svcdir}/started/$1
138 [[ -f ${svcdir}/inactive/$1 ]] && rm -f ${svcdir}/inactive/$1
139 return $?
140 }
141 mark_service_started() {
142 [[ -z $1 ]] && return 1
143 ln -snf /etc/init.d/${1} ${svcdir}/started/$1
144 [[ -f ${svcdir}/starting/$1 ]] && rm -f ${svcdir}/starting/$1
145 [[ -f ${svcdir}/inactive/$1 ]] && rm -f ${svcdir}/inactive/$1
146 return $?
147 }
148 fi

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20