forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.c
8496 lines (7655 loc) · 228 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/config.h"
#include "ruby/io.h"
#include "internal.h"
#include "ruby/thread.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
#ifdef HAVE_PROCESS_H
#include <process.h>
#endif
#include <time.h>
#include <ctype.h>
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#ifdef HAVE_VFORK_H
# include <vfork.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifndef MAXPATHLEN
# define MAXPATHLEN 1024
#endif
#include "ruby/st.h"
#include <sys/stat.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_TIMES_H
#include <sys/times.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
# ifdef __CYGWIN__
int initgroups(const char *, rb_gid_t);
# endif
#endif
#ifdef HAVE_SYS_ID_H
#include <sys/id.h>
#endif
#ifdef __APPLE__
# include <mach/mach_time.h>
#endif
/* define system APIs */
#ifdef _WIN32
#undef open
#define open rb_w32_uopen
#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(__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
int setreuid(rb_uid_t ruid, rb_uid_t euid);
#endif
#ifdef BROKEN_SETREGID
#define setregid ruby_setregid
int setregid(rb_gid_t rgid, rb_gid_t egid);
#endif
#if defined(HAVE_44BSD_SETUID) || defined(__APPLE__)
#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
static void check_uid_switch(void);
static void check_gid_switch(void);
static int exec_async_signal_safe(const struct rb_execarg *, char *, size_t);
#if 1
#define p_uid_from_name p_uid_from_name
#define p_gid_from_name p_gid_from_name
#endif
#if defined(HAVE_PWD_H)
# if defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
# define USE_GETPWNAM_R 1
# define GETPW_R_SIZE_INIT sysconf(_SC_GETPW_R_SIZE_MAX)
# define GETPW_R_SIZE_DEFAULT 0x1000
# define GETPW_R_SIZE_LIMIT 0x10000
# endif
# ifdef USE_GETPWNAM_R
# define PREPARE_GETPWNAM \
VALUE getpw_buf = 0
# define FINISH_GETPWNAM \
(getpw_buf ? (void)rb_str_resize(getpw_buf, 0) : (void)0)
# define OBJ2UID1(id) obj2uid((id), &getpw_buf)
# define OBJ2UID(id) obj2uid0(id)
static rb_uid_t obj2uid(VALUE id, VALUE *getpw_buf);
static inline rb_uid_t
obj2uid0(VALUE id)
{
rb_uid_t uid;
PREPARE_GETPWNAM;
uid = OBJ2UID1(id);
FINISH_GETPWNAM;
return uid;
}
# else
# define PREPARE_GETPWNAM /* do nothing */
# define FINISH_GETPWNAM /* do nothing */
# define OBJ2UID1(id) obj2uid((id))
# define OBJ2UID(id) obj2uid((id))
static rb_uid_t obj2uid(VALUE id);
# endif
#else
# define PREPARE_GETPWNAM /* do nothing */
# define FINISH_GETPWNAM /* do nothing */
# define OBJ2UID1(id) NUM2UIDT(id)
# define OBJ2UID(id) NUM2UIDT(id)
# ifdef p_uid_from_name
# undef p_uid_from_name
# define p_uid_from_name rb_f_notimplement
# endif
#endif
#if defined(HAVE_GRP_H)
# if defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
# define USE_GETGRNAM_R
# define GETGR_R_SIZE_INIT sysconf(_SC_GETGR_R_SIZE_MAX)
# define GETGR_R_SIZE_DEFAULT 0x1000
# define GETGR_R_SIZE_LIMIT 0x10000
# endif
# ifdef USE_GETGRNAM_R
# define PREPARE_GETGRNAM \
VALUE getgr_buf = 0
# define FINISH_GETGRNAM \
(getgr_buf ? (void)rb_str_resize(getgr_buf, 0) : (void)0)
# define OBJ2GID1(id) obj2gid((id), &getgr_buf)
# define OBJ2GID(id) obj2gid0(id)
static rb_gid_t obj2gid(VALUE id, VALUE *getgr_buf);
static inline rb_gid_t
obj2gid0(VALUE id)
{
rb_gid_t gid;
PREPARE_GETGRNAM;
gid = OBJ2GID1(id);
FINISH_GETGRNAM;
return gid;
}
static rb_gid_t obj2gid(VALUE id, VALUE *getgr_buf);
# else
# define PREPARE_GETGRNAM /* do nothing */
# define FINISH_GETGRNAM /* do nothing */
# define OBJ2GID1(id) obj2gid((id))
# define OBJ2GID(id) obj2gid((id))
static rb_gid_t obj2gid(VALUE id);
# endif
#else
# define PREPARE_GETGRNAM /* do nothing */
# define FINISH_GETGRNAM /* do nothing */
# define OBJ2GID1(id) NUM2GIDT(id)
# define OBJ2GID(id) NUM2GIDT(id)
# ifdef p_gid_from_name
# undef p_gid_from_name
# define p_gid_from_name rb_f_notimplement
# endif
#endif
#if SIZEOF_CLOCK_T == SIZEOF_INT
typedef unsigned int unsigned_clock_t;
#elif SIZEOF_CLOCK_T == SIZEOF_LONG
typedef unsigned long unsigned_clock_t;
#elif defined(HAVE_LONG_LONG) && SIZEOF_CLOCK_T == SIZEOF_LONG_LONG
typedef unsigned LONG_LONG unsigned_clock_t;
#endif
#ifndef HAVE_SIG_T
typedef void (*sig_t) (int);
#endif
static ID id_in, id_out, id_err, id_pid, id_uid, id_gid, id_exception;
static ID id_close, id_child;
#ifdef HAVE_SETPGID
static ID id_pgroup;
#endif
#ifdef _WIN32
static ID id_new_pgroup;
#endif
static ID id_unsetenv_others, id_chdir, id_umask, id_close_others, id_ENV;
static ID id_nanosecond, id_microsecond, id_millisecond, id_second;
static ID id_float_microsecond, id_float_millisecond, id_float_second;
static ID id_GETTIMEOFDAY_BASED_CLOCK_REALTIME, id_TIME_BASED_CLOCK_REALTIME;
#ifdef HAVE_TIMES
static ID id_TIMES_BASED_CLOCK_MONOTONIC;
static ID id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID;
#endif
#ifdef RUSAGE_SELF
static ID id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID;
#endif
static ID id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID;
#ifdef __APPLE__
static ID id_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC;
#endif
static ID id_hertz;
/* execv and execl are async-signal-safe since SUSv4 (POSIX.1-2008, XPG7) */
#if defined(__sun) && !defined(_XPG7) /* Solaris 10, 9, ... */
#define execv(path, argv) (rb_async_bug_errno("unreachable: async-signal-unsafe execv() is called", 0))
#define execl(path, arg0, arg1, arg2, term) do { extern char **environ; execle((path), (arg0), (arg1), (arg2), (term), (environ)); } while (0)
#define ALWAYS_NEED_ENVP 1
#else
#define ALWAYS_NEED_ENVP 0
#endif
static void
assert_close_on_exec(int fd)
{
#if VM_CHECK_MODE > 0
#if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(FD_CLOEXEC)
int flags = fcntl(fd, F_GETFD);
if (flags == -1) {
static const char m[] = "reserved FD closed unexpectedly?\n";
(void)!write(2, m, sizeof(m) - 1);
return;
}
if (flags & FD_CLOEXEC) return;
rb_bug("reserved FD did not have close-on-exec set");
#else
rb_bug("reserved FD without close-on-exec support");
#endif /* FD_CLOEXEC */
#endif /* VM_CHECK_MODE */
}
static inline int
close_unless_reserved(int fd)
{
if (rb_reserved_fd_p(fd)) { /* async-signal-safe */
assert_close_on_exec(fd);
return 0;
}
return close(fd); /* async-signal-safe */
}
/*#define DEBUG_REDIRECT*/
#if defined(DEBUG_REDIRECT)
#include <stdarg.h>
static void
ttyprintf(const char *fmt, ...)
{
va_list ap;
FILE *tty;
int save = errno;
#ifdef _WIN32
tty = fopen("con", "w");
#else
tty = fopen("/dev/tty", "w");
#endif
if (!tty)
return;
va_start(ap, fmt);
vfprintf(tty, fmt, ap);
va_end(ap);
fclose(tty);
errno = save;
}
static int
redirect_dup(int oldfd)
{
int ret;
ret = dup(oldfd);
ttyprintf("dup(%d) => %d\n", oldfd, ret);
return ret;
}
static int
redirect_dup2(int oldfd, int newfd)
{
int ret;
ret = dup2(oldfd, newfd);
ttyprintf("dup2(%d, %d) => %d\n", oldfd, newfd, ret);
return ret;
}
static int
redirect_cloexec_dup(int oldfd)
{
int ret;
ret = rb_cloexec_dup(oldfd);
ttyprintf("cloexec_dup(%d) => %d\n", oldfd, ret);
return ret;
}
static int
redirect_cloexec_dup2(int oldfd, int newfd)
{
int ret;
ret = rb_cloexec_dup2(oldfd, newfd);
ttyprintf("cloexec_dup2(%d, %d) => %d\n", oldfd, newfd, ret);
return ret;
}
static int
redirect_close(int fd)
{
int ret;
ret = close_unless_reserved(fd);
ttyprintf("close(%d) => %d\n", fd, ret);
return ret;
}
static int
parent_redirect_open(const char *pathname, int flags, mode_t perm)
{
int ret;
ret = rb_cloexec_open(pathname, flags, perm);
ttyprintf("parent_open(\"%s\", 0x%x, 0%o) => %d\n", pathname, flags, perm, ret);
return ret;
}
static int
parent_redirect_close(int fd)
{
int ret;
ret = close_unless_reserved(fd);
ttyprintf("parent_close(%d) => %d\n", fd, ret);
return ret;
}
#else
#define redirect_dup(oldfd) dup(oldfd)
#define redirect_dup2(oldfd, newfd) dup2((oldfd), (newfd))
#define redirect_cloexec_dup(oldfd) rb_cloexec_dup(oldfd)
#define redirect_cloexec_dup2(oldfd, newfd) rb_cloexec_dup2((oldfd), (newfd))
#define redirect_close(fd) close_unless_reserved(fd)
#define parent_redirect_open(pathname, flags, perm) rb_cloexec_open((pathname), (flags), (perm))
#define parent_redirect_close(fd) close_unless_reserved(fd)
#endif
/*
* Document-module: Process
*
* Module to handle processes.
*/
/*
* call-seq:
* Process.pid -> integer
*
* Returns the process id of this process. Not available on all
* platforms.
*
* Process.pid #=> 27415
*/
static VALUE
get_pid(void)
{
return PIDT2NUM(getpid());
}
/*
* call-seq:
* Process.ppid -> integer
*
* 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)
{
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;
}
/*
* call-seq:
* Process.last_status -> Process::Status or nil
*
* Returns the status of the last executed child process in the
* current thread.
*
* Process.wait Process.spawn("ruby", "-e", "exit 13")
* Process.last_status #=> #<Process::Status: pid 4825 exit 13>
*
* If no child process has ever been executed in the current
* thread, this returns +nil+.
*
* Process.last_status #=> nil
*/
static VALUE
proc_s_last_status(VALUE mod)
{
return rb_last_status_get();
}
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_ivar_set(th->last_status, id_status, INT2FIX(status));
rb_ivar_set(th->last_status, id_pid, PIDT2NUM(pid));
}
void
rb_last_status_clear(void)
{
GET_THREAD()->last_status = Qnil;
}
/*
* call-seq:
* stat.to_i -> integer
* stat.to_int -> integer
*
* Returns the bits in _stat_ as a <code>Integer</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_ivar_get(st, id_status);
}
#define PST2INT(st) NUM2INT(pst_to_i(st))
/*
* call-seq:
* stat.pid -> integer
*
* 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, id_pid);
}
static VALUE pst_message_status(VALUE str, int status);
static void
pst_message(VALUE str, rb_pid_t pid, int status)
{
rb_str_catf(str, "pid %ld", (long)pid);
pst_message_status(str, status);
}
static VALUE
pst_message_status(VALUE str, int status)
{
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
return str;
}
/*
* 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 -> integer
*
* 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 -> integer
*
* 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 -> integer 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 -> integer 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 -> integer 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
}
static rb_pid_t
do_waitpid(rb_pid_t pid, int *st, int flags)
{
#if defined HAVE_WAITPID
return waitpid(pid, st, flags);
#elif defined HAVE_WAIT4
return wait4(pid, st, flags, NULL);
#else
# error waitpid or wait4 is required.
#endif
}
struct waitpid_state {
struct list_node wnode;
rb_execution_context_t *ec;
rb_nativethread_cond_t *cond;
rb_pid_t ret;
rb_pid_t pid;
int status;
int options;
int errnum;
int sigwait_fd;
};
void rb_native_mutex_lock(rb_nativethread_lock_t *);
void rb_native_mutex_unlock(rb_nativethread_lock_t *);
void rb_native_cond_signal(rb_nativethread_cond_t *);
void rb_native_cond_wait(rb_nativethread_cond_t *, rb_nativethread_lock_t *);
rb_nativethread_cond_t *rb_sleep_cond_get(const rb_execution_context_t *);
void rb_sleep_cond_put(rb_nativethread_cond_t *);
int rb_sigwait_fd_get(const rb_thread_t *);
void rb_sigwait_sleep(const rb_thread_t *, int fd, const struct timespec *);
void rb_sigwait_fd_put(const rb_thread_t *, int fd);
static int
sigwait_fd_migrate_signaled_p(struct waitpid_state *w)
{
int signaled = FALSE;
rb_thread_t *th = w->ec ? rb_ec_thread_ptr(w->ec) : 0;
if (th) rb_native_mutex_lock(&th->interrupt_lock);
if (w->cond) {
rb_native_cond_signal(w->cond);
signaled = TRUE;
}
if (th) rb_native_mutex_unlock(&th->interrupt_lock);
return signaled;
}
/*
* When a thread is done using sigwait_fd and there are other threads
* sleeping on waitpid, we must kick one of the threads out of
* rb_native_cond_wait so it can switch to rb_sigwait_sleep
*/
static void
sigwait_fd_migrate_sleeper(rb_vm_t *vm)
{
struct waitpid_state *w = 0;
list_for_each(&vm->waiting_pids, w, wnode) {
if (sigwait_fd_migrate_signaled_p(w)) return;
}
list_for_each(&vm->waiting_grps, w, wnode) {
if (sigwait_fd_migrate_signaled_p(w)) return;
}
}
void
rb_sigwait_fd_migrate(rb_vm_t *vm)
{
rb_native_mutex_lock(&vm->waitpid_lock);
sigwait_fd_migrate_sleeper(vm);
rb_native_mutex_unlock(&vm->waitpid_lock);
}
static void
waitpid_notify(struct waitpid_state *w, rb_pid_t ret)
{
w->ret = ret;
list_del_init(&w->wnode);
if (w->cond) {
rb_native_cond_signal(w->cond);
}
else {
/* w is owned by this thread */
}
}
#ifdef _WIN32 /* for spawnvp result from mjit.c */
# define waitpid_sys(pid,status,options) \
(WaitForSingleObject((HANDLE)(pid), 0),\
GetExitCodeProcess((HANDLE)(pid), (LPDWORD)(status)))