summaryrefslogtreecommitdiff
path: root/mysql-test/lib/My/SafeProcess.pm
blob: ef3ba9146d1c0c089f662b37bc32237bc08ecb18 (plain)
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# -*- cperl -*-
# Copyright (C) 2004-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

package My::SafeProcess;

#
# Class that encapsulates process creation, monitoring and cleanup
#
# Spawns a monitor process which spawns a new process locally or
# remote using subclasses My::Process::Local or My::Process::Remote etc.
#
# The monitor process runs a simple event loop more or less just
# waiting for a reason to zap the process it monitors. Thus the user
# of this class does not need to care about process cleanup, it's
# handled automatically.
#
# The monitor process wait for:
#  - the parent process to close the pipe, in that case it
#    will zap the "monitored process" and exit
#  - the "monitored process" to exit, in which case it will exit
#    itself with same exit code as the "monitored process"
#  - the parent process to send the "shutdown" signal in wich case
#    monitor will kill the "monitored process" hard and exit
#
#
# When used it will look something like this:
# $> ps
#  [script.pl]
#   - [monitor for `mysqld`]
#     - [mysqld]
#   - [monitor for `mysqld`]
#     - [mysqld]
#   - [monitor for `mysqld`]
#     - [mysqld]
#
#

use strict;

use POSIX qw(WNOHANG);

use My::SafeProcess::Base;
use base 'My::SafeProcess::Base';

use My::Find;

my %running;

BEGIN {
  if ($^O eq "MSWin32") {
    eval 'sub IS_WIN32PERL () { 1 }';
  }
  else {
    eval 'sub IS_WIN32PERL () { 0 }';
  }
  if ($^O eq "cygwin") {
    eval 'sub IS_CYGWIN () { 1 }';
    # Make sure cygpath works
    if ((system("cygpath > /dev/null 2>&1") >> 8) != 1){
      die "Could not execute 'cygpath': $!";
    }
    eval 'sub fixpath {
            my ($path)= @_;
            return unless defined $path;
            $path= `cygpath -w $path`;
            chomp $path;
            return $path;
          }';
  }
  else {
    eval 'sub IS_CYGWIN () { 0 }';
  }
}

# Find the safe process binary or script
my @safe_process_cmd;
my $safe_kill;
if (IS_WIN32PERL or IS_CYGWIN){
  # Use my_safe_process.exe
  my $exe= my_find_bin(".", ["lib/My/SafeProcess", "My/SafeProcess"],
		       "my_safe_process");
  die "Could not find my_safe_process" unless $exe;
  push(@safe_process_cmd, $exe);

  # Use my_safe_kill.exe
  my $safe_kill= my_find_bin(".", "lib/My/SafeProcess", "my_safe_kill");
  die "Could not find my_safe_kill" unless $safe_kill;
}
else
{
  my $use_safe_process_binary= 1;
  if ($use_safe_process_binary) {
    # Use my_safe_process
    my $exe= my_find_bin(".", ["lib/My/SafeProcess", "My/SafeProcess"],
			 "my_safe_process");
    die "Could not find my_safe_process" unless $exe;
    push(@safe_process_cmd, $exe);
  }
  else
  {
    # Use safe_process.pl
    my $script=  "lib/My/SafeProcess/safe_process.pl";
    $script= "../$script" unless -f $script;
    die "Could not find safe_process.pl" unless -f $script;

    # Call $script with Perl interpreter
    push(@safe_process_cmd, $^X, $script);
  }
}


