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