-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
pcntl.c
1642 lines (1442 loc) · 44.2 KB
/
pcntl.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
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Jason Greene <[email protected]> |
+----------------------------------------------------------------------+
*/
#define PCNTL_DEBUG 0
#if PCNTL_DEBUG
#define DEBUG_OUT printf("DEBUG: ");printf
#define IF_DEBUG(z) z
#else
#define IF_DEBUG(z)
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_pcntl.h"
#include "pcntl_arginfo.h"
#include "php_signal.h"
#include "php_ticks.h"
#if defined(HAVE_GETPRIORITY) || defined(HAVE_SETPRIORITY) || defined(HAVE_WAIT3)
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#endif
#include <errno.h>
#ifdef HAVE_UNSHARE
#include <sched.h>
#endif
#ifdef HAVE_FORKX
#include <sys/fork.h>
#endif
#ifndef NSIG
# define NSIG 32
#endif
ZEND_DECLARE_MODULE_GLOBALS(pcntl)
static PHP_GINIT_FUNCTION(pcntl);
zend_module_entry pcntl_module_entry = {
STANDARD_MODULE_HEADER,
"pcntl",
ext_functions,
PHP_MINIT(pcntl),
PHP_MSHUTDOWN(pcntl),
PHP_RINIT(pcntl),
PHP_RSHUTDOWN(pcntl),
PHP_MINFO(pcntl),
PHP_PCNTL_VERSION,
PHP_MODULE_GLOBALS(pcntl),
PHP_GINIT(pcntl),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
#ifdef COMPILE_DL_PCNTL
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(pcntl)
#endif
static void (*orig_interrupt_function)(zend_execute_data *execute_data);
#ifdef HAVE_STRUCT_SIGINFO_T
static void pcntl_signal_handler(int, siginfo_t*, void*);
static void pcntl_siginfo_to_zval(int, siginfo_t*, zval*);
#else
static void pcntl_signal_handler(int);
#endif
static void pcntl_signal_dispatch(void);
static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer);
static void pcntl_interrupt_function(zend_execute_data *execute_data);
void php_register_signal_constants(INIT_FUNC_ARGS)
{
/* Wait Constants */
#ifdef WNOHANG
REGISTER_LONG_CONSTANT("WNOHANG", (zend_long) WNOHANG, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef WUNTRACED
REGISTER_LONG_CONSTANT("WUNTRACED", (zend_long) WUNTRACED, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef HAVE_WCONTINUED
REGISTER_LONG_CONSTANT("WCONTINUED", (zend_long) WCONTINUED, CONST_CS | CONST_PERSISTENT);
#endif
/* Signal Constants */
REGISTER_LONG_CONSTANT("SIG_IGN", (zend_long) SIG_IGN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIG_DFL", (zend_long) SIG_DFL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIG_ERR", (zend_long) SIG_ERR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGHUP", (zend_long) SIGHUP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGINT", (zend_long) SIGINT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGQUIT", (zend_long) SIGQUIT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGILL", (zend_long) SIGILL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGTRAP", (zend_long) SIGTRAP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGABRT", (zend_long) SIGABRT, CONST_CS | CONST_PERSISTENT);
#ifdef SIGIOT
REGISTER_LONG_CONSTANT("SIGIOT", (zend_long) SIGIOT, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SIGBUS", (zend_long) SIGBUS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGFPE", (zend_long) SIGFPE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGKILL", (zend_long) SIGKILL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGUSR1", (zend_long) SIGUSR1, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGSEGV", (zend_long) SIGSEGV, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGUSR2", (zend_long) SIGUSR2, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGPIPE", (zend_long) SIGPIPE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGALRM", (zend_long) SIGALRM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGTERM", (zend_long) SIGTERM, CONST_CS | CONST_PERSISTENT);
#ifdef SIGSTKFLT
REGISTER_LONG_CONSTANT("SIGSTKFLT",(zend_long) SIGSTKFLT, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SIGCLD
REGISTER_LONG_CONSTANT("SIGCLD", (zend_long) SIGCLD, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SIGCHLD
REGISTER_LONG_CONSTANT("SIGCHLD", (zend_long) SIGCHLD, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SIGCONT", (zend_long) SIGCONT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGSTOP", (zend_long) SIGSTOP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGTSTP", (zend_long) SIGTSTP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGTTIN", (zend_long) SIGTTIN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGTTOU", (zend_long) SIGTTOU, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGURG", (zend_long) SIGURG , CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGXCPU", (zend_long) SIGXCPU, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGXFSZ", (zend_long) SIGXFSZ, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGVTALRM",(zend_long) SIGVTALRM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGPROF", (zend_long) SIGPROF, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGWINCH", (zend_long) SIGWINCH, CONST_CS | CONST_PERSISTENT);
#ifdef SIGPOLL
REGISTER_LONG_CONSTANT("SIGPOLL", (zend_long) SIGPOLL, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SIGIO
REGISTER_LONG_CONSTANT("SIGIO", (zend_long) SIGIO, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SIGPWR
REGISTER_LONG_CONSTANT("SIGPWR", (zend_long) SIGPWR, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SIGSYS
REGISTER_LONG_CONSTANT("SIGSYS", (zend_long) SIGSYS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGBABY", (zend_long) SIGSYS, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SIGRTMIN
REGISTER_LONG_CONSTANT("SIGRTMIN", (zend_long) SIGRTMIN, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SIGRTMAX
REGISTER_LONG_CONSTANT("SIGRTMAX", (zend_long) SIGRTMAX, CONST_CS | CONST_PERSISTENT);
#endif
#if defined(HAVE_GETPRIORITY) || defined(HAVE_SETPRIORITY)
REGISTER_LONG_CONSTANT("PRIO_PGRP", PRIO_PGRP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PRIO_USER", PRIO_USER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PRIO_PROCESS", PRIO_PROCESS, CONST_CS | CONST_PERSISTENT);
#if defined(PRIO_DARWIN_BG)
REGISTER_LONG_CONSTANT("PRIO_DARWIN_BG", PRIO_DARWIN_BG, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PRIO_DARWIN_THREAD", PRIO_DARWIN_THREAD, CONST_CS | CONST_PERSISTENT);
#endif
#endif
/* {{{ "how" argument for sigprocmask */
#ifdef HAVE_SIGPROCMASK
REGISTER_LONG_CONSTANT("SIG_BLOCK", SIG_BLOCK, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIG_UNBLOCK", SIG_UNBLOCK, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIG_SETMASK", SIG_SETMASK, CONST_CS | CONST_PERSISTENT);
#endif
/* }}} */
/* {{{ si_code */
#if defined(HAVE_SIGWAITINFO) && defined(HAVE_SIGTIMEDWAIT)
REGISTER_LONG_CONSTANT("SI_USER", SI_USER, CONST_CS | CONST_PERSISTENT);
#ifdef SI_NOINFO
REGISTER_LONG_CONSTANT("SI_NOINFO", SI_NOINFO, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SI_KERNEL
REGISTER_LONG_CONSTANT("SI_KERNEL", SI_KERNEL, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SI_QUEUE", SI_QUEUE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SI_TIMER", SI_TIMER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SI_MESGQ", SI_MESGQ, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SI_ASYNCIO", SI_ASYNCIO, CONST_CS | CONST_PERSISTENT);
#ifdef SI_SIGIO
REGISTER_LONG_CONSTANT("SI_SIGIO", SI_SIGIO, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SI_TKILL
REGISTER_LONG_CONSTANT("SI_TKILL", SI_TKILL, CONST_CS | CONST_PERSISTENT);
#endif
/* si_code for SIGCHILD */
#ifdef CLD_EXITED
REGISTER_LONG_CONSTANT("CLD_EXITED", CLD_EXITED, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef CLD_KILLED
REGISTER_LONG_CONSTANT("CLD_KILLED", CLD_KILLED, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef CLD_DUMPED
REGISTER_LONG_CONSTANT("CLD_DUMPED", CLD_DUMPED, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef CLD_TRAPPED
REGISTER_LONG_CONSTANT("CLD_TRAPPED", CLD_TRAPPED, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef CLD_STOPPED
REGISTER_LONG_CONSTANT("CLD_STOPPED", CLD_STOPPED, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef CLD_CONTINUED
REGISTER_LONG_CONSTANT("CLD_CONTINUED", CLD_CONTINUED, CONST_CS | CONST_PERSISTENT);
#endif
/* si_code for SIGTRAP */
#ifdef TRAP_BRKPT
REGISTER_LONG_CONSTANT("TRAP_BRKPT", TRAP_BRKPT, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef TRAP_TRACE
REGISTER_LONG_CONSTANT("TRAP_TRACE", TRAP_TRACE, CONST_CS | CONST_PERSISTENT);
#endif
/* si_code for SIGPOLL */
#ifdef POLL_IN
REGISTER_LONG_CONSTANT("POLL_IN", POLL_IN, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef POLL_OUT
REGISTER_LONG_CONSTANT("POLL_OUT", POLL_OUT, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef POLL_MSG
REGISTER_LONG_CONSTANT("POLL_MSG", POLL_MSG, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef POLL_ERR
REGISTER_LONG_CONSTANT("POLL_ERR", POLL_ERR, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef POLL_PRI
REGISTER_LONG_CONSTANT("POLL_PRI", POLL_PRI, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef POLL_HUP
REGISTER_LONG_CONSTANT("POLL_HUP", POLL_HUP, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef ILL_ILLOPC
REGISTER_LONG_CONSTANT("ILL_ILLOPC", ILL_ILLOPC, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef ILL_ILLOPN
REGISTER_LONG_CONSTANT("ILL_ILLOPN", ILL_ILLOPN, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef ILL_ILLADR
REGISTER_LONG_CONSTANT("ILL_ILLADR", ILL_ILLADR, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef ILL_ILLTRP
REGISTER_LONG_CONSTANT("ILL_ILLTRP", ILL_ILLTRP, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef ILL_PRVOPC
REGISTER_LONG_CONSTANT("ILL_PRVOPC", ILL_PRVOPC, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef ILL_PRVREG
REGISTER_LONG_CONSTANT("ILL_PRVREG", ILL_PRVREG, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef ILL_COPROC
REGISTER_LONG_CONSTANT("ILL_COPROC", ILL_COPROC, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef ILL_BADSTK
REGISTER_LONG_CONSTANT("ILL_BADSTK", ILL_BADSTK, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef FPE_INTDIV
REGISTER_LONG_CONSTANT("FPE_INTDIV", FPE_INTDIV, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef FPE_INTOVF
REGISTER_LONG_CONSTANT("FPE_INTOVF", FPE_INTOVF, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef FPE_FLTDIV
REGISTER_LONG_CONSTANT("FPE_FLTDIV", FPE_FLTDIV, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef FPE_FLTOVF
REGISTER_LONG_CONSTANT("FPE_FLTOVF", FPE_FLTOVF, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef FPE_FLTUND
REGISTER_LONG_CONSTANT("FPE_FLTUND", FPE_FLTINV, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef FPE_FLTRES
REGISTER_LONG_CONSTANT("FPE_FLTRES", FPE_FLTRES, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef FPE_FLTINV
REGISTER_LONG_CONSTANT("FPE_FLTINV", FPE_FLTINV, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef FPE_FLTSUB
REGISTER_LONG_CONSTANT("FPE_FLTSUB", FPE_FLTSUB, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SEGV_MAPERR
REGISTER_LONG_CONSTANT("SEGV_MAPERR", SEGV_MAPERR, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SEGV_ACCERR
REGISTER_LONG_CONSTANT("SEGV_ACCERR", SEGV_ACCERR, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef BUS_ADRALN
REGISTER_LONG_CONSTANT("BUS_ADRALN", BUS_ADRALN, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef BUS_ADRERR
REGISTER_LONG_CONSTANT("BUS_ADRERR", BUS_ADRERR, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef BUS_OBJERR
REGISTER_LONG_CONSTANT("BUS_OBJERR", BUS_OBJERR, CONST_CS | CONST_PERSISTENT);
#endif
#endif /* defined(HAVE_SIGWAITINFO) && defined(HAVE_SIGTIMEDWAIT) */
/* }}} */
/* unshare(/clone) constants */
#ifdef HAVE_UNSHARE
REGISTER_LONG_CONSTANT("CLONE_NEWNS", CLONE_NEWNS, CONST_CS | CONST_PERSISTENT);
#ifdef CLONE_NEWIPC
REGISTER_LONG_CONSTANT("CLONE_NEWIPC", CLONE_NEWIPC, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef CLONE_NEWUTS
REGISTER_LONG_CONSTANT("CLONE_NEWUTS", CLONE_NEWUTS, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef CLONE_NEWNET
REGISTER_LONG_CONSTANT("CLONE_NEWNET", CLONE_NEWNET, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef CLONE_NEWPID
REGISTER_LONG_CONSTANT("CLONE_NEWPID", CLONE_NEWPID, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef CLONE_NEWUSER
REGISTER_LONG_CONSTANT("CLONE_NEWUSER", CLONE_NEWUSER, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef CLONE_NEWCGROUP
REGISTER_LONG_CONSTANT("CLONE_NEWCGROUP", CLONE_NEWCGROUP, CONST_CS | CONST_PERSISTENT);
#endif
#endif
#ifdef HAVE_RFORK
#ifdef RFPROC
REGISTER_LONG_CONSTANT("RFPROC", RFPROC, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef RFNOWAIT
REGISTER_LONG_CONSTANT("RFNOWAIT", RFNOWAIT, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef RFCFDG
REGISTER_LONG_CONSTANT("RFCFDG", RFCFDG, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef RFFDG
REGISTER_LONG_CONSTANT("RFFDG", RFFDG, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef RFLINUXTHPN
REGISTER_LONG_CONSTANT("RFLINUXTHPN", RFLINUXTHPN, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef RFTSIGZMB
REGISTER_LONG_CONSTANT("RFTSIGZMB", RFTSIGZMB, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef RFTHREAD
REGISTER_LONG_CONSTANT("RFTHREAD", RFTHREAD, CONST_CS | CONST_PERSISTENT);
#endif
#endif
#ifdef HAVE_FORKX
REGISTER_LONG_CONSTANT("FORK_NOSIGCHLD", FORK_NOSIGCHLD, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("FORK_WAITPID", FORK_WAITPID, CONST_CS | CONST_PERSISTENT);
#endif
}
static void php_pcntl_register_errno_constants(INIT_FUNC_ARGS)
{
#ifdef EINTR
REGISTER_PCNTL_ERRNO_CONSTANT(EINTR);
#endif
#ifdef ECHILD
REGISTER_PCNTL_ERRNO_CONSTANT(ECHILD);
#endif
#ifdef EINVAL
REGISTER_PCNTL_ERRNO_CONSTANT(EINVAL);
#endif
#ifdef EAGAIN
REGISTER_PCNTL_ERRNO_CONSTANT(EAGAIN);
#endif
#ifdef ESRCH
REGISTER_PCNTL_ERRNO_CONSTANT(ESRCH);
#endif
#ifdef EACCES
REGISTER_PCNTL_ERRNO_CONSTANT(EACCES);
#endif
#ifdef EPERM
REGISTER_PCNTL_ERRNO_CONSTANT(EPERM);
#endif
#ifdef ENOMEM
REGISTER_PCNTL_ERRNO_CONSTANT(ENOMEM);
#endif
#ifdef E2BIG
REGISTER_PCNTL_ERRNO_CONSTANT(E2BIG);
#endif
#ifdef EFAULT
REGISTER_PCNTL_ERRNO_CONSTANT(EFAULT);
#endif
#ifdef EIO
REGISTER_PCNTL_ERRNO_CONSTANT(EIO);
#endif
#ifdef EISDIR
REGISTER_PCNTL_ERRNO_CONSTANT(EISDIR);
#endif
#ifdef ELIBBAD
REGISTER_PCNTL_ERRNO_CONSTANT(ELIBBAD);
#endif
#ifdef ELOOP
REGISTER_PCNTL_ERRNO_CONSTANT(ELOOP);
#endif
#ifdef EMFILE
REGISTER_PCNTL_ERRNO_CONSTANT(EMFILE);
#endif
#ifdef ENAMETOOLONG
REGISTER_PCNTL_ERRNO_CONSTANT(ENAMETOOLONG);
#endif
#ifdef ENFILE
REGISTER_PCNTL_ERRNO_CONSTANT(ENFILE);
#endif
#ifdef ENOENT
REGISTER_PCNTL_ERRNO_CONSTANT(ENOENT);
#endif
#ifdef ENOEXEC
REGISTER_PCNTL_ERRNO_CONSTANT(ENOEXEC);
#endif
#ifdef ENOTDIR
REGISTER_PCNTL_ERRNO_CONSTANT(ENOTDIR);
#endif
#ifdef ETXTBSY
REGISTER_PCNTL_ERRNO_CONSTANT(ETXTBSY);
#endif
#ifdef ENOSPC
REGISTER_PCNTL_ERRNO_CONSTANT(ENOSPC);
#endif
#ifdef EUSERS
REGISTER_PCNTL_ERRNO_CONSTANT(EUSERS);
#endif
#ifdef ECAPMODE
REGISTER_PCNTL_ERRNO_CONSTANT(ECAPMODE);
#endif
}
static PHP_GINIT_FUNCTION(pcntl)
{
#if defined(COMPILE_DL_PCNTL) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
memset(pcntl_globals, 0, sizeof(*pcntl_globals));
}
PHP_RINIT_FUNCTION(pcntl)
{
php_add_tick_function(pcntl_signal_dispatch_tick_function, NULL);
zend_hash_init(&PCNTL_G(php_signal_table), 16, NULL, ZVAL_PTR_DTOR, 0);
PCNTL_G(head) = PCNTL_G(tail) = PCNTL_G(spares) = NULL;
PCNTL_G(async_signals) = 0;
PCNTL_G(last_error) = 0;
PCNTL_G(num_signals) = NSIG;
#ifdef SIGRTMAX
/* At least FreeBSD reports an incorrecrt NSIG that does not include realtime signals.
* As SIGRTMAX may be a dynamic value, adjust the value in INIT. */
if (NSIG < SIGRTMAX + 1) {
PCNTL_G(num_signals) = SIGRTMAX + 1;
}
#endif
return SUCCESS;
}
PHP_MINIT_FUNCTION(pcntl)
{
php_register_signal_constants(INIT_FUNC_ARGS_PASSTHRU);
php_pcntl_register_errno_constants(INIT_FUNC_ARGS_PASSTHRU);
orig_interrupt_function = zend_interrupt_function;
zend_interrupt_function = pcntl_interrupt_function;
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(pcntl)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(pcntl)
{
struct php_pcntl_pending_signal *sig;
/* FIXME: if a signal is delivered after this point, things will go pear shaped;
* need to remove signal handlers */
zend_hash_destroy(&PCNTL_G(php_signal_table));
while (PCNTL_G(head)) {
sig = PCNTL_G(head);
PCNTL_G(head) = sig->next;
efree(sig);
}
while (PCNTL_G(spares)) {
sig = PCNTL_G(spares);
PCNTL_G(spares) = sig->next;
efree(sig);
}
return SUCCESS;
}
PHP_MINFO_FUNCTION(pcntl)
{
php_info_print_table_start();
php_info_print_table_header(2, "pcntl support", "enabled");
php_info_print_table_end();
}
/* {{{ Forks the currently running process following the same behavior as the UNIX fork() system call*/
PHP_FUNCTION(pcntl_fork)
{
pid_t id;
ZEND_PARSE_PARAMETERS_NONE();
id = fork();
if (id == -1) {
PCNTL_G(last_error) = errno;
php_error_docref(NULL, E_WARNING, "Error %d", errno);
}
RETURN_LONG((zend_long) id);
}
/* }}} */
/* {{{ Set an alarm clock for delivery of a signal*/
PHP_FUNCTION(pcntl_alarm)
{
zend_long seconds;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(seconds);
ZEND_PARSE_PARAMETERS_END();
RETURN_LONG((zend_long) alarm(seconds));
}
/* }}} */
#define PHP_RUSAGE_PARA(from, to, field) \
add_assoc_long(to, #field, from.field)
#ifndef _OSD_POSIX
#define PHP_RUSAGE_SPECIAL(from, to) \
PHP_RUSAGE_PARA(from, to, ru_oublock); \
PHP_RUSAGE_PARA(from, to, ru_inblock); \
PHP_RUSAGE_PARA(from, to, ru_msgsnd); \
PHP_RUSAGE_PARA(from, to, ru_msgrcv); \
PHP_RUSAGE_PARA(from, to, ru_maxrss); \
PHP_RUSAGE_PARA(from, to, ru_ixrss); \
PHP_RUSAGE_PARA(from, to, ru_idrss); \
PHP_RUSAGE_PARA(from, to, ru_minflt); \
PHP_RUSAGE_PARA(from, to, ru_majflt); \
PHP_RUSAGE_PARA(from, to, ru_nsignals); \
PHP_RUSAGE_PARA(from, to, ru_nvcsw); \
PHP_RUSAGE_PARA(from, to, ru_nivcsw); \
PHP_RUSAGE_PARA(from, to, ru_nswap);
#else /*_OSD_POSIX*/
#define PHP_RUSAGE_SPECIAL(from, to)
#endif
#define PHP_RUSAGE_COMMON(from ,to) \
PHP_RUSAGE_PARA(from, to, ru_utime.tv_usec); \
PHP_RUSAGE_PARA(from, to, ru_utime.tv_sec); \
PHP_RUSAGE_PARA(from, to, ru_stime.tv_usec); \
PHP_RUSAGE_PARA(from, to, ru_stime.tv_sec);
#define PHP_RUSAGE_TO_ARRAY(from, to) \
if (to) { \
PHP_RUSAGE_SPECIAL(from, to) \
PHP_RUSAGE_COMMON(from, to); \
}
/* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */
PHP_FUNCTION(pcntl_waitpid)
{
zend_long pid, options = 0;
zval *z_status = NULL, *z_rusage = NULL;
int status;
pid_t child_id;
#ifdef HAVE_WAIT4
struct rusage rusage;
#endif
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_LONG(pid)
Z_PARAM_ZVAL(z_status)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(options)
Z_PARAM_ZVAL(z_rusage)
ZEND_PARSE_PARAMETERS_END();
status = zval_get_long(z_status);
#ifdef HAVE_WAIT4
if (z_rusage) {
z_rusage = zend_try_array_init(z_rusage);
if (!z_rusage) {
RETURN_THROWS();
}
memset(&rusage, 0, sizeof(struct rusage));
child_id = wait4((pid_t) pid, &status, options, &rusage);
} else {
child_id = waitpid((pid_t) pid, &status, options);
}
#else
child_id = waitpid((pid_t) pid, &status, options);
#endif
if (child_id < 0) {
PCNTL_G(last_error) = errno;
}
#ifdef HAVE_WAIT4
if (child_id > 0) {
PHP_RUSAGE_TO_ARRAY(rusage, z_rusage);
}
#endif
ZEND_TRY_ASSIGN_REF_LONG(z_status, status);
RETURN_LONG((zend_long) child_id);
}
/* }}} */
/* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */
PHP_FUNCTION(pcntl_wait)
{
zend_long options = 0;
zval *z_status = NULL, *z_rusage = NULL;
int status;
pid_t child_id;
#ifdef HAVE_WAIT3
struct rusage rusage;
#endif
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_ZVAL(z_status)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(options)
Z_PARAM_ZVAL(z_rusage)
ZEND_PARSE_PARAMETERS_END();
status = zval_get_long(z_status);
#ifdef HAVE_WAIT3
if (z_rusage) {
z_rusage = zend_try_array_init(z_rusage);
if (!z_rusage) {
RETURN_THROWS();
}
memset(&rusage, 0, sizeof(struct rusage));
child_id = wait3(&status, options, &rusage);
} else if (options) {
child_id = wait3(&status, options, NULL);
} else {
child_id = wait(&status);
}
#else
child_id = wait(&status);
#endif
if (child_id < 0) {
PCNTL_G(last_error) = errno;
}
#ifdef HAVE_WAIT3
if (child_id > 0) {
PHP_RUSAGE_TO_ARRAY(rusage, z_rusage);
}
#endif
ZEND_TRY_ASSIGN_REF_LONG(z_status, status);
RETURN_LONG((zend_long) child_id);
}
/* }}} */
#undef PHP_RUSAGE_PARA
#undef PHP_RUSAGE_SPECIAL
#undef PHP_RUSAGE_COMMON
#undef PHP_RUSAGE_TO_ARRAY
/* {{{ Returns true if the child status code represents a successful exit */
PHP_FUNCTION(pcntl_wifexited)
{
zend_long status_word;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(status_word)
ZEND_PARSE_PARAMETERS_END();
#ifdef WIFEXITED
int int_status_word = (int) status_word;
if (WIFEXITED(int_status_word)) {
RETURN_TRUE;
}
#endif
RETURN_FALSE;
}
/* }}} */
/* {{{ Returns true if the child status code represents a stopped process (WUNTRACED must have been used with waitpid) */
PHP_FUNCTION(pcntl_wifstopped)
{
zend_long status_word;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(status_word)
ZEND_PARSE_PARAMETERS_END();
#ifdef WIFSTOPPED
int int_status_word = (int) status_word;
if (WIFSTOPPED(int_status_word)) {
RETURN_TRUE;
}
#endif
RETURN_FALSE;
}
/* }}} */
/* {{{ Returns true if the child status code represents a process that was terminated due to a signal */
PHP_FUNCTION(pcntl_wifsignaled)
{
zend_long status_word;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(status_word)
ZEND_PARSE_PARAMETERS_END();
#ifdef WIFSIGNALED
int int_status_word = (int) status_word;
if (WIFSIGNALED(int_status_word)) {
RETURN_TRUE;
}
#endif
RETURN_FALSE;
}
/* }}} */
/* {{{ Returns true if the child status code represents a process that was resumed due to a SIGCONT signal */
PHP_FUNCTION(pcntl_wifcontinued)
{
zend_long status_word;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(status_word)
ZEND_PARSE_PARAMETERS_END();
#ifdef HAVE_WCONTINUED
int int_status_word = (int) status_word;
if (WIFCONTINUED(int_status_word)) {
RETURN_TRUE;
}
#endif
RETURN_FALSE;
}
/* }}} */
/* {{{ Returns the status code of a child's exit */
PHP_FUNCTION(pcntl_wexitstatus)
{
zend_long status_word;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(status_word)
ZEND_PARSE_PARAMETERS_END();
#ifdef WEXITSTATUS
int int_status_word = (int) status_word;
RETURN_LONG(WEXITSTATUS(int_status_word));
#else
RETURN_FALSE;
#endif
}
/* }}} */
/* {{{ Returns the number of the signal that terminated the process who's status code is passed */
PHP_FUNCTION(pcntl_wtermsig)
{
zend_long status_word;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(status_word)
ZEND_PARSE_PARAMETERS_END();
#ifdef WTERMSIG
int int_status_word = (int) status_word;
RETURN_LONG(WTERMSIG(int_status_word));
#else
RETURN_FALSE;
#endif
}
/* }}} */
/* {{{ Returns the number of the signal that caused the process to stop who's status code is passed */
PHP_FUNCTION(pcntl_wstopsig)
{
zend_long status_word;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(status_word)
ZEND_PARSE_PARAMETERS_END();
#ifdef WSTOPSIG
int int_status_word = (int) status_word;
RETURN_LONG(WSTOPSIG(int_status_word));
#else
RETURN_FALSE;
#endif
}
/* }}} */
/* {{{ Executes specified program in current process space as defined by exec(2) */
PHP_FUNCTION(pcntl_exec)
{
zval *args = NULL, *envs = NULL;
zval *element;
HashTable *args_hash, *envs_hash;
int argc = 0, argi = 0;
int envc = 0, envi = 0;
char **argv = NULL, **envp = NULL;
char **current_arg, **pair;
size_t pair_length;
zend_string *key;
char *path;
size_t path_len;
zend_ulong key_num;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_PATH(path, path_len)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(args)
Z_PARAM_ARRAY(envs)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_NUM_ARGS() > 1) {
/* Build argument list */
SEPARATE_ARRAY(args);
args_hash = Z_ARRVAL_P(args);
argc = zend_hash_num_elements(args_hash);
argv = safe_emalloc((argc + 2), sizeof(char *), 0);
*argv = path;
current_arg = argv+1;
ZEND_HASH_FOREACH_VAL(args_hash, element) {
if (argi >= argc) break;
if (!try_convert_to_string(element)) {
efree(argv);
RETURN_THROWS();
}
*current_arg = Z_STRVAL_P(element);
argi++;
current_arg++;
} ZEND_HASH_FOREACH_END();
*current_arg = NULL;
} else {
argv = emalloc(2 * sizeof(char *));
argv[0] = path;
argv[1] = NULL;
}
if ( ZEND_NUM_ARGS() == 3 ) {
/* Build environment pair list */
SEPARATE_ARRAY(envs);
envs_hash = Z_ARRVAL_P(envs);
envc = zend_hash_num_elements(envs_hash);
pair = envp = safe_emalloc((envc + 1), sizeof(char *), 0);
ZEND_HASH_FOREACH_KEY_VAL(envs_hash, key_num, key, element) {
if (envi >= envc) break;
if (!key) {
key = zend_long_to_str(key_num);
} else {
zend_string_addref(key);
}
if (!try_convert_to_string(element)) {
zend_string_release(key);
efree(argv);
efree(envp);
RETURN_THROWS();
}
/* Length of element + equal sign + length of key + null */
ZEND_ASSERT(Z_STRLEN_P(element) < SIZE_MAX && ZSTR_LEN(key) < SIZE_MAX);
*pair = safe_emalloc(Z_STRLEN_P(element) + 1, sizeof(char), ZSTR_LEN(key) + 1);
pair_length = Z_STRLEN_P(element) + ZSTR_LEN(key) + 2;
strlcpy(*pair, ZSTR_VAL(key), ZSTR_LEN(key) + 1);
strlcat(*pair, "=", pair_length);
strlcat(*pair, Z_STRVAL_P(element), pair_length);
/* Cleanup */
zend_string_release_ex(key, 0);
envi++;
pair++;
} ZEND_HASH_FOREACH_END();
*(pair) = NULL;
if (execve(path, argv, envp) == -1) {
PCNTL_G(last_error) = errno;
php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
}
/* Cleanup */
for (pair = envp; *pair != NULL; pair++) efree(*pair);
efree(envp);
} else {
if (execv(path, argv) == -1) {
PCNTL_G(last_error) = errno;
php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
}
}
efree(argv);
RETURN_FALSE;
}
/* }}} */
/* {{{ Assigns a system signal handler to a PHP function */
PHP_FUNCTION(pcntl_signal)
{
zval *handle;
zend_long signo;
bool restart_syscalls = 1;
bool restart_syscalls_is_null = 1;
char *error = NULL;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_LONG(signo)
Z_PARAM_ZVAL(handle)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL_OR_NULL(restart_syscalls, restart_syscalls_is_null)
ZEND_PARSE_PARAMETERS_END();
if (signo < 1) {
zend_argument_value_error(1, "must be greater than or equal to 1");
RETURN_THROWS();
}
if (signo >= PCNTL_G(num_signals)) {
zend_argument_value_error(1, "must be less than %d", PCNTL_G(num_signals));
RETURN_THROWS();
}
if (!PCNTL_G(spares)) {
/* since calling malloc() from within a signal handler is not portable,
* pre-allocate a few records for recording signals */
int i;
for (i = 0; i < PCNTL_G(num_signals); i++) {
struct php_pcntl_pending_signal *psig;
psig = emalloc(sizeof(*psig));
psig->next = PCNTL_G(spares);
PCNTL_G(spares) = psig;
}
}
/* If restart_syscalls was not explicitly specified and the signal is SIGALRM, then default
* restart_syscalls to false. PHP used to enforce that restart_syscalls is false for SIGALRM,
* so we keep this differing default to reduce the degree of BC breakage. */
if (restart_syscalls_is_null && signo == SIGALRM) {
restart_syscalls = 0;
}
/* Special long value case for SIG_DFL and SIG_IGN */
if (Z_TYPE_P(handle) == IS_LONG) {
if (Z_LVAL_P(handle) != (zend_long) SIG_DFL && Z_LVAL_P(handle) != (zend_long) SIG_IGN) {
zend_argument_value_error(2, "must be either SIG_DFL or SIG_IGN when an integer value is given");
RETURN_THROWS();
}
if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == (void *)SIG_ERR) {
PCNTL_G(last_error) = errno;
php_error_docref(NULL, E_WARNING, "Error assigning signal");
RETURN_FALSE;
}
zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
RETURN_TRUE;
}