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