File: common-functions

package info (click to toggle)
ifupdown-scripts-zg2 0.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 484 kB
  • ctags: 55
  • sloc: sh: 4,037; makefile: 37
file content (337 lines) | stat: -rwxr-xr-x 6,031 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/bin/bash
# $Header: /var/local/cvs/debian/ifupdown-scripts-zg2/scripts/ifupdown-scripts-zg2.d/common-functions,v 1.1 2004/09/12 18:00:04 mh Exp $

# set global vars, make statefile existent, and do basic sanity checks

VERBOSE=$IF_VERBOSE
[ -z "$VERBOSE" ] && unset VERBOSE

#set -x

me="$0"
[ $VERBOSE ] && echo "$me $IFACE $MODE"

[ "$METHOD" = "manual" ] || exit 0
[ "$ADDRFAM" = "inet" ] || exit 0

# common functions for interface scripts

#
# misc helper functions
#

error()
{
  echo "$me: ERR: $1" >&2
}

warn()
{
  echo "$me: WARN: $1" >&2
}

abort()
{
  error "$1"
  add_down "abort" "1"
  exit 1
}

msg()
{
  echo "$me: $1" >&2
}

verbose()
{
  [ $VERBOSE ] && echo "$me: $*"
}

cmd()
{
  local RET
  [ $VERBOSE ] && echo "$me: $*"
  SHOW_ERRORS=1
  if [ "$1" == "--no-errors" ]; then
    shift
    SHOW_ERRORS=0
  fi
  eval $1
  RET=$?
  if [ "$RET" -ne 0 -a "$NO_ERRORS" == "1" ]; then
    echo >&2 "$me: $1 returned return value $RET"
  fi
  return $RET
}

#
# functions for handling the state
#

# add_down KEY VAL
add_down()
{
  KEY="$1"
  VAL="$2"
  # add ip commands to down script
  touch $STATEFILE
  chown root $STATEFILE
  chmod 644 $STATEFILE

  verbose "add_down $1 $2"
  echo "$1: $2" >> $STATEFILE
}

get_down()
{
  KEY="$1"
  
  # execute and remove commands from state file
  
  if [ "$STATEFILE" != "`find $STATEFILE -type f -and -user root -and -group root -and -not -perm +0022`" ]; then
    abort "$STATEFILE isn't a plain file, or is writeable by non-root user" >&2
  fi

  # read state file and return commands

  < $STATEFILE sed -n "/^$KEY:/s/^[^\:]*\:[[:space:]]\(.*\)/\1/p"
}

exec_down()
{
  KEY="$1"
  CMD="$2"
  verbose "exec_down $KEY $CMD"
  
  # execute and remove commands from state file
  
  if [ "$STATEFILE" != "`find $STATEFILE -type f -and -user root -and -group root -and -not -perm +0022`" ]; then
    abort "$STATEFILE isn't a plain file, or is writeable by non-root user" >&2
  fi

  # get temp file name
  
  TMPFILE=`mktemp $STATEDIR/$IFACE.state.XXXXXX`
  if [ $? -ne 0 ]; then
    abort "Can't create temp file $TMPFILE. This shouldn't happen" >&2
  fi
  chmod 644 $TMPFILE

  # read state file and execute commands _in_ _reverse_ _order_

  COMMAND="$CMD"
  if [ "$CMD" == "exec" ]; then
    COMMAND=""
  fi
  if [ -n "$CMD" ]; then
    < $STATEFILE sed -n "/^$KEY:/s/^[^\:]*\:[[:space:]]\(.*\)/\1/p" \
    | tac | while read line; do
      verbose "exec_down $COMMAND $line"
      eval "cmd \"$COMMAND $line\""
    done
  fi
  
  # remove commands from STATEFILE
  
  grep -v "^${KEY}:" $STATEFILE > $TMPFILE
  mv -f $TMPFILE $STATEFILE
}

remove_state()
{
  rm -f "/var/lib/ifupdown-scripts-zg2/$IFACE.state"
}

# dev_state_entry DEVICE KEY
dev_state_entry()
{
  local dev key
  dev="$1"; key="$2"

  # check parameter
  local state
  state="$STATEDIR/$dev.state"
  [ ! -f "$state" ] && return 1

  < $state awk -F ':[ \t]*' "/^$key: / "'{ sub("^[^:]*:[ \t]*", ""); print; }'
}

