summaryrefslogtreecommitdiff
path: root/ace/Process_Manager.cpp
blob: e18129d1e7a2de0a3d253b0a5738e7617370007a (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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
// $Id$

// Process_Manager.cpp
#define ACE_BUILD_DLL
#include "ace/Synch_T.h"
#include "ace/Process.h"
#include "ace/Process_Manager.h"
#include "ace/Object_Manager.h"

#if !defined (__ACE_INLINE__)
#include "ace/Process_Manager.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID (ace, Process_Manager, "$Id$")

ACE_ALLOC_HOOK_DEFINE (ACE_Process_Manager)

// Singleton instance.
ACE_Process_Manager *ACE_Process_Manager::instance_ = 0;

// Controls whether the <Process_Manager> is deleted when we shut down
// (we can only delete it safely if we created it!)
int ACE_Process_Manager::delete_instance_ = 0;

void
ACE_Process_Descriptor::dump (void) const
{
  ACE_TRACE ("ACE_Process_Descriptor::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));

  ACE_DEBUG ((LM_DEBUG,
              ASYS_TEXT ("\nproc_id_ = %d"),
              this->process_->getpid()));

  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

void
ACE_Process_Manager::dump (void) const
{
  ACE_TRACE ("ACE_Process_Manager::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));

  ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("\nmax_process_table_size_ = %d"), this->max_process_table_size_));
  ACE_DEBUG ((LM_DEBUG,  ASYS_TEXT ("\ncurrent_count_ = %d"), this->current_count_));

  for (size_t i = 0; i < this->current_count_; i++)
    this->process_table_[i].dump ();

  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

ACE_Process_Descriptor::ACE_Process_Descriptor (void)
  : delete_process_ (0),
	process_ (0),
	exit_notify_ (0)
{
  ACE_TRACE ("ACE_Process_Descriptor::ACE_Process_Descriptor");
}

ACE_Process_Manager *
ACE_Process_Manager::instance (void)
{
  ACE_TRACE ("ACE_Process_Manager::instance");

  if (ACE_Process_Manager::instance_ == 0)
    {
      // Perform Double-Checked Locking Optimization.
      ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
                                *ACE_Static_Object_Lock::instance (), 0));

      if (ACE_Process_Manager::instance_ == 0)
        {
          ACE_NEW_RETURN (ACE_Process_Manager::instance_,
                          ACE_Process_Manager,
                          0);
          ACE_Process_Manager::delete_instance_ = 1;
        }
    }

  return ACE_Process_Manager::instance_;
}

ACE_Process_Manager *
ACE_Process_Manager::instance (ACE_Process_Manager *tm)
{
  ACE_TRACE ("ACE_Process_Manager::instance");
  ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
                            *ACE_Static_Object_Lock::instance (), 0));

  ACE_Process_Manager *t = ACE_Process_Manager::instance_;
  // We can't safely delete it since we don't know who created it!
  ACE_Process_Manager::delete_instance_ = 0;

  ACE_Process_Manager::instance_ = tm;
  return t;
}

int
ACE_Process_Manager::resize (size_t size)
{
  ACE_TRACE ("ACE_Process_Manager::resize");

  ACE_Process_Descriptor *temp;
  
  ACE_NEW_RETURN (temp,
                  ACE_Process_Descriptor[size],
                  -1);

  for (size_t i = 0;
       i < this->current_count_;
       i++)
    // Structure assignment.
    temp[i] = this->process_table_[i]; 

  this->max_process_table_size_ = size;

  delete [] this->process_table_;

  this->process_table_ = temp;
  return 0;
}

// Create and initialize the table to keep track of the process pool.

int
ACE_Process_Manager::open (size_t size,
                           ACE_Reactor *r)
{
  ACE_TRACE ("ACE_Process_Manager::open");

  if (r) 
    {
      ACE_Event_Handler::reactor (r);
#if !defined (ACE_WIN32)
      // (No signals for child-exited on Win32) Assign the
      // Process_Manager a dummy I/O descriptor.  Note that even
      // though we open this file "Write Only" we still need to use
      // the ACE_Event_Handler::NULL_MASK when registering this with
      // the ACE_Reactor (see below).
      this->dummy_handle_ = ACE_OS::open (ACE_DEV_NULL,
                                          O_WRONLY);
      ACE_ASSERT (this->dummy_handle_ != ACE_INVALID_HANDLE);
  
      // Register signal handler object.  Note that NULL_MASK is used
      // to keep the ACE_Reactor from calling us back on the
      // "/dev/null" descriptor.  NULL_MASK just reserves a "slot" in
      // the Reactor's internal demuxing table, but doesn't cause it
      // to dispatch the event handler directly.  Instead, we use the
      // signal handler to do this.
      if (reactor ()->register_handler
          (this,
           ACE_Event_Handler::NULL_MASK) == -1)
        ACE_ERROR ((LM_ERROR,
                    "%p\n%a",
                    "register_handler",
                    1));

      if (reactor ()->register_handler
          (SIGCHLD,
           this) == -1)
        ACE_ERROR ((LM_ERROR,
                    "%p\n%a",
                    "register_handler",
                    1));
#endif	// !defined (ACE_WIN32)
    }
  
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  if (this->max_process_table_size_ < size)
    this->resize (size);
  return 0;

}

