1 #***************************************************************************
3 # Project ___| | | | _ \| |
5 # | (__| |_| | _ <| |___
6 # \___|\___/|_| \_\_____|
8 # Copyright (C) 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 https://curl.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.
21 # SPDX-License-Identifier: curl
23 ###########################################################################
31 use base qw(Exporter);
43 clear_advisor_read_lock
46 # portable sleeping needs Time::HiRes
51 # portable sleeping falls back to native Sleep on Win32
68 #######################################################################
69 # portable_sleep uses Time::HiRes::sleep if available and falls back
70 # to the classic approach of using select(undef, undef, undef, ...).
71 # even though that one is not portable due to being implemented using
72 # select on Windows: https://perldoc.perl.org/perlport.html#select
73 # Therefore it uses Win32::Sleep on Windows systems instead.
78 if($Time::HiRes::VERSION) {
79 Time::HiRes::sleep($seconds);
82 Win32::Sleep($seconds*1000);
85 select(undef, undef, undef, $seconds);
89 #######################################################################
90 # pidfromfile returns the pid stored in the given pidfile. The value
91 # of the returned pid will never be a negative value. It will be zero
92 # on any file related error or if a pid can not be extracted from the
99 if(-f $pidfile && -s $pidfile && open(my $pidfh, "<", "$pidfile")) {
102 $pid = 0 if($pid < 0);
107 #######################################################################
108 # pidexists checks if a process with a given pid exists and is alive.
109 # This will return the positive pid if the process exists and is alive.
110 # This will return the negative pid if the process exists differently.
111 # This will return 0 if the process could not be found.
117 # verify if currently existing Windows process
118 if ($pid > 65536 && os_is_win()) {
120 if($^O ne 'MSWin32') {
121 my $filter = "PID eq $pid";
122 my $result = `tasklist -fi \"$filter\" 2>nul`;
123 if(index($result, "$pid") != -1) {
130 # verify if currently existing and alive
139 #######################################################################
140 # pidterm asks the process with a given pid to terminate gracefully.
146 # request the process to quit
147 if ($pid > 65536 && os_is_win()) {
149 if($^O ne 'MSWin32') {
150 my $filter = "PID eq $pid";
151 my $result = `tasklist -fi \"$filter\" 2>nul`;
152 if(index($result, "$pid") != -1) {
153 system("taskkill -fi \"$filter\" >nul 2>&1");
159 # signal the process to terminate
164 #######################################################################
165 # pidkill kills the process with a given pid mercilessly and forcefully.
171 # request the process to quit
172 if ($pid > 65536 && os_is_win()) {
174 if($^O ne 'MSWin32') {
175 my $filter = "PID eq $pid";
176 my $result = `tasklist -fi \"$filter\" 2>nul`;
177 if(index($result, "$pid") != -1) {
178 system("taskkill -f -fi \"$filter\" >nul 2>&1");
179 # Windows XP Home compatibility
180 system("tskill $pid >nul 2>&1");
186 # signal the process to terminate
191 #######################################################################
192 # pidwait waits for the process with a given pid to be terminated.
198 # check if the process exists
199 if ($pid > 65536 && os_is_win()) {
200 if($flags == &WNOHANG) {
201 return pidexists($pid)?0:$pid;
203 while(pidexists($pid)) {
204 portable_sleep(0.01);
209 # wait on the process to terminate
210 return waitpid($pid, $flags);
213 #######################################################################
214 # processexists checks if a process with the pid stored in the given
215 # pidfile exists and is alive. This will return 0 on any file related
216 # error or if a pid can not be extracted from the given file. When a
217 # process with the same pid as the one extracted from the given file
218 # is currently alive this returns that positive pid. Otherwise, when
219 # the process is not alive, will return the negative value of the pid.
222 use POSIX ":sys_wait_h";
225 # fetch pid from pidfile
226 my $pid = pidfromfile($pidfile);
229 # verify if currently alive
230 if(pidexists($pid)) {
234 # get rid of the certainly invalid pidfile
235 unlink($pidfile) if($pid == pidfromfile($pidfile));
236 # reap its dead children, if not done yet
237 pidwait($pid, &WNOHANG);
238 # negative return value means dead process
245 #######################################################################
246 # killpid attempts to gracefully stop processes in the given pid list
247 # with a SIGTERM signal and SIGKILLs those which haven't died on time.
250 my ($verbose, $pidlist) = @_;
251 use POSIX ":sys_wait_h";
256 # The 'pidlist' argument is a string of whitespace separated pids.
257 return if(not defined($pidlist));
259 # Make 'requested' hold the non-duplicate pids from 'pidlist'.
260 @requested = split(' ', $pidlist);
261 return if(not @requested);
262 if(scalar(@requested) > 2) {
263 @requested = sort({$a <=> $b} @requested);
265 for(my $i = scalar(@requested) - 2; $i >= 0; $i--) {
266 if($requested[$i] == $requested[$i+1]) {
267 splice @requested, $i+1, 1;
271 # Send a SIGTERM to processes which are alive to gracefully stop them.
272 foreach my $tmp (@requested) {
274 if($tmp =~ /^(\d+)$/) {
277 if(pidexists($pid)) {
278 print("RUN: Process with pid $pid signalled to die\n")
281 push @signalled, $pid;
284 print("RUN: Process with pid $pid already dead\n")
286 # if possible reap its dead children
287 pidwait($pid, &WNOHANG);
288 push @reapchild, $pid;
294 # Allow all signalled processes five seconds to gracefully die.
296 my $twentieths = 5 * 20;
297 while($twentieths--) {
298 for(my $i = scalar(@signalled) - 1; $i >= 0; $i--) {
299 my $pid = $signalled[$i];
300 if(!pidexists($pid)) {
301 print("RUN: Process with pid $pid gracefully died\n")
303 splice @signalled, $i, 1;
304 # if possible reap its dead children
305 pidwait($pid, &WNOHANG);
306 push @reapchild, $pid;
309 last if(not scalar(@signalled));
310 portable_sleep(0.05);
314 # Mercilessly SIGKILL processes still alive.
316 foreach my $pid (@signalled) {
318 print("RUN: Process with pid $pid forced to die with SIGKILL\n")
321 # if possible reap its dead children
322 pidwait($pid, &WNOHANG);
323 push @reapchild, $pid;
328 # Reap processes dead children for sure.
330 foreach my $pid (@reapchild) {
338 #######################################################################
339 # killsockfilters kills sockfilter processes for a given server.
341 sub killsockfilters {
342 my ($piddir, $proto, $ipvnum, $idnum, $verbose, $which) = @_;
347 return if($proto !~ /^(ftp|imap|pop3|smtp)$/);
349 die "unsupported sockfilter: $which"
350 if($which && ($which !~ /^(main|data)$/));
352 $server = servername_id($proto, $ipvnum, $idnum) if($verbose);
354 if(!$which || ($which eq 'main')) {
355 $pidfile = mainsockf_pidfilename($piddir, $proto, $ipvnum, $idnum);
356 $pid = processexists($pidfile);
358 printf("* kill pid for %s-%s => %d\n", $server,
359 ($proto eq 'ftp')?'ctrl':'filt', $pid) if($verbose);
363 unlink($pidfile) if(-f $pidfile);
366 return if($proto ne 'ftp');
368 if(!$which || ($which eq 'data')) {
369 $pidfile = datasockf_pidfilename($piddir, $proto, $ipvnum, $idnum);
370 $pid = processexists($pidfile);
372 printf("* kill pid for %s-data => %d\n", $server,
377 unlink($pidfile) if(-f $pidfile);
381 #######################################################################
382 # killallsockfilters kills sockfilter processes for all servers.
384 sub killallsockfilters {
385 my ($piddir, $verbose) = @_;
387 for my $proto (('ftp', 'imap', 'pop3', 'smtp')) {
388 for my $ipvnum (('4', '6')) {
389 for my $idnum (('1', '2')) {
390 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose);
397 sub set_advisor_read_lock {
401 if(open($fileh, ">", "$filename") && close($fileh)) {
404 printf "Error creating lock file $filename error: $!\n";
408 sub clear_advisor_read_lock {