]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/init.d/functions
- translations for DONE etc are always taken from rc-scripts.mo
[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.36 2000/02/18 17:59:19 misiek 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 if [ -f /etc/sysconfig/system ]; then
16         . /etc/sysconfig/system
17 fi
18
19 if [ -z "$COLUMNS" ]; then 
20         COLUMNS=80
21 fi
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}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 # Some functions to handle PLD-style messages
95 show() 
96 {       
97         what="`nls --nls-domain rc-scripts "DONE"`"; typeset -i offset=${#what}
98         text="`nls "$*"`.................................................................................."
99         awk "BEGIN {printf \"%.$((75 - $offset))s\", \"$text\";}"
100         # move to column number 67
101         termput hpa 67
102 }
103
104 busy() 
105 {
106         echo -n "`termput setaf 6`[`termput setaf 5` `nls --nls-domain rc-scripts "BUSY"` `termput setaf 6`]`termput setaf 7`"
107 }
108
109 ok() 
110 {
111         echo  "`termput setaf 6`[`termput setaf 2` `nls --nls-domain rc-scripts "DONE"` `termput setaf 6`]`termput setaf 7`"
112 }
113
114 started()
115 {
116         echo  "`termput setaf 6`[`termput setaf 2` `nls --nls-domain rc-scripts "WORK"` `termput setaf 6`]`termput setaf 7`"
117 }
118
119 fail() 
120 {
121         echo  "`termput setaf 6`[`termput setaf 1` `nls --nls-domain rc-scripts "FAIL"` `termput setaf 6`]`termput setaf 7`"
122 }
123
124 died() 
125 {
126         echo  "`termput setaf 6`[`termput setaf 1` `nls --nls-domain rc-scripts "DIED"` `termput setaf 6`]`termput setaf 7`"
127 }
128
129 deltext() 
130 {
131         echo -ne "`nls --nls-domain rc-scripts "\b\b\b\b\b\b\b\b"`"     
132 }
133
134 # Usage run_cmd Message command_to_run
135 run_cmd()
136 {
137         exit_code=0
138         _ERRORS=""
139         MESSAGE=$1
140         show "$MESSAGE"; busy
141         shift
142         if _ERRORS="`initlog -c \"$*\" 2>&1`"; then
143                 deltext; ok
144         else
145                 deltext; fail;  [ -n "$_ERRORS" ] && echo $_ERRORS
146                 exit_code=1
147         fi
148         unset _ERRORS
149         return $exit_code
150 }
151
152 # compatibility functions
153 action()
154 {
155         STRING=$1
156         shift
157         run_cmd "$STRING" "$*"
158 }
159                         
160 # A function to start a program (now it's usefull on read-only filesystem too)
161 daemon() 
162 {
163         nicelevel=0
164         exit_code=0
165         _ERRORS=""
166         [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
167         # Test syntax.
168         case $1 in
169         '')     echo '$0: Usage: daemon [+/-nicelevel] {program}'
170                 return 1;;
171         -*|+*) SERVICE_RUN_NICE_LEVEL=$1
172                 shift;;
173         esac
174
175         # make sure it doesn't core dump anywhere; while this could mask
176         # problems with the daemon, it also closes some security problems
177         ulimit -c 0
178
179         # And start it up.
180         busy
181         if _ERRORS="`nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} initlog -c "$*" 2>&1`"; then
182                 deltext
183                 ok
184         else
185                 exit_code=1
186                 deltext
187                 fail
188                 [ -n "$_ERRORS" ] && echo $_ERRORS
189         fi
190         unset _ERRORS
191         return $exit_code
192 }
193
194 # A function to stop a program.
195 killproc() 
196 {
197         # Test syntax.
198         if [ $# = 0 ]; then
199                 nls "Usage: killproc {program} [signal]"
200                 return 1
201         fi
202
203         busy
204         
205         notset=0
206         # check for second arg to be kill level
207         if [ "$2" != "" ] ; then
208                 killlevel=$2
209         else
210                 notset=1
211                 killlevel="-9"
212         fi
213
214         # Save basename.
215         base=`basename $1`
216
217         # Find pid.
218         pid=`pidofproc $base`
219
220         # Kill it.
221         if [ "$pid" != "" ] ; then
222                 if [ "$notset" = "1" ] ; then
223                         if ps h $pid>/dev/null 2>&1; then
224                                 # TERM first, then KILL if not dead
225                                 kill -TERM $pid >/dev/null 2>&1
226                                 usleep 100000
227                                 if ps h $pid >/dev/null 2>&1 ; then
228                                         sleep 1
229                                         if ps h $pid >/dev/null 2>&1 ; then
230                                                 sleep 3
231                                                 if ps h $pid >/dev/null 2>&1 ; then
232                                                         kill -KILL $pid >/dev/null 2>&1
233                                                 fi
234                                         fi
235                                 fi
236                                 ps h $pid >/dev/null 2>&1 && (deltext; fail) || (deltext; ok)
237                         else
238                                 deltext; died
239                         fi
240                 # use specified level only
241                 else
242                         if ps h $pid >/dev/null 2>&1; then
243                                 kill $killlevel $pid && (deltext; ok) || (deltext; fail)
244                         else
245                                 deltext; died
246                         fi
247                 fi
248         else
249                 deltext
250                 died
251         fi
252
253         # Remove pid file if any.
254         if [ "$notset" = "1" ]; then
255                 rm -f /var/run/$base.pid
256         fi
257 }
258
259 # A function to find the pid of a program.
260 pidofproc() 
261 {
262         # Test syntax.
263         if [ $# = 0 ] ; then
264                 nls "Usage: pidofproc {program}\n"
265                 return 1
266         fi
267
268         # First try "/var/run/*.pid" files
269         if [ -f /var/run/$1.pid ] ; then
270                 pid=`head -1 /var/run/$1.pid`
271                 if [ "$pid" != "" ] ; then
272                         echo $pid
273                         return 0
274                 fi
275         fi
276
277         # Next try "pidof"
278         pid=`pidof $1`
279         if [ "$pid" != "" ] ; then
280                 echo $pid
281                 return 0
282         fi
283
284         # Finally try to extract it from ps
285         ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
286                            { if ((prog == $5) || (("(" prog ")") == $5) ||
287                              (("[" prog "]") == $5) ||
288                            ((prog ":") == $5)) { print $1 ; exit 0 } }' $1
289 }
290
291 status() 
292 {
293         # Test syntax.
294         if [ $# = 0 ] ; then
295                 nls "Usage: status {program}\n"
296                 return 1
297         fi
298
299         # First try "pidof"
300         pid=`pidof $1`
301         if [ "$pid" != "" ] ; then
302                 nls "%s (pid %s) is running..." "$1" "$pid"
303                 return 0
304         else
305                 pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
306                            { if ((prog == $5) || (("(" prog ")") == $5) ||
307                              (("[" prog "]") == $5) ||
308                            ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
309                 if [ "$pid" != "" ] ; then
310                         nls "%s (pid %s) is running..." "$1" "$pid"
311                         return 0
312                 fi
313         fi
314
315         # Next try "/var/run/*.pid" files
316         if [ -f /var/run/$1.pid ] ; then
317                 pid=`head -1 /var/run/$1.pid`
318                 if [ "$pid" != "" ] ; then
319                         nls "%s dead but pid file exists" "$1"
320                         return 1
321                 fi
322         fi
323         # See if /var/lock/subsys/$1 exists
324         if [ -f /var/lock/subsys/$1 ]; then
325                 nls "%s dead but subsys locked" "$1"
326                 return 2
327         fi
328         nls "%s is stopped" "$1"
329         return 3
330 }
This page took 0.059958 seconds and 4 git commands to generate.