sub new {
  my $class= shift;

  my %opts=
    (
     verbose     => 0,
     @_
    );

  my $path     = delete($opts{'path'})    or die "path required";
  my $args     = delete($opts{'args'})    or die "args required";
  my $input    = delete($opts{'input'});
  my $output   = delete($opts{'output'});
  my $error    = delete($opts{'error'});
  my $verbose  = delete($opts{'verbose'});
  my $host     = delete($opts{'host'});
  my $shutdown = delete($opts{'shutdown'});

#  if (defined $host) {
#    $safe_script=  "lib/My/SafeProcess/safe_process_cpcd.pl";
#  }

  if (IS_CYGWIN){
    # safe_procss is a windows program and need
    # windows paths
    $path= fixpath($path);
    $input= fixpath($input);
    $output= fixpath($output);
    $error= fixpath($error);
  }

  my @safe_args;
  my ($safe_path, $safe_script)= @safe_process_cmd;
  push(@safe_args, $safe_script) if defined $safe_script;

  push(@safe_args, "--verbose") if $verbose > 0;

  # Point the safe_process at the right parent if running on cygwin
  push(@safe_args, "--parent-pid=".Cygwin::pid_to_winpid($$)) if IS_CYGWIN;

  push(@safe_args, "--");
  push(@safe_args, $path); # The program safe_process should execute
  push(@safe_args, @$$args);

  print "### safe_path: ", $safe_path, " ", join(" ", @safe_args), "\n"
    if $verbose > 1;

  my ($pid, $winpid)= create_process(
			  path      => $safe_path,
			  input     => $input,
			  output    => $output,
			  error     => $error,
                          append    => $opts{append},
			  args      => \@safe_args,
			 );

  my $name     = delete($opts{'name'}) || "SafeProcess$pid";
  my $proc= bless
    ({
      SAFE_PID  => $pid,
      SAFE_WINPID  => $winpid,
      SAFE_NAME => $name,
      SAFE_SHUTDOWN => $shutdown,
     }, $class);

  # Put the new process in list of running
  $running{$pid}= $proc;
  return $proc;

}


sub run {
  my $proc= new(@_);
  $proc->wait_one();
  return $proc->exit_status();
}

#
# Start a process that returns after "duration" seconds
# or when it's parent process does not exist anymore
#
sub timer {
  my $class= shift;
  my $duration= shift or die "duration required";
  my $parent_pid= $$;

  my $pid= My::SafeProcess::Base::_safe_fork();
  if ($pid){
    # Parent
    my $proc= bless
      ({
	SAFE_PID  => $pid,
	SAFE_NAME => "timer",
       }, $class);

    # Put the new process in list of running
    $running{$pid}= $proc;
    return $proc;
  }

  # Child, install signal handlers and sleep for "duration"
  $SIG{INT}= 'DEFAULT';

  $SIG{TERM}= sub {
    #print STDERR "timer $$: woken up, exiting!\n";
    exit(0);
  };

  $0= "safe_timer($duration)";
  my $count_down= $duration;
  while($count_down--){

    # Check that parent is still alive
    if (kill(0, $parent_pid) == 0){
      #print STDERR "timer $$: parent gone, exiting!\n";
      exit(0);
    }

    sleep(1);
  }
  print STDERR "timer $$: expired after $duration seconds\n";
  exit(0);
}


#
# Shutdown process nicely, and wait for shutdown_timeout seconds
# If processes hasn't shutdown, kill them hard and wait for return
#
sub shutdown {
  my $shutdown_timeout= shift;
  my @processes= @_;

  return if (@processes == 0);

  #print "shutdown: @processes\n";

  # Call shutdown function if process has one, else
  # use kill
  foreach my $proc (@processes){
    my $shutdown= $proc->{SAFE_SHUTDOWN};
    if ($shutdown_timeout > 0 and defined $shutdown){
      $shutdown->();
    }
    else {
      $proc->start_kill();
    }
  }

  my @kill_processes= ();

  # Wait for shutdown_timeout for processes to exit
  foreach my $proc (@processes){
    my $ret= $proc->wait_one($shutdown_timeout);
    if ($ret != 0) {
      push(@kill_processes, $proc);
    }
    # Only wait for the first process with shutdown timeout
    $shutdown_timeout= 0;
  }

  # Return if all servers has exited
  return if (@kill_processes == 0);

  foreach my $proc (@kill_processes){
    $proc->start_kill();
  }

  foreach my $proc (@kill_processes){
    $proc->wait_one();
  }
  return;
}


#
# Tell the process to die as fast as possible
#
sub start_kill {
  my ($self)= @_;
  die "usage: \$safe_proc->start_kill()" unless (@_ == 1 and ref $self);
  #print "start_kill $self\n";

  if (defined $safe_kill and $self->{SAFE_WINPID}){
    # Use my_safe_kill to tell my_safe_process
    # it's time to kill it's child and return
    my $pid= $self->{SAFE_WINPID};
    my $ret= system($safe_kill, $pid);
    #print STDERR "start_kill, safe_killed $pid, ret: $ret\n";
  } else {
    my $pid= $self->{SAFE_PID};
    die "Can't kill not started process" unless defined $pid;
    my $ret= kill(15, $pid);
    #print STDERR "start_kill, sent signal 15 to $pid, ret: $ret\n";
  }
  return 1;
}


