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