mirror of
https://github.com/MariaDB/server.git
synced 2025-08-20 05:03:09 +03:00
BUILD-CMAKE: WL#5161 : Documentation on how to build with CMake on Unix/Windows BUILD/Makefile.am: Add new file BUILD/autorun.sh: WL#5161 : use choose_configure instead of autotools configure script (choose configure will call cmake if cmake is available) BUILD/choose_configure.sh: WL#5161 : use choose_configure instead of autotools configure script (choose configure will call cmake if cmake is available) CMakeLists.txt: WL#5161 : Rewrite top-level CMakeLists.txt. Remove Windows specifics - compiler flags handling moved to configure.cmake - storage engine/plugin stuff moved into cmake/plugin.cmake - copy docs Makefile.am: Added new files client/CMakeLists.txt: WL#5161 : Rewrite CMakeLists.txt to be platform-independent Handle packagng (add INSTALL commands) cmake/Makefile.am: WL#5161 : use choose_configure instead of autotools configure script (choose configure will call cmake if cmake is available) cmake/abi_check.cmake: Custom targets for abi_check (for cmake) cmake/bison.cmake: - Check bison availability - Add RUN_BISON macro (used to create sql_yacc.cc and sql_yacc.h) cmake/cat.cmake: Add helper script to concatenate files. cmake/character_sets.cmake: Handle configuration parameters WITH_EXTRA_CHARSETS cmake/check_minimal_version.cmake: Helper script to check the minimum required version of cmake cmake/configure.pl: Add perl script to convert ./configure parameters for cmake cmake/create_initial_db.cmake.in: Add script helper to create initial database. (on Windows, we pack initial db with the redistribution package) cmake/do_abi_check.cmake: Perform abi check cmake/dtrace.cmake: Handle dtrace in CMake Build. Check for dtrace availablility, run dtrace -G on solaris in prelink step cmake/dtrace_prelink.cmake: Run dtrace -G on Solaris in pre-link step, link the object it creates together with library or executable cmake/install_macros.cmake: Helper macros for packaging (install pdb on Windows, install symlinks on Unix) cmake/make_dist.cmake.in: "make dist" - - pack autotools ./configure script with the source (renamed to configure.am) - pack bison output cmake/merge_archives_unix.cmake.in: script to merge static libraries on Unix cmake/misc.cmake: Build helper macros - MERGE_STATIC_LIBS We use it when building client library and embedded (avoid recompilation) - Convert source file paths to absolute names. We use it in to locate files of a different project, when the files need to be recompiled (e.g in embedded several storage engines are recompiled with -DEMBEDDED_LIBRARY) cmake/mysql_version.cmake: Extract version info from configure.in Handle package names. cmake/plugin.cmake: Rewrote storage/mysql_storage_engine.cmake to handle other types of plugins and do it in OS-independent manner. cmake/readline.cmake: Macros to handle WITH_READLINE/WITH_LIBEDIT parameters cmake/ssl.cmake: Add macros to handle WITH_SSL parameter. cmake/stack_direction.c: Helper to check stack direction. cmake/zlib.cmake: Add macros to handle WITH_ZLIB parameter cmd-line-utils/libedit/CMakeLists.txt: Build libedit with cmake. cmd-line-utils/libedit/Makefile.am: Add new file cmd-line-utils/readline/CMakeLists.txt: Build readline with CMake. cmd-line-utils/readline/Makefile.am: Add new file config.h.cmake: WL#5161 : Add config.h template for cmake configure.cmake: WL#5161 : Add platform tests ( for cmake) configure.in: Added new subdirectories dbug/CMakeLists.txt: WL#5161 extra/CMakeLists.txt: WL#5161 extra/yassl/CMakeLists.txt: WL#5161 extra/yassl/taocrypt/CMakeLists.txt: WL#5161 include/Makefile.am: Add new file include/keycache.h: remove configure-win.h and remove HUGE_PTR defined there. include/my_global.h: use my_config.h for Windows, not config-win.h anymore include/my_pthread.h: - Move thread_safe_increment from config-win.h to other headers (config-win.h is not used anymore) - Declare pthread_cancel on Windows (it is used in daemon_example) include/my_sys.h: Add malloc.h on Windows (we use -D_WIN32_LEAN_AND_MEAN now, and with this define malloc.h is not included automatically via windows.h) include/mysql/plugin.h: Handle pure-C plugins with Microsoft compiler. include/thr_alarm.h: remove rf_SetTimer that used to be defined in config-win.h Replace with UINT_PTR (we do not use config-win.h anymore and typedef was needed in this single place only) libmysql/CMakeLists.txt: Avoid pointless recompilation of source files in client library if possible. Merge static libs (dbug, mysys) to create static client library. libmysqld/CMakeLists.txt: Avoid pointless recompilation of source files when building embedded. Instead, merge dbug and mysys (and some other static libs) into embedded. libmysqld/examples/CMakeLists.txt: Embedded compilation on Unix libmysqld/lib_sql.cc: Do not define THD::clear_error() in lib_sql.cc for embedded. Instead, use the same inline definition from sql_class.h as in none-embedded case (fixes duplicate symbol errors on Windows and removes pointless #ifdef EMBEDDED) man/CMakeLists.txt: Install man files. man/Makefile.am: Add new file. mysql-test/CMakeLists.txt: Install mysql-test files mysql-test/Makefile.am: Add new files mysql-test/lib/My/ConfigFactory.pm: Allow testing with mtr in out-of-source builds. mysql-test/lib/My/Find.pm: the build configurations are now also available on Unix Xcode on Mac uses the Release, RelwithDebinfo and Debug subdirectories for executables. Earlier, build configurations were available only on Windows. mysql-test/lib/My/SafeProcess.pm: Allow testing with mtr in out-of-source builds. mysql-test/lib/My/SafeProcess/CMakeLists.txt: Port CMakeLists.txt to Unix mysql-test/lib/My/SafeProcess/safe_kill_win.cc: add stdlib.h (to be able to compile with -DWIN32_LEAN_AND_MEAN) mysql-test/lib/My/SafeProcess/safe_process_win.cc: Add stdlib.h (to be able to compile with -DWIN32_LEAN_AND_MEAN) define JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE if not defined (can happen using MinGW compiler that comes with old SDK) mysql-test/mtr.out-of-source: Allow testing with mtr in out-of-source builds. mysql-test/mysql-test-run.pl: Allow testing with mtr in out-of-source builds. Use common find_plugin macro for Windows and unix. mysql-test/t/fulltext_plugin.test: This test can now run on Windows as well. mysys/CMakeLists.txt: Port CMakeLists.txt to Unix mysys/my_create.c: config-win.h is dead => NO_OPEN_3 is never defined. mysys/my_getsystime.c: config-win.h is dead => define OFFSET_TO_EPOCH where it is used. mysys/my_winthread.c: Add win32 pthread_cancel - used by daemon_example mysys/mysys_priv.h: config-win.h is dead => include <sys/stat.h> where it is used fix prototype of my_win_(f)stat plugin/daemon_example/CMakeLists.txt: Compile daemon_example with CMake plugin/daemon_example/Makefile.am: Add new file plugin/fulltext/CMakeLists.txt: Compile full-text example with CMake plugin/fulltext/Makefile.am: Add new file. plugin/semisync/CMakeLists.txt: Fix semisync to use common macro for plugins. regex/CMakeLists.txt: Use absolute filenames, when adding regex library (we recompile files in embedded, and want to locate sources via GET_TARGET_PROPERTY( ... SOURCES ..)) regex/regex2.h: Remove pointless typedef (produces error with MinGW compiler) scripts/CMakeLists.txt: Add configure/install for scripts sql-bench/CMakeLists.txt: install sql-bench files sql-bench/Makefile.am: Add new file sql/CMakeLists.txt: Port CmakeLists.txt to Unix sql/nt_servc.cc: compile server with -DWIN32_LEAN_AND_MEAN sql/share/CMakeLists.txt: Install charsets sql/share/Makefile.am: Add new file sql/sql_builtin.cc.in: Handle pure-C plugins on Windows. sql/sql_class.h: Use the same clear_error macro in embedded and not embedded. Fixes pointless #ifdef and avoids duplicate symbols when linking on Windows. storage/Makefile.am: storage/mysql_storage_engine.cmake => cmake/plugin.cmake storage/archive/CMakeLists.txt: Add names for static and dynamic plugin libraries. Link archive with zlib storage/blackhole/CMakeLists.txt: Add names for static and dynamic storage engine libraries storage/csv/CMakeLists.txt: Add names for static and dynamic storage engine libraries storage/example/CMakeLists.txt: Add names for static and dynamic storage engine libraries storage/federated/CMakeLists.txt: Add names for static and dynamic storage engine libraries storage/heap/CMakeLists.txt: Add names for static and dynamic storage engine libraries storage/ibmdb2i/CMakeLists.txt: Better port for ibmdb2i plugin storage/innobase/CMakeLists.txt: Run system checks. Add names for static and dynamic storage engine libraries. storage/innobase/include/page0page.ic: Fix compile error on OpenSolaris. storage/myisam/CMakeLists.txt: Port CmakeLists.txt to Unix storage/myisammrg/CMakeLists.txt: Add names for static and dynamic storage engine libraries storage/mysql_storage_engine.cmake: storage/mysql_storage_engine.cmake => cmake/plugin.cmake support-files/CMakeLists.txt: Configure and install some files from support-files. support-files/Makefile.am: Add new file tests/CMakeLists.txt: In general case, mysqlclient library can be dependent on C++ runtime(if it includes yassl and is not compiled with gcc or MSVC) unittest/mysys/CMakeLists.txt: Add unit tests unittest/mysys/Makefile.am: Add new file unittest/mytap/CMakeLists.txt: Add library for unit tests unittest/mytap/Makefile.am: Add new file unittest/mytap/tap.c: fix function definitions to match declarations win/create_def_file.js: Fix link error with intel compiler (icl defines of special label for exception handler)
625 lines
14 KiB
Perl
625 lines
14 KiB
Perl
# -*- 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 Carp;
|
|
use POSIX qw(WNOHANG);
|
|
|
|
use My::SafeProcess::Base;
|
|
use base 'My::SafeProcess::Base';
|
|
|
|
use My::Find;
|
|
use My::Platform;
|
|
|
|
my %running;
|
|
my $_verbose= 0;
|
|
|
|
END {
|
|
# Kill any children still running
|
|
for my $proc (values %running){
|
|
if ( $proc->is_child($$) ){
|
|
#print "Killing: $proc\n";
|
|
if ($proc->wait_one(0)){
|
|
$proc->kill();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
sub is_child {
|
|
my ($self, $parent_pid)= @_;
|
|
croak "usage: \$safe_proc->is_child()" unless (@_ == 2 and ref $self);
|
|
return ($self->{PARENT} == $parent_pid);
|
|
}
|
|
|
|
|
|
# Find the safe process binary or script
|
|
my @safe_process_cmd;
|
|
my $safe_kill;
|
|
my $bindir;
|
|
if(defined $ENV{MTR_BINDIR})
|
|
{
|
|
# This is an out-of-source build. Build directory
|
|
# is given in MTR_BINDIR env.variable
|
|
$bindir = $ENV{MTR_BINDIR}."/mysql-test";
|
|
}
|
|
else
|
|
{
|
|
$bindir = ".";
|
|
}
|
|
|
|
if (IS_WIN32PERL or IS_CYGWIN){
|
|
# Use my_safe_process.exe
|
|
my $exe= my_find_bin($bindir, ["lib/My/SafeProcess", "My/SafeProcess"],
|
|
"my_safe_process");
|
|
push(@safe_process_cmd, $exe);
|
|
|
|
# Use my_safe_kill.exe
|
|
$safe_kill= my_find_bin($bindir, "lib/My/SafeProcess", "my_safe_kill");
|
|
}
|
|
else
|
|
{
|
|
# Use my_safe_process
|
|
my $exe= my_find_bin($bindir, ["lib/My/SafeProcess", "My/SafeProcess"],
|
|
"my_safe_process");
|
|
push(@safe_process_cmd, $exe);
|
|
}
|
|
|
|
|
|
sub new {
|
|
my $class= shift;
|
|
|
|
my %opts=
|
|
(
|
|
verbose => 0,
|
|
@_
|
|
);
|
|
|
|
my $path = delete($opts{'path'}) or croak "path required @_";
|
|
my $args = delete($opts{'args'}) or croak "args required @_";
|
|
my $input = delete($opts{'input'});
|
|
my $output = delete($opts{'output'});
|
|
my $error = delete($opts{'error'});
|
|
my $verbose = delete($opts{'verbose'});
|
|
my $nocore = delete($opts{'nocore'});
|
|
my $host = delete($opts{'host'});
|
|
my $shutdown = delete($opts{'shutdown'});
|
|
my $user_data= delete($opts{'user_data'});
|
|
|
|
# if (defined $host) {
|
|
# $safe_script= "lib/My/SafeProcess/safe_process_cpcd.pl";
|
|
# }
|
|
|
|
if (IS_CYGWIN){
|
|
$path= mixed_path($path);
|
|
$input= mixed_path($input);
|
|
$output= mixed_path($output);
|
|
$error= mixed_path($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;
|
|
push(@safe_args, "--nocore") if $nocore;
|
|
|
|
# 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= 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 => $pid, # Inidicates this is always a real process
|
|
SAFE_NAME => $name,
|
|
SAFE_SHUTDOWN => $shutdown,
|
|
PARENT => $$,
|
|
SAFE_USER_DATA => $user_data,
|
|
}, $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 croak "duration required";
|
|
my $parent_pid= $$;
|
|
|
|
my $pid= My::SafeProcess::Base::_safe_fork();
|
|
if ($pid){
|
|
# Parent
|
|
my $proc= bless
|
|
({
|
|
SAFE_PID => $pid,
|
|
SAFE_NAME => "timer",
|
|
PARENT => $$,
|
|
}, $class);
|
|
|
|
# Put the new process in list of running
|
|
$running{$pid}= $proc;
|
|
return $proc;
|
|
}
|
|
|
|
# Child, install signal handlers and sleep for "duration"
|
|
$SIG{INT}= 'IGNORE';
|
|
|
|
$SIG{TERM}= sub {
|
|
#print STDERR "timer $$: woken up, exiting!\n";
|
|
exit(0);
|
|
};
|
|
|
|
$0= "safe_timer($duration)";
|
|
|
|
if (IS_WIN32PERL){
|
|
# Just a thread in same process
|
|
sleep($duration);
|
|
print STDERR "timer $$: expired after $duration seconds\n";
|
|
exit(0);
|
|
}
|
|
|
|
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= @_;
|
|
_verbose("shutdown, timeout: $shutdown_timeout, @processes");
|
|
|
|
return if (@processes == 0);
|
|
|
|
# Call shutdown function if process has one, else
|
|
# use kill
|
|
foreach my $proc (@processes){
|
|
_verbose(" proc: $proc");
|
|
my $shutdown= $proc->{SAFE_SHUTDOWN};
|
|
if ($shutdown_timeout > 0 and defined $shutdown){
|
|
$shutdown->();
|
|
$proc->{WAS_SHUTDOWN}= 1;
|
|
}
|
|
else {
|
|
$proc->start_kill();
|
|
}
|
|
}
|
|
|
|
my @kill_processes= ();
|
|
|
|
# Wait max shutdown_timeout seconds for those process
|
|
# that has been shutdown
|
|
foreach my $proc (@processes){
|
|
next unless $proc->{WAS_SHUTDOWN};
|
|
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;
|
|
}
|
|
|
|
# Wait infinitely for those process
|
|
# that has been killed
|
|
foreach my $proc (@processes){
|
|
next if $proc->{WAS_SHUTDOWN};
|
|
my $ret= $proc->wait_one(undef);
|
|
if ($ret != 0) {
|
|
warn "Wait for killed process failed!";
|
|
push(@kill_processes, $proc);
|
|
# Try one more time, best option...
|
|
}
|
|
}
|
|
|
|
# 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(undef);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
sub _winpid ($) {
|
|
my ($pid)= @_;
|
|
|
|
# In win32 perl, the pid is already the winpid
|
|
return $pid unless IS_CYGWIN;
|
|
|
|
# In cygwin, the pid is the pseudo process ->
|
|
# get the real winpid of my_safe_process
|
|
return Cygwin::pid_to_winpid($pid);
|
|
}
|
|
|
|
|
|
#
|
|
# Tell the process to die as fast as possible
|
|
#
|
|
sub start_kill {
|
|
my ($self)= @_;
|
|
croak "usage: \$safe_proc->start_kill()" unless (@_ == 1 and ref $self);
|
|
_verbose("start_kill: $self");
|
|
my $ret= 1;
|
|
|
|
my $pid= $self->{SAFE_PID};
|
|
die "INTERNAL ERROR: no pid" unless defined $pid;
|
|
|
|
if (IS_WINDOWS and defined $self->{SAFE_WINPID})
|
|
{
|
|
die "INTERNAL ERROR: no safe_kill" unless defined $safe_kill;
|
|
|
|
my $winpid= _winpid($pid);
|
|
$ret= system($safe_kill, $winpid) >> 8;
|
|
|
|
if ($ret == 3){
|
|
print "Couldn't open the winpid: $winpid ",
|
|
"for pid: $pid, try one more time\n";
|
|
sleep(1);
|
|
$winpid= _winpid($pid);
|
|
$ret= system($safe_kill, $winpid) >> 8;
|
|
print "Couldn't open the winpid: $winpid ",
|
|
"for pid: $pid, continue and see what happens...\n";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$pid= $self->{SAFE_PID};
|
|
die "Can't kill not started process" unless defined $pid;
|
|
$ret= kill("TERM", $pid);
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
|
|
sub dump_core {
|
|
my ($self)= @_;
|
|
return if IS_WINDOWS;
|
|
my $pid= $self->{SAFE_PID};
|
|
die "Can't cet core from not started process" unless defined $pid;
|
|
_verbose("Sending ABRT to $self");
|
|
kill ("ABRT", $pid);
|
|
return 1;
|
|
}
|
|
|
|
|
|
#
|
|
# Kill the process as fast as possible
|
|
# and wait for it to return
|
|
#
|
|
sub kill {
|
|
my ($self)= @_;
|
|
croak "usage: \$safe_proc->kill()" unless (@_ == 1 and ref $self);
|
|
|
|
$self->start_kill();
|
|
$self->wait_one();
|
|
return 1;
|
|
}
|
|
|
|
|
|
sub _collect {
|
|
my ($self)= @_;
|
|
|
|
$self->{EXIT_STATUS}= $?;
|
|
_verbose("_collect: $self");
|
|
|
|
# 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)= @_;
|
|
croak "usage: \$safe_proc->wait_one([timeout])" unless ref $self;
|
|
|
|
_verbose("wait_one $self, $timeout");
|
|
|
|
if ( ! defined($self->{SAFE_PID}) ) {
|
|
# No pid => not running
|
|
_verbose("No pid => not running");
|
|
return 0;
|
|
}
|
|
|
|
if ( defined $self->{EXIT_STATUS} ) {
|
|
# Exit status already set => not running
|
|
_verbose("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;
|
|
}
|
|
#_verbose("blocking: $blocking, use_alarm: $use_alarm");
|
|
|
|
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
|
|
_verbose("Got timeout");
|
|
return 1;
|
|
}
|
|
# Got pid _and_ alarm, continue
|
|
_verbose("Got pid and alarm, continue");
|
|
}
|
|
|
|
if ( $retpid == 0 ) {
|
|
# 0 => still running
|
|
_verbose("0 => still running");
|
|
return 1;
|
|
}
|
|
|
|
if ( not $blocking and $retpid == -1 ) {
|
|
# still running
|
|
_verbose("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);
|
|
}
|
|
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: $ret_pid in running list\n";
|
|
print STDERR "running: ". join(", ", keys(%running)). "\n";
|
|
return undef;
|
|
}
|
|
$proc->_collect;
|
|
return $proc;
|
|
}
|
|
|
|
|
|
#
|
|
# Wait for all processes to exit
|
|
#
|
|
sub wait_all {
|
|
while(keys %running)
|
|
{
|
|
wait_any();
|
|
}
|
|
}
|
|
|
|
|
|
#
|
|
# Check if any process has exited, but don't wait.
|
|
#
|
|
# Returns a reference to the SafeProcess that
|
|
# exited or undefined
|
|
#
|
|
sub check_any {
|
|
for my $proc (values %running){
|
|
if ( $proc->is_child($$) ) {
|
|
if (not $proc->wait_one(0)) {
|
|
_verbose ("Found exited $proc");
|
|
return $proc;
|
|
}
|
|
}
|
|
}
|
|
return undef;
|
|
}
|
|
|
|
|
|
# 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.= "]";
|
|
}
|
|
|
|
sub _verbose {
|
|
return unless $_verbose;
|
|
print STDERR " ## ", @_, "\n";
|
|
}
|
|
|
|
|
|
sub pid {
|
|
my ($self)= @_;
|
|
return $self->{SAFE_PID};
|
|
}
|
|
|
|
sub user_data {
|
|
my ($self)= @_;
|
|
return $self->{SAFE_USER_DATA};
|
|
}
|
|
|
|
|
|
1;
|