#
# Kill the process as fast as possible
# and wait for it to return
#
sub kill {
  my ($self)= @_;
  die "usage: \$safe_proc->kill()" unless (@_ == 1 and ref $self);

  $self->start_kill();
  $self->wait_one();
  return 1;
}


sub _collect {
  my ($self)= @_;

  #print "_collect\n";
  $self->{EXIT_STATUS}= $?;

  # Take the process out of running list
  my $pid= $self->{SAFE_PID};
  die unless delete($running{$pid});
}


# Wait for process to exit
# optionally with a timeout
#
# timeout
#   undef -> wait blocking infinitely
#   0     -> just poll with WNOHANG
#   >0    -> wait blocking for max timeout seconds
#
# RETURN VALUES
#  0 Not running
#  1 Still running
#
sub wait_one {
  my ($self, $timeout)= @_;
  die "usage: \$safe_proc->wait_one([timeout])" unless ref $self;

  #print "wait_one $self, $timeout\n";

  if ( ! defined($self->{SAFE_PID}) ) {
    # No pid => not running
    return 0;
  }

  if ( defined $self->{EXIT_STATUS} ) {
    # Exit status already set => not running
    return 0;
  }

  my $pid= $self->{SAFE_PID};

  my $use_alarm;
  my $blocking;
  if (defined $timeout)
  {
    if ($timeout == 0)
    {
      # 0 -> just poll with WNOHANG
      $blocking= 0;
      $use_alarm= 0;
    }
    else
    {
      # >0 -> wait blocking for max timeout seconds
      $blocking= 1;
      $use_alarm= 1;
    }
  }
  else
  {
    # undef -> wait blocking infinitely
    $blocking= 1;
    $use_alarm= 0;
  }

  my $retpid;
  eval
  {
    # alarm should break the wait
    local $SIG{ALRM}= sub { die "waitpid timeout"; };

    alarm($timeout) if $use_alarm;

    $retpid= waitpid($pid, $blocking ? 0 : &WNOHANG);

    alarm(0) if $use_alarm;
  };

  if ($@)
  {
    die "Got unexpected: $@" if ($@ !~ /waitpid timeout/);
    if (!defined $retpid) {
      # Got timeout
      return 1;
    }
    # Got pid _and_ alarm, continue
  }

  if ( $retpid == 0 ) {
    # 0 => still running
    return 1;
  }

  if ( not $blocking and $retpid == -1 ) {
    # still running
    return 1;
  }

  warn "wait_one: expected pid $pid but got $retpid"
    unless( $retpid == $pid );

  $self->_collect();
  return 0;
}


#
# Wait for any process to exit
#
# Returns a reference to the SafeProcess that
# exited or undefined
#
sub wait_any {
  my $ret_pid;
  if (IS_WIN32PERL) {
    # Can't wait for -1 => use a polling loop
    do {
      Win32::Sleep(10); # 10 milli seconds
      foreach my $pid (keys %running){
	$ret_pid= waitpid($pid, &WNOHANG);
	last if $pid == $ret_pid;
      }
    } while ($ret_pid == 0);

    # Special processig of return code
    # since negative pids are valid
    if ($ret_pid == 0 or $ret_pid == -1) {
      print STDERR "wait_any, got invalid pid: $ret_pid\n";
      return undef;
    }
  }
  else
  {
    $ret_pid= waitpid(-1, 0);
    if ($ret_pid <= 0){
      # No more processes to wait for
      print STDERR "wait_any, got invalid pid: $ret_pid\n";
      return undef;
    }
  }

  # Look it up in "running" table
  my $proc= $running{$ret_pid};
  unless (defined $proc){
    print STDERR "Could not find pid in running list\n";
    print STDERR "running: ". join(", ", keys(%running)). "\n";
    return undef;
  }
  $proc->_collect;
  return $proc;
}

#
# Overload string operator
# and fallback to default functions if no
# overloaded function is found
#
use overload
  '""' => \&self2str,
  fallback => 1;


#
# Return the process as a nicely formatted string
#
sub self2str {
  my ($self)= @_;
  my $pid=  $self->{SAFE_PID};
  my $winpid=  $self->{SAFE_WINPID};
  my $name= $self->{SAFE_NAME};
  my $exit_status= $self->{EXIT_STATUS};

  my $str= "[$name - pid: $pid";
  $str.= ", winpid: $winpid"      if defined $winpid;
  $str.= ", exit: $exit_status"   if defined $exit_status;
  $str.= "]";
}


1;