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