1 #***************************************************************************
3 # Project ___| | | | _ \| |
5 # | (__| |_| | _ <| |___
6 # \___|\___/|_| \_\_____|
8 # Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
10 # This software is licensed as described in the file COPYING, which
11 # you should have received as part of this distribution. The terms
12 # are also available at http://curl.haxx.se/docs/copyright.html.
14 # You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 # copies of the Software, and permit persons to whom the Software is
16 # furnished to do so, under the terms of the COPYING file.
18 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 # KIND, either express or implied.
22 ###########################################################################
25 #use warnings; # requires perl 5.006 or later
28 my $DEFAULT_TIMEOUT_START = 90; # default allowed time for a process to startup
29 my $DEFAULT_TIMEOUT_STOP = 90; # default allowed time for a process to stop
31 my $ONE_HALF_STOP_TIMEOUT = int($DEFAULT_TIMEOUT_STOP / 2);
32 my $ONE_THIRD_STOP_TIMEOUT = int($DEFAULT_TIMEOUT_STOP / 3);
33 my $ONE_SIXTH_STOP_TIMEOUT = int($DEFAULT_TIMEOUT_STOP / 6);
35 my $pidpattern = qr/^\-?(\d+)$/; # pre-compiled pid pattern regexp
38 ######################################################################
39 # pidfromfile returns the pid stored in the given pidfile. The value
40 # of the returned pid will never be a negative value. It will be zero
41 # on any file related error or if a pid can not be extracted from the
42 # file. Otherwise it will be a positive value, even If the pid number
43 # stored in the file is a negative one.
48 my $pid = 0; # on failure or empty file return 0
51 if(not defined $pidfile) {
55 if(open(PIDF, "<$pidfile")) {
62 $pidline =~ s/^[+-]?0+//;
63 if($pidline =~ $pidpattern) {
73 ######################################################################
74 # unlinkpidfiles unlinks/deletes the given pidfiles. The first argument
75 # 'pidfiles' is a string of whitespace separated pidfiles. If successful
76 # returns 0, on error it returns the number of files not deleted.
81 if(not defined $pidfiles) {
86 for $pidfile (split(" ", $pidfiles)) {
88 if(unlink($pidfile) == 0) {
97 ######################################################################
98 # checkalivepid checks if the process of the given pid is alive. The
99 # argument must represent a single pid and be a valid number, if not
100 # it will return 0. It will also return 0 if the pid argument is zero
101 # or negative. If the pid argument is positive and it is alive returns
102 # the same positive pid, otherwise, if it is not alive it will return
103 # the negative value of the pid argument.
108 if(not defined $pid) {
111 if ($pid !~ $pidpattern) {
112 return 0; # invalid argument
116 return $pid; # positive means it is alive
119 return -$pid; # negative means dead process
122 return 0; # not a positive pid argument
126 ######################################################################
127 # checkalivepidfile checks if the process of the pid stored in the
128 # given pidfile is alive. It will return 0 on any file related error
129 # or if a pid can not be extracted from the file. If the process of
130 # the pid present in the file is alive it returns that positive pid,
131 # if it is not alive it will return the negative value of the pid.
133 sub checkalivepidfile {
136 my $pid = pidfromfile($pidfile);
137 my $ret = checkalivepid($pid);
142 ######################################################################
143 # signalpids signals processes in the second argument with the signal
144 # given in the first argument. The second argument 'pids' is a string
145 # of whitespace separated pids. Of the given pids only those that are
146 # positive and are actually alive will be signalled, and no matter
147 # how many times a pid is repeated it will only be signalled once.
150 my ($signal, $pids)=@_;
152 if((not defined $signal) || (not defined $pids)) {
156 # avoid sorting if only one pid
157 if(checkalivepid($pids) > 0) {
158 kill($signal, $pids);
163 for(sort({$a <=> $b} split(" ", $pids))) {
164 if($_ =~ $pidpattern) {
168 if(checkalivepid($pid) > 0) {
177 ######################################################################
178 # signalpidfile signals the process of the pid stored in the given
179 # pidfile with the signal given in the first argument if the process
180 # with that pid is actually alive.
183 my ($signal, $pidfile)=@_;
185 my $pid = pidfromfile($pidfile);
187 signalpids($signal, $pid);
192 ######################################################################
193 # waitdeadpid waits until all processes given in the first argument
194 # are not alive, waiting at most timeout seconds. The first argument
195 # 'pids' is a string of whitespace separated pids. Returns 1 when all
196 # pids are not alive. Returns 0 when the specified timeout has expired
197 # and at least one of the specified pids is still alive.
200 my ($pids, $timeout)=@_;
202 if(not defined $pids) {
205 if((not defined $timeout) || ($timeout < 1)) {
206 $timeout = $DEFAULT_TIMEOUT_STOP;
210 for(split(" ", $pids)) {
211 if($_ =~ $pidpattern) {
213 if(checkalivepid($pid) > 0) {
219 return 1; # not a single pid is alive
223 return 0; # at least one pid is still alive after timeout seconds
227 ######################################################################
228 # waitalivepidfile waits until the given pidfile has a pid that is
229 # alive, waiting at most timeout seconds. It returns the positive pid
230 # When it is alive, otherwise it returns 0 when timeout seconds have
231 # elapsed and the pidfile does not have a pid that is alive.
233 sub waitalivepidfile {
234 my ($pidfile, $timeout)=@_;
236 if(not defined $pidfile) {
239 if((not defined $timeout) || ($timeout < 1)) {
240 $timeout = $DEFAULT_TIMEOUT_START;
243 my $pid = checkalivepidfile($pidfile);
245 return $pid; # positive means it is alive
249 return 0; # no pid in pidfile or not alive
253 ######################################################################
254 # stopprocess ends the given pid(s), waiting for them to die. The 'pids'
255 # argument is a string of whitespace separated pids. Returns 1 if all
256 # of the processes have been successfully stopped. If unable to stop
257 # any of them in DEFAULT_TIMEOUT_STOP seconds then it returns 0.
262 if(not defined $pids) {
265 signalpids("KILL", $pids);
266 if(waitdeadpid($pids, $ONE_HALF_STOP_TIMEOUT) == 0) {
267 signalpids("KILL", $pids);
268 if(waitdeadpid($pids, $ONE_THIRD_STOP_TIMEOUT) == 0) {
269 signalpids("KILL", $pids);
270 if(waitdeadpid($pids, $ONE_SIXTH_STOP_TIMEOUT) == 0) {
271 return 0; # at least one pid is still alive !!!
275 return 1; # not a single pid is alive
279 ######################################################################
280 # stopprocesspidfile ends the test server process of the given pidfile,
281 # waiting for it to die, and unlinking/deleting the given pidfile. If
282 # the given process was not running or has been successfully stopped it
283 # returns 1. If unable to stop it in DEFAULT_TIMEOUT_STOP seconds then
286 sub stopprocesspidfile {
289 if(not defined $pidfile) {
292 my $ret = 1; # assume success stopping it
293 my $pid = checkalivepidfile($pidfile);
295 $ret = stopprocess($pid);
297 unlinkpidfiles($pidfile);
302 ######################################################################
303 # ftpkillslave ends a specific slave, waiting for it to die, and
304 # unlinking/deleting its pidfiles. If the given ftpslave was not
305 # running or has been successfully stopped it returns 1. If unable
306 # to stop it in DEFAULT_TIMEOUT_STOP seconds then it returns 0.
311 if(not defined $id) {
314 if(not defined $ext) {
317 my $ret = 1; # assume success stopping them
320 for my $base (('filt', 'data')) {
321 my $pidfile = ".sock$base$id$ext.pid";
322 my $pid = checkalivepidfile($pidfile);
323 $pidfiles .= " $pidfile";
329 $ret = stopprocess($pids);
332 unlinkpidfiles($pidfiles);
338 ######################################################################
339 # ftpkillslaves ends all the ftpslave processes, waiting for them to
340 # die, unlinking/deleting its pidfiles. If they were not running or
341 # have been successfully stopped it returns 1. If unable to stop any
342 # of them in DEFAULT_TIMEOUT_STOP seconds then returns 0.
346 my $ret = 1; # assume success stopping them
349 for my $ext (("", "ipv6")) {
350 for my $id (("", "2")) {
351 for my $base (('filt', 'data')) {
352 my $pidfile = ".sock$base$id$ext.pid";
353 my $pid = checkalivepidfile($pidfile);
354 $pidfiles .= " $pidfile";
362 $ret = stopprocess($pids);
365 unlinkpidfiles($pidfiles);
371 ######################################################################
372 # library files end with 1; to make 'require' and 'use' succeed.