mtr_process.pl 9.83 KB
Newer Older
unknown's avatar
unknown committed
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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
# -*- cperl -*-

# 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;

use POSIX ":sys_wait_h";

sub mtr_run ($$$$$$);
sub mtr_spawn ($$$$$$);
sub mtr_stop_servers ($);
sub mtr_kill_leftovers ();

# static in C
sub spawn_impl ($$$$$$$);

##############################################################################
#
#  Execute an external command
#
##############################################################################

# This function try to mimic the C version used in "netware/mysql_test_run.c"
# FIXME learn it to handle append mode as well, a "new" flag or a "append"

sub mtr_run ($$$$$$) {
  my $path=       shift;
  my $arg_list_t= shift;
  my $input=      shift;
  my $output=     shift;
  my $error=      shift;
  my $pid_file=   shift;

  return spawn_impl($path,$arg_list_t,1,$input,$output,$error,$pid_file);
}

sub mtr_spawn ($$$$$$) {
  my $path=       shift;
  my $arg_list_t= shift;
  my $input=      shift;
  my $output=     shift;
  my $error=      shift;
  my $pid_file=   shift;

  return spawn_impl($path,$arg_list_t,0,$input,$output,$error,$pid_file);
}


##############################################################################
#
#  If $join is set, we return the error code, else we return the PID
#
##############################################################################

sub spawn_impl ($$$$$$$) {
  my $path=       shift;
  my $arg_list_t= shift;
  my $join=       shift;
  my $input=      shift;
  my $output=     shift;
  my $error=      shift;
  my $pid_file=   shift;                 # FIXME

  # FIXME really needing a PATH???
  # $ENV{'PATH'}= "/bin:/usr/bin:/usr/local/bin:/usr/bsd:/usr/X11R6/bin:/usr/openwin/bin:/usr/bin/X11:$ENV{'PATH'}";

  $ENV{'TZ'}=             "GMT-3";         # for UNIX_TIMESTAMP tests to work
  $ENV{'LC_COLLATE'}=     "C";
  $ENV{'MYSQL_TEST_DIR'}= $::glob_mysql_test_dir;
  $ENV{'MASTER_MYPORT'}=  $::opt_master_myport;
  $ENV{'SLAVE_MYPORT'}=   $::opt_slave_myport;
# $ENV{'MYSQL_TCP_PORT'}= '@MYSQL_TCP_PORT@'; # FIXME
  $ENV{'MYSQL_TCP_PORT'}= 3306;
  $ENV{'MASTER_MYSOCK'}=  $::master->[0]->{'path_mysock'};

  if ( $::opt_script_debug )
  {
    print STDERR "-" x 78, "\n";
    print STDERR "STDIN  $input\n" if $input;
    print STDERR "STDOUT $output\n" if $output;
    print STDERR "STDERR $error\n" if $error;
    print STDERR "DAEMON\n" if !$join;
    print STDERR "EXEC $path ", join(" ",@$arg_list_t), "\n";
    print STDERR "-" x 78, "\n";
  }

  my $pid= fork();

  if ( $pid )
  {
    # Parent, i.e. the main script
    if ( $join )
    {
      # We run a command and wait for the result
      # FIXME this need to be improved
      waitpid($pid,0);
      my $exit_value=  $? >> 8;
      my $signal_num=  $? & 127;
      my $dumped_core= $? & 128;
      if ( $signal_num )
      {
        die("spawn got signal $signal_num");
      }
      if ( $dumped_core )
      {
        die("spawn dumped core");
      }
      return $exit_value;
    }
    else
    {
      # We spawned a process we don't wait for
      return $pid;
    }
  }
  else
  {
    # Child, redirect output and exec
    # FIXME I tried POSIX::setsid() here to detach and, I hoped,
    # avoid zombies. But everything went wild, somehow the parent
    # became a deamon as well, and was hard to kill ;-)
    # Need to catch SIGCHLD and do waitpid or something instead......

    $SIG{INT}= 'DEFAULT';         # Parent do some stuff, we don't

    if ( $output )
    {
      open(STDOUT,">",$output) or die "Can't redirect STDOUT to \"$output\": $!";
    }
    if ( $error )
    {
      if ( $output eq $error )
      {
        open(STDERR,">&STDOUT") or die "Can't dup STDOUT: $!";
      }
      else
      {
        open(STDERR,">",$error) or die "Can't redirect STDERR to \"$output\": $!";
      }
    }
    if ( $input )
    {
      open(STDIN,"<",$input) or die "Can't redirect STDIN to \"$input\": $!";
    }
    exec($path,@$arg_list_t);
  }
}

##############################################################################
#
#  Kill processes left from previous runs
#
##############################################################################

