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