sysvsem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Tom May <tom@go2net.com> |
  14. | Gavin Sherry <gavin@linuxworld.com.au> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #ifdef HAVE_SYSVSEM
  22. #include <sys/types.h>
  23. #include <sys/ipc.h>
  24. #include <sys/sem.h>
  25. #include <errno.h>
  26. #include "sysvsem_arginfo.h"
  27. #include "php_sysvsem.h"
  28. #include "ext/standard/info.h"
  29. #if !HAVE_SEMUN
  30. union semun {
  31. int val; /* value for SETVAL */
  32. struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
  33. unsigned short int *array; /* array for GETALL, SETALL */
  34. struct seminfo *__buf; /* buffer for IPC_INFO */
  35. };
  36. #undef HAVE_SEMUN
  37. #define HAVE_SEMUN 1
  38. #endif
  39. /* {{{ sysvsem_module_entry */
  40. zend_module_entry sysvsem_module_entry = {
  41. STANDARD_MODULE_HEADER,
  42. "sysvsem",
  43. ext_functions,
  44. PHP_MINIT(sysvsem),
  45. NULL,
  46. NULL,
  47. NULL,
  48. PHP_MINFO(sysvsem),
  49. PHP_SYSVSEM_VERSION,
  50. STANDARD_MODULE_PROPERTIES
  51. };
  52. /* }}} */
  53. #ifdef COMPILE_DL_SYSVSEM
  54. ZEND_GET_MODULE(sysvsem)
  55. #endif
  56. /* Semaphore functions using System V semaphores. Each semaphore
  57. * actually consists of three semaphores allocated as a unit under the
  58. * same key. Semaphore 0 (SYSVSEM_SEM) is the actual semaphore, it is
  59. * initialized to max_acquire and decremented as processes acquire it.
  60. * The value of semaphore 1 (SYSVSEM_USAGE) is a count of the number
  61. * of processes using the semaphore. After calling semget(), if a
  62. * process finds that the usage count is 1, it will set the value of
  63. * SYSVSEM_SEM to max_acquire. This allows max_acquire to be set and
  64. * track the PHP code without having a global init routine or external
  65. * semaphore init code. Except see the bug regarding a race condition
  66. * php_sysvsem_get(). Semaphore 2 (SYSVSEM_SETVAL) serializes the
  67. * calls to GETVAL SYSVSEM_USAGE and SETVAL SYSVSEM_SEM. It can be
  68. * acquired only when it is zero.
  69. */
  70. #define SYSVSEM_SEM 0
  71. #define SYSVSEM_USAGE 1
  72. #define SYSVSEM_SETVAL 2
  73. /* SysvSemaphore class */
  74. zend_class_entry *sysvsem_ce;
  75. static zend_object_handlers sysvsem_object_handlers;
  76. static inline sysvsem_sem *sysvsem_from_obj(zend_object *obj) {
  77. return (sysvsem_sem *)((char *)(obj) - XtOffsetOf(sysvsem_sem, std));
  78. }
  79. #define Z_SYSVSEM_P(zv) sysvsem_from_obj(Z_OBJ_P(zv))
  80. static zend_object *sysvsem_create_object(zend_class_entry *class_type) {
  81. sysvsem_sem *intern = zend_object_alloc(sizeof(sysvsem_sem), class_type);
  82. zend_object_std_init(&intern->std, class_type);
  83. object_properties_init(&intern->std, class_type);
  84. intern->std.handlers = &sysvsem_object_handlers;
  85. return &intern->std;
  86. }
  87. static zend_function *sysvsem_get_constructor(zend_object *object) {
  88. zend_throw_error(NULL, "Cannot directly construct SysvSemaphore, use sem_get() instead");
  89. return NULL;
  90. }
  91. static void sysvsem_free_obj(zend_object *object)
  92. {
  93. sysvsem_sem *sem_ptr = sysvsem_from_obj(object);
  94. struct sembuf sop[2];
  95. int opcount = 1;
  96. /*
  97. * if count == -1, semaphore has been removed
  98. * Need better way to handle this
  99. */
  100. if (sem_ptr->count == -1 || !sem_ptr->auto_release) {
  101. zend_object_std_dtor(&sem_ptr->std);
  102. return;
  103. }
  104. /* Decrement the usage count. */
  105. sop[0].sem_num = SYSVSEM_USAGE;
  106. sop[0].sem_op = -1;
  107. sop[0].sem_flg = SEM_UNDO;
  108. /* Release the semaphore if it has been acquired but not released. */
  109. if (sem_ptr->count) {
  110. sop[1].sem_num = SYSVSEM_SEM;
  111. sop[1].sem_op = sem_ptr->count;
  112. sop[1].sem_flg = SEM_UNDO;
  113. opcount++;
  114. }
  115. semop(sem_ptr->semid, sop, opcount);
  116. zend_object_std_dtor(&sem_ptr->std);
  117. }
  118. /* }}} */
  119. /* {{{ PHP_MINIT_FUNCTION */
  120. PHP_MINIT_FUNCTION(sysvsem)
  121. {
  122. sysvsem_ce = register_class_SysvSemaphore();
  123. sysvsem_ce->create_object = sysvsem_create_object;
  124. memcpy(&sysvsem_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  125. sysvsem_object_handlers.offset = XtOffsetOf(sysvsem_sem, std);
  126. sysvsem_object_handlers.free_obj = sysvsem_free_obj;
  127. sysvsem_object_handlers.get_constructor = sysvsem_get_constructor;
  128. sysvsem_object_handlers.clone_obj = NULL;
  129. sysvsem_object_handlers.compare = zend_objects_not_comparable;
  130. return SUCCESS;
  131. }
  132. /* }}} */
  133. /* {{{ PHP_MINFO_FUNCTION */
  134. PHP_MINFO_FUNCTION(sysvsem)
  135. {
  136. php_info_print_table_start();
  137. php_info_print_table_row(2, "sysvsem support", "enabled");
  138. php_info_print_table_end();
  139. }
  140. /* }}} */
  141. #define SETVAL_WANTS_PTR
  142. #if defined(_AIX)
  143. #undef SETVAL_WANTS_PTR
  144. #endif
  145. /* {{{ Return an id for the semaphore with the given key, and allow max_acquire (default 1) processes to acquire it simultaneously */
  146. PHP_FUNCTION(sem_get)
  147. {
  148. zend_long key, max_acquire = 1, perm = 0666;
  149. bool auto_release = 1;
  150. int semid;
  151. struct sembuf sop[3];
  152. int count;
  153. sysvsem_sem *sem_ptr;
  154. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l|llb", &key, &max_acquire, &perm, &auto_release)) {
  155. RETURN_THROWS();
  156. }
  157. /* Get/create the semaphore. Note that we rely on the semaphores
  158. * being zeroed when they are created. Despite the fact that
  159. * the(?) Linux semget() man page says they are not initialized,
  160. * the kernel versions 2.0.x and 2.1.z do in fact zero them.
  161. */
  162. semid = semget(key, 3, perm|IPC_CREAT);
  163. if (semid == -1) {
  164. php_error_docref(NULL, E_WARNING, "Failed for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
  165. RETURN_FALSE;
  166. }
  167. /* Find out how many processes are using this semaphore. Note
  168. * that on Linux (at least) there is a race condition here because
  169. * semaphore undo on process exit is not atomic, so we could
  170. * acquire SYSVSEM_SETVAL before a crashed process has decremented
  171. * SYSVSEM_USAGE in which case count will be greater than it
  172. * should be and we won't set max_acquire. Fortunately this
  173. * doesn't actually matter in practice.
  174. */
  175. /* Wait for sem 1 to be zero . . . */
  176. sop[0].sem_num = SYSVSEM_SETVAL;
  177. sop[0].sem_op = 0;
  178. sop[0].sem_flg = 0;
  179. /* . . . and increment it so it becomes non-zero . . . */
  180. sop[1].sem_num = SYSVSEM_SETVAL;
  181. sop[1].sem_op = 1;
  182. sop[1].sem_flg = SEM_UNDO;
  183. /* . . . and increment the usage count. */
  184. sop[2].sem_num = SYSVSEM_USAGE;
  185. sop[2].sem_op = 1;
  186. sop[2].sem_flg = SEM_UNDO;
  187. while (semop(semid, sop, 3) == -1) {
  188. if (errno != EINTR) {
  189. php_error_docref(NULL, E_WARNING, "Failed acquiring SYSVSEM_SETVAL for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
  190. break;
  191. }
  192. }
  193. /* Get the usage count. */
  194. count = semctl(semid, SYSVSEM_USAGE, GETVAL, NULL);
  195. if (count == -1) {
  196. php_error_docref(NULL, E_WARNING, "Failed for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
  197. }
  198. /* If we are the only user, then take this opportunity to set the max. */
  199. if (count == 1) {
  200. #if HAVE_SEMUN
  201. /* This is correct for Linux which has union semun. */
  202. union semun semarg;
  203. semarg.val = max_acquire;
  204. if (semctl(semid, SYSVSEM_SEM, SETVAL, semarg) == -1) {
  205. php_error_docref(NULL, E_WARNING, "Failed for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
  206. }
  207. #elif defined(SETVAL_WANTS_PTR)
  208. /* This is correct for Solaris 2.6 which does not have union semun. */
  209. if (semctl(semid, SYSVSEM_SEM, SETVAL, &max_acquire) == -1) {
  210. php_error_docref(NULL, E_WARNING, "Failed for key 0x%lx: %s", key, strerror(errno));
  211. }
  212. #else
  213. /* This works for i.e. AIX */
  214. if (semctl(semid, SYSVSEM_SEM, SETVAL, max_acquire) == -1) {
  215. php_error_docref(NULL, E_WARNING, "Failed for key 0x%lx: %s", key, strerror(errno));
  216. }
  217. #endif
  218. }
  219. /* Set semaphore 1 back to zero. */
  220. sop[0].sem_num = SYSVSEM_SETVAL;
  221. sop[0].sem_op = -1;
  222. sop[0].sem_flg = SEM_UNDO;
  223. while (semop(semid, sop, 1) == -1) {
  224. if (errno != EINTR) {
  225. php_error_docref(NULL, E_WARNING, "Failed releasing SYSVSEM_SETVAL for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
  226. break;
  227. }
  228. }
  229. object_init_ex(return_value, sysvsem_ce);
  230. sem_ptr = Z_SYSVSEM_P(return_value);
  231. sem_ptr->key = key;
  232. sem_ptr->semid = semid;
  233. sem_ptr->count = 0;
  234. sem_ptr->auto_release = (int) auto_release;
  235. }
  236. /* }}} */
  237. /* {{{ php_sysvsem_semop */
  238. static void php_sysvsem_semop(INTERNAL_FUNCTION_PARAMETERS, int acquire)
  239. {
  240. zval *arg_id;
  241. bool nowait = 0;
  242. sysvsem_sem *sem_ptr;
  243. struct sembuf sop;
  244. if (acquire) {
  245. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &arg_id, sysvsem_ce, &nowait) == FAILURE) {
  246. RETURN_THROWS();
  247. }
  248. } else {
  249. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg_id, sysvsem_ce) == FAILURE) {
  250. RETURN_THROWS();
  251. }
  252. }
  253. sem_ptr = Z_SYSVSEM_P(arg_id);
  254. if (!acquire && sem_ptr->count == 0) {
  255. php_error_docref(NULL, E_WARNING, "SysV semaphore for key 0x%x is not currently acquired", sem_ptr->key);
  256. RETURN_FALSE;
  257. }
  258. sop.sem_num = SYSVSEM_SEM;
  259. sop.sem_op = acquire ? -1 : 1;
  260. sop.sem_flg = SEM_UNDO | (nowait ? IPC_NOWAIT : 0);
  261. while (semop(sem_ptr->semid, &sop, 1) == -1) {
  262. if (errno != EINTR) {
  263. if (errno != EAGAIN) {
  264. php_error_docref(NULL, E_WARNING, "Failed to %s key 0x%x: %s", acquire ? "acquire" : "release", sem_ptr->key, strerror(errno));
  265. }
  266. RETURN_FALSE;
  267. }
  268. }
  269. sem_ptr->count -= acquire ? -1 : 1;
  270. RETURN_TRUE;
  271. }
  272. /* }}} */
  273. /* {{{ Acquires the semaphore with the given id, blocking if necessary */
  274. PHP_FUNCTION(sem_acquire)
  275. {
  276. php_sysvsem_semop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  277. }
  278. /* }}} */
  279. /* {{{ Releases the semaphore with the given id */
  280. PHP_FUNCTION(sem_release)
  281. {
  282. php_sysvsem_semop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  283. }
  284. /* }}} */
  285. /* {{{ Removes semaphore from Unix systems */
  286. /*
  287. * contributed by Gavin Sherry gavin@linuxworld.com.au
  288. * Fri Mar 16 00:50:13 EST 2001
  289. */
  290. PHP_FUNCTION(sem_remove)
  291. {
  292. zval *arg_id;
  293. sysvsem_sem *sem_ptr;
  294. #if HAVE_SEMUN
  295. union semun un;
  296. struct semid_ds buf;
  297. #endif
  298. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg_id, sysvsem_ce) == FAILURE) {
  299. RETURN_THROWS();
  300. }
  301. sem_ptr = Z_SYSVSEM_P(arg_id);
  302. #if HAVE_SEMUN
  303. un.buf = &buf;
  304. if (semctl(sem_ptr->semid, 0, IPC_STAT, un) < 0) {
  305. #else
  306. if (semctl(sem_ptr->semid, 0, IPC_STAT, NULL) < 0) {
  307. #endif
  308. php_error_docref(NULL, E_WARNING, "SysV semaphore for key 0x%x does not (any longer) exist", sem_ptr->key);
  309. RETURN_FALSE;
  310. }
  311. #if HAVE_SEMUN
  312. if (semctl(sem_ptr->semid, 0, IPC_RMID, un) < 0) {
  313. #else
  314. if (semctl(sem_ptr->semid, 0, IPC_RMID, NULL) < 0) {
  315. #endif
  316. php_error_docref(NULL, E_WARNING, "Failed for SysV semaphore for key 0x%x: %s", sem_ptr->key, strerror(errno));
  317. RETURN_FALSE;
  318. }
  319. /* let release_sysvsem_sem know we have removed
  320. * the semaphore to avoid issues with releasing.
  321. */
  322. sem_ptr->count = -1;
  323. RETURN_TRUE;
  324. }
  325. /* }}} */
  326. #endif /* HAVE_SYSVSEM */