// Initialize the synchronization variables.

ACE_Process_Manager::ACE_Process_Manager (size_t size,ACE_Reactor *r)
  : ACE_Event_Handler(),
	process_table_ (0),
    max_process_table_size_ (0), 
    current_count_ (0),
	dummy_handle_ (ACE_INVALID_HANDLE),
	default_exit_handler_ (0)
#if defined (ACE_HAS_THREADS)
    , zero_cond_ (lock_)
#endif /* ACE_HAS_THREADS */
{
  ACE_TRACE ("ACE_Process_Manager::ACE_Process_Manager");

  if (this->open (size,r) == -1)
    ACE_ERROR ((LM_ERROR,
                ASYS_TEXT ("%p\n"),
                ASYS_TEXT ("ACE_Process_Manager")));
}

// Close up and release all resources.

int
ACE_Process_Manager::close (void)
{
  ACE_TRACE ("ACE_Process_Manager::close");

  if (this->reactor ())
    {
      this->reactor ()->remove_handler (this, 0);
      this->reactor (0);
    }

  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  if (this->process_table_ != 0)
    {
      for (size_t i = 0; i < this->current_count_; ++i) 
        if (this->process_table_[i].exit_notify_ != 0)
          this->process_table_[i].exit_notify_->handle_close
            (this->process_table_[i].process_->gethandle (),
             0);

      delete [] this->process_table_;
      this->process_table_ = 0;
      this->max_process_table_size_ = 0;
      this->current_count_ = 0;
    }
  return 0;
}

ACE_Process_Manager::~ACE_Process_Manager (void)
{
  ACE_TRACE ("ACE_Process_Manager::~ACE_Process_Manager");
  this->close ();
}

ACE_HANDLE
ACE_Process_Manager::get_handle (void) const
{
  return this->dummy_handle_;
}

// This is called when the Reactor notices that a process has exited.
// On Windoze, it knows which process it was, and passes the process'
// handle.  On Unix, what has actually happened is a SIGCHLD invoked
// the handle_signal routine, which fooled the Reactor into thinking
// that this routine needed to be called. (On Unix, we reap as many
// children as are dead.)

int
ACE_Process_Manager::handle_input (ACE_HANDLE proc)
{
  ACE_TRACE ("ACE_Process_Manager::handle_input");

#if defined (ACE_WIN32)
  DWORD status = 0;
  BOOL result = ::GetExitCodeProcess (proc, &status);

  if (result) 
    {
      if (status != STILL_ACTIVE) 
        {
          {
            ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex,ace_mon,lock_,-1));
            int	 i = find_proc ( proc );
            pid_t pid = i != -1 ? process_table_[i].process_->getpid () : -1;
            this->notify_proc_handler (proc, pid, status);
            this->remove_proc (pid);
          }
          ACE_Reactor *r = reactor();
          if (r)
            r->remove_handler ( proc, 0 );
        } 
      else
        {
          // Huh?  Process still active -- shouldn't have been called yet!
        }
    } 
  else 
    {
      // <GetExitCodeProcess> failed.
      ACE_ERROR ((LM_ERROR,
                  "%p\n%a",
                  "handle_input: GetExitCodeProcess failed",
                  0));
    }
#else	// !defined (ACE_WIN32)
  ACE_UNUSED_ARG (proc);	
  // <proc> is <dummy_handle_> on unix.
  while (this->reap() > 0) 
    continue;
#endif /* ACE_WIN32 */
  return 0;
}

int
ACE_Process_Manager::handle_signal (int signum, siginfo_t *, ucontext_t *)
{
  return reactor ()->ready_ops
    (this->dummy_handle_,
     ACE_Event_Handler::READ_MASK,
     ACE_Reactor::ADD_MASK);
}

int
ACE_Process_Manager::register_handler (ACE_Event_Handler *eh, pid_t pid)
{
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  if (pid == -1) 
    {
      if (this->default_exit_handler_ != 0)
        this->default_exit_handler_->handle_close (ACE_INVALID_HANDLE,
                                                   0);
      this->default_exit_handler_ = eh;
      return 0;
    }

  int i = this->find_proc (pid);

  if (i == -1)
    return -1;
  else
    {
      ACE_Process_Descriptor &proc_desc = this->process_table_[i];

      if (proc_desc.exit_notify_ != 0)
        proc_desc.exit_notify_->handle_close (ACE_INVALID_HANDLE, 0);
      proc_desc.exit_notify_ = eh;
      return 0;
    }
}

