This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.c
5780 lines (5164 loc) · 145 KB
/
process.c
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
process.c -
$Author$
created at: Tue Aug 10 14:30:50 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby/ruby.h"
#include "ruby/io.h"
#include "ruby/util.h"
#include "vm_core.h"
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <time.h>
#include <ctype.h>
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
struct timeval rb_time_interval(VALUE);
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifndef MAXPATHLEN
# define MAXPATHLEN 1024
#endif
#include "ruby/st.h"
#ifdef __EMX__
#undef HAVE_GETPGRP
#endif
#include <sys/stat.h>
#ifdef HAVE_SYS_TIMES_H
#include <sys/times.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
#if defined(HAVE_TIMES) || defined(_WIN32)
static VALUE rb_cProcessTms;
#endif
#ifndef WIFEXITED
#define WIFEXITED(w) (((w) & 0xff) == 0)
#endif
#ifndef WIFSIGNALED
#define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
#endif
#ifndef WIFSTOPPED
#define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
#endif
#ifndef WEXITSTATUS
#define WEXITSTATUS(w) (((w) >> 8) & 0xff)
#endif
#ifndef WTERMSIG
#define WTERMSIG(w) ((w) & 0x7f)
#endif
#ifndef WSTOPSIG
#define WSTOPSIG WEXITSTATUS
#endif
#if defined(__APPLE__) && ( defined(__MACH__) || defined(__DARWIN__) ) && !defined(__MacOS_X__)
#define __MacOS_X__ 1
#endif
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__)
#define HAVE_44BSD_SETUID 1
#define HAVE_44BSD_SETGID 1
#endif
#ifdef __NetBSD__
#undef HAVE_SETRUID
#undef HAVE_SETRGID
#endif
#ifdef BROKEN_SETREUID
#define setreuid ruby_setreuid
#endif
#ifdef BROKEN_SETREGID
#define setregid ruby_setregid
#endif
#if defined(HAVE_44BSD_SETUID) || defined(__MacOS_X__)
#if !defined(USE_SETREUID) && !defined(BROKEN_SETREUID)
#define OBSOLETE_SETREUID 1
#endif
#if !defined(USE_SETREGID) && !defined(BROKEN_SETREGID)
#define OBSOLETE_SETREGID 1
#endif
#endif
#if SIZEOF_RLIM_T == SIZEOF_INT
# define RLIM2NUM(v) UINT2NUM(v)
# define NUM2RLIM(v) NUM2UINT(v)
#elif SIZEOF_RLIM_T == SIZEOF_LONG
# define RLIM2NUM(v) ULONG2NUM(v)
# define NUM2RLIM(v) NUM2ULONG(v)
#elif SIZEOF_RLIM_T == SIZEOF_LONG_LONG
# define RLIM2NUM(v) ULL2NUM(v)
# define NUM2RLIM(v) NUM2ULL(v)
#endif
#define preserving_errno(stmts) \
do {int saved_errno = errno; stmts; errno = saved_errno;} while (0)
ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
ssize_t rb_io_bufread(VALUE io, void *buf, size_t size);
/*
* call-seq:
* Process.pid -> fixnum
*
* Returns the process id of this process. Not available on all
* platforms.
*
* Process.pid #=> 27415
*/
static VALUE
get_pid(void)
{
rb_secure(2);
return PIDT2NUM(getpid());
}
/*
* call-seq:
* Process.ppid -> fixnum
*
* Returns the process id of the parent of this process. Returns
* untrustworthy value on Win32/64. Not available on all platforms.
*
* puts "I am #{Process.pid}"
* Process.fork { puts "Dad is #{Process.ppid}" }
*
* <em>produces:</em>
*
* I am 27417
* Dad is 27417
*/
static VALUE
get_ppid(void)
{
rb_secure(2);
return PIDT2NUM(getppid());
}
/*********************************************************************
*
* Document-class: Process::Status
*
* <code>Process::Status</code> encapsulates the information on the
* status of a running or terminated system process. The built-in
* variable <code>$?</code> is either +nil+ or a
* <code>Process::Status</code> object.
*
* fork { exit 99 } #=> 26557
* Process.wait #=> 26557
* $?.class #=> Process::Status
* $?.to_i #=> 25344
* $? >> 8 #=> 99
* $?.stopped? #=> false
* $?.exited? #=> true
* $?.exitstatus #=> 99
*
* Posix systems record information on processes using a 16-bit
* integer. The lower bits record the process status (stopped,
* exited, signaled) and the upper bits possibly contain additional
* information (for example the program's return code in the case of
* exited processes). Pre Ruby 1.8, these bits were exposed directly
* to the Ruby program. Ruby now encapsulates these in a
* <code>Process::Status</code> object. To maximize compatibility,
* however, these objects retain a bit-oriented interface. In the
* descriptions that follow, when we talk about the integer value of
* _stat_, we're referring to this 16 bit value.
*/
static VALUE rb_cProcessStatus;
VALUE
rb_last_status_get(void)
{
return GET_THREAD()->last_status;
}
void
rb_last_status_set(int status, rb_pid_t pid)
{
rb_thread_t *th = GET_THREAD();
th->last_status = rb_obj_alloc(rb_cProcessStatus);
rb_iv_set(th->last_status, "status", INT2FIX(status));
rb_iv_set(th->last_status, "pid", PIDT2NUM(pid));
}
static void
rb_last_status_clear(void)
{
GET_THREAD()->last_status = Qnil;
}
/*
* call-seq:
* stat.to_i -> fixnum
* stat.to_int -> fixnum
*
* Returns the bits in _stat_ as a <code>Fixnum</code>. Poking
* around in these bits is platform dependent.
*
* fork { exit 0xab } #=> 26566
* Process.wait #=> 26566
* sprintf('%04x', $?.to_i) #=> "ab00"
*/
static VALUE
pst_to_i(VALUE st)
{
return rb_iv_get(st, "status");
}
#define PST2INT(st) NUM2INT(pst_to_i(st))
/*
* call-seq:
* stat.pid -> fixnum
*
* Returns the process ID that this status object represents.
*
* fork { exit } #=> 26569
* Process.wait #=> 26569
* $?.pid #=> 26569
*/
static VALUE
pst_pid(VALUE st)
{
return rb_attr_get(st, rb_intern("pid"));
}
static void
pst_message(VALUE str, rb_pid_t pid, int status)
{
rb_str_catf(str, "pid %ld", (long)pid);
if (WIFSTOPPED(status)) {
int stopsig = WSTOPSIG(status);
const char *signame = ruby_signal_name(stopsig);
if (signame) {
rb_str_catf(str, " stopped SIG%s (signal %d)", signame, stopsig);
}
else {
rb_str_catf(str, " stopped signal %d", stopsig);
}
}
if (WIFSIGNALED(status)) {
int termsig = WTERMSIG(status);
const char *signame = ruby_signal_name(termsig);
if (signame) {
rb_str_catf(str, " SIG%s (signal %d)", signame, termsig);
}
else {
rb_str_catf(str, " signal %d", termsig);
}
}
if (WIFEXITED(status)) {
rb_str_catf(str, " exit %d", WEXITSTATUS(status));
}
#ifdef WCOREDUMP
if (WCOREDUMP(status)) {
rb_str_cat2(str, " (core dumped)");
}
#endif
}
/*
* call-seq:
* stat.to_s -> string
*
* Show pid and exit status as a string.
*
* system("false")
* p $?.to_s #=> "pid 12766 exit 1"
*
*/
static VALUE
pst_to_s(VALUE st)
{
rb_pid_t pid;
int status;
VALUE str;
pid = NUM2PIDT(pst_pid(st));
status = PST2INT(st);
str = rb_str_buf_new(0);
pst_message(str, pid, status);
return str;
}
/*
* call-seq:
* stat.inspect -> string
*
* Override the inspection method.
*
* system("false")
* p $?.inspect #=> "#<Process::Status: pid 12861 exit 1>"
*
*/
static VALUE
pst_inspect(VALUE st)
{
rb_pid_t pid;
int status;
VALUE vpid, str;
vpid = pst_pid(st);
if (NIL_P(vpid)) {
return rb_sprintf("#<%s: uninitialized>", rb_class2name(CLASS_OF(st)));
}
pid = NUM2PIDT(vpid);
status = PST2INT(st);
str = rb_sprintf("#<%s: ", rb_class2name(CLASS_OF(st)));
pst_message(str, pid, status);
rb_str_cat2(str, ">");
return str;
}
/*
* call-seq:
* stat == other -> true or false
*
* Returns +true+ if the integer value of _stat_
* equals <em>other</em>.
*/
static VALUE
pst_equal(VALUE st1, VALUE st2)
{
if (st1 == st2) return Qtrue;
return rb_equal(pst_to_i(st1), st2);
}
/*
* call-seq:
* stat & num -> fixnum
*
* Logical AND of the bits in _stat_ with <em>num</em>.
*
* fork { exit 0x37 }
* Process.wait
* sprintf('%04x', $?.to_i) #=> "3700"
* sprintf('%04x', $? & 0x1e00) #=> "1600"
*/
static VALUE
pst_bitand(VALUE st1, VALUE st2)
{
int status = PST2INT(st1) & NUM2INT(st2);
return INT2NUM(status);
}
/*
* call-seq:
* stat >> num -> fixnum
*
* Shift the bits in _stat_ right <em>num</em> places.
*
* fork { exit 99 } #=> 26563
* Process.wait #=> 26563
* $?.to_i #=> 25344
* $? >> 8 #=> 99
*/
static VALUE
pst_rshift(VALUE st1, VALUE st2)
{
int status = PST2INT(st1) >> NUM2INT(st2);
return INT2NUM(status);
}
/*
* call-seq:
* stat.stopped? -> true or false
*
* Returns +true+ if this process is stopped. This is only
* returned if the corresponding <code>wait</code> call had the
* <code>WUNTRACED</code> flag set.
*/
static VALUE
pst_wifstopped(VALUE st)
{
int status = PST2INT(st);
if (WIFSTOPPED(status))
return Qtrue;
else
return Qfalse;
}
/*
* call-seq:
* stat.stopsig -> fixnum or nil
*
* Returns the number of the signal that caused _stat_ to stop
* (or +nil+ if self is not stopped).
*/
static VALUE
pst_wstopsig(VALUE st)
{
int status = PST2INT(st);
if (WIFSTOPPED(status))
return INT2NUM(WSTOPSIG(status));
return Qnil;
}
/*
* call-seq:
* stat.signaled? -> true or false
*
* Returns +true+ if _stat_ terminated because of
* an uncaught signal.
*/
static VALUE
pst_wifsignaled(VALUE st)
{
int status = PST2INT(st);
if (WIFSIGNALED(status))
return Qtrue;
else
return Qfalse;
}
/*
* call-seq:
* stat.termsig -> fixnum or nil
*
* Returns the number of the signal that caused _stat_ to
* terminate (or +nil+ if self was not terminated by an
* uncaught signal).
*/
static VALUE
pst_wtermsig(VALUE st)
{
int status = PST2INT(st);
if (WIFSIGNALED(status))
return INT2NUM(WTERMSIG(status));
return Qnil;
}
/*
* call-seq:
* stat.exited? -> true or false
*
* Returns +true+ if _stat_ exited normally (for
* example using an <code>exit()</code> call or finishing the
* program).
*/
static VALUE
pst_wifexited(VALUE st)
{
int status = PST2INT(st);
if (WIFEXITED(status))
return Qtrue;
else
return Qfalse;
}
/*
* call-seq:
* stat.exitstatus -> fixnum or nil
*
* Returns the least significant eight bits of the return code of
* _stat_. Only available if <code>exited?</code> is
* +true+.
*
* fork { } #=> 26572
* Process.wait #=> 26572
* $?.exited? #=> true
* $?.exitstatus #=> 0
*
* fork { exit 99 } #=> 26573
* Process.wait #=> 26573
* $?.exited? #=> true
* $?.exitstatus #=> 99
*/
static VALUE
pst_wexitstatus(VALUE st)
{
int status = PST2INT(st);
if (WIFEXITED(status))
return INT2NUM(WEXITSTATUS(status));
return Qnil;
}
/*
* call-seq:
* stat.success? -> true, false or nil
*
* Returns +true+ if _stat_ is successful, +false+ if not.
* Returns +nil+ if <code>exited?</code> is not +true+.
*/
static VALUE
pst_success_p(VALUE st)
{
int status = PST2INT(st);
if (!WIFEXITED(status))
return Qnil;
return WEXITSTATUS(status) == EXIT_SUCCESS ? Qtrue : Qfalse;
}
/*
* call-seq:
* stat.coredump? -> true or false
*
* Returns +true+ if _stat_ generated a coredump
* when it terminated. Not available on all platforms.
*/
static VALUE
pst_wcoredump(VALUE st)
{
#ifdef WCOREDUMP
int status = PST2INT(st);
if (WCOREDUMP(status))
return Qtrue;
else
return Qfalse;
#else
return Qfalse;
#endif
}
#if !defined(HAVE_WAITPID) && !defined(HAVE_WAIT4)
#define NO_WAITPID
static st_table *pid_tbl;
struct wait_data {
rb_pid_t pid;
int status;
};
static int
wait_each(rb_pid_t pid, int status, struct wait_data *data)
{
if (data->status != -1) return ST_STOP;
data->pid = pid;
data->status = status;
return ST_DELETE;
}
static int
waitall_each(rb_pid_t pid, int status, VALUE ary)
{
rb_last_status_set(status, pid);
rb_ary_push(ary, rb_assoc_new(PIDT2NUM(pid), rb_last_status_get()));
return ST_DELETE;
}
#else
struct waitpid_arg {
rb_pid_t pid;
int *st;
int flags;
};
#endif
static VALUE
rb_waitpid_blocking(void *data)
{
rb_pid_t result;
#ifndef NO_WAITPID
struct waitpid_arg *arg = data;
#endif
#if defined NO_WAITPID
result = wait(data);
#elif defined HAVE_WAITPID
result = waitpid(arg->pid, arg->st, arg->flags);
#else /* HAVE_WAIT4 */
result = wait4(arg->pid, arg->st, arg->flags, NULL);
#endif
return (VALUE)result;
}
rb_pid_t
rb_waitpid(rb_pid_t pid, int *st, int flags)
{
rb_pid_t result;
#ifndef NO_WAITPID
struct waitpid_arg arg;
retry:
arg.pid = pid;
arg.st = st;
arg.flags = flags;
result = (rb_pid_t)rb_thread_blocking_region(rb_waitpid_blocking, &arg,
RUBY_UBF_PROCESS, 0);
if (result < 0) {
if (errno == EINTR) {
RUBY_VM_CHECK_INTS();
goto retry;
}
return (rb_pid_t)-1;
}
#else /* NO_WAITPID */
if (pid_tbl) {
st_data_t status, piddata = (st_data_t)pid;
if (pid == (rb_pid_t)-1) {
struct wait_data data;
data.pid = (rb_pid_t)-1;
data.status = -1;
st_foreach(pid_tbl, wait_each, (st_data_t)&data);
if (data.status != -1) {
rb_last_status_set(data.status, data.pid);
return data.pid;
}
}
else if (st_delete(pid_tbl, &piddata, &status)) {
rb_last_status_set(*st = (int)status, pid);
return pid;
}
}
if (flags) {
rb_raise(rb_eArgError, "can't do waitpid with flags");
}
for (;;) {
result = (rb_pid_t)rb_thread_blocking_region(rb_waitpid_blocking,
st, RUBY_UBF_PROCESS, 0);
if (result < 0) {
if (errno == EINTR) {
rb_thread_schedule();
continue;
}
return (rb_pid_t)-1;
}
if (result == pid || pid == (rb_pid_t)-1) {
break;
}
if (!pid_tbl)
pid_tbl = st_init_numtable();
st_insert(pid_tbl, pid, (st_data_t)st);
if (!rb_thread_alone()) rb_thread_schedule();
}
#endif
if (result > 0) {
rb_last_status_set(*st, result);
}
return result;
}
/* [MG]:FIXME: I wasn't sure how this should be done, since ::wait()
has historically been documented as if it didn't take any arguments
despite the fact that it's just an alias for ::waitpid(). The way I
have it below is more truthful, but a little confusing.
I also took the liberty of putting in the pid values, as they're
pretty useful, and it looked as if the original 'ri' output was
supposed to contain them after "[...]depending on the value of
aPid:".
The 'ansi' and 'bs' formats of the ri output don't display the
definition list for some reason, but the plain text one does.
*/
/*
* call-seq:
* Process.wait() -> fixnum
* Process.wait(pid=-1, flags=0) -> fixnum
* Process.waitpid(pid=-1, flags=0) -> fixnum
*
* Waits for a child process to exit, returns its process id, and
* sets <code>$?</code> to a <code>Process::Status</code> object
* containing information on that process. Which child it waits on
* depends on the value of _pid_:
*
* > 0:: Waits for the child whose process ID equals _pid_.
*
* 0:: Waits for any child whose process group ID equals that of the
* calling process.
*
* -1:: Waits for any child process (the default if no _pid_ is
* given).
*
* < -1:: Waits for any child whose process group ID equals the absolute
* value of _pid_.
*
* The _flags_ argument may be a logical or of the flag values
* <code>Process::WNOHANG</code> (do not block if no child available)
* or <code>Process::WUNTRACED</code> (return stopped children that
* haven't been reported). Not all flags are available on all
* platforms, but a flag value of zero will work on all platforms.
*
* Calling this method raises a <code>SystemError</code> if there are
* no child processes. Not available on all platforms.
*
* include Process
* fork { exit 99 } #=> 27429
* wait #=> 27429
* $?.exitstatus #=> 99
*
* pid = fork { sleep 3 } #=> 27440
* Time.now #=> 2008-03-08 19:56:16 +0900
* waitpid(pid, Process::WNOHANG) #=> nil
* Time.now #=> 2008-03-08 19:56:16 +0900
* waitpid(pid, 0) #=> 27440
* Time.now #=> 2008-03-08 19:56:19 +0900
*/
static VALUE
proc_wait(int argc, VALUE *argv)
{
VALUE vpid, vflags;
rb_pid_t pid;
int flags, status;
rb_secure(2);
flags = 0;
if (argc == 0) {
pid = -1;
}
else {
rb_scan_args(argc, argv, "02", &vpid, &vflags);
pid = NUM2PIDT(vpid);
if (argc == 2 && !NIL_P(vflags)) {
flags = NUM2UINT(vflags);
}
}
if ((pid = rb_waitpid(pid, &status, flags)) < 0)
rb_sys_fail(0);
if (pid == 0) {
rb_last_status_clear();
return Qnil;
}
return PIDT2NUM(pid);
}
/*
* call-seq:
* Process.wait2(pid=-1, flags=0) -> [pid, status]
* Process.waitpid2(pid=-1, flags=0) -> [pid, status]
*
* Waits for a child process to exit (see Process::waitpid for exact
* semantics) and returns an array containing the process id and the
* exit status (a <code>Process::Status</code> object) of that
* child. Raises a <code>SystemError</code> if there are no child
* processes.
*
* Process.fork { exit 99 } #=> 27437
* pid, status = Process.wait2
* pid #=> 27437
* status.exitstatus #=> 99
*/
static VALUE
proc_wait2(int argc, VALUE *argv)
{
VALUE pid = proc_wait(argc, argv);
if (NIL_P(pid)) return Qnil;
return rb_assoc_new(pid, rb_last_status_get());
}
/*
* call-seq:
* Process.waitall -> [ [pid1,status1], ...]
*
* Waits for all children, returning an array of
* _pid_/_status_ pairs (where _status_ is a
* <code>Process::Status</code> object).
*
* fork { sleep 0.2; exit 2 } #=> 27432
* fork { sleep 0.1; exit 1 } #=> 27433
* fork { exit 0 } #=> 27434
* p Process.waitall
*
* <em>produces</em>:
*
* [[30982, #<Process::Status: pid 30982 exit 0>],
* [30979, #<Process::Status: pid 30979 exit 1>],
* [30976, #<Process::Status: pid 30976 exit 2>]]
*/
static VALUE
proc_waitall(void)
{
VALUE result;
rb_pid_t pid;
int status;
rb_secure(2);
result = rb_ary_new();
#ifdef NO_WAITPID
if (pid_tbl) {
st_foreach(pid_tbl, waitall_each, result);
}
#else
rb_last_status_clear();
#endif
for (pid = -1;;) {
#ifdef NO_WAITPID
pid = wait(&status);
#else
pid = rb_waitpid(-1, &status, 0);
#endif
if (pid == -1) {
if (errno == ECHILD)
break;
#ifdef NO_WAITPID
if (errno == EINTR) {
rb_thread_schedule();
continue;
}
#endif
rb_sys_fail(0);
}
#ifdef NO_WAITPID
rb_last_status_set(status, pid);
#endif
rb_ary_push(result, rb_assoc_new(PIDT2NUM(pid), rb_last_status_get()));
}
return result;
}
static inline ID
id_pid(void)
{
ID pid;
CONST_ID(pid, "pid");
return pid;
}
static VALUE
detach_process_pid(VALUE thread)
{
return rb_thread_local_aref(thread, id_pid());
}
static VALUE
detach_process_watcher(void *arg)
{
rb_pid_t cpid, pid = (rb_pid_t)(VALUE)arg;
int status;
while ((cpid = rb_waitpid(pid, &status, 0)) == 0) {
/* wait while alive */
}
return rb_last_status_get();
}
VALUE
rb_detach_process(rb_pid_t pid)
{
VALUE watcher = rb_thread_create(detach_process_watcher, (void*)(VALUE)pid);
rb_thread_local_aset(watcher, id_pid(), PIDT2NUM(pid));
rb_define_singleton_method(watcher, "pid", detach_process_pid, 0);
return watcher;
}
/*
* call-seq:
* Process.detach(pid) -> thread
*
* Some operating systems retain the status of terminated child
* processes until the parent collects that status (normally using
* some variant of <code>wait()</code>. If the parent never collects
* this status, the child stays around as a <em>zombie</em> process.
* <code>Process::detach</code> prevents this by setting up a
* separate Ruby thread whose sole job is to reap the status of the
* process _pid_ when it terminates. Use <code>detach</code>
* only when you do not intent to explicitly wait for the child to
* terminate.
*
* The waiting thread returns the exit status of the detached process
* when it terminates, so you can use <code>Thread#join</code> to
* know the result. If specified _pid_ is not a valid child process
* ID, the thread returns +nil+ immediately.
*
* The waiting thread has <code>pid</code> method which returns the pid.
*
* In this first example, we don't reap the first child process, so
* it appears as a zombie in the process status display.
*
* p1 = fork { sleep 0.1 }
* p2 = fork { sleep 0.2 }
* Process.waitpid(p2)
* sleep 2
* system("ps -ho pid,state -p #{p1}")
*
* <em>produces:</em>
*
* 27389 Z
*
* In the next example, <code>Process::detach</code> is used to reap
* the child automatically.
*
* p1 = fork { sleep 0.1 }
* p2 = fork { sleep 0.2 }
* Process.detach(p1)
* Process.waitpid(p2)
* sleep 2
* system("ps -ho pid,state -p #{p1}")
*
* <em>(produces no output)</em>
*/
static VALUE
proc_detach(VALUE obj, VALUE pid)
{
rb_secure(2);
return rb_detach_process(NUM2PIDT(pid));
}
#ifndef HAVE_STRING_H
char *strtok();
#endif
void rb_thread_stop_timer_thread(void);
void rb_thread_start_timer_thread(void);
void rb_thread_reset_timer_thread(void);
static int forked_child = 0;
/*
* On old MacOS X, exec() may return ENOTSUPP if the process have multiple threads.
* Therefore we have to kill internal threads at once. [ruby-core: 10583]