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