PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/release/src/router/mysql/mysql-test/lib/v1/mtr_timer.pl

https://gitlab.com/envieidoc/tomato
Perl | 159 lines | 88 code | 30 blank | 41 comment | 6 complexity | b4e9dc056922f7b8f8f042a70984d15c MD5 | raw file
  1. # -*- cperl -*-
  2. # Copyright (c) 2005, 2006 MySQL AB, 2008 Sun Microsystems, Inc.
  3. # Use is subject to license terms.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; version 2 of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. # This is a library file used by the Perl version of mysql-test-run,
  18. # and is part of the translation of the Bourne shell script with the
  19. # same name.
  20. use Errno;
  21. use strict;
  22. sub mtr_init_timers ();
  23. sub mtr_timer_start($$$);
  24. sub mtr_timer_stop($$);
  25. sub mtr_timer_stop_all($);
  26. ##############################################################################
  27. #
  28. # Initiate the structure shared by all timers
  29. #
  30. ##############################################################################
  31. sub mtr_init_timers () {
  32. my $timers = { timers => {}, pids => {}};
  33. return $timers;
  34. }
  35. ##############################################################################
  36. #
  37. # Start, stop and poll a timer
  38. #
  39. # As alarm() isn't portable to Windows, we use separate processes to
  40. # implement timers.
  41. #
  42. ##############################################################################
  43. sub mtr_timer_start($$$) {
  44. my ($timers,$name,$duration)= @_;
  45. if ( exists $timers->{'timers'}->{$name} )
  46. {
  47. # We have an old running timer, kill it
  48. mtr_warning("There is an old timer running");
  49. mtr_timer_stop($timers,$name);
  50. }
  51. FORK:
  52. {
  53. my $tpid= fork();
  54. if ( ! defined $tpid )
  55. {
  56. if ( $! == $!{EAGAIN} ) # See "perldoc Errno"
  57. {
  58. mtr_warning("Got EAGAIN from fork(), sleep 1 second and redo");
  59. sleep(1);
  60. redo FORK;
  61. }
  62. else
  63. {
  64. mtr_error("can't fork timer, error: $!");
  65. }
  66. }
  67. if ( $tpid )
  68. {
  69. # Parent, record the information
  70. mtr_verbose("Starting timer for '$name',",
  71. "duration: $duration, pid: $tpid");
  72. $timers->{'timers'}->{$name}->{'pid'}= $tpid;
  73. $timers->{'timers'}->{$name}->{'duration'}= $duration;
  74. $timers->{'pids'}->{$tpid}= $name;
  75. }
  76. else
  77. {
  78. # Child, install signal handlers and sleep for "duration"
  79. # Don't do the ^C cleanup in the timeout child processes!
  80. # There is actually a race here, if we get ^C after fork(), but before
  81. # clearing the signal handler.
  82. $SIG{INT}= 'DEFAULT';
  83. $SIG{TERM}= sub {
  84. mtr_verbose("timer $$ woke up, exiting!");
  85. exit(0);
  86. };
  87. $0= "mtr_timer(timers,$name,$duration)";
  88. sleep($duration);
  89. mtr_verbose("timer $$ expired after $duration seconds");
  90. exit(0);
  91. }
  92. }
  93. }
  94. sub mtr_timer_stop ($$) {
  95. my ($timers,$name)= @_;
  96. if ( exists $timers->{'timers'}->{$name} )
  97. {
  98. my $tpid= $timers->{'timers'}->{$name}->{'pid'};
  99. mtr_verbose("Stopping timer for '$name' with pid $tpid");
  100. # FIXME as Cygwin reuses pids fast, maybe check that is
  101. # the expected process somehow?!
  102. kill(15, $tpid);
  103. # As the timers are so simple programs, we trust them to terminate,
  104. # and use blocking wait for it. We wait just to avoid a zombie.
  105. waitpid($tpid,0);
  106. delete $timers->{'timers'}->{$name}; # Remove the timer information
  107. delete $timers->{'pids'}->{$tpid}; # and PID reference
  108. return 1;
  109. }
  110. mtr_error("Asked to stop timer '$name' not started");
  111. }
  112. sub mtr_timer_stop_all ($) {
  113. my $timers= shift;
  114. foreach my $name ( keys %{$timers->{'timers'}} )
  115. {
  116. mtr_timer_stop($timers, $name);
  117. }
  118. return 1;
  119. }
  120. sub mtr_timer_timeout ($$) {
  121. my ($timers,$pid)= @_;
  122. return "" unless exists $timers->{'pids'}->{$pid};
  123. # Got a timeout(the process with $pid is recorded as being a timer)
  124. # return the name of the timer
  125. return $timers->{'pids'}->{$pid};
  126. }
  127. 1;