/[baselayout]/branches/rc-scripts-1_6/net-scripts/net.modules.d/helpers.d/functions
Gentoo

Contents of /branches/rc-scripts-1_6/net-scripts/net.modules.d/helpers.d/functions

Parent Directory Parent Directory | Revision Log Revision Log


Revision 842 - (show annotations) (download)
Thu Jan 13 14:51:03 2005 UTC (9 years, 7 months ago) by uberlord
File size: 2269 byte(s)
wpa_supplicant forces ctrl_interface to /var/run/wpa_supplicant
and removes stale directories
clean_pidfile function no longer errors on empty pidfiles
ipppd fixes for shutdown

1 # net-scripts helper functions
2 # Version 1.0.0
3 # Copyright (c) 2004 Gentoo Foundation
4 # Distributed under the terms of the GNU General Public License V2
5 # Contributed by Roy Marples (uberlord@gentoo.org)
6
7 # int netmask2cidr(char *netmask)
8 #
9 # Returns the CIDR of a given netmask
10 netmask2cidr() {
11 local binary="" i
12
13 for i in ${1//./ }; do
14 while [[ ${i} != 0 ]]; do
15 binary=$[${i}%2]${binary}
16 (( i=i>>1 ))
17 done
18 done
19 binary=${binary%%0*}
20 echo ${#binary}
21 }
22
23 # char* netmask2cidr(int cidr)
24 #
25 # Returns the netmask of a given CIDR
26 cidr2netmask() {
27 local cidr=${1} netmask="" done=0 i sum=0 cur=128
28 local octets frac
29
30 (( octets=cidr/8 ))
31 (( frac=cidr%8 ))
32 while [[ octets -gt 0 ]]; do
33 netmask="${netmask}.255"
34 (( octets-- ))
35 (( done++ ))
36 done
37
38 if [[ ${done} -lt 4 ]]; then
39 for (( i=0; i<${frac}; i++ )); do
40 (( sum+=cur ))
41 (( cur/=2 ))
42 done
43 netmask="${netmask}.${sum}"
44 (( done++ ))
45
46 while [[ ${done} -lt 4 ]]; do
47 netmask="${netmask}.0"
48 (( done++ ))
49 done
50 fi
51
52 echo ${netmask:1}
53 }
54
55 # char* interface_device(char *iface)
56 #
57 # Gets the base device of the interface
58 # Can handle eth0:1 and eth0.1
59 # Which returns eth0 in this case
60 interface_device() {
61 local dev=${1%%.*}
62 [[ ${dev} == ${1} ]] && dev=${1%%:*}
63 echo ${dev}
64 }
65
66 # char* interface_type(char* iface)
67 #
68 # Returns the base type of the interface
69 # eth, ippp, etc
70 interface_type() {
71 echo ${1%%[0-9]*}
72 }
73
74 # char* interface_variable(char *iface)
75 #
76 # Returns a bash variable name based on the interface
77 interface_variable() {
78 echo ${1//[![:word:]]/_}
79 }
80
81 # bool clean_pidfile(char *file)
82 #
83 # Removes the given pidfile if the process is not running
84 # Returns 1 if the process is still running otherwise 0
85 clean_pidfile() {
86 local pidfile=${1}
87
88 [[ ! -f ${pidfile} ]] && return 0
89 local pid=$( cat ${pidfile} )
90
91 if [[ -n ${pid} ]]; then
92 local cmd=${pidfile##*/}
93 cmd=${cmd%%-*}
94 ps -p ${pid} 2>/dev/null | grep -q ${cmd} && return 1
95 fi
96
97 rm -f ${pidfile}
98 return 0
99 }
100
101 # bool process_finished(int pid, char* cmd)
102 #
103 # We wait for 10 seconds until the command ${cmd}
104 # stops running on the process ${pid}
105 process_finished() {
106 local i pid=${1} cmd=${2} secs=${3:-9}
107
108 for (( i=0; i<secs; i++ )); do
109 ps -p ${pid} | grep -q ${cmd} || return 0
110 sleep 1
111 done
112
113 return 1
114 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20