00001
00002
00003
00004 #include "ace/Process_Manager.h"
00005
00006 #if !defined (__ACE_INLINE__)
00007 #include "ace/Process_Manager.inl"
00008 #endif
00009
00010 #include "ace/ACE.h"
00011 #include "ace/Guard_T.h"
00012 #include "ace/Process.h"
00013 #include "ace/Signal.h"
00014 #include "ace/Object_Manager.h"
00015 #include "ace/Log_Msg.h"
00016 #include "ace/Reactor.h"
00017 #include "ace/Countdown_Time.h"
00018 #include "ace/OS_NS_sys_wait.h"
00019 #include "ace/OS_NS_signal.h"
00020 #include "ace/OS_NS_unistd.h"
00021 #include "ace/OS_NS_sys_time.h"
00022
00023 ACE_RCSID (ace,
00024 Process_Manager,
00025 "$Id: Process_Manager.cpp 79332 2007-08-13 20:30:44Z sowayaa $")
00026
00027 #if defined (ACE_HAS_SIG_C_FUNC)
00028 extern "C" void
00029 ACE_Process_Manager_cleanup (void *instance, void *arg)
00030 {
00031 ACE_Process_Manager::cleanup (instance, arg);
00032 }
00033 #endif
00034
00035 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00036
00037 void
00038 ACE_Process_Manager::cleanup (void *, void *)
00039 {
00040 ACE_Process_Manager::close_singleton ();
00041 }
00042
00043
00044
00045
00046 #if !defined (ACE_WIN32) && !defined (ACE_LACKS_UNIX_SIGNALS)
00047 static void
00048 sigchld_nop (int, siginfo_t *, ucontext_t *)
00049 {
00050 return;
00051 }
00052 #endif
00053
00054
00055 ACE_ALLOC_HOOK_DEFINE(ACE_Process_Manager)
00056
00057
00058 ACE_Process_Manager *ACE_Process_Manager::instance_ = 0;
00059
00060
00061
00062 int ACE_Process_Manager::delete_instance_ = 0;
00063
00064 ACE_Process_Manager::Process_Descriptor::~Process_Descriptor (void)
00065 {
00066 }
00067
00068 void
00069 ACE_Process_Manager::Process_Descriptor::dump (void) const
00070 {
00071 #if defined (ACE_HAS_DUMP)
00072 ACE_TRACE ("ACE_Process_Manager::Process_Descriptor::dump");
00073
00074 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00075
00076 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nproc_id_ = %d"),
00077 this->process_->getpid( )));
00078
00079 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00080 #endif
00081 }
00082
00083 void
00084 ACE_Process_Manager::dump (void) const
00085 {
00086 #if defined (ACE_HAS_DUMP)
00087 ACE_TRACE ("ACE_Process_Manager::dump");
00088
00089 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00090
00091 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nmax_process_table_size_ = %d"), this->max_process_table_size_));
00092 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\ncurrent_count_ = %d"), this->current_count_));
00093
00094 for (size_t i = 0; i < this->current_count_; i++)
00095 this->process_table_[i].dump ();
00096
00097 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00098 #endif
00099 }
00100
00101 ACE_Process_Manager::Process_Descriptor::Process_Descriptor (void)
00102 : process_ (0),
00103 exit_notify_ (0)
00104 {
00105 ACE_TRACE ("ACE_Process_Manager::Process_Descriptor::Process_Descriptor");
00106 }
00107
00108 ACE_Process_Manager *
00109 ACE_Process_Manager::instance (void)
00110 {
00111 ACE_TRACE ("ACE_Process_Manager::instance");
00112
00113 if (ACE_Process_Manager::instance_ == 0)
00114 {
00115
00116 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00117 *ACE_Static_Object_Lock::instance (), 0));
00118
00119 if (ACE_Process_Manager::instance_ == 0)
00120 {
00121 ACE_NEW_RETURN (ACE_Process_Manager::instance_,
00122 ACE_Process_Manager,
00123 0);
00124 ACE_Process_Manager::delete_instance_ = 1;
00125
00126
00127
00128
00129
00130 #if defined ACE_HAS_SIG_C_FUNC
00131 ACE_Object_Manager::at_exit (ACE_Process_Manager::instance_,
00132 ACE_Process_Manager_cleanup,
00133 0);
00134 #else
00135 ACE_Object_Manager::at_exit (ACE_Process_Manager::instance_,
00136 ACE_Process_Manager::cleanup,
00137 0);
00138 #endif
00139
00140 }
00141 }
00142
00143 return ACE_Process_Manager::instance_;
00144 }
00145
00146 ACE_Process_Manager *
00147 ACE_Process_Manager::instance (ACE_Process_Manager *tm)
00148 {
00149 ACE_TRACE ("ACE_Process_Manager::instance");
00150 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00151 *ACE_Static_Object_Lock::instance (), 0));
00152
00153 ACE_Process_Manager *t = ACE_Process_Manager::instance_;
00154
00155 ACE_Process_Manager::delete_instance_ = 0;
00156
00157
00158
00159
00160
00161 #if defined ACE_HAS_SIG_C_FUNC
00162 ACE_Object_Manager::at_exit (ACE_Process_Manager::instance_,
00163 ACE_Process_Manager_cleanup,
00164 0);
00165 #else
00166 ACE_Object_Manager::at_exit (ACE_Process_Manager::instance_,
00167 ACE_Process_Manager::cleanup,
00168 0);
00169 #endif
00170
00171 ACE_Process_Manager::instance_ = tm;
00172 return t;
00173 }
00174
00175 void
00176 ACE_Process_Manager::close_singleton( void )
00177 {
00178 ACE_TRACE ("ACE_Process_Manager::close_singleton");
00179
00180 ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
00181 *ACE_Static_Object_Lock::instance ()));
00182
00183 if (ACE_Process_Manager::delete_instance_)
00184 {
00185 delete ACE_Process_Manager::instance_;
00186 ACE_Process_Manager::instance_ = 0;
00187 ACE_Process_Manager::delete_instance_ = 0;
00188 }
00189 }
00190
00191 int
00192 ACE_Process_Manager::resize (size_t size)
00193 {
00194 ACE_TRACE ("ACE_Process_Manager::resize");
00195
00196 if (size <= this->max_process_table_size_)
00197 return 0;
00198
00199 Process_Descriptor *temp = 0;
00200
00201 ACE_NEW_RETURN (temp,
00202 Process_Descriptor[size],
00203 -1);
00204
00205 for (size_t i = 0;
00206 i < this->current_count_;
00207 i++)
00208
00209 temp[i] = this->process_table_[i];
00210
00211 this->max_process_table_size_ = size;
00212
00213 delete [] this->process_table_;
00214
00215 this->process_table_ = temp;
00216 return 0;
00217 }
00218
00219
00220
00221 int
00222 ACE_Process_Manager::open (size_t size,
00223 ACE_Reactor *r)
00224 {
00225 ACE_TRACE ("ACE_Process_Manager::open");
00226
00227 if (r)
00228 {
00229 this->reactor (r);
00230 #if !defined (ACE_WIN32) && !defined (ACE_LACKS_UNIX_SIGNALS)
00231
00232 if (r->register_handler (SIGCHLD, this) == -1)
00233 return -1;
00234 #endif
00235 }
00236
00237 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));
00238
00239 if (this->max_process_table_size_ < size)
00240 this->resize (size);
00241 return 0;
00242 }
00243
00244
00245
00246 ACE_Process_Manager::ACE_Process_Manager (size_t size,
00247 ACE_Reactor *r)
00248 : ACE_Event_Handler (),
00249 process_table_ (0),
00250 max_process_table_size_ (0),
00251 current_count_ (0),
00252 default_exit_handler_ (0)
00253 #if defined (ACE_HAS_THREADS)
00254 , lock_ ()
00255 #endif
00256 {
00257 ACE_TRACE ("ACE_Process_Manager::ACE_Process_Manager");
00258
00259 if (this->open (size,
00260 r) == -1)
00261 ACE_ERROR ((LM_ERROR,
00262 ACE_TEXT ("%p\n"),
00263 ACE_TEXT ("ACE_Process_Manager")));
00264 }
00265
00266
00267
00268 int
00269 ACE_Process_Manager::close (void)
00270 {
00271 ACE_TRACE ("ACE_Process_Manager::close");
00272
00273 if (this->reactor () != 0)
00274 {
00275 #if !defined (ACE_WIN32) && !defined (ACE_LACKS_UNIX_SIGNALS)
00276 this->reactor ()->remove_handler (SIGCHLD, (ACE_Sig_Action *) 0);
00277 #endif
00278 this->reactor (0);
00279 }
00280
00281 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));
00282
00283 if (this->process_table_ != 0)
00284 {
00285 while (this->current_count_ > 0)
00286 this->remove_proc (0);
00287
00288 delete [] this->process_table_;
00289 this->process_table_ = 0;
00290 this->max_process_table_size_ = 0;
00291 this->current_count_ = 0;
00292 }
00293
00294 if (this->default_exit_handler_ != 0)
00295 this->default_exit_handler_->handle_close (ACE_INVALID_HANDLE,0);
00296 this->default_exit_handler_ = 0;
00297
00298 return 0;
00299 }
00300
00301 ACE_Process_Manager::~ACE_Process_Manager (void)
00302 {
00303 ACE_TRACE ("ACE_Process_Manager::~ACE_Process_Manager");
00304 this->close ();
00305 }
00306
00307 #if !defined (ACE_WIN32)
00308
00309
00310
00311
00312
00313
00314
00315 int
00316 ACE_Process_Manager::handle_input (ACE_HANDLE)
00317 {
00318 ACE_TRACE ("ACE_Process_Manager::handle_input");
00319
00320 pid_t pid;
00321
00322 do
00323 pid = this->wait (0,
00324 ACE_Time_Value::zero);
00325 while (pid != 0 && pid != ACE_INVALID_PID);
00326
00327 return 0;
00328 }
00329
00330 #endif
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 int
00341 ACE_Process_Manager::handle_signal (int,
00342 siginfo_t *si,
00343 ucontext_t *)
00344 {
00345 #if defined (ACE_WIN32)
00346 ACE_HANDLE proc = si->si_handle_;
00347 ACE_exitcode status = 0;
00348 BOOL result = ::GetExitCodeProcess (proc,
00349 &status);
00350 if (result)
00351 {
00352 if (status != STILL_ACTIVE)
00353 {
00354 {
00355 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, lock_, -1));
00356
00357 ssize_t const i = this->find_proc (proc);
00358 if (i == -1)
00359 return -1;
00360 #if 0
00361 pid_t pid = i != -1
00362 ? process_table_[i].process_->getpid ()
00363 : ACE_INVALID_PID;
00364 #endif
00365 this->notify_proc_handler (i, status);
00366 this->remove_proc (i);
00367 }
00368 return -1;
00369 }
00370 else
00371 ACE_ERROR_RETURN ((LM_ERROR,
00372 ACE_TEXT ("Process still active")
00373 ACE_TEXT (" -- shouldn't have been called yet!\n")),
00374 0);
00375 }
00376 else
00377 {
00378
00379 ACE_ERROR_RETURN ((LM_ERROR,
00380 ACE_TEXT ("GetExitCodeProcess failed")),
00381 -1);
00382 }
00383 #else
00384 ACE_UNUSED_ARG (si);
00385 return reactor ()->notify (this, ACE_Event_Handler::READ_MASK);
00386 #endif
00387 }
00388
00389 int
00390 ACE_Process_Manager::register_handler (ACE_Event_Handler *eh,
00391 pid_t pid)
00392 {
00393 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));
00394
00395 if (pid == ACE_INVALID_PID)
00396 {
00397 if (this->default_exit_handler_ != 0)
00398 this->default_exit_handler_->handle_close (ACE_INVALID_HANDLE, 0);
00399 this->default_exit_handler_ = eh;
00400 return 0;
00401 }
00402
00403 ssize_t const i = this->find_proc (pid);
00404
00405 if (i == -1)
00406 {
00407 errno = EINVAL;
00408 return -1;
00409 }
00410
00411 Process_Descriptor &proc_desc = this->process_table_[i];
00412
00413 if (proc_desc.exit_notify_ != 0)
00414 proc_desc.exit_notify_->handle_close (ACE_INVALID_HANDLE, 0);
00415 proc_desc.exit_notify_ = eh;
00416 return 0;
00417 }
00418
00419
00420
00421 pid_t
00422 ACE_Process_Manager::spawn (ACE_Process_Options &options,
00423 ACE_Event_Handler *event_handler)
00424 {
00425 ACE_Process *process = 0;
00426 ACE_NEW_RETURN (process,
00427 ACE_Managed_Process,
00428 ACE_INVALID_PID);
00429
00430 pid_t const pid = spawn (process, options, event_handler);
00431 if (pid == ACE_INVALID_PID || pid == 0)
00432 delete process;
00433
00434 return pid;
00435 }
00436
00437
00438
00439 pid_t
00440 ACE_Process_Manager::spawn (ACE_Process *process,
00441 ACE_Process_Options &options,
00442 ACE_Event_Handler *event_handler)
00443 {
00444 ACE_TRACE ("ACE_Process_Manager::spawn");
00445
00446 pid_t const pid = process->spawn (options);
00447
00448
00449 if (pid == ACE_INVALID_PID || pid == 0)
00450 return pid;
00451
00452 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex,
00453 ace_mon, this->lock_, -1));
00454
00455 if (this->append_proc (process, event_handler) == -1)
00456
00457 return ACE_INVALID_PID;
00458
00459 return pid;
00460 }
00461
00462
00463
00464 int
00465 ACE_Process_Manager::spawn_n (size_t n,
00466 ACE_Process_Options &options,
00467 pid_t *child_pids,
00468 ACE_Event_Handler *event_handler)
00469 {
00470 ACE_TRACE ("ACE_Process_Manager::spawn_n");
00471
00472 if (child_pids != 0)
00473 for (size_t i = 0;
00474 i < n;
00475 ++i)
00476 child_pids[i] = ACE_INVALID_PID;
00477
00478 for (size_t i = 0;
00479 i < n;
00480 i++)
00481 {
00482 pid_t const pid = this->spawn (options, event_handler);
00483 if (pid == ACE_INVALID_PID || pid == 0)
00484
00485 return pid;
00486 else if (child_pids != 0)
00487 child_pids[i] = pid;
00488 }
00489
00490 return 0;
00491 }
00492
00493
00494
00495
00496 int
00497 ACE_Process_Manager::append_proc (ACE_Process *proc,
00498 ACE_Event_Handler *event_handler)
00499 {
00500 ACE_TRACE ("ACE_Process_Manager::append_proc");
00501
00502
00503
00504 if (this->current_count_ >= this->max_process_table_size_)
00505 {
00506 size_t new_size = this->max_process_table_size_ * 2;
00507 if (new_size == 0)
00508 new_size = ACE_Process_Manager::DEFAULT_SIZE;
00509 if (this->resize (new_size) == -1)
00510 return -1;
00511 }
00512
00513 Process_Descriptor &proc_desc =
00514 this->process_table_[this->current_count_];
00515
00516 proc_desc.process_ = proc;
00517 proc_desc.exit_notify_ = event_handler;
00518
00519 #if defined (ACE_WIN32)
00520
00521
00522
00523
00524 ACE_Reactor * const r = this->reactor ();
00525 if (r != 0)
00526 r->register_handler (this, proc->gethandle ());
00527 #endif
00528
00529 ++this->current_count_;
00530 return 0;
00531 }
00532
00533
00534
00535
00536 int
00537 ACE_Process_Manager::insert_proc (ACE_Process *proc,
00538 ACE_Event_Handler *event_handler)
00539 {
00540 ACE_TRACE ("ACE_Process_Manager::insert_proc");
00541
00542
00543
00544 if (this->find_proc (proc->getpid ()) != -1)
00545 return -1;
00546
00547 return this->append_proc (proc, event_handler);
00548 }
00549
00550
00551
00552 int
00553 ACE_Process_Manager::remove (pid_t pid)
00554 {
00555 ACE_TRACE ("ACE_Process_Manager::remove");
00556
00557 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));
00558
00559 ssize_t const i = this->find_proc (pid);
00560
00561 if (i != -1)
00562 return this->remove_proc (i);
00563
00564
00565 return -1;
00566 }
00567
00568
00569
00570 int
00571 ACE_Process_Manager::remove_proc (size_t i)
00572 {
00573 ACE_TRACE ("ACE_Process_Manager::remove_proc");
00574
00575
00576
00577
00578 if (this->process_table_[i].exit_notify_ != 0)
00579 {
00580 this->process_table_[i].exit_notify_->handle_close
00581 (this->process_table_[i].process_->gethandle(),
00582 0);
00583 this->process_table_[i].exit_notify_ = 0;
00584 }
00585
00586 #if defined (ACE_WIN32)
00587 ACE_Reactor * const r = this->reactor ();
00588 if (r != 0)
00589 r->remove_handler (this->process_table_[i].process_->gethandle (),
00590 ACE_Event_Handler::DONT_CALL);
00591 #endif
00592
00593 this->process_table_[i].process_->unmanage ();
00594
00595 this->process_table_[i].process_ = 0;
00596
00597 this->current_count_--;
00598
00599 if (this->current_count_ > 0)
00600
00601
00602 this->process_table_[i] =
00603 this->process_table_[this->current_count_];
00604
00605 return 0;
00606 }
00607
00608 int
00609 ACE_Process_Manager::terminate (pid_t pid)
00610 {
00611 ACE_TRACE ("ACE_Process_Manager::terminate");
00612
00613 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));
00614
00615
00616
00617 ssize_t const i = this->find_proc (pid);
00618
00619 if (i == -1)
00620
00621 return -1;
00622
00623 return ACE::terminate_process (pid);
00624 }
00625
00626 int
00627 ACE_Process_Manager::terminate (pid_t pid, int sig)
00628 {
00629 ACE_TRACE ("ACE_Process_Manager::terminate");
00630
00631 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));
00632
00633
00634
00635 ssize_t const i = this->find_proc (pid);
00636
00637 if (i == -1)
00638
00639 return -1;
00640
00641 return ACE_OS::kill (pid, sig);
00642 }
00643
00644
00645 int
00646 ACE_Process_Manager::set_scheduler (const ACE_Sched_Params & params,
00647 pid_t pid)
00648 {
00649 ACE_TRACE ("ACE_Process_Manager::set_scheduler");
00650
00651 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex,
00652 ace_mon, this->lock_, -1));
00653
00654
00655
00656 ssize_t const i = this->find_proc (pid);
00657
00658 if (i == -1)
00659
00660 return ACE_INVALID_PID;
00661
00662 return ACE_OS::sched_params (params, pid);
00663 }
00664
00665 int
00666 ACE_Process_Manager::set_scheduler_all (const ACE_Sched_Params & params)
00667 {
00668 ACE_TRACE ("ACE_Process_Manager::set_scheduler_all");
00669
00670 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex,
00671 ace_mon, this->lock_, -1));
00672
00673 for (size_t i = 0; i < this->current_count_; ++i)
00674 {
00675 pid_t const pid = this->process_table_[i].process_->getpid ();
00676 if (ACE_OS::sched_params (params, pid) != 0)
00677 return -1;
00678 }
00679 return 0;
00680
00681 }
00682
00683
00684
00685
00686
00687 ssize_t
00688 ACE_Process_Manager::find_proc (pid_t pid)
00689 {
00690 ACE_TRACE ("ACE_Process_Manager::find_proc");
00691
00692 for (size_t i = 0; i < this->current_count_; ++i)
00693 if (pid == this->process_table_[i].process_->getpid ())
00694 return i;
00695
00696 return -1;
00697 }
00698
00699 #if defined (ACE_WIN32)
00700
00701
00702
00703 ssize_t
00704 ACE_Process_Manager::find_proc (ACE_HANDLE h)
00705 {
00706 ACE_TRACE ("ACE_Process_Manager::find_proc");
00707
00708 for (size_t i = 0; i < this->current_count_; ++i)
00709 if (h == this->process_table_[i].process_->gethandle ())
00710 return i;
00711
00712 return -1;
00713 }
00714 #endif
00715
00716
00717
00718
00719 int
00720 ACE_Process_Manager::wait (const ACE_Time_Value &timeout)
00721 {
00722 ACE_TRACE ("ACE_Process_Manager::wait");
00723
00724 ACE_Time_Value until = timeout;
00725 ACE_Time_Value remaining = timeout;
00726
00727 if (until < ACE_Time_Value::max_time)
00728 until += ACE_OS::gettimeofday ();
00729
00730 while (this->current_count_ > 0)
00731 {
00732 pid_t const pid = this->wait (0, remaining);
00733
00734 if (pid == ACE_INVALID_PID)
00735 return -1;
00736 else if (pid == 0)
00737 break;
00738
00739 remaining = until < ACE_Time_Value::max_time
00740 ? until - ACE_OS::gettimeofday ()
00741 : ACE_Time_Value::max_time;
00742
00743 if (remaining <= ACE_Time_Value::zero)
00744 break;
00745
00746
00747 }
00748 return static_cast<int> (this->current_count_);
00749 }
00750
00751
00752
00753
00754
00755
00756
00757
00758 pid_t
00759 ACE_Process_Manager::wait (pid_t pid,
00760 ACE_exitcode *status)
00761 {
00762 ACE_TRACE ("ACE_Process_Manager::wait");
00763
00764 return this->wait (pid,
00765 ACE_Time_Value::max_time,
00766 status);
00767 }
00768
00769
00770
00771
00772
00773 pid_t
00774 ACE_Process_Manager::wait (pid_t pid,
00775 const ACE_Time_Value &timeout,
00776 ACE_exitcode *status)
00777 {
00778 ACE_TRACE ("ACE_Process_Manager::wait");
00779
00780 ACE_exitcode local_stat = 0;
00781 if (status == 0)
00782 status = &local_stat;
00783
00784 *status = 0;
00785
00786 ssize_t idx = -1;
00787 ACE_Process *proc = 0;
00788
00789 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));
00790
00791 if (pid != 0)
00792 {
00793 idx = this->find_proc (pid);
00794 if (idx == -1)
00795 return ACE_INVALID_PID;
00796 else
00797 proc = process_table_[idx].process_;
00798 }
00799
00800 if (proc != 0)
00801 pid = proc->wait (timeout, status);
00802 else
00803 {
00804
00805 #if defined (ACE_WIN32)
00806 HANDLE *handles = 0;
00807
00808 ACE_NEW_RETURN (handles,
00809 HANDLE[this->current_count_],
00810 ACE_INVALID_PID);
00811
00812 for (size_t i = 0;
00813 i < this->current_count_;
00814 ++i)
00815 handles[i] =
00816 process_table_[i].process_->gethandle ();
00817
00818 DWORD handle_count = static_cast<DWORD> (this->current_count_);
00819 DWORD result = ::WaitForMultipleObjects (handle_count,
00820 handles,
00821 FALSE,
00822 timeout == ACE_Time_Value::max_time
00823 ? INFINITE
00824 : timeout.msec ());
00825 if (result == WAIT_FAILED)
00826 pid = ACE_INVALID_PID;
00827 else if (result == WAIT_TIMEOUT)
00828 pid = 0;
00829 else
00830 {
00831
00832
00833
00834
00835 # if defined (ghs) || defined (__MINGW32__) || (defined (_MSC_VER) && _MSC_VER >= 1300)
00836 ACE_ASSERT (result < WAIT_OBJECT_0 + this->current_count_);
00837 # else
00838 ACE_ASSERT (result >= WAIT_OBJECT_0
00839 && result < WAIT_OBJECT_0 + this->current_count_);
00840 # endif
00841
00842 idx = this->find_proc (handles[result - WAIT_OBJECT_0]);
00843
00844 if (idx != -1)
00845 {
00846 pid = process_table_[idx].process_->getpid ();
00847 result = ::GetExitCodeProcess (handles[result - WAIT_OBJECT_0],
00848 status);
00849 if (result == 0)
00850 {
00851
00852 this->remove_proc (idx);
00853 pid = ACE_INVALID_PID;
00854 }
00855 }
00856 else
00857 {
00858
00859
00860 delete [] handles;
00861 ACE_ERROR_RETURN ((LM_ERROR,
00862 ACE_TEXT ("Process removed")
00863 ACE_TEXT (" -- somebody's ignoring the lock!\n")),
00864 -1);
00865 }
00866 }
00867
00868 delete [] handles;
00869 #else
00870 if (timeout == ACE_Time_Value::max_time)
00871 {
00872 pid = ACE_OS::waitpid (-1, status, 0);
00873 }
00874 else if (timeout == ACE_Time_Value::zero)
00875 {
00876 pid = ACE_OS::waitpid (-1, status, WNOHANG);
00877 }
00878 else
00879 {
00880 # if defined (ACE_LACKS_UNIX_SIGNALS)
00881 pid = 0;
00882 ACE_Time_Value sleeptm (1);
00883 if (sleeptm > timeout)
00884 sleeptm = timeout;
00885 ACE_Time_Value tmo (timeout);
00886 for (ACE_Countdown_Time time_left (&tmo); tmo > ACE_Time_Value::zero ; time_left.update ())
00887 {
00888 pid = ACE_OS::waitpid (-1, status, WNOHANG);
00889 if (pid > 0 || pid == ACE_INVALID_PID)
00890 break;
00891
00892
00893
00894 ACE_OS::sleep (sleeptm);
00895 }
00896 # else
00897
00898
00899
00900
00901
00902 ACE_Sig_Action old_action;
00903 if (this->reactor () == 0)
00904 {
00905 ACE_Sig_Action do_sigchld ((ACE_SignalHandler)sigchld_nop);
00906 do_sigchld.register_action (SIGCHLD, &old_action);
00907 }
00908
00909 ACE_Time_Value tmo (timeout);
00910 for (ACE_Countdown_Time time_left (&tmo); ; time_left.update ())
00911 {
00912 pid = ACE_OS::waitpid (-1, status, WNOHANG);
00913 # if defined (ACE_VXWORKS) && (ACE_VXWORKS >= 0x600)
00914 if (pid > 0 || (pid == ACE_INVALID_PID && errno != EINTR))
00915 # else
00916 if (pid > 0 || pid == ACE_INVALID_PID)
00917 # endif
00918 break;
00919
00920
00921
00922
00923
00924
00925 if (-1 == ACE_OS::sleep (tmo) && errno == EINTR)
00926 continue;
00927
00928 pid = 0;
00929 break;
00930 }
00931
00932
00933 if (this->reactor () == 0)
00934 {
00935 old_action.register_action (SIGCHLD);
00936 }
00937 # endif
00938 }
00939 #endif
00940 }
00941
00942 if (pid != ACE_INVALID_PID && pid != 0)
00943 {
00944 if (proc == 0)
00945 {
00946 idx = this->find_proc (pid);
00947 if (idx == -1)
00948 {
00949
00950 ACE_DEBUG ((LM_DEBUG,
00951 ACE_TEXT ("(%P|%t) oops, reaped unmanaged %d\n"),
00952 pid));
00953 return pid;
00954 }
00955 else
00956 proc = process_table_[idx].process_;
00957 }
00958 else
00959 ACE_ASSERT (pid == proc->getpid ());
00960
00961 this->notify_proc_handler (idx,
00962 *status);
00963 this->remove (pid);
00964 }
00965
00966 return pid;
00967 }
00968
00969
00970
00971 int
00972 ACE_Process_Manager::reap (pid_t pid,
00973 ACE_exitcode *stat_loc,
00974 int options)
00975 {
00976 ACE_TRACE ("ACE_Process_Manager::reap");
00977
00978 return this->wait (pid,
00979 (ACE_BIT_ENABLED (options, WNOHANG)
00980 ? ACE_Time_Value::zero
00981 : ACE_Time_Value::max_time),
00982 stat_loc);
00983 }
00984
00985
00986
00987
00988
00989 int
00990 ACE_Process_Manager::notify_proc_handler (size_t i,
00991 ACE_exitcode exit_code)
00992 {
00993 if (i < this->current_count_)
00994 {
00995 Process_Descriptor &proc_desc =
00996 this->process_table_[i];
00997
00998 proc_desc.process_->exit_code (exit_code);
00999
01000 if (proc_desc.exit_notify_ != 0)
01001 proc_desc.exit_notify_->handle_exit (proc_desc.process_);
01002 else if (this->default_exit_handler_ != 0
01003 && this->default_exit_handler_->handle_exit (proc_desc.process_) < 0)
01004 {
01005 this->default_exit_handler_->handle_close
01006 (ACE_INVALID_HANDLE,
01007 0);
01008 this->default_exit_handler_ = 0;
01009 }
01010 return 1;
01011 }
01012 else
01013 {
01014 ACE_DEBUG ((LM_DEBUG,
01015 ACE_TEXT ("(%P:%t|%T) ACE_Process_Manager::notify_proc_handler:")
01016 ACE_TEXT (" unknown/unmanaged process reaped\n")));
01017 return 0;
01018 }
01019 }
01020
01021 ACE_END_VERSIONED_NAMESPACE_DECL