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