pcntl.c 36 KB

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