]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/init.d/functions
- introducing German translation
[projects/rc-scripts.git] / rc.d / init.d / functions
1 # functions     This file contains functions to be used by most or all
2 #               shell scripts in the /etc/init.d directory.
3 #
4 # $Id: functions,v 1.56 2000/09/18 22:32:04 saq Exp $
5 #
6 # Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
7 # Hacked by:    Greg Galloway and Marc Ewing
8 # Modified for PLD by:
9 #               Marek Obuchowicz <elephant@pld.org.pl>
10 #               Arkadiusz Mi¶kiewicz <misiek@pld.org.pl> 
11 #               Micha³ Kochanowicz <mkochano@pld.org.pl>
12
13 # First set up a default search path.
14 export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
15
16 # Set defaults
17 INIT_COL=67
18
19 # Source configuration if available - may override default values
20 [ -f /etc/sysconfig/system ] && . /etc/sysconfig/system
21
22 [ -z "$COLUMNS" ] && COLUMNS=80
23
24 # Colors workaround
25 termput() 
26 {
27         if [ ! -d /usr/share/terminfo ] || \
28              [ ! -x /usr/bin/tput -a ! -x /bin/tput ]; then
29                 case "$1" in
30                   hpa)
31                         echo -ne "\033[$(($2+1))G"
32                         ;;
33                   cuu*)
34                         echo -ne "\033[${2}A"
35                         ;;
36                   el)
37                         echo -ne "\033[0K"
38                         ;;
39                   setaf)
40                         is_yes "$COLOR_INIT" && echo -ne "\033[0;3${2}m"
41                         ;;
42                   op)
43                         termput setaf 9
44                         ;;
45                   esac
46         else
47                 tput "$@"
48         fi
49 }
50
51 # printf equivalent
52 printf_()
53 {
54         text="$1" ;
55         shift ;
56         if [ $# -gt 0 ]; then
57                 m="$1";
58                 shift;
59                 while [ $# -gt 0 ]; do
60                         m="$m\",\"$1" ;
61                         shift ;
62                 done
63         fi
64         awk "BEGIN {printf \"$text\", \"$m\"; }"
65 }
66                             
67 # National language support function
68 nls()
69 {
70 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
71         OLD_NLS_DOMAIN="$NLS_DOMAIN"
72         if [ "$1" = "--nls-domain" ]; then
73                 shift
74                 NLS_DOMAIN="$1"
75                 shift
76         fi
77         if [ -z "$NLS_DOMAIN" ]; then
78                 NLS_DOMAIN="rc-scripts"
79         fi
80         MESSAGE="$1"
81         # avoid translating empty text. --misiek
82         if [ -n "$MESSAGE" ]; then
83                 text="`TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="$NLS_DOMAIN" "$MESSAGE"`"
84         else
85                 text="$MESSAGE"
86         fi
87         shift
88         printf_ "$text" "$@"
89         echo
90         NLS_DOMAIN="$OLD_NLS_DOMAIN"
91 else
92         echo "$@"
93 fi
94 }
95
96 msg_Network_Down()
97 {
98         nls "ERROR: Networking is down. %s can't be run." "$1"
99 }
100
101 msg_starting()
102 {
103         show "Starting %s service" "$1"
104 }
105
106 msg_Already_Running()
107 {
108         nls "%s service is already running." "$1"
109 }
110
111 msg_stopping()
112 {
113         show "Stopping %s service" "$1"
114 }
115
116 msg_Not_Running()
117 {
118         nls "%s service is not running." "$1"
119 }
120
121 msg_reloading()
122 {
123         show "Reloading %s service" "$1"
124 }
125
126 msg_Usage()
127 {
128         nls "Usage: %s" "$*"
129 }
130
131 # Some functions to handle PLD-style messages
132 show() 
133 {       
134         what="`nls --nls-domain rc-scripts "DONE"`"; typeset -i offset=${#what}
135         text="`nls "$@"`"
136         echo -n "$text"
137         awk "BEGIN { for (j=length(\"$text\"); j<$INIT_COL; j++) printf \".\" }"
138 }
139
140 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
141 # First is the text to display, second is color number to use (argument to
142 # tput setaf). If second argument is not given, default (2, green) will be
143 # used).
144 progress()
145 {
146         if [ -n "$2" ]; then COLOR="$2"; else COLOR="2"; fi
147         deltext
148         echo -n "`termput setaf 6`[`termput setaf "$COLOR"` `nls --nls-domain rc-scripts "$1"` `termput setaf 6`]`termput op`"
149 }
150
151 busy() 
152 {
153         progress "BUSY" 5
154 }
155
156 ok() 
157 {
158         progress "DONE"
159         echo
160 }
161
162 started()
163 {
164         progress "WORK"
165         echo
166 }
167
168 fail() 
169 {
170         progress "FAIL" 1
171         echo
172 }
173
174 died() 
175 {
176         progress "DIED" 1
177         echo
178 }
179
180 deltext() 
181 {
182         termput hpa $INIT_COL
183 }
184
185 # Usage run_cmd Message command_to_run
186 run_cmd()
187 {
188         exit_code=0
189         _ERRORS=""
190         MESSAGE=$1
191         show "$MESSAGE"; busy
192         shift
193         if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
194                 ok
195         else
196                 fail;  [ -n "$_ERRORS" ] && echo $_ERRORS
197                 exit_code=1
198         fi
199         unset _ERRORS
200         return $exit_code
201 }
202
203 # compatibility functions
204 action()
205 {
206         STRING=$1
207         shift
208         run_cmd "$STRING" "$*"
209 }
210                         
211 # A function to start a program (now it's usefull on read-only filesystem too)
212 daemon() 
213 {
214         nicelevel=0
215         exit_code=0
216         _ERRORS=""
217         [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
218         # Test syntax.
219         case $1 in
220         '')     msg_Usage " daemon [+/-nicelevel] {program}"
221                 return 1;;
222         -*|+*) SERVICE_RUN_NICE_LEVEL=$1
223                 shift;;
224         esac
225
226         # make sure it doesn't core dump anywhere; while this could mask
227         # problems with the daemon, it also closes some security problems
228         ulimit -c 0
229
230         # And start it up.
231         busy
232         if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
233                 ok
234         else
235                 exit_code=1
236                 fail
237                 [ -n "$_ERRORS" ] && echo $_ERRORS
238         fi
239         unset _ERRORS
240         return $exit_code
241 }
242
243 # A function to stop a program.
244 killproc() 
245 {
246         # Test syntax.
247         if [ $# = 0 ]; then
248                 msg_Usage " killproc {program} [signal]"
249                 return 1
250         fi
251
252         busy
253         
254         notset=0
255         # check for second arg to be kill level
256         if [ "$2" != "" ] ; then
257                 killlevel=$2
258         else
259                 notset=1
260                 killlevel="-9"
261         fi
262
263         # Save basename.
264         base=`basename $1`
265
266         # Find pid.
267         pid=`pidofproc $base`
268
269         # Kill it.
270         if [ "$pid" != "" ] ; then
271                 if [ "$notset" = "1" ] ; then
272                         if ps h $pid>/dev/null 2>&1; then
273                                 # TERM first, then KILL if not dead
274                                 kill -TERM $pid >/dev/null 2>&1
275                                 usleep 100000
276                                 if ps h $pid >/dev/null 2>&1 ; then
277                                         sleep 1
278                                         if ps h $pid >/dev/null 2>&1 ; then
279                                                 sleep 3
280                                                 if ps h $pid >/dev/null 2>&1 ; then
281                                                         kill -KILL $pid >/dev/null 2>&1
282                                                 fi
283                                         fi
284                                 fi
285                                 ps h $pid >/dev/null 2>&1 && fail || ok
286                         else
287                                 died
288                         fi
289                 # use specified level only
290                 else
291                         if ps h $pid >/dev/null 2>&1; then
292                                 kill $killlevel $pid && ok || fail
293                         else
294                                 died
295                         fi
296                 fi
297         else
298                 died
299         fi
300
301         # Remove pid file if any.
302         if [ "$notset" = "1" ]; then
303                 rm -f /var/run/$base.pid
304         fi
305 }
306
307 # A function to find the pid of a program.
308 pidofproc() 
309 {
310         # Test syntax.
311         if [ $# = 0 ] ; then
312                 msg_Usage " pidofproc {program}"
313                 return 1
314         fi
315
316         # First try "/var/run/*.pid" files
317         if [ -f /var/run/$1.pid ] ; then
318                 pid=`head -1 /var/run/$1.pid`
319                 if [ "$pid" != "" ] ; then
320                         echo $pid
321                         return 0
322                 fi
323         fi
324
325         # Next try "pidof"
326         pid=`pidof $1`
327         if [ "$pid" != "" ] ; then
328                 echo $pid
329                 return 0
330         fi
331
332         # Finally try to extract it from ps
333         ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
334                            { if ((prog == $5) || (("(" prog ")") == $5) ||
335                              (("[" prog "]") == $5) ||
336                            ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
337 }
338
339 status() 
340 {
341         # Test syntax.
342         if [ $# = 0 ] ; then
343                 msg_Usage " status {program}"
344                 return 1
345         fi
346
347         # First try "pidof"
348         pid=`pidof $1`
349         if [ "$pid" != "" ] ; then
350                 nls "%s (pid %s) is running..." "$1" "$pid"
351                 return 0
352         else
353                 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
354                            { if ((prog == $5) || (("(" prog ")") == $5) ||
355                              (("[" prog "]") == $5) ||
356                            ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
357                 if [ "$pid" != "" ] ; then
358                         nls "%s (pid %s) is running..." "$1" "$pid"
359                         return 0
360                 fi
361         fi
362
363         # Next try "/var/run/*.pid" files
364         if [ -f /var/run/$1.pid ] ; then
365                 pid=`head -1 /var/run/$1.pid`
366                 if [ "$pid" != "" ] ; then
367                         nls "%s dead but pid file exists" "$1"
368                         return 1
369                 fi
370         fi
371         # See if /var/lock/subsys/$1 exists
372         if [ -f /var/lock/subsys/$1 ]; then
373                 nls "%s dead but subsys locked" "$1"
374                 return 2
375         fi
376         nls "%s is stopped" "$1"
377         return 3
378 }
379
380 # Confirm whether we really want to run this service
381 confirm() {
382         echo -n "`nls "Start service"` $1 `nls "(Y)es/(N)o/(C)ontinue? [Y] "`"
383         read answer
384         case $answer in
385                 y|Y|t|T|j|J|"")
386                         return 0
387                 ;;
388                 c|C|k|K|w|W)
389                         return 2
390                 ;;
391                 n|N)
392                         return 1
393                 ;;
394                 *)
395                         confirm $1
396                         return $?
397                 ;;
398         esac
399 }
400
401 is_yes()
402 {
403         # Test syntax   
404         if [ $# = 0 ] ; then
405                 msg_Usage " is_yes {value}"
406                 return 2
407         fi
408
409         # Check value
410         [ "$1" = "yes" ] ||\
411         [ "$1" = "Yes" ] ||\
412         [ "$1" = "YES" ] ||\
413         [ "$1" = "true" ] ||\
414         [ "$1" = "True" ] ||\
415         [ "$1" = "TRUE" ] ||\
416         [ "$1" = "Y" ]||\
417         [ "$1" = "y" ]||\
418         [ "$1" = "1" ] ||\
419                 return 1
420         return 0
421 }
422
423 is_no()
424 {
425         # Test syntax
426         if [ $# = 0 ] ; then
427                 msg_Usage " is_no {value}"
428                 return 2
429         fi
430         
431         # Check value
432         [ "$1" = "no" ] ||\
433         [ "$1" = "No" ] ||\
434         [ "$1" = "NO" ] ||\
435         [ "$1" = "false" ] ||\
436         [ "$1" = "False" ] ||\
437         [ "$1" = "FALSE" ] ||\
438         [ "$1" = "N" ] ||\
439         [ "$1" = "n" ] ||\
440         [ "$1" = "0" ] ||\
441         [ "$1" = "" ] ||\
442                 return 1
443         return 0
444 }
445
446 _modprobe()
447 {
448         parsed=no
449         while is_no $parsed ; do
450                 case "$1" in
451                         "single")
452                                 single=yes
453                                 shift
454                                 ;;
455                         "die")
456                                 die=yes
457                                 shift
458                                 ;;
459                         -*)
460                                 args="$args $1"
461                                 shift
462                                 ;;
463                         *)
464                                 parsed=yes
465                                 ;;
466                 esac
467         done
468         if is_yes "${single}" ; then
469                 foo="$@"
470                 show "Loading %s kernel module(s)" "$foo"
471                 unset foo
472                 busy
473         fi
474         if [ -x /sbin/modprobe ] ; then
475                 /sbin/modprobe -s $args "$@"
476                 result=$?
477         else
478                 deltext ; fail
479                 result=1
480         fi
481         if is_yes "${single}" ; then
482                 deltext
483                 if [ $result == "0" ] ; then
484                         is_yes "${single}" && ok
485                 else
486                         fail
487                         if is_yes "$die" ; then
488                                 nls "Could not load %s kernel module(s)" "$@"
489                                 exit 1
490                         fi
491                 fi
492         fi
493         unset single die args result
494 }
This page took 0.06237 seconds and 4 git commands to generate.