pcntl.c 42 KB

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