int
ACE_Process_Manager::handle_close (ACE_HANDLE handle,
                                   ACE_Reactor_Mask close_mask)
{
  ACE_TRACE ("ACE_Process_Manager::handle_close");

  ACE_ASSERT (handle==this->dummy_handle_ );

  ACE_OS::close ( dummy_handle_ );

  return 0;
}
	
// Create a new process.

pid_t
ACE_Process_Manager::spawn (ACE_Process_Options &options)
{
  ACE_Process *process;
  ACE_NEW_RETURN (process,
                  ACE_Process,
                  ACE_INVALID_PID);

  pid_t pid = spawn (process,
                     options);

  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  int i = ACE_INVALID_PID;

  if (pid != -1)
    i = this->find_proc (pid);

  if (i != -1)
    this->process_table_[i].delete_process_ = 1;

  return pid;
}

// Create a new process.

pid_t
ACE_Process_Manager::spawn (ACE_Process *process,
                            ACE_Process_Options &options)
{
  ACE_TRACE ("ACE_Process_Manager::spawn");

  pid_t pid = process->spawn (options);

  // Only include the pid in the parent's table.
  if (pid == -1 || pid == 0)
    return pid;
  else
    {
      ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

      if (this->append_proc (process) == -1)
        return ACE_INVALID_PID;
      else
        return pid;
    }
}

// Create N new processs.

int 
ACE_Process_Manager::spawn_n (size_t n,
                              ACE_Process_Options &options,
                              pid_t *child_pids)
{
  ACE_TRACE ("ACE_Process_Manager::spawn_n");

  // This doesn't work (yet).
  for (size_t i = 0; i < n; i++)
    {
      pid_t pid = this->spawn (options);
      if (pid <= 0)
        // We're in the child or something's gone wrong.
        return pid;
      else if (child_pids != 0)
        child_pids[i] = pid;
    }

  return 0;
}

// Append a process into the pool (does not check for duplicates).
// Must be called with locks held.

int
ACE_Process_Manager::append_proc (ACE_Process *proc)
{
  ACE_TRACE ("ACE_Process_Manager::append_proc");

  // Try to resize the array to twice its existing size if we run out
  // of space...
  if (this->current_count_ >= this->max_process_table_size_ 
      && this->resize (this->max_process_table_size_ * 2) == -1)
    return -1;
  else
    {
      ACE_Process_Descriptor &proc_desc = 
	this->process_table_[this->current_count_];

      proc_desc.delete_process_ = 0;	// pending better info from caller
      proc_desc.process_ = proc;

#if defined (ACE_WIN32)
      // If we have a Reactor, then we're supposed to reap Processes
      // automagically.  Get a handle to this new Process and tell the
      // Reactor we're interested in handling_input () on it.

      ACE_Reactor *r = this->reactor ();
      if (r != 0)
        r->register_handler (proc->gethandle (),
                             this,
                             ACE_Event_Handler::READ_MASK);
#endif /* ACE_WIN32 */

      this->current_count_++;
      return 0;
    }
}

// Insert a process into the pool (checks for duplicates and doesn't
// allow them to be inserted twice).

int
ACE_Process_Manager::insert_proc (ACE_Process *proc)
{
  ACE_TRACE ("ACE_Process_Manager::insert_proc");

#if 0
  // Check for duplicates and bail out if they're already
  // registered...
  if (this->find_proc (proc->getpid ()) != -1)
    return -1;

  return this->append_proc (proc);
#else
  ACE_UNUSED_ARG (proc);
  ACE_NOTSUP_RETURN (-1);
#endif 
}

// Remove a process from the pool.

int
ACE_Process_Manager::remove (pid_t pid)
{
  ACE_TRACE ("ACE_Process_Manager::remove");

  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));
  return this->remove_proc (pid);
}

// Remove a process from the pool.  Must be called with locks held.

int
ACE_Process_Manager::remove_proc (pid_t pid)
{
  ACE_TRACE ("ACE_Process_Manager::remove");

  int i = this->find_proc (pid);

  if (i == -1)
    return -1;
  else
    {
      if (this->process_table_[i].delete_process_)
        delete this->process_table_[i].process_;
	  
      this->current_count_--;

      if (this->current_count_ > 0)
	// Compact the table by moving the last item into the slot
	// vacated by the index being removed (this is a structure
	// assignment).
	this->process_table_[i] =
          this->process_table_[this->current_count_];

#if defined (ACE_HAS_THREADS)
      // Tell all waiters when there are no more threads left in the
      // pool.
      if (this->current_count_ == 0)
	this->zero_cond_.broadcast ();
#endif /* ACE_HAS_THREADS */
      return 0;
    }
}

