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