aboutsummaryrefslogtreecommitdiff
path: root/tests/ftp.pm
blob: 32c6779e3a47362b82f807d2356d2e9746a7a808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://curl.haxx.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# $Id$
###########################################################################

use strict;
use warnings;


my $DEFAULT_TIMEOUT_START = 90; # default allowed time for a process to startup
my $DEFAULT_TIMEOUT_STOP  = 90; # default allowed time for a process to stop

my $ONE_HALF_STOP_TIMEOUT  = int($DEFAULT_TIMEOUT_STOP / 2);
my $ONE_THIRD_STOP_TIMEOUT = int($DEFAULT_TIMEOUT_STOP / 3);
my $ONE_SIXTH_STOP_TIMEOUT = int($DEFAULT_TIMEOUT_STOP / 6);

my $pidpattern = qr/^\-?(\d+)$/; # pre-compiled pid pattern regexp


######################################################################
# pidfromfile returns the pid stored in the given pidfile.  The value
# of the returned pid will never be a negative value. It will be zero
# on any file related error or if a pid can not be extracted from the
# file. Otherwise it will be a positive value, even If the pid number
# stored in the file is a negative one.
#
sub pidfromfile {
    my ($pidfile)=@_;

    my $pid = 0; # on failure or empty file return 0
    my $pidline;

    if(not defined $pidfile) {
        return 0;
    }
    if(-f $pidfile) {
        if(open(PIDF, "<$pidfile")) {
            my $pidline = <PIDF>;
            close(PIDF);
            chomp $pidline;
            $pidline =~ s/^\s+//;
            $pidline =~ s/\s+$//;
            $pidline =~ s/^[+-]?0+//;
            if($pidline =~ $pidpattern) {
                $pid = $1;
            }
        }
    }
    return $pid;
}


######################################################################
# unlinkpidfiles unlinks/deletes the given pidfiles. The first argument
# 'pidfiles' is a string of whitespace separated pidfiles. If successful
# returns 0, on error it returns the number of files not deleted.
#
sub unlinkpidfiles {
    my ($pidfiles)=@_;

    if(not defined $pidfiles) {
        return 0;
    }
    my $pidfile;
    my $errcount = 0;
    for $pidfile (split(" ", $pidfiles)) {
        if($pidfile) {
            if(unlink($pidfile) == 0) {
                $errcount++;
            }
        }
    }
    return $errcount;
}


######################################################################
# checkalivepid checks if the process of the given pid is alive. The
# argument must represent a single pid and be a valid number, if not
# it will return 0. It will also return 0 if the pid argument is zero
# or negative. If the pid argument is positive and it is alive returns
# the same positive pid, otherwise, if it is not alive it will return
# the negative value of the pid argument.
#
sub checkalivepid {
    my ($pid)=@_;

    if(not defined $pid) {
        return 0;
    }
    if ($pid !~ $pidpattern) {
        return 0; # invalid argument
    }
    if($pid > 0) {
        if(kill(0, $pid)) {
            return $pid; # positive means it is alive
        }
        else {
            return -$pid; # negative means dead process
        }
    }
    return 0; # not a positive pid argument
}


######################################################################
# checkalivepidfile checks if the process of the pid stored in the
# given pidfile is alive. It will return 0 on any file related error
# or if a pid can not be extracted from the file. If the process of
# the pid present in the file is alive it returns that positive pid,
# if it is not alive it will return the negative value of the pid.
#
sub checkalivepidfile {
    my ($pidfile)=@_;

    my $pid = pidfromfile($pidfile);
    my $ret = checkalivepid($pid);
    return $ret;
}


######################################################################
# signalpids signals processes in the second argument with the signal
# given in the first argument. The second argument 'pids' is a string
# of whitespace separated pids. Of the given pids only those that are
# positive and are actually alive will be signalled, and no matter
# how many times a pid is repeated it will only be signalled once.
#
sub signalpids {
    my ($signal, $pids)=@_;

    if((not defined $signal) || (not defined $pids)) {
        return;
    }
    my $prev = 0;
    for(sort({$a <=> $b} split(" ", $pids))) {
        if($_ =~ $pidpattern) {
            my $pid = $1;
            if($prev != $pid) {
                $prev = $pid;
                if(checkalivepid($pid) > 0) {
                    kill($signal, $pid);
                }
            }
        }
    }
}