sub mtr_kill_leftovers () {

  # First, kill all masters and slaves that would conflict with
  # this run. Make sure to remove the PID file, if any.

  my @args;

  for ( my $idx; $idx < 2; $idx++ )
  {
#    if ( $::master->[$idx]->{'pid'} )
#    {
      push(@args,
           $::master->[$idx]->{'path_mypid'},
           $::master->[$idx]->{'path_mysock'},
         );
#    }
  }

  for ( my $idx; $idx < 3; $idx++ )
  {
#    if ( $::slave->[$idx]->{'pid'} )
#    {
      push(@args,
           $::slave->[$idx]->{'path_mypid'},
           $::slave->[$idx]->{'path_mysock'},
         );
#    }
  }

  mtr_stop_servers(\@args);

  # We scan the "var/run/" directory for other process id's to kill
  my $rundir= "$::glob_mysql_test_dir/var/run"; # FIXME $path_run_dir or something

  if ( -d $rundir )
  {
    opendir(RUNDIR, $rundir)
      or mtr_error("can't open directory \"$rundir\": $!");

    my @pids;

    while ( my $elem= readdir(RUNDIR) )
    {
      my $pidfile= "$rundir/$elem";

      if ( -f $pidfile )
      {
        my $pid= mtr_get_pid_from_file($pidfile);
        if ( ! unlink($pidfile) )
        {
          mtr_error("can't remove $pidfile");
        }
        push(@pids, $pid);
      }
    }
    closedir(RUNDIR);

    my $retries= 10;                    # 10 seconds
    do
    {
      kill(9, @pids);
    } while ( $retries-- and  kill(0, @pids) );

    if ( kill(0, @pids) )
    {
      mtr_error("can't kill processes " . join(" ", @pids));
    }

  }
}

##############################################################################
#
#  Shut down mysqld servers
#
##############################################################################

# To speed things we kill servers in parallel.
# The argument is a list of 'pidfiles' and 'socketfiles'.
# We use the pidfiles and socketfiles to try to terminate the servers.
# This is not perfect, there could still be other server processes
# left.

sub mtr_stop_servers ($) {
  my $spec= shift;

  # First try nice normal shutdown using 'mysqladmin'

  {
    my @args= @$spec;
    while ( @args )
    {
      my $pidfile=  shift @args;        # FIXME not used here....
      my $sockfile= shift @args;

      if ( -f $sockfile )
      {

        # FIXME wrong log.....
        # FIXME, stderr.....
        # Shutdown time must be high as slave may be in reconnect
        my $opts= 
          [
           "--no-defaults",
           "-uroot",
           "--socket=$sockfile",
           "--connect_timeout=5",
           "--shutdown_timeout=70",
           "shutdown",
         ];
        # We don't wait for termination of mysqladmin
        mtr_spawn($::exe_mysqladmin, $opts,
                  "", $::path_manager_log, $::path_manager_log, "");
      }
    }
  }

  # Wait for them all to remove their socket file

 SOCKREMOVED:
  for (my $loop= $::opt_sleep_time_for_delete; $loop; $loop--)
  {
    my $sockfiles_left= 0;
    my @args= @$spec;
    while ( @args )
    {
      my $pidfile=  shift @args;
      my $sockfile= shift @args;
      if ( -f $sockfile or -f $pidfile )
      {
        $sockfiles_left++;              # Could be that pidfile is left
      }
    }
    if ( ! $sockfiles_left )
    {
      last SOCKREMOVED;
    }
    if ( $loop > 1 )
    {
      sleep(1);                 # One second
    }
  }

  # We may have killed all that left a socket, but we are not sure we got
  # them all killed. We now check the PID file, if any

  # Try nice kill with SIG_TERM

  {
    my @args= @$spec;
    while ( @args )
    {
      my $pidfile=  shift @args;
      my $sockfile= shift @args;
      if (-f $pidfile)
      {
        my $pid= mtr_get_pid_from_file($pidfile);
        mtr_warning("process $pid not cooperating with mysqladmin, " .
                    "will send TERM signal to process");
        kill(15,$pid);          # SIG_TERM
      }
    }
  }

  # Wait for them all to die

  for (my $loop= $::opt_sleep_time_for_delete; $loop; $loop--)
  {
    my $pidfiles_left= 0;
    my @args= @$spec;
    while ( @args )
    {
      my $pidfile=  shift @args;
      my $sockfile= shift @args;
      if ( -f $pidfile )
      {
        $pidfiles_left++;
      }
    }
    if ( ! $pidfiles_left )
    {
      return;
    }
    if ( $loop > 1 )
    {
      sleep(1);                 # One second
    }
  }

  # Try hard kill with SIG_KILL

  {
    my @args= @$spec;
    while ( @args )
    {
      my $pidfile=  shift @args;
      my $sockfile= shift @args;
      if (-f $pidfile)
      {
        my $pid= mtr_get_pid_from_file($pidfile);
        mtr_warning("$pid did not die from TERM signal, ",
                    "will send KILL signal to process");
        kill(9,$pid);
      }
    }
  }

  # We check with Perl "kill 0" if process still exists

 PIDFILES:
  for (my $loop= $::opt_sleep_time_for_delete; $loop; $loop--)
  {
    my $not_terminated= 0;
    my @args= @$spec;
    while ( @args )
    {
      my $pidfile=  shift @args;
      my $sockfile= shift @args;
      if (-f $pidfile)
      {
        my $pid= mtr_get_pid_from_file($pidfile);
        if ( ! kill(0,$pid) )
        {
          $not_terminated++;
          mtr_warning("could't kill $pid");
        }
      }
    }
    if ( ! $not_terminated )
    {
      last PIDFILES;
    }
    if ( $loop > 1 )
    {
      sleep(1);                 # One second
    }
  }

  {
    my $pidfiles_left= 0;
    my @args= @$spec;
    while ( @args )
    {
      my $pidfile=  shift @args;
      my $sockfile= shift @args;
      if ( -f $pidfile )
      {
        if ( ! unlink($pidfile) )
        {
          $pidfiles_left++;
          mtr_warning("could't delete $pidfile");
        }
      }
    }
    if ( $pidfiles_left )
    {
      mtr_error("one or more pid files could not be deleted");
    }
  }

  # FIXME We just assume they are all dead, we don't know....
}


1;