xref: /PHP-7.4/ext/sysvsem/sysvsem.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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    | Authors: Tom May <tom@go2net.com>                                    |
16    |          Gavin Sherry <gavin@linuxworld.com.au>                      |
17    +----------------------------------------------------------------------+
18  */
19 
20 /* Latest update build anc tested on Linux 2.2.14
21  *
22  * This has been built and tested on Solaris 2.6 and Linux 2.1.122.
23  * It may not compile or execute correctly on other systems.
24  *
25  * sas: Works for me on Linux 2.0.36 and FreeBSD 3.0-current
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include "php.h"
33 
34 #if HAVE_SYSVSEM
35 
36 #include <sys/types.h>
37 #include <sys/ipc.h>
38 #include <sys/sem.h>
39 #include <errno.h>
40 
41 #include "php_sysvsem.h"
42 #include "ext/standard/info.h"
43 
44 #if !HAVE_SEMUN
45 
46 union semun {
47 	int val;                    /* value for SETVAL */
48 	struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
49 	unsigned short int *array;  /* array for GETALL, SETALL */
50 	struct seminfo *__buf;      /* buffer for IPC_INFO */
51 };
52 
53 #undef HAVE_SEMUN
54 #define HAVE_SEMUN 1
55 
56 #endif
57 
58 /* {{{ arginfo */
59 ZEND_BEGIN_ARG_INFO_EX(arginfo_sem_get, 0, 0, 1)
60 	ZEND_ARG_INFO(0, key)
61 	ZEND_ARG_INFO(0, max_acquire)
62 	ZEND_ARG_INFO(0, perm)
63 	ZEND_ARG_INFO(0, auto_release)
64 ZEND_END_ARG_INFO()
65 
66 ZEND_BEGIN_ARG_INFO_EX(arginfo_sem_acquire, 0, 0, 1)
67 	ZEND_ARG_INFO(0, sem_identifier)
68 	ZEND_ARG_INFO(0, nowait)
69 ZEND_END_ARG_INFO()
70 
71 ZEND_BEGIN_ARG_INFO_EX(arginfo_sem_release, 0, 0, 1)
72 	ZEND_ARG_INFO(0, sem_identifier)
73 ZEND_END_ARG_INFO()
74 
75 ZEND_BEGIN_ARG_INFO_EX(arginfo_sem_remove, 0, 0, 1)
76 	ZEND_ARG_INFO(0, sem_identifier)
77 ZEND_END_ARG_INFO()
78 /* }}} */
79 
80 /* {{{ sysvsem_functions[]
81  */
82 static const zend_function_entry sysvsem_functions[] = {
83 	PHP_FE(sem_get,			arginfo_sem_get)
84 	PHP_FE(sem_acquire,		arginfo_sem_acquire)
85 	PHP_FE(sem_release,		arginfo_sem_release)
86 	PHP_FE(sem_remove,		arginfo_sem_remove)
87 	PHP_FE_END
88 };
89 /* }}} */
90 
91 /* {{{ sysvsem_module_entry
92  */
93 zend_module_entry sysvsem_module_entry = {
94 	STANDARD_MODULE_HEADER,
95 	"sysvsem",
96 	sysvsem_functions,
97 	PHP_MINIT(sysvsem),
98 	NULL,
99 	NULL,
100 	NULL,
101 	PHP_MINFO(sysvsem),
102 	PHP_SYSVSEM_VERSION,
103 	STANDARD_MODULE_PROPERTIES
104 };
105 /* }}} */
106 
107 #ifdef COMPILE_DL_SYSVSEM
ZEND_GET_MODULE(sysvsem)108 ZEND_GET_MODULE(sysvsem)
109 #endif
110 
111 
112 THREAD_LS sysvsem_module php_sysvsem_module;
113 
114 /* Semaphore functions using System V semaphores.  Each semaphore
115  * actually consists of three semaphores allocated as a unit under the
116  * same key.  Semaphore 0 (SYSVSEM_SEM) is the actual semaphore, it is
117  * initialized to max_acquire and decremented as processes acquire it.
118  * The value of semaphore 1 (SYSVSEM_USAGE) is a count of the number
119  * of processes using the semaphore.  After calling semget(), if a
120  * process finds that the usage count is 1, it will set the value of
121  * SYSVSEM_SEM to max_acquire.  This allows max_acquire to be set and
122  * track the PHP code without having a global init routine or external
123  * semaphore init code.  Except see the bug regarding a race condition
124  * php_sysvsem_get().  Semaphore 2 (SYSVSEM_SETVAL) serializes the
125  * calls to GETVAL SYSVSEM_USAGE and SETVAL SYSVSEM_SEM.  It can be
126  * acquired only when it is zero.
127  */
128 
129 #define SYSVSEM_SEM		0
130 #define SYSVSEM_USAGE	1
131 #define SYSVSEM_SETVAL	2
132 
133 /* {{{ release_sysvsem_sem
134  */
135 static void release_sysvsem_sem(zend_resource *rsrc)
136 {
137 	sysvsem_sem *sem_ptr = (sysvsem_sem *)rsrc->ptr;
138 	struct sembuf sop[2];
139 	int opcount = 1;
140 /*
141  * if count == -1, semaphore has been removed
142  * Need better way to handle this
143  */
144 
145 	if (sem_ptr->count == -1 || !sem_ptr->auto_release) {
146 		efree(sem_ptr);
147 		return;
148 	}
149 	/* Decrement the usage count. */
150 
151 	sop[0].sem_num = SYSVSEM_USAGE;
152 	sop[0].sem_op  = -1;
153 	sop[0].sem_flg = SEM_UNDO;
154 
155 	/* Release the semaphore if it has been acquired but not released. */
156 
157 	if (sem_ptr->count) {
158 
159 		sop[1].sem_num = SYSVSEM_SEM;
160 		sop[1].sem_op  = sem_ptr->count;
161 		sop[1].sem_flg = SEM_UNDO;
162 
163 		opcount++;
164 	}
165 
166 	semop(sem_ptr->semid, sop, opcount);
167 	efree(sem_ptr);
168 }
169 /* }}} */
170 
171 /* {{{ PHP_MINIT_FUNCTION
172  */
PHP_MINIT_FUNCTION(sysvsem)173 PHP_MINIT_FUNCTION(sysvsem)
174 {
175 	php_sysvsem_module.le_sem = zend_register_list_destructors_ex(release_sysvsem_sem, NULL, "sysvsem", module_number);
176 	return SUCCESS;
177 }
178 /* }}} */
179 
180 /* {{{ PHP_MINFO_FUNCTION
181  */
PHP_MINFO_FUNCTION(sysvsem)182 PHP_MINFO_FUNCTION(sysvsem)
183 {
184 	php_info_print_table_start();
185 	php_info_print_table_row(2, "sysvsem support", "enabled");
186 	php_info_print_table_end();
187 }
188 /* }}} */
189 
190 #define SETVAL_WANTS_PTR
191 
192 #if defined(_AIX)
193 #undef SETVAL_WANTS_PTR
194 #endif
195 
196 /* {{{ proto resource sem_get(int key [, int max_acquire [, int perm [, int auto_release]])
197    Return an id for the semaphore with the given key, and allow max_acquire (default 1) processes to acquire it simultaneously */
PHP_FUNCTION(sem_get)198 PHP_FUNCTION(sem_get)
199 {
200 	zend_long key, max_acquire = 1, perm = 0666, auto_release = 1;
201 	int semid;
202 	struct sembuf sop[3];
203 	int count;
204 	sysvsem_sem *sem_ptr;
205 
206 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l|lll", &key, &max_acquire, &perm, &auto_release)) {
207 		RETURN_FALSE;
208 	}
209 
210 	/* Get/create the semaphore.  Note that we rely on the semaphores
211 	 * being zeroed when they are created.  Despite the fact that
212 	 * the(?)  Linux semget() man page says they are not initialized,
213 	 * the kernel versions 2.0.x and 2.1.z do in fact zero them.
214 	 */
215 
216 	semid = semget(key, 3, perm|IPC_CREAT);
217 	if (semid == -1) {
218 		php_error_docref(NULL, E_WARNING, "failed for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
219 		RETURN_FALSE;
220 	}
221 
222 	/* Find out how many processes are using this semaphore.  Note
223 	 * that on Linux (at least) there is a race condition here because
224 	 * semaphore undo on process exit is not atomic, so we could
225 	 * acquire SYSVSEM_SETVAL before a crashed process has decremented
226 	 * SYSVSEM_USAGE in which case count will be greater than it
227 	 * should be and we won't set max_acquire.  Fortunately this
228 	 * doesn't actually matter in practice.
229 	 */
230 
231 	/* Wait for sem 1 to be zero . . . */
232 
233 	sop[0].sem_num = SYSVSEM_SETVAL;
234 	sop[0].sem_op  = 0;
235 	sop[0].sem_flg = 0;
236 
237 	/* . . . and increment it so it becomes non-zero . . . */
238 
239 	sop[1].sem_num = SYSVSEM_SETVAL;
240 	sop[1].sem_op  = 1;
241 	sop[1].sem_flg = SEM_UNDO;
242 
243 	/* . . . and increment the usage count. */
244 
245 	sop[2].sem_num = SYSVSEM_USAGE;
246 	sop[2].sem_op  = 1;
247 	sop[2].sem_flg = SEM_UNDO;
248 	while (semop(semid, sop, 3) == -1) {
249 		if (errno != EINTR) {
250 			php_error_docref(NULL, E_WARNING, "failed acquiring SYSVSEM_SETVAL for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
251 			break;
252 		}
253 	}
254 
255 	/* Get the usage count. */
256 	count = semctl(semid, SYSVSEM_USAGE, GETVAL, NULL);
257 	if (count == -1) {
258 		php_error_docref(NULL, E_WARNING, "failed for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
259 	}
260 
261 	/* If we are the only user, then take this opportunity to set the max. */
262 
263 	if (count == 1) {
264 #if HAVE_SEMUN
265 		/* This is correct for Linux which has union semun. */
266 		union semun semarg;
267 		semarg.val = max_acquire;
268 		if (semctl(semid, SYSVSEM_SEM, SETVAL, semarg) == -1) {
269 			php_error_docref(NULL, E_WARNING, "failed for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
270 		}
271 #elif defined(SETVAL_WANTS_PTR)
272 		/* This is correct for Solaris 2.6 which does not have union semun. */
273 		if (semctl(semid, SYSVSEM_SEM, SETVAL, &max_acquire) == -1) {
274 			php_error_docref(NULL, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
275 		}
276 #else
277 		/* This works for i.e. AIX */
278 		if (semctl(semid, SYSVSEM_SEM, SETVAL, max_acquire) == -1) {
279 			php_error_docref(NULL, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
280 		}
281 #endif
282 	}
283 
284 	/* Set semaphore 1 back to zero. */
285 
286 	sop[0].sem_num = SYSVSEM_SETVAL;
287 	sop[0].sem_op  = -1;
288 	sop[0].sem_flg = SEM_UNDO;
289 	while (semop(semid, sop, 1) == -1) {
290 		if (errno != EINTR) {
291 			php_error_docref(NULL, E_WARNING, "failed releasing SYSVSEM_SETVAL for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
292 			break;
293 		}
294 	}
295 
296 	sem_ptr = (sysvsem_sem *) emalloc(sizeof(sysvsem_sem));
297 	sem_ptr->key   = key;
298 	sem_ptr->semid = semid;
299 	sem_ptr->count = 0;
300 	sem_ptr->auto_release = auto_release;
301 
302 	RETVAL_RES(zend_register_resource(sem_ptr, php_sysvsem_module.le_sem));
303 	sem_ptr->id = Z_RES_HANDLE_P(return_value);
304 }
305 /* }}} */
306 
307 /* {{{ php_sysvsem_semop
308  */
php_sysvsem_semop(INTERNAL_FUNCTION_PARAMETERS,int acquire)309 static void php_sysvsem_semop(INTERNAL_FUNCTION_PARAMETERS, int acquire)
310 {
311 	zval *arg_id;
312 	zend_bool nowait = 0;
313 	sysvsem_sem *sem_ptr;
314 	struct sembuf sop;
315 
316 	if (acquire) {
317 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|b", &arg_id, &nowait) == FAILURE) {
318 			return;
319 		}
320 	} else {
321 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg_id) == FAILURE) {
322 			return;
323 		}
324 	}
325 
326 	if ((sem_ptr = (sysvsem_sem *)zend_fetch_resource(Z_RES_P(arg_id), "SysV semaphore", php_sysvsem_module.le_sem)) == NULL) {
327 		RETURN_FALSE;
328 	}
329 
330 	if (!acquire && sem_ptr->count == 0) {
331 		php_error_docref(NULL, E_WARNING, "SysV semaphore " ZEND_LONG_FMT " (key 0x%x) is not currently acquired", Z_LVAL_P(arg_id), sem_ptr->key);
332 		RETURN_FALSE;
333 	}
334 
335 	sop.sem_num = SYSVSEM_SEM;
336 	sop.sem_op  = acquire ? -1 : 1;
337 	sop.sem_flg = SEM_UNDO | (nowait ? IPC_NOWAIT : 0);
338 
339 	while (semop(sem_ptr->semid, &sop, 1) == -1) {
340 		if (errno != EINTR) {
341 			if (errno != EAGAIN) {
342 				php_error_docref(NULL, E_WARNING, "failed to %s key 0x%x: %s", acquire ? "acquire" : "release", sem_ptr->key, strerror(errno));
343 			}
344 			RETURN_FALSE;
345 		}
346 	}
347 
348 	sem_ptr->count -= acquire ? -1 : 1;
349 	RETURN_TRUE;
350 }
351 /* }}} */
352 
353 /* {{{ proto bool sem_acquire(resource id)
354    Acquires the semaphore with the given id, blocking if necessary */
PHP_FUNCTION(sem_acquire)355 PHP_FUNCTION(sem_acquire)
356 {
357 	php_sysvsem_semop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
358 }
359 /* }}} */
360 
361 /* {{{ proto bool sem_release(resource id)
362    Releases the semaphore with the given id */
PHP_FUNCTION(sem_release)363 PHP_FUNCTION(sem_release)
364 {
365 	php_sysvsem_semop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
366 }
367 /* }}} */
368 
369 /* {{{ proto bool sem_remove(resource id)
370    Removes semaphore from Unix systems */
371 
372 /*
373  * contributed by Gavin Sherry gavin@linuxworld.com.au
374  * Fri Mar 16 00:50:13 EST 2001
375  */
376 
PHP_FUNCTION(sem_remove)377 PHP_FUNCTION(sem_remove)
378 {
379 	zval *arg_id;
380 	sysvsem_sem *sem_ptr;
381 #if HAVE_SEMUN
382 	union semun un;
383 	struct semid_ds buf;
384 #endif
385 
386 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg_id) == FAILURE) {
387 		return;
388 	}
389 
390 	if ((sem_ptr = (sysvsem_sem *)zend_fetch_resource(Z_RES_P(arg_id), "SysV semaphore", php_sysvsem_module.le_sem)) == NULL) {
391 		RETURN_FALSE;
392 	}
393 
394 #if HAVE_SEMUN
395 	un.buf = &buf;
396 	if (semctl(sem_ptr->semid, 0, IPC_STAT, un) < 0) {
397 #else
398 	if (semctl(sem_ptr->semid, 0, IPC_STAT, NULL) < 0) {
399 #endif
400 		php_error_docref(NULL, E_WARNING, "SysV semaphore " ZEND_LONG_FMT " does not (any longer) exist", Z_LVAL_P(arg_id));
401 		RETURN_FALSE;
402 	}
403 
404 #if HAVE_SEMUN
405 	if (semctl(sem_ptr->semid, 0, IPC_RMID, un) < 0) {
406 #else
407 	if (semctl(sem_ptr->semid, 0, IPC_RMID, NULL) < 0) {
408 #endif
409 		php_error_docref(NULL, E_WARNING, "failed for SysV semaphore " ZEND_LONG_FMT ": %s", Z_LVAL_P(arg_id), strerror(errno));
410 		RETURN_FALSE;
411 	}
412 
413 	/* let release_sysvsem_sem know we have removed
414 	 * the semaphore to avoid issues with releasing.
415 	 */
416 
417 	sem_ptr->count = -1;
418 	RETURN_TRUE;
419 }
420 
421 /* }}} */
422 
423 #endif /* HAVE_SYSVSEM */
424