ftp.pm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  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.
  13. #
  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.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. ###########################################################################
  22. use strict;
  23. use warnings;
  24. use serverhelp qw(
  25. servername_id
  26. mainsockf_pidfilename
  27. datasockf_pidfilename
  28. );
  29. #######################################################################
  30. # pidfromfile returns the pid stored in the given pidfile. The value
  31. # of the returned pid will never be a negative value. It will be zero
  32. # on any file related error or if a pid can not be extracted from the
  33. # given file.
  34. #
  35. sub pidfromfile {
  36. my $pidfile = $_[0];
  37. my $pid = 0;
  38. if(-f $pidfile && -s $pidfile && open(PIDFH, "<$pidfile")) {
  39. $pid = 0 + <PIDFH>;
  40. close(PIDFH);
  41. $pid = 0 unless($pid > 0);
  42. }
  43. return $pid;
  44. }
  45. #######################################################################
  46. # pidexists checks if a process with a given pid exists and is alive.
  47. # This will return the positive pid if the process exists and is alive.
  48. # This will return the negative pid if the process exists differently.
  49. # This will return 0 if the process could not be found.
  50. #
  51. sub pidexists {
  52. my $pid = $_[0];
  53. if($pid > 0) {
  54. # verify if currently existing and alive
  55. if(kill(0, $pid)) {
  56. return $pid;
  57. }
  58. # verify if currently existing Windows process
  59. if($^O eq "msys") {
  60. my $filter = "PID eq $pid";
  61. my $result = `tasklist -fi \"$filter\" 2>nul`;
  62. if(index($result, "$pid") != -1) {
  63. return -$pid;
  64. }
  65. }
  66. }
  67. return 0;
  68. }
  69. #######################################################################
  70. # pidterm asks the process with a given pid to terminate gracefully.
  71. #
  72. sub pidterm {
  73. my $pid = $_[0];
  74. if($pid > 0) {
  75. # signal the process to terminate
  76. kill("TERM", $pid);
  77. # request the process to quit
  78. if($^O eq "msys") {
  79. my $filter = "PID eq $pid";
  80. my $result = `tasklist -fi \"$filter\" 2>nul`;
  81. if(index($result, "$pid") != -1) {
  82. system("taskkill -fi \"$filter\" >nul 2>&1");
  83. }
  84. }
  85. }
  86. }
  87. #######################################################################
  88. # pidkill kills the process with a given pid mercilessly andforcefully.
  89. #
  90. sub pidkill {
  91. my $pid = $_[0];
  92. if($pid > 0) {
  93. # signal the process to terminate
  94. kill("KILL", $pid);
  95. # request the process to quit
  96. if($^O eq "msys") {
  97. my $filter = "PID eq $pid";
  98. my $result = `tasklist -fi \"$filter\" 2>nul`;
  99. if(index($result, "$pid") != -1) {
  100. system("taskkill -f -fi \"$filter\" >nul 2>&1");
  101. # Windows XP Home compatibility
  102. system("tskill $pid >nul 2>&1");
  103. }
  104. }
  105. }
  106. }
  107. #######################################################################
  108. # processexists checks if a process with the pid stored in the given
  109. # pidfile exists and is alive. This will return 0 on any file related
  110. # error or if a pid can not be extracted from the given file. When a
  111. # process with the same pid as the one extracted from the given file
  112. # is currently alive this returns that positive pid. Otherwise, when
  113. # the process is not alive, will return the negative value of the pid.
  114. #
  115. sub processexists {
  116. use POSIX ":sys_wait_h";
  117. my $pidfile = $_[0];
  118. # fetch pid from pidfile
  119. my $pid = pidfromfile($pidfile);
  120. if($pid > 0) {
  121. # verify if currently alive
  122. if(pidexists($pid)) {
  123. return $pid;
  124. }
  125. else {
  126. # get rid of the certainly invalid pidfile
  127. unlink($pidfile) if($pid == pidfromfile($pidfile));
  128. # reap its dead children, if not done yet
  129. waitpid($pid, &WNOHANG);
  130. # negative return value means dead process
  131. return -$pid;
  132. }
  133. }
  134. return 0;
  135. }
  136. #######################################################################
  137. # killpid attempts to gracefully stop processes in the given pid list
  138. # with a SIGTERM signal and SIGKILLs those which haven't died on time.
  139. #
  140. sub killpid {
  141. use POSIX ":sys_wait_h";
  142. my ($verbose, $pidlist) = @_;
  143. my @requested;
  144. my @signalled;
  145. my @reapchild;
  146. # The 'pidlist' argument is a string of whitespace separated pids.
  147. return if(not defined($pidlist));
  148. # Make 'requested' hold the non-duplicate pids from 'pidlist'.
  149. @requested = split(' ', $pidlist);
  150. return if(not @requested);
  151. if(scalar(@requested) > 2) {
  152. @requested = sort({$a <=> $b} @requested);
  153. }
  154. for(my $i = scalar(@requested) - 2; $i >= 0; $i--) {
  155. if($requested[$i] == $requested[$i+1]) {
  156. splice @requested, $i+1, 1;
  157. }
  158. }
  159. # Send a SIGTERM to processes which are alive to gracefully stop them.
  160. foreach my $tmp (@requested) {
  161. chomp $tmp;
  162. if($tmp =~ /^(\d+)$/) {
  163. my $pid = $1;
  164. if($pid > 0) {
  165. if(pidexists($pid)) {
  166. print("RUN: Process with pid $pid signalled to die\n")
  167. if($verbose);
  168. pidterm($pid);
  169. push @signalled, $pid;
  170. }
  171. else {
  172. print("RUN: Process with pid $pid already dead\n")
  173. if($verbose);
  174. # if possible reap its dead children
  175. waitpid($pid, &WNOHANG);
  176. push @reapchild, $pid;
  177. }
  178. }
  179. }
  180. }
  181. # Allow all signalled processes five seconds to gracefully die.
  182. if(@signalled) {
  183. my $twentieths = 5 * 20;
  184. while($twentieths--) {
  185. for(my $i = scalar(@signalled) - 1; $i >= 0; $i--) {
  186. my $pid = $signalled[$i];
  187. if(!pidexists($pid)) {
  188. print("RUN: Process with pid $pid gracefully died\n")
  189. if($verbose);
  190. splice @signalled, $i, 1;
  191. # if possible reap its dead children
  192. waitpid($pid, &WNOHANG);
  193. push @reapchild, $pid;
  194. }
  195. }
  196. last if(not scalar(@signalled));
  197. select(undef, undef, undef, 0.05);
  198. }
  199. }
  200. # Mercilessly SIGKILL processes still alive.
  201. if(@signalled) {
  202. foreach my $pid (@signalled) {
  203. if($pid > 0) {
  204. print("RUN: Process with pid $pid forced to die with SIGKILL\n")
  205. if($verbose);
  206. pidkill($pid);
  207. # if possible reap its dead children
  208. waitpid($pid, &WNOHANG);
  209. push @reapchild, $pid;
  210. }
  211. }
  212. }
  213. # Reap processes dead children for sure.
  214. if(@reapchild) {
  215. foreach my $pid (@reapchild) {
  216. if($pid > 0) {
  217. waitpid($pid, 0);
  218. }
  219. }
  220. }
  221. }
  222. #######################################################################
  223. # killsockfilters kills sockfilter processes for a given server.
  224. #
  225. sub killsockfilters {
  226. my ($proto, $ipvnum, $idnum, $verbose, $which) = @_;
  227. my $server;
  228. my $pidfile;
  229. my $pid;
  230. return if($proto !~ /^(ftp|imap|pop3|smtp)$/);
  231. die "unsupported sockfilter: $which"
  232. if($which && ($which !~ /^(main|data)$/));
  233. $server = servername_id($proto, $ipvnum, $idnum) if($verbose);
  234. if(!$which || ($which eq 'main')) {
  235. $pidfile = mainsockf_pidfilename($proto, $ipvnum, $idnum);
  236. $pid = processexists($pidfile);
  237. if($pid > 0) {
  238. printf("* kill pid for %s-%s => %d\n", $server,
  239. ($proto eq 'ftp')?'ctrl':'filt', $pid) if($verbose);
  240. pidkill($pid);
  241. waitpid($pid, 0);
  242. }
  243. unlink($pidfile) if(-f $pidfile);
  244. }
  245. return if($proto ne 'ftp');
  246. if(!$which || ($which eq 'data')) {
  247. $pidfile = datasockf_pidfilename($proto, $ipvnum, $idnum);
  248. $pid = processexists($pidfile);
  249. if($pid > 0) {
  250. printf("* kill pid for %s-data => %d\n", $server,
  251. $pid) if($verbose);
  252. pidkill($pid);
  253. waitpid($pid, 0);
  254. }
  255. unlink($pidfile) if(-f $pidfile);
  256. }
  257. }
  258. #######################################################################
  259. # killallsockfilters kills sockfilter processes for all servers.
  260. #
  261. sub killallsockfilters {
  262. my $verbose = $_[0];
  263. for my $proto (('ftp', 'imap', 'pop3', 'smtp')) {
  264. for my $ipvnum (('4', '6')) {
  265. for my $idnum (('1', '2')) {
  266. killsockfilters($proto, $ipvnum, $idnum, $verbose);
  267. }
  268. }
  269. }
  270. }
  271. sub set_advisor_read_lock {
  272. my ($filename) = @_;
  273. if(open(FILEH, ">$filename")) {
  274. close(FILEH);
  275. return;
  276. }
  277. printf "Error creating lock file $filename error: $!";
  278. }
  279. sub clear_advisor_read_lock {
  280. my ($filename) = @_;
  281. if(-f $filename) {
  282. unlink($filename);
  283. }
  284. }
  285. 1;