# state_entry KEY
state_entry()
{
  dev_state_entry "$IFACE" "$1"
  return $?
}

# find_if_by_state KEY PATTERN
list_if_by_state()
{
  local key pat
  key="$1"; pat="$2"; shift 2

  [ -z "$key" -o -z "$pat" ] && return 1

  local sfile dev
  if [ $# -eq 0 ]; then
    for sfile in $STATEDIR/*.state; do
      dev="${sfile%.state}"
      dev="${dev##*/}"

      if dev_state_entry "$dev" "$key" | grep -q "$pat"; then
        echo "$dev"
      fi
    done
  else
    for dev; do
      if dev_state_entry "$dev" "$key" | grep -q "$pat"; then
        echo "$dev"
      fi
    done
  fi
}

#
# helper functions for network interfaces
#

# list_ifaces
list_ifaces()
{
  < /proc/net/dev awk -F ':' '/^.*:/ { print $1; }'
}

# if_exists IFACE
if_exists()
{
  for iface in `list_ifaces`; do
    [ "$iface" = "$1" ] && return 0
  done
  return 1
}

# is_if_up IFACE
is_if_up()
{
  local state

  ip link show "$1" \
  | sed -n '/<.*>/s/^[^<]*<\([^>]*+\)>.*$/\1/p' \
  | tr ',' '\n' \
  | while read state; do
    if [ "$state" = "UP" ]; then
      true
      break
    else
      false
    fi
  done
  return $?
}

# get_if_mac IFACE
get_if_mac()
{
  ip link show "$1" 2>/dev/null | awk -F " " '/link\/ether/ { print $2; }'
}

#
# helper functions for vlan management
#

VLAN_CFG=/proc/net/vlan/config

if [ ! -r "$VLAN_CFG" ]; then
  VLAN_SUPPORT=no
  # provide dummy functions
  find_vlan() { return 0; }
  get_vlan_id() { echo 0; }
  is_active_vlan_master() { return 1; }
else
VLAN_SUPPORT=yes

# the real functions
read_vlan_config()
{
  < "$VLAN_CFG" awk -F '|' 'NR>2 { print $1" "$2" "$3; }'
}

#find_vlan MASTER VLANID
find_vlan()
{
  local iface vlanid master
  read_vlan_config \
  | while read iface vlanid master; do
    if [ "$master" = "$1" -a "$vlanid" = "$2" ]; then
      echo "$iface"
      break
    fi
  done
}

# get_vlan_id IFACE
get_vlan_id()
{
  local iface vlanid master
  read_vlan_config \
  | while read iface vlanid master; do
    if [ "$iface" = "$1" ]; then
      echo "$vlanid"
      exit 42
    fi
  done
  [ $? -eq 42 ] || echo 0
  return 0
}

# is_active_vlan_master IFACE
is_active_vlan_master()
{
  local iface vlanid master
  read_vlan_config | (
    while read iface vlanid master; do
      if [ "$master" = "$1" ]; then
        exit 0
      fi
    done
    exit 1
  )
  return $?
}

fi

# init code that should be executed for every script

# physical device defaults to logical
if [ -z "$IF_DEVICE" ]; then
  IF_DEVICE="$IFACE"
  export IF_DEVICE
fi

STATEDIR="/var/lib/ifupdown-scripts-zg2"
STATEFILE="$STATEDIR/$IFACE.state"

if ! [ -e $STATEFILE ]; then 
  touch $STATEFILE
  chown root $STATEFILE
  chmod 644 $STATEFILE
  echo "" > $STATEFILE
fi

if [ "$STATEFILE" != "`find $STATEFILE -type f -and -user root -and -group root -and -not -perm +0022`" ]; then
  abort "$STATEFILE isn't a plain file, or is writeable by non-root user" >&2
fi

# check for abort condition
if [ -f "$STATEFILE" -a "$MODE" = 'start' ]; then
  if grep -q '^abort:' $STATEFILE; then
    exit 0
  fi
fi

# Sanitize VLAN id
IF_VLAN_ID=$[$IF_VLAN_ID+0]

# vi:sw=2

# end of file