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