pcntl.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Jason Greene <jason@inetgurus.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #define PCNTL_DEBUG 0
  17. #if PCNTL_DEBUG
  18. #define DEBUG_OUT printf("DEBUG: ");printf
  19. #define IF_DEBUG(z) z
  20. #else
  21. #define IF_DEBUG(z)
  22. #endif
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "php.h"
  27. #include "php_ini.h"
  28. #include "ext/standard/info.h"
  29. #include "php_pcntl.h"
  30. #include "pcntl_arginfo.h"
  31. #include "php_signal.h"
  32. #include "php_ticks.h"
  33. #if defined(HAVE_GETPRIORITY) || defined(HAVE_SETPRIORITY) || defined(HAVE_WAIT3)
  34. #include <sys/wait.h>
  35. #include <sys/time.h>
  36. #include <sys/resource.h>
  37. #endif
  38. #include <errno.h>
  39. #ifdef HAVE_UNSHARE
  40. #include <sched.h>
  41. #endif
  42. #ifndef NSIG
  43. # define NSIG 32
  44. #endif
  45. ZEND_DECLARE_MODULE_GLOBALS(pcntl)
  46. static PHP_GINIT_FUNCTION(pcntl);
  47. zend_module_entry pcntl_module_entry = {
  48. STANDARD_MODULE_HEADER,
  49. "pcntl",
  50. ext_functions,
  51. PHP_MINIT(pcntl),
  52. PHP_MSHUTDOWN(pcntl),
  53. PHP_RINIT(pcntl),
  54. PHP_RSHUTDOWN(pcntl),
  55. PHP_MINFO(pcntl),
  56. PHP_PCNTL_VERSION,
  57. PHP_MODULE_GLOBALS(pcntl),
  58. PHP_GINIT(pcntl),
  59. NULL,
  60. NULL,
  61. STANDARD_MODULE_PROPERTIES_EX
  62. };
  63. #ifdef COMPILE_DL_PCNTL
  64. #ifdef ZTS
  65. ZEND_TSRMLS_CACHE_DEFINE()
  66. #endif
  67. ZEND_GET_MODULE(pcntl)
  68. #endif
  69. static void (*orig_interrupt_function)(zend_execute_data *execute_data);
  70. #ifdef HAVE_STRUCT_SIGINFO_T
  71. static void pcntl_signal_handler(int, siginfo_t*, void*);
  72. static void pcntl_siginfo_to_zval(int, siginfo_t*, zval*);
  73. #else
  74. static void pcntl_signal_handler(int);
  75. #endif
  76. static void pcntl_signal_dispatch(void);
  77. static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer);
  78. static void pcntl_interrupt_function(zend_execute_data *execute_data);
  79. void php_register_signal_constants(INIT_FUNC_ARGS)
  80. {
  81. /* Wait Constants */
  82. #ifdef WNOHANG
  83. REGISTER_LONG_CONSTANT("WNOHANG", (zend_long) WNOHANG, CONST_CS | CONST_PERSISTENT);
  84. #endif
  85. #ifdef WUNTRACED
  86. REGISTER_LONG_CONSTANT("WUNTRACED", (zend_long) WUNTRACED, CONST_CS | CONST_PERSISTENT);
  87. #endif
  88. #ifdef HAVE_WCONTINUED
  89. REGISTER_LONG_CONSTANT("WCONTINUED", (zend_long) WCONTINUED, CONST_CS | CONST_PERSISTENT);
  90. #endif
  91. /* Signal Constants */
  92. REGISTER_LONG_CONSTANT("SIG_IGN", (zend_long) SIG_IGN, CONST_CS | CONST_PERSISTENT);
  93. REGISTER_LONG_CONSTANT("SIG_DFL", (zend_long) SIG_DFL, CONST_CS | CONST_PERSISTENT);
  94. REGISTER_LONG_CONSTANT("SIG_ERR", (zend_long) SIG_ERR, CONST_CS | CONST_PERSISTENT);
  95. REGISTER_LONG_CONSTANT("SIGHUP", (zend_long) SIGHUP, CONST_CS | CONST_PERSISTENT);
  96. REGISTER_LONG_CONSTANT("SIGINT", (zend_long) SIGINT, CONST_CS | CONST_PERSISTENT);
  97. REGISTER_LONG_CONSTANT("SIGQUIT", (zend_long) SIGQUIT, CONST_CS | CONST_PERSISTENT);
  98. REGISTER_LONG_CONSTANT("SIGILL", (zend_long) SIGILL, CONST_CS | CONST_PERSISTENT);
  99. REGISTER_LONG_CONSTANT("SIGTRAP", (zend_long) SIGTRAP, CONST_CS | CONST_PERSISTENT);
  100. REGISTER_LONG_CONSTANT("SIGABRT", (zend_long) SIGABRT, CONST_CS | CONST_PERSISTENT);
  101. #ifdef SIGIOT
  102. REGISTER_LONG_CONSTANT("SIGIOT", (zend_long) SIGIOT, CONST_CS | CONST_PERSISTENT);
  103. #endif
  104. REGISTER_LONG_CONSTANT("SIGBUS", (zend_long) SIGBUS, CONST_CS | CONST_PERSISTENT);
  105. REGISTER_LONG_CONSTANT("SIGFPE", (zend_long) SIGFPE, CONST_CS | CONST_PERSISTENT);
  106. REGISTER_LONG_CONSTANT("SIGKILL", (zend_long) SIGKILL, CONST_CS | CONST_PERSISTENT);
  107. REGISTER_LONG_CONSTANT("SIGUSR1", (zend_long) SIGUSR1, CONST_CS | CONST_PERSISTENT);
  108. REGISTER_LONG_CONSTANT("SIGSEGV", (zend_long) SIGSEGV, CONST_CS | CONST_PERSISTENT);
  109. REGISTER_LONG_CONSTANT("SIGUSR2", (zend_long) SIGUSR2, CONST_CS | CONST_PERSISTENT);
  110. REGISTER_LONG_CONSTANT("SIGPIPE", (zend_long) SIGPIPE, CONST_CS | CONST_PERSISTENT);
  111. REGISTER_LONG_CONSTANT("SIGALRM", (zend_long) SIGALRM, CONST_CS | CONST_PERSISTENT);
  112. REGISTER_LONG_CONSTANT("SIGTERM", (zend_long) SIGTERM, CONST_CS | CONST_PERSISTENT);
  113. #ifdef SIGSTKFLT
  114. REGISTER_LONG_CONSTANT("SIGSTKFLT",(zend_long) SIGSTKFLT, CONST_CS | CONST_PERSISTENT);
  115. #endif
  116. #ifdef SIGCLD
  117. REGISTER_LONG_CONSTANT("SIGCLD", (zend_long) SIGCLD, CONST_CS | CONST_PERSISTENT);
  118. #endif
  119. #ifdef SIGCHLD
  120. REGISTER_LONG_CONSTANT("SIGCHLD", (zend_long) SIGCHLD, CONST_CS | CONST_PERSISTENT);
  121. #endif
  122. REGISTER_LONG_CONSTANT("SIGCONT", (zend_long) SIGCONT, CONST_CS | CONST_PERSISTENT);
  123. REGISTER_LONG_CONSTANT("SIGSTOP", (zend_long) SIGSTOP, CONST_CS | CONST_PERSISTENT);
  124. REGISTER_LONG_CONSTANT("SIGTSTP", (zend_long) SIGTSTP, CONST_CS | CONST_PERSISTENT);
  125. REGISTER_LONG_CONSTANT("SIGTTIN", (zend_long) SIGTTIN, CONST_CS | CONST_PERSISTENT);
  126. REGISTER_LONG_CONSTANT("SIGTTOU", (zend_long) SIGTTOU, CONST_CS | CONST_PERSISTENT);
  127. REGISTER_LONG_CONSTANT("SIGURG", (zend_long) SIGURG , CONST_CS | CONST_PERSISTENT);
  128. REGISTER_LONG_CONSTANT("SIGXCPU", (zend_long) SIGXCPU, CONST_CS | CONST_PERSISTENT);
  129. REGISTER_LONG_CONSTANT("SIGXFSZ", (zend_long) SIGXFSZ, CONST_CS | CONST_PERSISTENT);
  130. REGISTER_LONG_CONSTANT("SIGVTALRM",(zend_long) SIGVTALRM, CONST_CS | CONST_PERSISTENT);
  131. REGISTER_LONG_CONSTANT("SIGPROF", (zend_long) SIGPROF, CONST_CS | CONST_PERSISTENT);
  132. REGISTER_LONG_CONSTANT("SIGWINCH", (zend_long) SIGWINCH, CONST_CS | CONST_PERSISTENT);
  133. #ifdef SIGPOLL
  134. REGISTER_LONG_CONSTANT("SIGPOLL", (zend_long) SIGPOLL, CONST_CS | CONST_PERSISTENT);
  135. #endif
  136. #ifdef SIGIO
  137. REGISTER_LONG_CONSTANT("SIGIO", (zend_long) SIGIO, CONST_CS | CONST_PERSISTENT);
  138. #endif
  139. #ifdef SIGPWR
  140. REGISTER_LONG_CONSTANT("SIGPWR", (zend_long) SIGPWR, CONST_CS | CONST_PERSISTENT);
  141. #endif
  142. #ifdef SIGSYS
  143. REGISTER_LONG_CONSTANT("SIGSYS", (zend_long) SIGSYS, CONST_CS | CONST_PERSISTENT);
  144. REGISTER_LONG_CONSTANT("SIGBABY", (zend_long) SIGSYS, CONST_CS | CONST_PERSISTENT);
  145. #endif
  146. #ifdef SIGRTMIN
  147. REGISTER_LONG_CONSTANT("SIGRTMIN", (zend_long) SIGRTMIN, CONST_CS | CONST_PERSISTENT);
  148. #endif
  149. #ifdef SIGRTMAX
  150. REGISTER_LONG_CONSTANT("SIGRTMAX", (zend_long) SIGRTMAX, CONST_CS | CONST_PERSISTENT);
  151. #endif
  152. #if defined(HAVE_GETPRIORITY) || defined(HAVE_SETPRIORITY)
  153. REGISTER_LONG_CONSTANT("PRIO_PGRP", PRIO_PGRP, CONST_CS | CONST_PERSISTENT);
  154. REGISTER_LONG_CONSTANT("PRIO_USER", PRIO_USER, CONST_CS | CONST_PERSISTENT);
  155. REGISTER_LONG_CONSTANT("PRIO_PROCESS", PRIO_PROCESS, CONST_CS | CONST_PERSISTENT);
  156. #if defined(PRIO_DARWIN_BG)
  157. REGISTER_LONG_CONSTANT("PRIO_DARWIN_BG", PRIO_DARWIN_BG, CONST_CS | CONST_PERSISTENT);
  158. REGISTER_LONG_CONSTANT("PRIO_DARWIN_THREAD", PRIO_DARWIN_THREAD, CONST_CS | CONST_PERSISTENT);
  159. #endif
  160. #endif
  161. /* {{{ "how" argument for sigprocmask */
  162. #ifdef HAVE_SIGPROCMASK
  163. REGISTER_LONG_CONSTANT("SIG_BLOCK", SIG_BLOCK, CONST_CS | CONST_PERSISTENT);
  164. REGISTER_LONG_CONSTANT("SIG_UNBLOCK", SIG_UNBLOCK, CONST_CS | CONST_PERSISTENT);
  165. REGISTER_LONG_CONSTANT("SIG_SETMASK", SIG_SETMASK, CONST_CS | CONST_PERSISTENT);
  166. #endif
  167. /* }}} */
  168. /* {{{ si_code */
  169. #if defined(HAVE_SIGWAITINFO) && defined(HAVE_SIGTIMEDWAIT)
  170. REGISTER_LONG_CONSTANT("SI_USER", SI_USER, CONST_CS | CONST_PERSISTENT);
  171. #ifdef SI_NOINFO
  172. REGISTER_LONG_CONSTANT("SI_NOINFO", SI_NOINFO, CONST_CS | CONST_PERSISTENT);
  173. #endif
  174. #ifdef SI_KERNEL
  175. REGISTER_LONG_CONSTANT("SI_KERNEL", SI_KERNEL, CONST_CS | CONST_PERSISTENT);
  176. #endif
  177. REGISTER_LONG_CONSTANT("SI_QUEUE", SI_QUEUE, CONST_CS | CONST_PERSISTENT);
  178. REGISTER_LONG_CONSTANT("SI_TIMER", SI_TIMER, CONST_CS | CONST_PERSISTENT);
  179. REGISTER_LONG_CONSTANT("SI_MESGQ", SI_MESGQ, CONST_CS | CONST_PERSISTENT);
  180. REGISTER_LONG_CONSTANT("SI_ASYNCIO", SI_ASYNCIO, CONST_CS | CONST_PERSISTENT);
  181. #ifdef SI_SIGIO
  182. REGISTER_LONG_CONSTANT("SI_SIGIO", SI_SIGIO, CONST_CS | CONST_PERSISTENT);
  183. #endif
  184. #ifdef SI_TKILL
  185. REGISTER_LONG_CONSTANT("SI_TKILL", SI_TKILL, CONST_CS | CONST_PERSISTENT);
  186. #endif
  187. /* si_code for SIGCHILD */
  188. #ifdef CLD_EXITED
  189. REGISTER_LONG_CONSTANT("CLD_EXITED", CLD_EXITED, CONST_CS | CONST_PERSISTENT);
  190. #endif
  191. #ifdef CLD_KILLED
  192. REGISTER_LONG_CONSTANT("CLD_KILLED", CLD_KILLED, CONST_CS | CONST_PERSISTENT);
  193. #endif
  194. #ifdef CLD_DUMPED
  195. REGISTER_LONG_CONSTANT("CLD_DUMPED", CLD_DUMPED, CONST_CS | CONST_PERSISTENT);
  196. #endif
  197. #ifdef CLD_TRAPPED
  198. REGISTER_LONG_CONSTANT("CLD_TRAPPED", CLD_TRAPPED, CONST_CS | CONST_PERSISTENT);
  199. #endif
  200. #ifdef CLD_STOPPED
  201. REGISTER_LONG_CONSTANT("CLD_STOPPED", CLD_STOPPED, CONST_CS | CONST_PERSISTENT);
  202. #endif
  203. #ifdef CLD_CONTINUED
  204. REGISTER_LONG_CONSTANT("CLD_CONTINUED", CLD_CONTINUED, CONST_CS | CONST_PERSISTENT);
  205. #endif
  206. /* si_code for SIGTRAP */
  207. #ifdef TRAP_BRKPT
  208. REGISTER_LONG_CONSTANT("TRAP_BRKPT", TRAP_BRKPT, CONST_CS | CONST_PERSISTENT);
  209. #endif
  210. #ifdef TRAP_TRACE
  211. REGISTER_LONG_CONSTANT("TRAP_TRACE", TRAP_TRACE, CONST_CS | CONST_PERSISTENT);
  212. #endif
  213. /* si_code for SIGPOLL */
  214. #ifdef POLL_IN
  215. REGISTER_LONG_CONSTANT("POLL_IN", POLL_IN, CONST_CS | CONST_PERSISTENT);
  216. #endif
  217. #ifdef POLL_OUT
  218. REGISTER_LONG_CONSTANT("POLL_OUT", POLL_OUT, CONST_CS | CONST_PERSISTENT);
  219. #endif
  220. #ifdef POLL_MSG
  221. REGISTER_LONG_CONSTANT("POLL_MSG", POLL_MSG, CONST_CS | CONST_PERSISTENT);
  222. #endif
  223. #ifdef POLL_ERR
  224. REGISTER_LONG_CONSTANT("POLL_ERR", POLL_ERR, CONST_CS | CONST_PERSISTENT);
  225. #endif
  226. #ifdef POLL_PRI
  227. REGISTER_LONG_CONSTANT("POLL_PRI", POLL_PRI, CONST_CS | CONST_PERSISTENT);
  228. #endif
  229. #ifdef POLL_HUP
  230. REGISTER_LONG_CONSTANT("POLL_HUP", POLL_HUP, CONST_CS | CONST_PERSISTENT);
  231. #endif
  232. #ifdef ILL_ILLOPC
  233. REGISTER_LONG_CONSTANT("ILL_ILLOPC", ILL_ILLOPC, CONST_CS | CONST_PERSISTENT);
  234. #endif
  235. #ifdef ILL_ILLOPN
  236. REGISTER_LONG_CONSTANT("ILL_ILLOPN", ILL_ILLOPN, CONST_CS | CONST_PERSISTENT);
  237. #endif
  238. #ifdef ILL_ILLADR
  239. REGISTER_LONG_CONSTANT("ILL_ILLADR", ILL_ILLADR, CONST_CS | CONST_PERSISTENT);
  240. #endif
  241. #ifdef ILL_ILLTRP
  242. REGISTER_LONG_CONSTANT("ILL_ILLTRP", ILL_ILLTRP, CONST_CS | CONST_PERSISTENT);
  243. #endif
  244. #ifdef ILL_PRVOPC
  245. REGISTER_LONG_CONSTANT("ILL_PRVOPC", ILL_PRVOPC, CONST_CS | CONST_PERSISTENT);
  246. #endif
  247. #ifdef ILL_PRVREG
  248. REGISTER_LONG_CONSTANT("ILL_PRVREG", ILL_PRVREG, CONST_CS | CONST_PERSISTENT);
  249. #endif
  250. #ifdef ILL_COPROC
  251. REGISTER_LONG_CONSTANT("ILL_COPROC", ILL_COPROC, CONST_CS | CONST_PERSISTENT);
  252. #endif
  253. #ifdef ILL_BADSTK
  254. REGISTER_LONG_CONSTANT("ILL_BADSTK", ILL_BADSTK, CONST_CS | CONST_PERSISTENT);
  255. #endif
  256. #ifdef FPE_INTDIV
  257. REGISTER_LONG_CONSTANT("FPE_INTDIV", FPE_INTDIV, CONST_CS | CONST_PERSISTENT);
  258. #endif
  259. #ifdef FPE_INTOVF
  260. REGISTER_LONG_CONSTANT("FPE_INTOVF", FPE_INTOVF, CONST_CS | CONST_PERSISTENT);
  261. #endif
  262. #ifdef FPE_FLTDIV
  263. REGISTER_LONG_CONSTANT("FPE_FLTDIV", FPE_FLTDIV, CONST_CS | CONST_PERSISTENT);
  264. #endif
  265. #ifdef FPE_FLTOVF
  266. REGISTER_LONG_CONSTANT("FPE_FLTOVF", FPE_FLTOVF, CONST_CS | CONST_PERSISTENT);
  267. #endif
  268. #ifdef FPE_FLTUND
  269. REGISTER_LONG_CONSTANT("FPE_FLTUND", FPE_FLTINV, CONST_CS | CONST_PERSISTENT);
  270. #endif
  271. #ifdef FPE_FLTRES
  272. REGISTER_LONG_CONSTANT("FPE_FLTRES", FPE_FLTRES, CONST_CS | CONST_PERSISTENT);
  273. #endif
  274. #ifdef FPE_FLTINV
  275. REGISTER_LONG_CONSTANT("FPE_FLTINV", FPE_FLTINV, CONST_CS | CONST_PERSISTENT);
  276. #endif
  277. #ifdef FPE_FLTSUB
  278. REGISTER_LONG_CONSTANT("FPE_FLTSUB", FPE_FLTSUB, CONST_CS | CONST_PERSISTENT);
  279. #endif
  280. #ifdef SEGV_MAPERR
  281. REGISTER_LONG_CONSTANT("SEGV_MAPERR", SEGV_MAPERR, CONST_CS | CONST_PERSISTENT);
  282. #endif
  283. #ifdef SEGV_ACCERR
  284. REGISTER_LONG_CONSTANT("SEGV_ACCERR", SEGV_ACCERR, CONST_CS | CONST_PERSISTENT);
  285. #endif
  286. #ifdef BUS_ADRALN
  287. REGISTER_LONG_CONSTANT("BUS_ADRALN", BUS_ADRALN, CONST_CS | CONST_PERSISTENT);
  288. #endif
  289. #ifdef BUS_ADRERR
  290. REGISTER_LONG_CONSTANT("BUS_ADRERR", BUS_ADRERR, CONST_CS | CONST_PERSISTENT);
  291. #endif
  292. #ifdef BUS_OBJERR
  293. REGISTER_LONG_CONSTANT("BUS_OBJERR", BUS_OBJERR, CONST_CS | CONST_PERSISTENT);
  294. #endif
  295. #endif /* defined(HAVE_SIGWAITINFO) && defined(HAVE_SIGTIMEDWAIT) */
  296. /* }}} */
  297. /* unshare(/clone) constants */
  298. #ifdef HAVE_UNSHARE
  299. REGISTER_LONG_CONSTANT("CLONE_NEWNS", CLONE_NEWNS, CONST_CS | CONST_PERSISTENT);
  300. #ifdef CLONE_NEWIPC
  301. REGISTER_LONG_CONSTANT("CLONE_NEWIPC", CLONE_NEWIPC, CONST_CS | CONST_PERSISTENT);
  302. #endif
  303. #ifdef CLONE_NEWUTS
  304. REGISTER_LONG_CONSTANT("CLONE_NEWUTS", CLONE_NEWUTS, CONST_CS | CONST_PERSISTENT);
  305. #endif
  306. #ifdef CLONE_NEWNET
  307. REGISTER_LONG_CONSTANT("CLONE_NEWNET", CLONE_NEWNET, CONST_CS | CONST_PERSISTENT);
  308. #endif
  309. #ifdef CLONE_NEWPID
  310. REGISTER_LONG_CONSTANT("CLONE_NEWPID", CLONE_NEWPID, CONST_CS | CONST_PERSISTENT);
  311. #endif
  312. #ifdef CLONE_NEWUSER
  313. REGISTER_LONG_CONSTANT("CLONE_NEWUSER", CLONE_NEWUSER, CONST_CS | CONST_PERSISTENT);
  314. #endif
  315. #ifdef CLONE_NEWCGROUP
  316. REGISTER_LONG_CONSTANT("CLONE_NEWCGROUP", CLONE_NEWCGROUP, CONST_CS | CONST_PERSISTENT);
  317. #endif
  318. #endif
  319. #ifdef HAVE_RFORK
  320. #ifdef RFPROC
  321. REGISTER_LONG_CONSTANT("RFPROC", RFPROC, CONST_CS | CONST_PERSISTENT);
  322. #endif
  323. #ifdef RFNOWAIT
  324. REGISTER_LONG_CONSTANT("RFNOWAIT", RFNOWAIT, CONST_CS | CONST_PERSISTENT);
  325. #endif
  326. #ifdef RFCFDG
  327. REGISTER_LONG_CONSTANT("RFCFDG", RFCFDG, CONST_CS | CONST_PERSISTENT);
  328. #endif
  329. #ifdef RFFDG
  330. REGISTER_LONG_CONSTANT("RFFDG", RFFDG, CONST_CS | CONST_PERSISTENT);
  331. #endif
  332. #ifdef RFLINUXTHPN
  333. REGISTER_LONG_CONSTANT("RFLINUXTHPN", RFLINUXTHPN, CONST_CS | CONST_PERSISTENT);
  334. #endif
  335. #ifdef RFTSIGZMB
  336. REGISTER_LONG_CONSTANT("RFTSIGZMB", RFTSIGZMB, CONST_CS | CONST_PERSISTENT);
  337. #endif
  338. #ifdef RFTHREAD
  339. REGISTER_LONG_CONSTANT("RFTHREAD", RFTHREAD, CONST_CS | CONST_PERSISTENT);
  340. #endif
  341. #endif
  342. }
  343. static void php_pcntl_register_errno_constants(INIT_FUNC_ARGS)
  344. {
  345. #ifdef EINTR
  346. REGISTER_PCNTL_ERRNO_CONSTANT(EINTR);
  347. #endif
  348. #ifdef ECHILD
  349. REGISTER_PCNTL_ERRNO_CONSTANT(ECHILD);
  350. #endif
  351. #ifdef EINVAL
  352. REGISTER_PCNTL_ERRNO_CONSTANT(EINVAL);
  353. #endif
  354. #ifdef EAGAIN
  355. REGISTER_PCNTL_ERRNO_CONSTANT(EAGAIN);
  356. #endif
  357. #ifdef ESRCH
  358. REGISTER_PCNTL_ERRNO_CONSTANT(ESRCH);
  359. #endif
  360. #ifdef EACCES
  361. REGISTER_PCNTL_ERRNO_CONSTANT(EACCES);
  362. #endif
  363. #ifdef EPERM
  364. REGISTER_PCNTL_ERRNO_CONSTANT(EPERM);
  365. #endif
  366. #ifdef ENOMEM
  367. REGISTER_PCNTL_ERRNO_CONSTANT(ENOMEM);
  368. #endif
  369. #ifdef E2BIG
  370. REGISTER_PCNTL_ERRNO_CONSTANT(E2BIG);
  371. #endif
  372. #ifdef EFAULT
  373. REGISTER_PCNTL_ERRNO_CONSTANT(EFAULT);
  374. #endif
  375. #ifdef EIO
  376. REGISTER_PCNTL_ERRNO_CONSTANT(EIO);
  377. #endif
  378. #ifdef EISDIR
  379. REGISTER_PCNTL_ERRNO_CONSTANT(EISDIR);
  380. #endif
  381. #ifdef ELIBBAD
  382. REGISTER_PCNTL_ERRNO_CONSTANT(ELIBBAD);
  383. #endif
  384. #ifdef ELOOP
  385. REGISTER_PCNTL_ERRNO_CONSTANT(ELOOP);
  386. #endif
  387. #ifdef EMFILE
  388. REGISTER_PCNTL_ERRNO_CONSTANT(EMFILE);
  389. #endif
  390. #ifdef ENAMETOOLONG
  391. REGISTER_PCNTL_ERRNO_CONSTANT(ENAMETOOLONG);
  392. #endif
  393. #ifdef ENFILE
  394. REGISTER_PCNTL_ERRNO_CONSTANT(ENFILE);
  395. #endif
  396. #ifdef ENOENT
  397. REGISTER_PCNTL_ERRNO_CONSTANT(ENOENT);
  398. #endif
  399. #ifdef ENOEXEC
  400. REGISTER_PCNTL_ERRNO_CONSTANT(ENOEXEC);
  401. #endif
  402. #ifdef ENOTDIR
  403. REGISTER_PCNTL_ERRNO_CONSTANT(ENOTDIR);
  404. #endif
  405. #ifdef ETXTBSY
  406. REGISTER_PCNTL_ERRNO_CONSTANT(ETXTBSY);
  407. #endif
  408. #ifdef ENOSPC
  409. REGISTER_PCNTL_ERRNO_CONSTANT(ENOSPC);
  410. #endif
  411. #ifdef EUSERS
  412. REGISTER_PCNTL_ERRNO_CONSTANT(EUSERS);
  413. #endif
  414. }
  415. static PHP_GINIT_FUNCTION(pcntl)
  416. {
  417. #if defined(COMPILE_DL_PCNTL) && defined(ZTS)
  418. ZEND_TSRMLS_CACHE_UPDATE();
  419. #endif
  420. memset(pcntl_globals, 0, sizeof(*pcntl_globals));
  421. }
  422. PHP_RINIT_FUNCTION(pcntl)
  423. {
  424. php_add_tick_function(pcntl_signal_dispatch_tick_function, NULL);
  425. zend_hash_init(&PCNTL_G(php_signal_table), 16, NULL, ZVAL_PTR_DTOR, 0);
  426. PCNTL_G(head) = PCNTL_G(tail) = PCNTL_G(spares) = NULL;
  427. PCNTL_G(async_signals) = 0;
  428. PCNTL_G(last_error) = 0;
  429. PCNTL_G(num_signals) = NSIG;
  430. #ifdef SIGRTMAX
  431. /* At least FreeBSD reports an incorrecrt NSIG that does not include realtime signals.
  432. * As SIGRTMAX may be a dynamic value, adjust the value in INIT. */
  433. if (NSIG < SIGRTMAX + 1) {
  434. PCNTL_G(num_signals) = SIGRTMAX + 1;
  435. }
  436. #endif
  437. return SUCCESS;
  438. }
  439. PHP_MINIT_FUNCTION(pcntl)
  440. {
  441. php_register_signal_constants(INIT_FUNC_ARGS_PASSTHRU);
  442. php_pcntl_register_errno_constants(INIT_FUNC_ARGS_PASSTHRU);
  443. orig_interrupt_function = zend_interrupt_function;
  444. zend_interrupt_function = pcntl_interrupt_function;
  445. return SUCCESS;
  446. }
  447. PHP_MSHUTDOWN_FUNCTION(pcntl)
  448. {
  449. return SUCCESS;
  450. }
  451. PHP_RSHUTDOWN_FUNCTION(pcntl)
  452. {
  453. struct php_pcntl_pending_signal *sig;
  454. /* FIXME: if a signal is delivered after this point, things will go pear shaped;
  455. * need to remove signal handlers */
  456. zend_hash_destroy(&PCNTL_G(php_signal_table));
  457. while (PCNTL_G(head)) {
  458. sig = PCNTL_G(head);
  459. PCNTL_G(head) = sig->next;
  460. efree(sig);
  461. }
  462. while (PCNTL_G(spares)) {
  463. sig = PCNTL_G(spares);
  464. PCNTL_G(spares) = sig->next;
  465. efree(sig);
  466. }
  467. return SUCCESS;
  468. }
  469. PHP_MINFO_FUNCTION(pcntl)
  470. {
  471. php_info_print_table_start();
  472. php_info_print_table_header(2, "pcntl support", "enabled");
  473. php_info_print_table_end();
  474. }
  475. /* {{{ Forks the currently running process following the same behavior as the UNIX fork() system call*/
  476. PHP_FUNCTION(pcntl_fork)
  477. {
  478. pid_t id;
  479. if (zend_parse_parameters_none() == FAILURE) {
  480. RETURN_THROWS();
  481. }
  482. id = fork();
  483. if (id == -1) {
  484. PCNTL_G(last_error) = errno;
  485. php_error_docref(NULL, E_WARNING, "Error %d", errno);
  486. }
  487. RETURN_LONG((zend_long) id);
  488. }
  489. /* }}} */
  490. /* {{{ Set an alarm clock for delivery of a signal*/
  491. PHP_FUNCTION(pcntl_alarm)
  492. {
  493. zend_long seconds;
  494. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &seconds) == FAILURE) {
  495. RETURN_THROWS();
  496. }
  497. RETURN_LONG((zend_long) alarm(seconds));
  498. }
  499. /* }}} */
  500. #define PHP_RUSAGE_PARA(from, to, field) \
  501. add_assoc_long(to, #field, from.field)
  502. #ifndef _OSD_POSIX
  503. #define PHP_RUSAGE_SPECIAL(from, to) \
  504. PHP_RUSAGE_PARA(from, to, ru_oublock); \
  505. PHP_RUSAGE_PARA(from, to, ru_inblock); \
  506. PHP_RUSAGE_PARA(from, to, ru_msgsnd); \
  507. PHP_RUSAGE_PARA(from, to, ru_msgrcv); \
  508. PHP_RUSAGE_PARA(from, to, ru_maxrss); \
  509. PHP_RUSAGE_PARA(from, to, ru_ixrss); \
  510. PHP_RUSAGE_PARA(from, to, ru_idrss); \
  511. PHP_RUSAGE_PARA(from, to, ru_minflt); \
  512. PHP_RUSAGE_PARA(from, to, ru_majflt); \
  513. PHP_RUSAGE_PARA(from, to, ru_nsignals); \
  514. PHP_RUSAGE_PARA(from, to, ru_nvcsw); \
  515. PHP_RUSAGE_PARA(from, to, ru_nivcsw); \
  516. PHP_RUSAGE_PARA(from, to, ru_nswap);
  517. #else /*_OSD_POSIX*/
  518. #define PHP_RUSAGE_SPECIAL(from, to)
  519. #endif
  520. #define PHP_RUSAGE_COMMON(from ,to) \
  521. PHP_RUSAGE_PARA(from, to, ru_utime.tv_usec); \
  522. PHP_RUSAGE_PARA(from, to, ru_utime.tv_sec); \
  523. PHP_RUSAGE_PARA(from, to, ru_stime.tv_usec); \
  524. PHP_RUSAGE_PARA(from, to, ru_stime.tv_sec);
  525. #define PHP_RUSAGE_TO_ARRAY(from, to) \
  526. if (to) { \
  527. PHP_RUSAGE_SPECIAL(from, to) \
  528. PHP_RUSAGE_COMMON(from, to); \
  529. }
  530. /* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */
  531. PHP_FUNCTION(pcntl_waitpid)
  532. {
  533. zend_long pid, options = 0;
  534. zval *z_status = NULL, *z_rusage = NULL;
  535. int status;
  536. pid_t child_id;
  537. #ifdef HAVE_WAIT4
  538. struct rusage rusage;
  539. #endif
  540. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz|lz", &pid, &z_status, &options, &z_rusage) == FAILURE) {
  541. RETURN_THROWS();
  542. }
  543. status = zval_get_long(z_status);
  544. #ifdef HAVE_WAIT4
  545. if (z_rusage) {
  546. z_rusage = zend_try_array_init(z_rusage);
  547. if (!z_rusage) {
  548. RETURN_THROWS();
  549. }
  550. memset(&rusage, 0, sizeof(struct rusage));
  551. child_id = wait4((pid_t) pid, &status, options, &rusage);
  552. } else {
  553. child_id = waitpid((pid_t) pid, &status, options);
  554. }
  555. #else
  556. child_id = waitpid((pid_t) pid, &status, options);
  557. #endif
  558. if (child_id < 0) {
  559. PCNTL_G(last_error) = errno;
  560. }
  561. #ifdef HAVE_WAIT4
  562. if (child_id > 0) {
  563. PHP_RUSAGE_TO_ARRAY(rusage, z_rusage);
  564. }
  565. #endif
  566. ZEND_TRY_ASSIGN_REF_LONG(z_status, status);
  567. RETURN_LONG((zend_long) child_id);
  568. }
  569. /* }}} */
  570. /* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */
  571. PHP_FUNCTION(pcntl_wait)
  572. {
  573. zend_long options = 0;
  574. zval *z_status = NULL, *z_rusage = NULL;
  575. int status;
  576. pid_t child_id;
  577. #ifdef HAVE_WAIT3
  578. struct rusage rusage;
  579. #endif
  580. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|lz", &z_status, &options, &z_rusage) == FAILURE) {
  581. RETURN_THROWS();
  582. }
  583. status = zval_get_long(z_status);
  584. #ifdef HAVE_WAIT3
  585. if (z_rusage) {
  586. z_rusage = zend_try_array_init(z_rusage);
  587. if (!z_rusage) {
  588. RETURN_THROWS();
  589. }
  590. memset(&rusage, 0, sizeof(struct rusage));
  591. child_id = wait3(&status, options, &rusage);
  592. } else if (options) {
  593. child_id = wait3(&status, options, NULL);
  594. } else {
  595. child_id = wait(&status);
  596. }
  597. #else
  598. child_id = wait(&status);
  599. #endif
  600. if (child_id < 0) {
  601. PCNTL_G(last_error) = errno;
  602. }
  603. #ifdef HAVE_WAIT3
  604. if (child_id > 0) {
  605. PHP_RUSAGE_TO_ARRAY(rusage, z_rusage);
  606. }
  607. #endif
  608. ZEND_TRY_ASSIGN_REF_LONG(z_status, status);
  609. RETURN_LONG((zend_long) child_id);
  610. }
  611. /* }}} */
  612. #undef PHP_RUSAGE_PARA
  613. #undef PHP_RUSAGE_SPECIAL
  614. #undef PHP_RUSAGE_COMMON
  615. #undef PHP_RUSAGE_TO_ARRAY
  616. /* {{{ Returns true if the child status code represents a successful exit */
  617. PHP_FUNCTION(pcntl_wifexited)
  618. {
  619. zend_long status_word;
  620. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &status_word) == FAILURE) {
  621. RETURN_THROWS();
  622. }
  623. #ifdef WIFEXITED
  624. int int_status_word = (int) status_word;
  625. if (WIFEXITED(int_status_word)) {
  626. RETURN_TRUE;
  627. }
  628. #endif
  629. RETURN_FALSE;
  630. }
  631. /* }}} */
  632. /* {{{ Returns true if the child status code represents a stopped process (WUNTRACED must have been used with waitpid) */
  633. PHP_FUNCTION(pcntl_wifstopped)
  634. {
  635. zend_long status_word;
  636. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &status_word) == FAILURE) {
  637. RETURN_THROWS();
  638. }
  639. #ifdef WIFSTOPPED
  640. int int_status_word = (int) status_word;
  641. if (WIFSTOPPED(int_status_word)) {
  642. RETURN_TRUE;
  643. }
  644. #endif
  645. RETURN_FALSE;
  646. }
  647. /* }}} */
  648. /* {{{ Returns true if the child status code represents a process that was terminated due to a signal */
  649. PHP_FUNCTION(pcntl_wifsignaled)
  650. {
  651. zend_long status_word;
  652. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &status_word) == FAILURE) {
  653. RETURN_THROWS();
  654. }
  655. #ifdef WIFSIGNALED
  656. int int_status_word = (int) status_word;
  657. if (WIFSIGNALED(int_status_word)) {
  658. RETURN_TRUE;
  659. }
  660. #endif
  661. RETURN_FALSE;
  662. }
  663. /* }}} */
  664. /* {{{ Returns true if the child status code represents a process that was resumed due to a SIGCONT signal */
  665. PHP_FUNCTION(pcntl_wifcontinued)
  666. {
  667. zend_long status_word;
  668. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &status_word) == FAILURE) {
  669. RETURN_THROWS();
  670. }
  671. #ifdef HAVE_WCONTINUED
  672. int int_status_word = (int) status_word;
  673. if (WIFCONTINUED(int_status_word)) {
  674. RETURN_TRUE;
  675. }
  676. #endif
  677. RETURN_FALSE;
  678. }
  679. /* }}} */
  680. /* {{{ Returns the status code of a child's exit */
  681. PHP_FUNCTION(pcntl_wexitstatus)
  682. {
  683. zend_long status_word;
  684. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &status_word) == FAILURE) {
  685. RETURN_THROWS();
  686. }
  687. #ifdef WEXITSTATUS
  688. int int_status_word = (int) status_word;
  689. RETURN_LONG(WEXITSTATUS(int_status_word));
  690. #else
  691. RETURN_FALSE;
  692. #endif
  693. }
  694. /* }}} */
  695. /* {{{ Returns the number of the signal that terminated the process who's status code is passed */
  696. PHP_FUNCTION(pcntl_wtermsig)
  697. {
  698. zend_long status_word;
  699. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &status_word) == FAILURE) {
  700. RETURN_THROWS();
  701. }
  702. #ifdef WTERMSIG
  703. int int_status_word = (int) status_word;
  704. RETURN_LONG(WTERMSIG(int_status_word));
  705. #else
  706. RETURN_FALSE;
  707. #endif
  708. }
  709. /* }}} */
  710. /* {{{ Returns the number of the signal that caused the process to stop who's status code is passed */
  711. PHP_FUNCTION(pcntl_wstopsig)
  712. {
  713. zend_long status_word;
  714. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &status_word) == FAILURE) {
  715. RETURN_THROWS();
  716. }
  717. #ifdef WSTOPSIG
  718. int int_status_word = (int) status_word;
  719. RETURN_LONG(WSTOPSIG(int_status_word));
  720. #else
  721. RETURN_FALSE;
  722. #endif
  723. }
  724. /* }}} */
  725. /* {{{ Executes specified program in current process space as defined by exec(2) */
  726. PHP_FUNCTION(pcntl_exec)
  727. {
  728. zval *args = NULL, *envs = NULL;
  729. zval *element;
  730. HashTable *args_hash, *envs_hash;
  731. int argc = 0, argi = 0;
  732. int envc = 0, envi = 0;
  733. char **argv = NULL, **envp = NULL;
  734. char **current_arg, **pair;
  735. size_t pair_length;
  736. zend_string *key;
  737. char *path;
  738. size_t path_len;
  739. zend_ulong key_num;
  740. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|aa", &path, &path_len, &args, &envs) == FAILURE) {
  741. RETURN_THROWS();
  742. }
  743. if (ZEND_NUM_ARGS() > 1) {
  744. /* Build argument list */
  745. SEPARATE_ARRAY(args);
  746. args_hash = Z_ARRVAL_P(args);
  747. argc = zend_hash_num_elements(args_hash);
  748. argv = safe_emalloc((argc + 2), sizeof(char *), 0);
  749. *argv = path;
  750. current_arg = argv+1;
  751. ZEND_HASH_FOREACH_VAL(args_hash, element) {
  752. if (argi >= argc) break;
  753. if (!try_convert_to_string(element)) {
  754. efree(argv);
  755. RETURN_THROWS();
  756. }
  757. *current_arg = Z_STRVAL_P(element);
  758. argi++;
  759. current_arg++;
  760. } ZEND_HASH_FOREACH_END();
  761. *current_arg = NULL;
  762. } else {
  763. argv = emalloc(2 * sizeof(char *));
  764. argv[0] = path;
  765. argv[1] = NULL;
  766. }
  767. if ( ZEND_NUM_ARGS() == 3 ) {
  768. /* Build environment pair list */
  769. SEPARATE_ARRAY(envs);
  770. envs_hash = Z_ARRVAL_P(envs);
  771. envc = zend_hash_num_elements(envs_hash);
  772. pair = envp = safe_emalloc((envc + 1), sizeof(char *), 0);
  773. ZEND_HASH_FOREACH_KEY_VAL(envs_hash, key_num, key, element) {
  774. if (envi >= envc) break;
  775. if (!key) {
  776. key = zend_long_to_str(key_num);
  777. } else {
  778. zend_string_addref(key);
  779. }
  780. if (!try_convert_to_string(element)) {
  781. zend_string_release(key);
  782. efree(argv);
  783. efree(envp);
  784. RETURN_THROWS();
  785. }
  786. /* Length of element + equal sign + length of key + null */
  787. ZEND_ASSERT(Z_STRLEN_P(element) < SIZE_MAX && ZSTR_LEN(key) < SIZE_MAX);
  788. *pair = safe_emalloc(Z_STRLEN_P(element) + 1, sizeof(char), ZSTR_LEN(key) + 1);
  789. pair_length = Z_STRLEN_P(element) + ZSTR_LEN(key) + 2;
  790. strlcpy(*pair, ZSTR_VAL(key), ZSTR_LEN(key) + 1);
  791. strlcat(*pair, "=", pair_length);
  792. strlcat(*pair, Z_STRVAL_P(element), pair_length);
  793. /* Cleanup */
  794. zend_string_release_ex(key, 0);
  795. envi++;
  796. pair++;
  797. } ZEND_HASH_FOREACH_END();
  798. *(pair) = NULL;
  799. if (execve(path, argv, envp) == -1) {
  800. PCNTL_G(last_error) = errno;
  801. php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
  802. }
  803. /* Cleanup */
  804. for (pair = envp; *pair != NULL; pair++) efree(*pair);
  805. efree(envp);
  806. } else {
  807. if (execv(path, argv) == -1) {
  808. PCNTL_G(last_error) = errno;
  809. php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
  810. }
  811. }
  812. efree(argv);
  813. RETURN_FALSE;
  814. }
  815. /* }}} */
  816. /* {{{ Assigns a system signal handler to a PHP function */
  817. PHP_FUNCTION(pcntl_signal)
  818. {
  819. zval *handle;
  820. zend_long signo;
  821. bool restart_syscalls = 1;
  822. bool restart_syscalls_is_null = 1;
  823. char *error = NULL;
  824. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz|b!", &signo, &handle, &restart_syscalls, &restart_syscalls_is_null) == FAILURE) {
  825. RETURN_THROWS();
  826. }
  827. if (signo < 1) {
  828. zend_argument_value_error(1, "must be greater than or equal to 1");
  829. RETURN_THROWS();
  830. }
  831. if (signo >= PCNTL_G(num_signals)) {
  832. zend_argument_value_error(1, "must be less than %d", PCNTL_G(num_signals));
  833. RETURN_THROWS();
  834. }
  835. if (!PCNTL_G(spares)) {
  836. /* since calling malloc() from within a signal handler is not portable,
  837. * pre-allocate a few records for recording signals */
  838. int i;
  839. for (i = 0; i < PCNTL_G(num_signals); i++) {
  840. struct php_pcntl_pending_signal *psig;
  841. psig = emalloc(sizeof(*psig));
  842. psig->next = PCNTL_G(spares);
  843. PCNTL_G(spares) = psig;
  844. }
  845. }
  846. /* If restart_syscalls was not explicitly specified and the signal is SIGALRM, then default
  847. * restart_syscalls to false. PHP used to enforce that restart_syscalls is false for SIGALRM,
  848. * so we keep this differing default to reduce the degree of BC breakage. */
  849. if (restart_syscalls_is_null && signo == SIGALRM) {
  850. restart_syscalls = 0;
  851. }
  852. /* Special long value case for SIG_DFL and SIG_IGN */
  853. if (Z_TYPE_P(handle) == IS_LONG) {
  854. if (Z_LVAL_P(handle) != (zend_long) SIG_DFL && Z_LVAL_P(handle) != (zend_long) SIG_IGN) {
  855. zend_argument_value_error(2, "must be either SIG_DFL or SIG_IGN when an integer value is given");
  856. RETURN_THROWS();
  857. }
  858. if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == (void *)SIG_ERR) {
  859. PCNTL_G(last_error) = errno;
  860. php_error_docref(NULL, E_WARNING, "Error assigning signal");
  861. RETURN_FALSE;
  862. }
  863. zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
  864. RETURN_TRUE;
  865. }
  866. if (!zend_is_callable_ex(handle, NULL, 0, NULL, NULL, &error)) {
  867. zend_string *func_name = zend_get_callable_name(handle);
  868. PCNTL_G(last_error) = EINVAL;
  869. zend_argument_type_error(2, "must be of type callable|int, %s given", zend_zval_type_name(handle));
  870. zend_string_release_ex(func_name, 0);
  871. efree(error);
  872. RETURN_THROWS();
  873. }
  874. ZEND_ASSERT(!error);
  875. /* Add the function name to our signal table */
  876. handle = zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
  877. Z_TRY_ADDREF_P(handle);
  878. if (php_signal4(signo, pcntl_signal_handler, (int) restart_syscalls, 1) == (void *)SIG_ERR) {
  879. PCNTL_G(last_error) = errno;
  880. php_error_docref(NULL, E_WARNING, "Error assigning signal");
  881. RETURN_FALSE;
  882. }
  883. RETURN_TRUE;
  884. }
  885. /* }}} */
  886. /* {{{ Gets signal handler */
  887. PHP_FUNCTION(pcntl_signal_get_handler)
  888. {
  889. zval *prev_handle;
  890. zend_long signo;
  891. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &signo) == FAILURE) {
  892. RETURN_THROWS();
  893. }
  894. if (signo < 1 || signo > 32) {
  895. zend_argument_value_error(1, "must be between 1 and 32");
  896. RETURN_THROWS();
  897. }
  898. if ((prev_handle = zend_hash_index_find(&PCNTL_G(php_signal_table), signo)) != NULL) {
  899. RETURN_COPY(prev_handle);
  900. } else {
  901. RETURN_LONG((zend_long)SIG_DFL);
  902. }
  903. }
  904. /* {{{ Dispatch signals to signal handlers */
  905. PHP_FUNCTION(pcntl_signal_dispatch)
  906. {
  907. if (zend_parse_parameters_none() == FAILURE) {
  908. RETURN_THROWS();
  909. }
  910. pcntl_signal_dispatch();
  911. RETURN_TRUE;
  912. }
  913. /* }}} */
  914. #ifdef HAVE_SIGPROCMASK
  915. /* {{{ Examine and change blocked signals */
  916. PHP_FUNCTION(pcntl_sigprocmask)
  917. {
  918. zend_long how, signo;
  919. zval *user_set, *user_oldset = NULL, *user_signo;
  920. sigset_t set, oldset;
  921. if (zend_parse_parameters(ZEND_NUM_ARGS(), "la|z", &how, &user_set, &user_oldset) == FAILURE) {
  922. RETURN_THROWS();
  923. }
  924. if (sigemptyset(&set) != 0 || sigemptyset(&oldset) != 0) {
  925. PCNTL_G(last_error) = errno;
  926. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  927. RETURN_FALSE;
  928. }
  929. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(user_set), user_signo) {
  930. signo = zval_get_long(user_signo);
  931. if (sigaddset(&set, signo) != 0) {
  932. PCNTL_G(last_error) = errno;
  933. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  934. RETURN_FALSE;
  935. }
  936. } ZEND_HASH_FOREACH_END();
  937. if (sigprocmask(how, &set, &oldset) != 0) {
  938. PCNTL_G(last_error) = errno;
  939. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  940. RETURN_FALSE;
  941. }
  942. if (user_oldset != NULL) {
  943. user_oldset = zend_try_array_init(user_oldset);
  944. if (!user_oldset) {
  945. RETURN_THROWS();
  946. }
  947. for (signo = 1; signo < PCNTL_G(num_signals); ++signo) {
  948. if (sigismember(&oldset, signo) != 1) {
  949. continue;
  950. }
  951. add_next_index_long(user_oldset, signo);
  952. }
  953. }
  954. RETURN_TRUE;
  955. }
  956. /* }}} */
  957. #endif
  958. #ifdef HAVE_STRUCT_SIGINFO_T
  959. # if defined(HAVE_SIGWAITINFO) && defined(HAVE_SIGTIMEDWAIT)
  960. static void pcntl_sigwaitinfo(INTERNAL_FUNCTION_PARAMETERS, int timedwait) /* {{{ */
  961. {
  962. zval *user_set, *user_signo, *user_siginfo = NULL;
  963. zend_long tv_sec = 0, tv_nsec = 0;
  964. sigset_t set;
  965. int signo;
  966. siginfo_t siginfo;
  967. struct timespec timeout;
  968. if (timedwait) {
  969. if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|zll", &user_set, &user_siginfo, &tv_sec, &tv_nsec) == FAILURE) {
  970. RETURN_THROWS();
  971. }
  972. } else {
  973. if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|z", &user_set, &user_siginfo) == FAILURE) {
  974. RETURN_THROWS();
  975. }
  976. }
  977. if (sigemptyset(&set) != 0) {
  978. PCNTL_G(last_error) = errno;
  979. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  980. RETURN_FALSE;
  981. }
  982. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(user_set), user_signo) {
  983. signo = zval_get_long(user_signo);
  984. if (sigaddset(&set, signo) != 0) {
  985. PCNTL_G(last_error) = errno;
  986. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  987. RETURN_FALSE;
  988. }
  989. } ZEND_HASH_FOREACH_END();
  990. if (timedwait) {
  991. timeout.tv_sec = (time_t) tv_sec;
  992. timeout.tv_nsec = tv_nsec;
  993. signo = sigtimedwait(&set, &siginfo, &timeout);
  994. } else {
  995. signo = sigwaitinfo(&set, &siginfo);
  996. }
  997. if (signo == -1 && errno != EAGAIN) {
  998. PCNTL_G(last_error) = errno;
  999. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  1000. }
  1001. /*
  1002. * sigtimedwait and sigwaitinfo can return 0 on success on some
  1003. * platforms, e.g. NetBSD
  1004. */
  1005. if (!signo && siginfo.si_signo) {
  1006. signo = siginfo.si_signo;
  1007. }
  1008. pcntl_siginfo_to_zval(signo, &siginfo, user_siginfo);
  1009. RETURN_LONG(signo);
  1010. }
  1011. /* }}} */
  1012. /* {{{ Synchronously wait for queued signals */
  1013. PHP_FUNCTION(pcntl_sigwaitinfo)
  1014. {
  1015. pcntl_sigwaitinfo(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1016. }
  1017. /* }}} */
  1018. /* {{{ Wait for queued signals */
  1019. PHP_FUNCTION(pcntl_sigtimedwait)
  1020. {
  1021. pcntl_sigwaitinfo(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1022. }
  1023. /* }}} */
  1024. # endif
  1025. static void pcntl_siginfo_to_zval(int signo, siginfo_t *siginfo, zval *user_siginfo) /* {{{ */
  1026. {
  1027. if (signo > 0 && user_siginfo) {
  1028. user_siginfo = zend_try_array_init(user_siginfo);
  1029. if (!user_siginfo) {
  1030. return;
  1031. }
  1032. add_assoc_long_ex(user_siginfo, "signo", sizeof("signo")-1, siginfo->si_signo);
  1033. add_assoc_long_ex(user_siginfo, "errno", sizeof("errno")-1, siginfo->si_errno);
  1034. add_assoc_long_ex(user_siginfo, "code", sizeof("code")-1, siginfo->si_code);
  1035. switch(signo) {
  1036. #ifdef SIGCHLD
  1037. case SIGCHLD:
  1038. add_assoc_long_ex(user_siginfo, "status", sizeof("status")-1, siginfo->si_status);
  1039. # ifdef si_utime
  1040. add_assoc_double_ex(user_siginfo, "utime", sizeof("utime")-1, siginfo->si_utime);
  1041. # endif
  1042. # ifdef si_stime
  1043. add_assoc_double_ex(user_siginfo, "stime", sizeof("stime")-1, siginfo->si_stime);
  1044. # endif
  1045. add_assoc_long_ex(user_siginfo, "pid", sizeof("pid")-1, siginfo->si_pid);
  1046. add_assoc_long_ex(user_siginfo, "uid", sizeof("uid")-1, siginfo->si_uid);
  1047. break;
  1048. case SIGUSR1:
  1049. case SIGUSR2:
  1050. add_assoc_long_ex(user_siginfo, "pid", sizeof("pid")-1, siginfo->si_pid);
  1051. add_assoc_long_ex(user_siginfo, "uid", sizeof("uid")-1, siginfo->si_uid);
  1052. break;
  1053. #endif
  1054. case SIGILL:
  1055. case SIGFPE:
  1056. case SIGSEGV:
  1057. case SIGBUS:
  1058. add_assoc_double_ex(user_siginfo, "addr", sizeof("addr")-1, (zend_long)siginfo->si_addr);
  1059. break;
  1060. #if defined(SIGPOLL) && !defined(__CYGWIN__)
  1061. case SIGPOLL:
  1062. add_assoc_long_ex(user_siginfo, "band", sizeof("band")-1, siginfo->si_band);
  1063. # ifdef si_fd
  1064. add_assoc_long_ex(user_siginfo, "fd", sizeof("fd")-1, siginfo->si_fd);
  1065. # endif
  1066. break;
  1067. #endif
  1068. }
  1069. #if defined(SIGRTMIN) && defined(SIGRTMAX)
  1070. if (SIGRTMIN <= signo && signo <= SIGRTMAX) {
  1071. add_assoc_long_ex(user_siginfo, "pid", sizeof("pid")-1, siginfo->si_pid);
  1072. add_assoc_long_ex(user_siginfo, "uid", sizeof("uid")-1, siginfo->si_uid);
  1073. }
  1074. #endif
  1075. }
  1076. }
  1077. /* }}} */
  1078. #endif
  1079. #ifdef HAVE_GETPRIORITY
  1080. /* {{{ Get the priority of any process */
  1081. PHP_FUNCTION(pcntl_getpriority)
  1082. {
  1083. zend_long who = PRIO_PROCESS;
  1084. zend_long pid;
  1085. bool pid_is_null = 1;
  1086. int pri;
  1087. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!l", &pid, &pid_is_null, &who) == FAILURE) {
  1088. RETURN_THROWS();
  1089. }
  1090. /* needs to be cleared, since any returned value is valid */
  1091. errno = 0;
  1092. pri = getpriority(who, pid_is_null ? getpid() : pid);
  1093. if (errno) {
  1094. PCNTL_G(last_error) = errno;
  1095. switch (errno) {
  1096. case ESRCH:
  1097. php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
  1098. break;
  1099. case EINVAL:
  1100. zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
  1101. RETURN_THROWS();
  1102. default:
  1103. php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
  1104. break;
  1105. }
  1106. RETURN_FALSE;
  1107. }
  1108. RETURN_LONG(pri);
  1109. }
  1110. /* }}} */
  1111. #endif
  1112. #ifdef HAVE_SETPRIORITY
  1113. /* {{{ Change the priority of any process */
  1114. PHP_FUNCTION(pcntl_setpriority)
  1115. {
  1116. zend_long who = PRIO_PROCESS;
  1117. zend_long pid;
  1118. bool pid_is_null = 1;
  1119. zend_long pri;
  1120. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l!l", &pri, &pid, &pid_is_null, &who) == FAILURE) {
  1121. RETURN_THROWS();
  1122. }
  1123. if (setpriority(who, pid_is_null ? getpid() : pid, pri)) {
  1124. PCNTL_G(last_error) = errno;
  1125. switch (errno) {
  1126. case ESRCH:
  1127. php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
  1128. break;
  1129. case EINVAL:
  1130. zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
  1131. RETURN_THROWS();
  1132. case EPERM:
  1133. php_error_docref(NULL, E_WARNING, "Error %d: A process was located, but neither its effective nor real user ID matched the effective user ID of the caller", errno);
  1134. break;
  1135. case EACCES:
  1136. php_error_docref(NULL, E_WARNING, "Error %d: Only a super user may attempt to increase the process priority", errno);
  1137. break;
  1138. default:
  1139. php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
  1140. break;
  1141. }
  1142. RETURN_FALSE;
  1143. }
  1144. RETURN_TRUE;
  1145. }
  1146. /* }}} */
  1147. #endif
  1148. /* {{{ Retrieve the error number set by the last pcntl function which failed. */
  1149. PHP_FUNCTION(pcntl_get_last_error)
  1150. {
  1151. if (zend_parse_parameters_none() == FAILURE) {
  1152. RETURN_THROWS();
  1153. }
  1154. RETURN_LONG(PCNTL_G(last_error));
  1155. }
  1156. /* }}} */
  1157. /* {{{ Retrieve the system error message associated with the given errno. */
  1158. PHP_FUNCTION(pcntl_strerror)
  1159. {
  1160. zend_long error;
  1161. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &error) == FAILURE) {
  1162. RETURN_THROWS();
  1163. }
  1164. RETURN_STRING(strerror(error));
  1165. }
  1166. /* }}} */
  1167. /* Our custom signal handler that calls the appropriate php_function */
  1168. #ifdef HAVE_STRUCT_SIGINFO_T
  1169. static void pcntl_signal_handler(int signo, siginfo_t *siginfo, void *context)
  1170. #else
  1171. static void pcntl_signal_handler(int signo)
  1172. #endif
  1173. {
  1174. struct php_pcntl_pending_signal *psig;
  1175. psig = PCNTL_G(spares);
  1176. if (!psig) {
  1177. /* oops, too many signals for us to track, so we'll forget about this one */
  1178. return;
  1179. }
  1180. PCNTL_G(spares) = psig->next;
  1181. psig->signo = signo;
  1182. psig->next = NULL;
  1183. #ifdef HAVE_STRUCT_SIGINFO_T
  1184. psig->siginfo = *siginfo;
  1185. #endif
  1186. /* the head check is important, as the tick handler cannot atomically clear both
  1187. * the head and tail */
  1188. if (PCNTL_G(head) && PCNTL_G(tail)) {
  1189. PCNTL_G(tail)->next = psig;
  1190. } else {
  1191. PCNTL_G(head) = psig;
  1192. }
  1193. PCNTL_G(tail) = psig;
  1194. PCNTL_G(pending_signals) = 1;
  1195. if (PCNTL_G(async_signals)) {
  1196. EG(vm_interrupt) = 1;
  1197. }
  1198. }
  1199. void pcntl_signal_dispatch()
  1200. {
  1201. zval params[2], *handle, retval;
  1202. struct php_pcntl_pending_signal *queue, *next;
  1203. sigset_t mask;
  1204. sigset_t old_mask;
  1205. if(!PCNTL_G(pending_signals)) {
  1206. return;
  1207. }
  1208. /* Mask all signals */
  1209. sigfillset(&mask);
  1210. sigprocmask(SIG_BLOCK, &mask, &old_mask);
  1211. /* Bail if the queue is empty or if we are already playing the queue */
  1212. if (!PCNTL_G(head) || PCNTL_G(processing_signal_queue)) {
  1213. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  1214. return;
  1215. }
  1216. /* Prevent reentrant handler calls */
  1217. PCNTL_G(processing_signal_queue) = 1;
  1218. queue = PCNTL_G(head);
  1219. PCNTL_G(head) = NULL; /* simple stores are atomic */
  1220. /* Allocate */
  1221. while (queue) {
  1222. if ((handle = zend_hash_index_find(&PCNTL_G(php_signal_table), queue->signo)) != NULL) {
  1223. if (Z_TYPE_P(handle) != IS_LONG) {
  1224. ZVAL_NULL(&retval);
  1225. ZVAL_LONG(&params[0], queue->signo);
  1226. #ifdef HAVE_STRUCT_SIGINFO_T
  1227. array_init(&params[1]);
  1228. pcntl_siginfo_to_zval(queue->signo, &queue->siginfo, &params[1]);
  1229. #else
  1230. ZVAL_NULL(&params[1]);
  1231. #endif
  1232. /* Call php signal handler - Note that we do not report errors, and we ignore the return value */
  1233. /* FIXME: this is probably broken when multiple signals are handled in this while loop (retval) */
  1234. call_user_function(NULL, NULL, handle, &retval, 2, params);
  1235. zval_ptr_dtor(&retval);
  1236. #ifdef HAVE_STRUCT_SIGINFO_T
  1237. zval_ptr_dtor(&params[1]);
  1238. #endif
  1239. }
  1240. }
  1241. next = queue->next;
  1242. queue->next = PCNTL_G(spares);
  1243. PCNTL_G(spares) = queue;
  1244. queue = next;
  1245. }
  1246. PCNTL_G(pending_signals) = 0;
  1247. /* Re-enable queue */
  1248. PCNTL_G(processing_signal_queue) = 0;
  1249. /* return signal mask to previous state */
  1250. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  1251. }
  1252. static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer)
  1253. {
  1254. return pcntl_signal_dispatch();
  1255. }
  1256. /* {{{ Enable/disable asynchronous signal handling and return the old setting. */
  1257. PHP_FUNCTION(pcntl_async_signals)
  1258. {
  1259. bool on, on_is_null = 1;
  1260. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b!", &on, &on_is_null) == FAILURE) {
  1261. RETURN_THROWS();
  1262. }
  1263. if (on_is_null) {
  1264. RETURN_BOOL(PCNTL_G(async_signals));
  1265. }
  1266. RETVAL_BOOL(PCNTL_G(async_signals));
  1267. PCNTL_G(async_signals) = on;
  1268. }
  1269. /* }}} */
  1270. #ifdef HAVE_UNSHARE
  1271. /* {{{ disassociate parts of the process execution context */
  1272. PHP_FUNCTION(pcntl_unshare)
  1273. {
  1274. zend_long flags;
  1275. ZEND_PARSE_PARAMETERS_START(1, 1)
  1276. Z_PARAM_LONG(flags)
  1277. ZEND_PARSE_PARAMETERS_END();
  1278. if (unshare(flags) == -1) {
  1279. PCNTL_G(last_error) = errno;
  1280. switch (errno) {
  1281. #ifdef EINVAL
  1282. case EINVAL:
  1283. zend_argument_value_error(1, "must be a combination of CLONE_* flags");
  1284. RETURN_THROWS();
  1285. break;
  1286. #endif
  1287. #ifdef ENOMEM
  1288. case ENOMEM:
  1289. php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory for unshare", errno);
  1290. break;
  1291. #endif
  1292. #ifdef EPERM
  1293. case EPERM:
  1294. php_error_docref(NULL, E_WARNING, "Error %d: No privilege to use these flags", errno);
  1295. break;
  1296. #endif
  1297. #ifdef ENOSPC
  1298. case ENOSPC:
  1299. php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for one of the specified namespaces", errno);
  1300. break;
  1301. #endif
  1302. #ifdef EUSERS
  1303. case EUSERS:
  1304. php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for the user namespace", errno);
  1305. break;
  1306. #endif
  1307. default:
  1308. php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
  1309. break;
  1310. }
  1311. RETURN_FALSE;
  1312. }
  1313. RETURN_TRUE;
  1314. }
  1315. /* }}} */
  1316. #endif
  1317. #ifdef HAVE_RFORK
  1318. /* {{{ proto bool pcntl_rfork(int flags [, int signal])
  1319. More control over the process creation is given over fork/vfork. */
  1320. PHP_FUNCTION(pcntl_rfork)
  1321. {
  1322. zend_long flags;
  1323. zend_long csignal = 0;
  1324. pid_t pid;
  1325. ZEND_PARSE_PARAMETERS_START(1, 2)
  1326. Z_PARAM_LONG(flags)
  1327. Z_PARAM_OPTIONAL
  1328. Z_PARAM_LONG(csignal)
  1329. ZEND_PARSE_PARAMETERS_END();
  1330. /* This is a flag to use with great caution in general, preferably not within PHP */
  1331. if ((flags & RFMEM) != 0) {
  1332. zend_argument_value_error(1, "must not include RFMEM value, not allowed within this context");
  1333. RETURN_THROWS();
  1334. }
  1335. if ((flags & RFSIGSHARE) != 0) {
  1336. zend_argument_value_error(1, "must not include RFSIGSHARE value, not allowed within this context");
  1337. RETURN_THROWS();
  1338. }
  1339. if ((flags & (RFFDG | RFCFDG)) == (RFFDG | RFCFDG)) {
  1340. zend_argument_value_error(1, "must not include both RFFDG and RFCFDG, because these flags are mutually exclusive");
  1341. RETURN_THROWS();
  1342. }
  1343. /* A new pid is required */
  1344. if (!(flags & (RFPROC))) {
  1345. flags |= RFPROC;
  1346. }
  1347. #ifdef RFTSIGZMB
  1348. if ((flags & RFTSIGZMB) != 0) {
  1349. flags |= RFTSIGFLAGS(csignal);
  1350. }
  1351. #endif
  1352. pid = rfork(flags);
  1353. if (pid == -1) {
  1354. PCNTL_G(last_error) = errno;
  1355. switch (errno) {
  1356. case EAGAIN:
  1357. php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
  1358. break;
  1359. default:
  1360. php_error_docref(NULL, E_WARNING, "Error %d", errno);
  1361. }
  1362. }
  1363. RETURN_LONG((zend_long) pid);
  1364. }
  1365. #endif
  1366. /* }}} */
  1367. static void pcntl_interrupt_function(zend_execute_data *execute_data)
  1368. {
  1369. pcntl_signal_dispatch();
  1370. if (orig_interrupt_function) {
  1371. orig_interrupt_function(execute_data);
  1372. }
  1373. }