######################################################################
# signalpidfile signals the process of the pid stored in the given
# pidfile with the signal given in the first argument if the process
# with that pid is actually alive.
#
sub signalpidfile {
    my ($signal, $pidfile)=@_;

    my $pid = pidfromfile($pidfile);
    if($pid > 0) {
        signalpids($signal, $pid);
    }
}

    
######################################################################
# waitdeadpid waits until all processes given in the first argument
# are not alive, waiting at most timeout seconds. The first argument
# 'pids' is a string of whitespace separated pids. Returns 1 when all
# pids are not alive. Returns 0 when the specified timeout has expired
# and at least one of the specified pids is still alive.
#
sub waitdeadpid {
    my ($pids, $timeout)=@_;

    if(not defined $pids) {
        return 1;
    }
    if((not defined $timeout) || ($timeout < 1)) {
        $timeout = $DEFAULT_TIMEOUT_STOP;
    }
    while($timeout--) {
        my $alive = 0;
        for(split(" ", $pids)) {
            if($_ =~ $pidpattern) {
                my $pid = $1;
                if(checkalivepid($pid) > 0) {
                    $alive++;
                }
            }
        }
        if($alive == 0) {
            return 1; # not a single pid is alive
        }
        sleep(1);
    }
    return 0; # at least one pid is still alive after timeout seconds
}


######################################################################
# waitalivepidfile waits until the given pidfile has a pid that is
# alive, waiting at most timeout seconds. It returns the positive pid
# When it is alive, otherwise it returns 0 when timeout seconds have
# elapsed and the pidfile does not have a pid that is alive.
#
sub waitalivepidfile {
    my ($pidfile, $timeout)=@_;

    if(not defined $pidfile) {
        return 0;
    }
    if((not defined $timeout) || ($timeout < 1)) {
        $timeout = $DEFAULT_TIMEOUT_START;
    }
    while($timeout--) {
        my $pid = checkalivepidfile($pidfile);
        if($pid > 0) {
            return $pid; # positive means it is alive
        }
        sleep(1);
    }
    return 0; # no pid in pidfile or not alive
}


######################################################################
# stopprocess ends the given pid(s), waiting for them to die. The 'pids'
# argument is a string of whitespace separated pids. Returns 1 if all
# of the processes have been successfully stopped. If unable to stop
# any of them in DEFAULT_TIMEOUT_STOP seconds then it returns 0.
#
sub stopprocess {
    my ($pids)=@_;

    if(not defined $pids) {
        return 1;
    }
    signalpids("TERM", $pids);
    if(waitdeadpid($pids, $ONE_HALF_STOP_TIMEOUT) == 0) {
        signalpids("INT", $pids);
        if(waitdeadpid($pids, $ONE_THIRD_STOP_TIMEOUT) == 0) {
            signalpids("KILL", $pids);
            if(waitdeadpid($pids, $ONE_SIXTH_STOP_TIMEOUT) == 0) {
                return 0; # at least one pid is still alive !!!
            }
        }
    }
    return 1; # not a single pid is alive
}


######################################################################
# stopprocesspidfile ends the test server process of the given pidfile,
# waiting for it to die, and unlinking/deleting the given pidfile. If
# the given process was not running or has been successfully stopped it
# returns 1. If unable to stop it in DEFAULT_TIMEOUT_STOP seconds then
# returns 0.
#
sub stopprocesspidfile {
    my ($pidfile)=@_;

    if(not defined $pidfile) {
        return 1;
    }
    my $ret = 1; # assume success stopping it
    my $pid = checkalivepidfile($pidfile);
    if($pid > 0) {
        $ret = stopprocess($pid);
    }
    unlinkpidfiles($pidfile);
    return $ret;
}


######################################################################
# ftpkillslave ends a specific slave, waiting for it to die, and
# unlinking/deleting its pidfiles. If the given ftpslave was not
# running or has been successfully stopped it returns 1. If unable
# to stop it in DEFAULT_TIMEOUT_STOP seconds then it returns 0.
#
sub ftpkillslave {
    my ($id, $ext)=@_;

    my $ret = 1; # assume success stopping them
    my $pids = "";
    my $pidfiles = "";
    for my $base (('filt', 'data')) {
        my $pidfile = ".sock$base$id$ext.pid";
        my $pid = checkalivepidfile($pidfile);
        $pidfiles .= " $pidfile";
        if($pid > 0) {
            $pids .= " $pid";
        }
    }
    if($pids) {
        $ret = stopprocess($pids);
    }
    if($pidfiles) {
        unlinkpidfiles($pidfiles);
    }
    return $ret;
}


######################################################################
# ftpkillslaves ends all the ftpslave processes, waiting for them to
# die, unlinking/deleting its pidfiles. If they were not running or
# have been successfully stopped it returns 1. If unable to stop any
# of them in DEFAULT_TIMEOUT_STOP seconds then returns 0.
#
sub ftpkillslaves {

    my $ret = 1; # assume success stopping them
    my $pids = "";
    my $pidfiles = "";
    for my $ext (("", "ipv6")) {
        for my $id (("", "2")) {
            for my $base (('filt', 'data')) {
                my $pidfile = ".sock$base$id$ext.pid";
                my $pid = checkalivepidfile($pidfile);
                $pidfiles .= " $pidfile";
                if($pid > 0) {
                    $pids .= " $pid";
                }
            }
        }
    }
    if($pids) {
        $ret = stopprocess($pids);
    }
    if($pidfiles) {
        unlinkpidfiles($pidfiles);
    }
    return $ret;
}


######################################################################
# library files end with 1; to make 'require' and 'use' succeed.
1;