mtr_im.pl

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PL 代码 · 共 776 行 · 第 1/2 页

PL
776
字号
# -*- cperl -*-# Copyright (C) 2006 MySQL AB# # This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; version 2 of the License.# # This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the# GNU General Public License for more details.# # You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA# This is a library file used by the Perl version of mysql-test-run,# and is part of the translation of the Bourne shell script with the# same name.use strict;# Private IM-related operations.sub mtr_im_kill_process ($$$$);sub mtr_im_load_pids ($);sub mtr_im_terminate ($);sub mtr_im_check_alive ($);sub mtr_im_check_main_alive ($);sub mtr_im_check_angel_alive ($);sub mtr_im_check_mysqlds_alive ($);sub mtr_im_check_mysqld_alive ($);sub mtr_im_cleanup ($);sub mtr_im_rm_file ($);sub mtr_im_errlog ($);sub mtr_im_kill ($);sub mtr_im_wait_for_connection ($$$);sub mtr_im_wait_for_mysqld($$$);# Public IM-related operations.sub mtr_im_start ($$);sub mtr_im_stop ($);################################################################################  Private operations.###############################################################################sub mtr_im_kill_process ($$$$) {  my $pid_lst= shift;  my $signal= shift;  my $total_retries= shift;  my $timeout= shift;  my %pids;  foreach my $pid ( @{$pid_lst} )  {    $pids{$pid}= 1;  }  for ( my $cur_attempt= 1; $cur_attempt <= $total_retries; ++$cur_attempt )  {    foreach my $pid ( keys %pids )    {      mtr_debug("Sending $signal to $pid...");      kill($signal, $pid);      unless ( kill (0, $pid) )      {        mtr_debug("Process $pid died.");        delete $pids{$pid};      }    }    return if scalar keys %pids == 0;    mtr_debug("Sleeping $timeout second(s) waiting for processes to die...");    sleep($timeout);  }  mtr_debug("Process(es) " .            join(' ', keys %pids) .            " is still alive after $total_retries " .            "of sending signal $signal.");}###########################################################################sub mtr_im_load_pids($) {  my $im= shift;  mtr_debug("Loading PID files...");  # Obtain mysqld-process pids.  my $instances = $im->{'instances'};  for ( my $idx= 0; $idx < 2; ++$idx )  {    mtr_debug("IM-guarded mysqld[$idx] PID file: '" .              $instances->[$idx]->{'path_pid'} . "'.");    my $mysqld_pid;    if ( -r $instances->[$idx]->{'path_pid'} )    {      $mysqld_pid= mtr_get_pid_from_file($instances->[$idx]->{'path_pid'});      mtr_debug("IM-guarded mysqld[$idx] PID: $mysqld_pid.");    }    else    {      $mysqld_pid= undef;      mtr_debug("IM-guarded mysqld[$idx]: no PID file.");    }    $instances->[$idx]->{'pid'}= $mysqld_pid;  }  # Re-read Instance Manager PIDs from the file, since during tests Instance  # Manager could have been restarted, so its PIDs could have been changed.  #   - IM-main  mtr_debug("IM-main PID file: '$im->{path_pid}'.");  if ( -f $im->{'path_pid'} )  {    $im->{'pid'} =      mtr_get_pid_from_file($im->{'path_pid'});    mtr_debug("IM-main PID: $im->{pid}.");  }  else  {    mtr_debug("IM-main: no PID file.");    $im->{'pid'}= undef;  }  #   - IM-angel  mtr_debug("IM-angel PID file: '$im->{path_angel_pid}'.");  if ( -f $im->{'path_angel_pid'} )  {    $im->{'angel_pid'} =      mtr_get_pid_from_file($im->{'path_angel_pid'});    mtr_debug("IM-angel PID: $im->{'angel_pid'}.");  }  else  {    mtr_debug("IM-angel: no PID file.");    $im->{'angel_pid'} = undef;  }}###########################################################################sub mtr_im_terminate($) {  my $im= shift;  # Load pids from pid-files. We should do it first of all, because IM deletes  # them on shutdown.  mtr_im_load_pids($im);  mtr_debug("Shutting Instance Manager down...");  # Ignoring SIGCHLD so that all children could rest in peace.  start_reap_all();  # Send SIGTERM to IM-main.  if ( defined $im->{'pid'} )  {    mtr_debug("IM-main pid: $im->{pid}.");    mtr_debug("Stopping IM-main...");    mtr_im_kill_process([ $im->{'pid'} ], 'TERM', 10, 1);  }  else  {    mtr_debug("IM-main pid: n/a.");  }  # If IM-angel was alive, wait for it to die.  if ( defined $im->{'angel_pid'} )  {    mtr_debug("IM-angel pid: $im->{'angel_pid'}.");    mtr_debug("Waiting for IM-angel to die...");    my $total_attempts= 10;    for ( my $cur_attempt=1; $cur_attempt <= $total_attempts; ++$cur_attempt )    {      unless ( kill (0, $im->{'angel_pid'}) )      {        mtr_debug("IM-angel died.");        last;      }      sleep(1);    }  }  else  {    mtr_debug("IM-angel pid: n/a.");  }  stop_reap_all();  # Re-load PIDs.  mtr_im_load_pids($im);}###########################################################################sub mtr_im_check_alive($) {  my $im= shift;  mtr_debug("Checking whether IM-components are alive...");  return 1 if mtr_im_check_main_alive($im);  return 1 if mtr_im_check_angel_alive($im);  return 1 if mtr_im_check_mysqlds_alive($im);  return 0;}###########################################################################sub mtr_im_check_main_alive($) {  my $im= shift;  # Check that the process, that we know to be IM's, is dead.  if ( defined $im->{'pid'} )  {    if ( kill (0, $im->{'pid'}) )    {      mtr_debug("IM-main (PID: $im->{pid}) is alive.");      return 1;    }    else    {      mtr_debug("IM-main (PID: $im->{pid}) is dead.");    }  }  else  {    mtr_debug("No PID file for IM-main.");  }  # Check that IM does not accept client connections.  if ( mtr_ping_port($im->{'port'}) )  {    mtr_debug("IM-main (port: $im->{port}) " .              "is accepting connections.");    mtr_im_errlog("IM-main is accepting connections on port " .                  "$im->{port}, but there is no " .                  "process information.");    return 1;  }  else  {    mtr_debug("IM-main (port: $im->{port}) " .              "does not accept connections.");    return 0;  }}###########################################################################sub mtr_im_check_angel_alive($) {  my $im= shift;  # Check that the process, that we know to be the Angel, is dead.  if ( defined $im->{'angel_pid'} )  {    if ( kill (0, $im->{'angel_pid'}) )    {      mtr_debug("IM-angel (PID: $im->{angel_pid}) is alive.");      return 1;    }    else    {      mtr_debug("IM-angel (PID: $im->{angel_pid}) is dead.");      return 0;    }  }  else  {    mtr_debug("No PID file for IM-angel.");    return 0;  }}###########################################################################sub mtr_im_check_mysqlds_alive($) {  my $im= shift;  mtr_debug("Checking for IM-guarded mysqld instances...");  my $instances = $im->{'instances'};  for ( my $idx= 0; $idx < 2; ++$idx )  {    mtr_debug("Checking mysqld[$idx]...");    return 1      if mtr_im_check_mysqld_alive($instances->[$idx]);  }}###########################################################################sub mtr_im_check_mysqld_alive($) {  my $mysqld_instance= shift;  # Check that the process is dead.  if ( defined $mysqld_instance->{'pid'} )  {    if ( kill (0, $mysqld_instance->{'pid'}) )    {      mtr_debug("Mysqld instance (PID: $mysqld_instance->{pid}) is alive.");      return 1;    }    else    {      mtr_debug("Mysqld instance (PID: $mysqld_instance->{pid}) is dead.");    }  }  else  {    mtr_debug("No PID file for mysqld instance.");  }  # Check that mysqld does not accept client connections.  if ( mtr_ping_port($mysqld_instance->{'port'}) )  {    mtr_debug("Mysqld instance (port: $mysqld_instance->{port}) " .              "is accepting connections.");    mtr_im_errlog("Mysqld is accepting connections on port " .                  "$mysqld_instance->{port}, but there is no " .                  "process information.");    return 1;  }  else  {    mtr_debug("Mysqld instance (port: $mysqld_instance->{port}) " .              "does not accept connections.");    return 0;  }}###########################################################################sub mtr_im_cleanup($) {  my $im= shift;  mtr_im_rm_file($im->{'path_pid'});  mtr_im_rm_file($im->{'path_sock'});  mtr_im_rm_file($im->{'path_angel_pid'});  for ( my $idx= 0; $idx < 2; ++$idx )  {    mtr_im_rm_file($im->{'instances'}->[$idx]->{'path_pid'});    mtr_im_rm_file($im->{'instances'}->[$idx]->{'path_sock'});  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?