-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.c
9733 lines (8768 loc) · 265 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/internal/config.h"
#include "ruby/fiber/scheduler.h"
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.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
#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 <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
#include "dln.h"
#include "hrtime.h"
#include "internal.h"
#include "internal/bits.h"
#include "internal/dir.h"
#include "internal/error.h"
#include "internal/eval.h"
#include "internal/hash.h"
#include "internal/io.h"
#include "internal/numeric.h"
#include "internal/object.h"
#include "internal/process.h"
#include "internal/thread.h"
#include "internal/variable.h"
#include "internal/warnings.h"
#include "rjit.h"
#include "ruby/io.h"
#include "ruby/st.h"
#include "ruby/thread.h"
#include "ruby/util.h"
#include "vm_core.h"
#include "vm_sync.h"
#include "ruby/ractor.h"
/* define system APIs */
#ifdef _WIN32
#undef open
#define open rb_w32_uopen
#endif
#if defined(HAVE_TIMES) || defined(_WIN32)
/*********************************************************************
*
* Document-class: Process::Tms
*
* Placeholder for rusage
*/
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);
VALUE rb_envtbl(void);
VALUE rb_env_to_hash(void);
#if 1
#define p_uid_from_name p_uid_from_name
#define p_gid_from_name p_gid_from_name
#endif
#if defined(HAVE_UNISTD_H)
# if defined(HAVE_GETLOGIN_R)
# define USE_GETLOGIN_R 1
# define GETLOGIN_R_SIZE_DEFAULT 0x100
# define GETLOGIN_R_SIZE_LIMIT 0x1000
# if defined(_SC_LOGIN_NAME_MAX)
# define GETLOGIN_R_SIZE_INIT sysconf(_SC_LOGIN_NAME_MAX)
# else
# define GETLOGIN_R_SIZE_INIT GETLOGIN_R_SIZE_DEFAULT
# endif
# elif defined(HAVE_GETLOGIN)
# define USE_GETLOGIN 1
# endif
#endif
#if defined(HAVE_PWD_H)
# if defined(HAVE_GETPWUID_R)
# define USE_GETPWUID_R 1
# elif defined(HAVE_GETPWUID)
# define USE_GETPWUID 1
# endif
# if defined(HAVE_GETPWNAM_R)
# define USE_GETPWNAM_R 1
# elif defined(HAVE_GETPWNAM)
# define USE_GETPWNAM 1
# endif
# if defined(HAVE_GETPWNAM_R) || defined(HAVE_GETPWUID_R)
# define GETPW_R_SIZE_DEFAULT 0x1000
# define GETPW_R_SIZE_LIMIT 0x10000
# if defined(_SC_GETPW_R_SIZE_MAX)
# define GETPW_R_SIZE_INIT sysconf(_SC_GETPW_R_SIZE_MAX)
# else
# define GETPW_R_SIZE_INIT GETPW_R_SIZE_DEFAULT
# endif
# 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
#define id_exception idException
static ID id_in, id_out, id_err, id_pid, id_uid, id_gid;
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;
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 CLOCK_REALTIME
static ID id_CLOCK_REALTIME;
# define RUBY_CLOCK_REALTIME ID2SYM(id_CLOCK_REALTIME)
#endif
#ifdef CLOCK_MONOTONIC
static ID id_CLOCK_MONOTONIC;
# define RUBY_CLOCK_MONOTONIC ID2SYM(id_CLOCK_MONOTONIC)
#endif
#ifdef CLOCK_PROCESS_CPUTIME_ID
static ID id_CLOCK_PROCESS_CPUTIME_ID;
# define RUBY_CLOCK_PROCESS_CPUTIME_ID ID2SYM(id_CLOCK_PROCESS_CPUTIME_ID)
#endif
#ifdef CLOCK_THREAD_CPUTIME_ID
static ID id_CLOCK_THREAD_CPUTIME_ID;
# define RUBY_CLOCK_THREAD_CPUTIME_ID ID2SYM(id_CLOCK_THREAD_CPUTIME_ID)
#endif
#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;
# define RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC ID2SYM(id_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
#endif
static ID id_hertz;
static rb_pid_t cached_pid;
/* 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)
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
static VALUE
get_pid(void)
{
if (UNLIKELY(!cached_pid)) { /* 0 is not a valid pid */
cached_pid = getpid();
}
/* pid should be likely POSFIXABLE() */
return PIDT2NUM(cached_pid);
}
#if defined HAVE_WORKING_FORK || defined HAVE_DAEMON
static void
clear_pid_cache(void)
{
cached_pid = 0;
}
#endif
/*
* call-seq:
* Process.pid -> integer
*
* Returns the process ID of the current process:
*
* Process.pid # => 15668
*
*/
static VALUE
proc_get_pid(VALUE _)
{
return get_pid();
}
static VALUE
get_ppid(void)
{
return PIDT2NUM(getppid());
}
/*
* call-seq:
* Process.ppid -> integer
*
* Returns the process ID of the parent of the current process:
*
* puts "Pid is #{Process.pid}."
* fork { puts "Parent pid is #{Process.ppid}." }
*
* Output:
*
* Pid is 271290.
* Parent pid is 271290.
*
* May not return a trustworthy value on certain platforms.
*/
static VALUE
proc_get_ppid(VALUE _)
{
return get_ppid();
}
/*********************************************************************
*
* Document-class: Process::Status
*
* A Process::Status contains information about a system process.
*
* Thread-local variable <tt>$?</tt> is initially +nil+.
* Some methods assign to it a Process::Status object
* that represents a system process (either running or terminated):
*
* `ruby -e "exit 99"`
* stat = $? # => #<Process::Status: pid 1262862 exit 99>
* stat.class # => Process::Status
* stat.to_i # => 25344
* stat.stopped? # => false
* stat.exited? # => true
* stat.exitstatus # => 99
*
*/
static VALUE rb_cProcessStatus;
struct rb_process_status {
rb_pid_t pid;
int status;
int error;
};
static const rb_data_type_t rb_process_status_type = {
.wrap_struct_name = "Process::Status",
.function = {
.dmark = NULL,
.dfree = RUBY_DEFAULT_FREE,
.dsize = NULL,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
};
static VALUE
rb_process_status_allocate(VALUE klass)
{
struct rb_process_status *data;
return TypedData_Make_Struct(klass, struct rb_process_status, &rb_process_status_type, data);
}
VALUE
rb_last_status_get(void)
{
return GET_THREAD()->last_status;
}
/*
* call-seq:
* Process.last_status -> Process::Status or nil
*
* Returns a Process::Status object representing the most recently exited
* child process in the current thread, or +nil+ if none:
*
* Process.spawn('ruby', '-e', 'exit 13')
* Process.wait
* Process.last_status # => #<Process::Status: pid 14396 exit 13>
*
* Process.spawn('ruby', '-e', 'exit 14')
* Process.wait
* Process.last_status # => #<Process::Status: pid 4692 exit 14>
*
* Process.spawn('ruby', '-e', 'exit 15')
* # 'exit 15' has not been reaped by #wait.
* Process.last_status # => #<Process::Status: pid 4692 exit 14>
* Process.wait
* Process.last_status # => #<Process::Status: pid 1380 exit 15>
*
*/
static VALUE
proc_s_last_status(VALUE mod)
{
return rb_last_status_get();
}
VALUE
rb_process_status_new(rb_pid_t pid, int status, int error)
{
VALUE last_status = rb_process_status_allocate(rb_cProcessStatus);
struct rb_process_status *data = RTYPEDDATA_GET_DATA(last_status);
data->pid = pid;
data->status = status;
data->error = error;
rb_obj_freeze(last_status);
return last_status;
}
static VALUE
process_status_dump(VALUE status)
{
VALUE dump = rb_class_new_instance(0, 0, rb_cObject);
struct rb_process_status *data;
TypedData_Get_Struct(status, struct rb_process_status, &rb_process_status_type, data);
if (data->pid) {
rb_ivar_set(dump, id_status, INT2NUM(data->status));
rb_ivar_set(dump, id_pid, PIDT2NUM(data->pid));
}
return dump;
}
static VALUE
process_status_load(VALUE real_obj, VALUE load_obj)
{
struct rb_process_status *data = rb_check_typeddata(real_obj, &rb_process_status_type);
VALUE status = rb_attr_get(load_obj, id_status);
VALUE pid = rb_attr_get(load_obj, id_pid);
data->pid = NIL_P(pid) ? 0 : NUM2PIDT(pid);
data->status = NIL_P(status) ? 0 : NUM2INT(status);
return real_obj;
}
void
rb_last_status_set(int status, rb_pid_t pid)
{
GET_THREAD()->last_status = rb_process_status_new(pid, status, 0);
}
static void
last_status_clear(rb_thread_t *th)
{
th->last_status = Qnil;
}
void
rb_last_status_clear(void)
{
last_status_clear(GET_THREAD());
}
static rb_pid_t
pst_pid(VALUE status)
{
struct rb_process_status *data;
TypedData_Get_Struct(status, struct rb_process_status, &rb_process_status_type, data);
return data->pid;
}
static int
pst_status(VALUE status)
{
struct rb_process_status *data;
TypedData_Get_Struct(status, struct rb_process_status, &rb_process_status_type, data);
return data->status;
}
/*
* call-seq:
* to_i -> integer
*
* Returns the system-dependent integer status of +self+:
*
* `cat /nop`
* $?.to_i # => 256
*/
static VALUE
pst_to_i(VALUE self)
{
int status = pst_status(self);
return RB_INT2NUM(status);
}
#define PST2INT(st) pst_status(st)
/*
* call-seq:
* pid -> integer
*
* Returns the process ID of the process:
*
* system("false")
* $?.pid # => 1247002
*
*/
static VALUE
pst_pid_m(VALUE self)
{
rb_pid_t pid = pst_pid(self);
return PIDT2NUM(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:
* to_s -> string
*
* Returns a string representation of +self+:
*
* `cat /nop`
* $?.to_s # => "pid 1262141 exit 1"
*
*
*/
static VALUE
pst_to_s(VALUE st)
{
rb_pid_t pid;
int status;
VALUE str;
pid = pst_pid(st);
status = PST2INT(st);
str = rb_str_buf_new(0);
pst_message(str, pid, status);
return str;
}
/*
* call-seq:
* inspect -> string
*
* Returns a string representation of +self+:
*
* system("false")
* $?.inspect # => "#<Process::Status: pid 1303494 exit 1>"
*
*/
static VALUE
pst_inspect(VALUE st)
{
rb_pid_t pid;
int status;
VALUE str;
pid = pst_pid(st);
if (!pid) {
return rb_sprintf("#<%s: uninitialized>", rb_class2name(CLASS_OF(st)));
}
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 whether the value of #to_i == +other+:
*
* `cat /nop`
* stat = $? # => #<Process::Status: pid 1170366 exit 1>
* sprintf('%x', stat.to_i) # => "100"
* stat == 0x100 # => true
*
*/
static VALUE
pst_equal(VALUE st1, VALUE st2)
{
if (st1 == st2) return Qtrue;
return rb_equal(pst_to_i(st1), st2);
}
/*
* call-seq:
* stat & mask -> integer
*
* This method is deprecated as #to_i value is system-specific; use
* predicate methods like #exited? or #stopped?, or getters like #exitstatus
* or #stopsig.
*
* Returns the logical AND of the value of #to_i with +mask+:
*
* `cat /nop`
* stat = $? # => #<Process::Status: pid 1155508 exit 1>
* sprintf('%x', stat.to_i) # => "100"
* stat & 0x00 # => 0
*
* ArgumentError is raised if +mask+ is negative.
*/
static VALUE
pst_bitand(VALUE st1, VALUE st2)
{
int status = PST2INT(st1);
int mask = NUM2INT(st2);
if (mask < 0) {
rb_raise(rb_eArgError, "negative mask value: %d", mask);
}
#define WARN_SUGGEST(suggest) \
rb_warn_deprecated_to_remove_at(3.5, "Process::Status#&", suggest)
switch (mask) {
case 0x80:
WARN_SUGGEST("Process::Status#coredump?");
break;
case 0x7f:
WARN_SUGGEST("Process::Status#signaled? or Process::Status#termsig");
break;
case 0xff:
WARN_SUGGEST("Process::Status#exited?, Process::Status#stopped? or Process::Status#coredump?");
break;
case 0xff00:
WARN_SUGGEST("Process::Status#exitstatus or Process::Status#stopsig");
break;
default:
WARN_SUGGEST("other Process::Status predicates");
break;
}
#undef WARN_SUGGEST
status &= mask;
return INT2NUM(status);
}
/*
* call-seq:
* stat >> places -> integer
*
* This method is deprecated as #to_i value is system-specific; use
* predicate methods like #exited? or #stopped?, or getters like #exitstatus
* or #stopsig.
*
* Returns the value of #to_i, shifted +places+ to the right:
*
* `cat /nop`
* stat = $? # => #<Process::Status: pid 1155508 exit 1>
* stat.to_i # => 256
* stat >> 1 # => 128
* stat >> 2 # => 64
*
* ArgumentError is raised if +places+ is negative.
*/
static VALUE
pst_rshift(VALUE st1, VALUE st2)
{
int status = PST2INT(st1);
int places = NUM2INT(st2);
if (places < 0) {
rb_raise(rb_eArgError, "negative shift value: %d", places);
}
#define WARN_SUGGEST(suggest) \
rb_warn_deprecated_to_remove_at(3.5, "Process::Status#>>", suggest)
switch (places) {
case 7:
WARN_SUGGEST("Process::Status#coredump?");
break;
case 8:
WARN_SUGGEST("Process::Status#exitstatus or Process::Status#stopsig");
break;
default:
WARN_SUGGEST("other Process::Status attributes");
break;
}
#undef WARN_SUGGEST
status >>= places;
return INT2NUM(status);
}
/*
* call-seq:
* stopped? -> true or false
*
* Returns +true+ if this process is stopped,
* and if the corresponding #wait call had the Process::WUNTRACED flag set,
* +false+ otherwise.
*/
static VALUE
pst_wifstopped(VALUE st)
{
int status = PST2INT(st);
return RBOOL(WIFSTOPPED(status));
}
/*
* call-seq:
* stopsig -> integer or nil
*