int
ACE_Process_Manager::terminate (pid_t pid)
{
  ACE_TRACE ("ACE_Process_Manager::terminate");

  // Check for duplicates and bail out if they're already
  // registered...
  int i = this->find_proc (pid);

  if (i == -1)
    return -1;
  else
    {
      int result = ACE::terminate_process (this->process_table_[i].process_->getpid ());

      if (result != -1)
	{ 
          // Save/restore errno.
          ACE_Errno_Guard error (errno);
	  this->remove (this->process_table_[i].process_->getpid ()); 
	  return 0; 
	} 
      else 
	return -1; 
    }
}

int
ACE_Process_Manager::terminate (pid_t pid, int sig)
{
  ACE_TRACE ("ACE_Process_Manager::terminate");

  // Check for duplicates and bail out if they're already
  // registered...
  int i = this->find_proc (pid);

  if (i == -1)
    return -1;
  else
    {
		return ACE_OS::kill (this->process_table_[i].process_->getpid (), sig);
    }
}

// Locate the index in the table associated with <pid>.  Must be
// called with the lock held.

int 
ACE_Process_Manager::find_proc (pid_t pid)
{
  ACE_TRACE ("ACE_Process_Manager::find_proc");

  for (size_t i = 0; i < this->current_count_; i++)
    if (pid == this->process_table_[i].process_->getpid ())
      return i;

  return -1;    
}

#if defined (ACE_WIN32)
// Locate the index in the table associated with <h>.  Must be called
// with the lock held.

int 
ACE_Process_Manager::find_proc (ACE_HANDLE h)
{
  ACE_TRACE ("ACE_Process_Manager::find_proc");

  for (size_t i = 0; i < this->current_count_; i++)
    if (h == this->process_table_[i].process_->gethandle ())
      return i;

  return -1;    
}
#endif /* ACE_WIN32 */

// Wait for all the processs to exit.  This implementation requires a
// cooperative signal handler or <ACE_OS::sigwait> thread, so it's not
// portable to Win32.

int
ACE_Process_Manager::wait (ACE_Time_Value *timeout)
{
  ACE_TRACE ("ACE_Process_Manager::wait");

#if defined (ACE_HAS_THREADS)
  ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1);

  while (this->current_count_ > 0)
    if (this->zero_cond_.wait (timeout) == -1)
      return -1;
#else
  ACE_UNUSED_ARG (timeout);
#endif /* ACE_HAS_THREADS */

  return 0;
}

// The following method *is* portable all the operating systems that
// support processes.

int
ACE_Process_Manager::wait (void)
{
  ACE_TRACE ("ACE_Process_Manager::wait");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  for (size_t i = 0; i < this->current_count_; i++)
    {
      ACE_Process_Descriptor &proc_desc = 
	this->process_table_[i];

      pid_t pid = proc_desc.process_->wait ();
      
      if (pid != -1)
        this->remove_proc (pid);
      return pid;
    }

  return 0;
}

// Collect a single child processes' exit status by calling
// <ACE_OS::wait>.  Calls the appropriate exit_notify, if registered.

int 
ACE_Process_Manager::wait (pid_t pid,
                           int *stat_loc,
                           int options)
{
  ACE_TRACE ("ACE_Process_Manager::wait");

  int local_stat = 0;

  if (stat_loc == 0)
    stat_loc = &local_stat;

  pid = ACE_OS::waitpid (pid, stat_loc, options);

  if (pid > 0) 
    {
      ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));
      this->notify_proc_handler (ACE_HANDLE (pid),
                                 pid,
                                 *stat_loc);
    }

  return pid;
}

int 
ACE_Process_Manager::reap (pid_t pid,
                           int *stat_loc,
                           int options)
{
  ACE_TRACE ("ACE_Process_Manager::reap");

  return this->wait (pid, stat_loc, options);
}

// Notify either the process-specific handler or the generic handler.
// If process-specific, call handle_close on the handler.  Returns 1
// if process found, 0 if not.  Must be called with locks held.

int
ACE_Process_Manager::notify_proc_handler (ACE_HANDLE h,
                                          pid_t pid,
                                          int status)
{
  int i = this->find_proc (pid);

  if (i != -1) 
    {
      ACE_Process_Descriptor &proc_desc = this->process_table_[i];

      if (proc_desc.exit_notify_ != 0) 
        {
          proc_desc.exit_notify_->handle_input (h);
          proc_desc.exit_notify_->handle_close (h, 0);
          proc_desc.exit_notify_ = 0;
        }
      else if (this->default_exit_handler_ != 0
               && this->default_exit_handler_->handle_input (h) < 0)
        this->register_handler (0);
    } 
  else
    ACE_DEBUG ((LM_DEBUG,
                "(%P:%t|%T) ACE_Process_Manager::notify_proc_handler: unknown process %d reaped\n",
                pid));

  return i != -1;
}