File: init-functions

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (321 lines) | stat: -rw-r--r-- 5,075 bytes parent folder | download | duplicates (10)
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
# /lib/lsb/init-functions replacement for syslog-ng Premium Edition

RED=
YELLOW=
NORMAL=
FANCYTTY=
PSOPTS=

ECHO=echo

OS=`uname -s`

# LSB Functions
# ripped from ubuntu/debian lsb
log_use_fancy_output() {
  case "$TERM" in
    dumb|raw|serial|vt*|cons*|unknown)
      return 1
      ;;
    *)
      if [ -n "$FANCYTTY" ] && [ $FANCYTTY -ne 1 ]; then
        return 1
      fi
      RED="\033[31m"
      YELLOW="\033[33m"
      NORMAL="\033[0m"
      return 0
      ;;
  esac
  return 1
}

log_success_msg() {
  $ECHO "$@"
}

log_failure_msg() {
  log_use_fancy_output
  $ECHO "${RED}$@${NORMAL}"
}

log_warning_msg() {
  log_use_fancy_output
  $ECHO "${YELLOW}$@${NORMAL}"
}

# NON-LSB Functions

log_begin_msg() {
  if [ -z "$1" ]; then
    return 1
  fi

  $ECHO "$@\c "
}

log_daemon_msg() {
  if [ -z "$1" ];then
    return 1
  fi

  if [ -z "$2" ]; then
    $ECHO "$1:\c "
    return
  fi

  $ECHO "$1: $2\c "
}

log_progress_msg() {
  if [ -z "$1" ]; then
    return 1
  fi

  if [ $1 -eq 0 ]; then
    $ECHO "."
  else
    $ECHO " failed"
  fi
}

log_end_msg() {
  if [ -z "$1" ]; then
    return 1
  fi

  log_fancy_output
  if [ $1 -eq 0 ];then
    $ECHO "."
  else
    $ECHO "${RED}failed!${NORMAL}"
  fi
  return $1
}

log_action_msg() {
  $ECHO "$@."
}

log_action_begin_msg() {
  $ECHO "$@...\c "
}

log_action_cont_msg() {
  $ECHO "$@...\c "
}

log_action_end_msg() {
  if [ -z "$2" ]; then
    end="."
  else
    end=" ($2)."
  fi

  log_fancy_output
  if [ $1 -eq 0 ]; then
    $ECHO "done${end}"
  else
    $ECHO "${RED}failed${end}${NORMAL}"
  fi
}

# overide log_*_msg if the init-functions file exits on the system
[ -f /lib/lsb/init-functions ] && . /lib/lsb/init-functions

# in these three cases we have to use our functions
unalias start_daemon 2>/dev/null
unalias killproc 2>/dev/null
unalias pidofproc 2>/dev/null


disable_xpg_echo() {
  # \X chars only passed to echo under bash, if xpg_echo enabled or echo -e
  # used on Linux.
  if [ -n "$BASH_VERSION" ];then
    shopt -s xpg_echo
  fi
}

case $OS in
  # default ps -e cuts the process' name at 8 characters, so we have to list it
  # in a long form
  SunOS)
        PSOPTS=" -o pid -o tty -o time -o comm"
        disable_xpg_echo
        ;;
  Linux)
        if [ -z "$BASH_VERSION" ]; then
          # beware of dash's builtin echo ...
          ECHO="/bin/echo -e"
        fi
        ;;
  *)    disable_xpg_echo ;;
esac


_checkpid() {
  _pid=$1
  _proc=$2
  
  for _ret in `ps -e $PSOPTS | grep "$_proc" | sed -e 's/^  *//' -e 's/ .*//'`; do
    if [ -n "$_ret" ] && [ "$_ret" = "$_pid" ]; then
      return 0
    fi
  done
  return 1
}

_pid_from_pidfile() {
  pidfile=$1
  procname=$2
  pid=

  if [ -f "$pidfile" ];then
    pid=`head -1 $pidfile 2> /dev/null`
    if [ $? -ne 0 ]; then
      # on slow machines (or ones under high load) the pidfile could be
      # erased between -f and `head` ...
      return 3
    fi
    if _checkpid $pid $procname; then
      return 0
    fi
    return 1 # pidfile exist, but proc not running
  fi

  return 3 # proc not running
}

pidofproc() {
  proc=
  pidfile=
  
  if [ "$1" = "-p" ];then
    pidfile=$2
    shift
    shift
  fi

  if [ -z "$1" ];then
   echo "Usage: pidofproc [-p pidfile] {program}"
   return 1
  fi
  proc=$1
  shift

  base=`basename ${proc}`
  if [ -z "${pidfile}" ]; then
    pidfile=/var/run/${base}.pid
  fi

  if [ -f ${pidfile} ];then
    _pid_from_pidfile "${pidfile}" "${base}"
    _ret=$?

    if [ $_ret -eq 0 ]; then
      if kill -0 ${pid} 2> /dev/null; then
        echo "$pid"
        return 0
      fi
    else
      return $_ret
    fi
  fi

  return 4
}

start_daemon() {
  proc=
  pidfile=
  force=
  nice=
  
  if [ "$1" = "-f" ]; then
    force="force"
    shift
  fi

  if [ "$1" = "-n" ];then
    nice="nice $2"
    shift
    shift
  fi

  if [ "$1" = "-p" ];then
    pidfile=$2
    shift
    shift
  fi

  if [ -z "$1" ];then
   echo "Usage: start_daemon [-f] [-n nicelevel] [-p pidfile] {program} [args]"
   return 1
  fi
  proc=$1
  shift

  base=`basename ${proc}`
  if [ -z "${pidfile}" ]; then
    pidfile=/var/run/${base}.pid
  fi
  _pid_from_pidfile "${pidfile}" "${base}"
  if [ -n "$pid" ] && [ -z "$force" ];then
    return
  fi

  $nice $proc $*
  return $?
}

killproc() {
  proc=
  signal=-15
  pidfile=
  
  if [ "$1" = "-p" ];then
    pidfile=$2
    shift
    shift
  fi

  if [ -z "$1" ];then
   echo "Usage: killproc [-p pidfile] {program}"
   return 1
  fi
  proc=$1
  shift

  if [ -n "$1" ];then
    signal=$1
    echo "${signal}" | grep '^-' >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      signal="-${signal}"
    fi
  fi

  base=`basename $proc`
  if [ -z "${pidfile}" ]; then
    pidfile=/var/run/${base}.pid
  fi

  _pid_from_pidfile "${pidfile}" "${base}"; 
  _ret=$?
  if [ $_ret -ne 0 ];then 
    rm -f ${pidfile}
    return $_ret
  fi

  if [ -n "$pid" ]; then
    kill $signal $pid
    _status="$?"
    if [ $_status -eq  0 ];then
      pidofproc -p "${pidfile}" "$proc" >/dev/null || rm -f "${pidfile}"
    fi
  fi
  return $_retval
}

# vim: ft=sh ts=2 expandtab