xref: /PHP-8.4/ext/sysvshm/sysvshm.c (revision 992ac1c2)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Christian Cartus <cartus@atrior.de>                          |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20 
21 #include "php.h"
22 
23 #ifdef HAVE_SYSVSHM
24 
25 #include <errno.h>
26 
27 #include "php_sysvshm.h"
28 #include "sysvshm_arginfo.h"
29 #include "ext/standard/info.h"
30 #include "ext/standard/php_var.h"
31 #include "zend_smart_str.h"
32 
33 /* SysvSharedMemory class */
34 
35 zend_class_entry *sysvshm_ce;
36 static zend_object_handlers sysvshm_object_handlers;
37 
sysvshm_from_obj(zend_object * obj)38 static inline sysvshm_shm *sysvshm_from_obj(zend_object *obj) {
39 	return (sysvshm_shm *)((char *)(obj) - XtOffsetOf(sysvshm_shm, std));
40 }
41 
42 #define Z_SYSVSHM_P(zv) sysvshm_from_obj(Z_OBJ_P(zv))
43 
sysvshm_create_object(zend_class_entry * class_type)44 static zend_object *sysvshm_create_object(zend_class_entry *class_type) {
45 	sysvshm_shm *intern = zend_object_alloc(sizeof(sysvshm_shm), class_type);
46 
47 	zend_object_std_init(&intern->std, class_type);
48 	object_properties_init(&intern->std, class_type);
49 
50 	return &intern->std;
51 }
52 
sysvshm_get_constructor(zend_object * object)53 static zend_function *sysvshm_get_constructor(zend_object *object) {
54 	zend_throw_error(NULL, "Cannot directly construct SysvSharedMemory, use shm_attach() instead");
55 	return NULL;
56 }
57 
sysvshm_free_obj(zend_object * object)58 static void sysvshm_free_obj(zend_object *object)
59 {
60 	sysvshm_shm *sysvshm = sysvshm_from_obj(object);
61 
62 	if (sysvshm->ptr) {
63 		shmdt((void *) sysvshm->ptr);
64 	}
65 
66 	zend_object_std_dtor(&sysvshm->std);
67 }
68 
69 /* {{{ sysvshm_module_entry */
70 zend_module_entry sysvshm_module_entry = {
71 	STANDARD_MODULE_HEADER,
72 	"sysvshm",
73 	ext_functions,
74 	PHP_MINIT(sysvshm),
75 	NULL,
76 	NULL,
77 	NULL,
78 	PHP_MINFO(sysvshm),
79 	PHP_SYSVSHM_VERSION,
80 	STANDARD_MODULE_PROPERTIES
81 };
82 /* }}} */
83 
84 #ifdef COMPILE_DL_SYSVSHM
85 ZEND_GET_MODULE(sysvshm)
86 #endif
87 
88 #undef shm_ptr					/* undefine AIX-specific macro */
89 
90 /* TODO: Make this thread-safe. */
91 sysvshm_module php_sysvshm;
92 
93 static int php_put_shm_data(sysvshm_chunk_head *ptr, zend_long key, const char *data, zend_long len);
94 static zend_long php_check_shm_data(sysvshm_chunk_head *ptr, zend_long key);
95 static int php_remove_shm_data(sysvshm_chunk_head *ptr, zend_long shm_varpos);
96 
97 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(sysvshm)98 PHP_MINIT_FUNCTION(sysvshm)
99 {
100 	sysvshm_ce = register_class_SysvSharedMemory();
101 	sysvshm_ce->create_object = sysvshm_create_object;
102 	sysvshm_ce->default_object_handlers = &sysvshm_object_handlers;
103 
104 	memcpy(&sysvshm_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
105 	sysvshm_object_handlers.offset = XtOffsetOf(sysvshm_shm, std);
106 	sysvshm_object_handlers.free_obj = sysvshm_free_obj;
107 	sysvshm_object_handlers.get_constructor = sysvshm_get_constructor;
108 	sysvshm_object_handlers.clone_obj = NULL;
109 	sysvshm_object_handlers.compare = zend_objects_not_comparable;
110 
111 	if (cfg_get_long("sysvshm.init_mem", &php_sysvshm.init_mem) == FAILURE) {
112 		php_sysvshm.init_mem=10000;
113 	}
114 	return SUCCESS;
115 }
116 /* }}} */
117 
118 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(sysvshm)119 PHP_MINFO_FUNCTION(sysvshm)
120 {
121 	php_info_print_table_start();
122 	php_info_print_table_row(2, "sysvshm support", "enabled");
123 	php_info_print_table_end();
124 }
125 /* }}} */
126 
127 /* {{{ Creates or open a shared memory segment */
PHP_FUNCTION(shm_attach)128 PHP_FUNCTION(shm_attach)
129 {
130 	sysvshm_shm *shm_list_ptr;
131 	char *shm_ptr;
132 	sysvshm_chunk_head *chunk_ptr;
133 	zend_long shm_key, shm_id, shm_size, shm_flag = 0666;
134 	bool shm_size_is_null = 1;
135 
136 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l|l!l", &shm_key, &shm_size, &shm_size_is_null, &shm_flag)) {
137 		RETURN_THROWS();
138 	}
139 
140 	if (shm_size_is_null) {
141 		shm_size = php_sysvshm.init_mem;
142 	}
143 
144 	if (shm_size < 1) {
145 		zend_argument_value_error(2, "must be greater than 0");
146 		RETURN_THROWS();
147 	}
148 
149 	/* get the id from a specified key or create new shared memory */
150 	if ((shm_id = shmget(shm_key, 0, 0)) < 0) {
151 		if (shm_size < (zend_long)sizeof(sysvshm_chunk_head)) {
152 			php_error_docref(NULL, E_WARNING, "Failed for key 0x" ZEND_XLONG_FMT ": memorysize too small", shm_key);
153 			RETURN_FALSE;
154 		}
155 		if ((shm_id = shmget(shm_key, shm_size, shm_flag | IPC_CREAT | IPC_EXCL)) < 0) {
156 			php_error_docref(NULL, E_WARNING, "Failed for key 0x" ZEND_XLONG_FMT ": %s", shm_key, strerror(errno));
157 			RETURN_FALSE;
158 		}
159 	}
160 
161 	if ((shm_ptr = shmat(shm_id, NULL, 0)) == (void *) -1) {
162 		php_error_docref(NULL, E_WARNING, "Failed for key 0x" ZEND_XLONG_FMT ": %s", shm_key, strerror(errno));
163 		RETURN_FALSE;
164 	}
165 
166 	/* check if shm is already initialized */
167 	chunk_ptr = (sysvshm_chunk_head *) shm_ptr;
168 	if (strcmp((char*) &(chunk_ptr->magic), "PHP_SM") != 0) {
169 		strcpy((char*) &(chunk_ptr->magic), "PHP_SM");
170 		chunk_ptr->start = sizeof(sysvshm_chunk_head);
171 		chunk_ptr->end = chunk_ptr->start;
172 		chunk_ptr->total = shm_size;
173 		chunk_ptr->free = shm_size-chunk_ptr->end;
174 	}
175 
176 	object_init_ex(return_value, sysvshm_ce);
177 
178 	shm_list_ptr = Z_SYSVSHM_P(return_value);
179 
180 	shm_list_ptr->key = shm_key;
181 	shm_list_ptr->id = shm_id;
182 	shm_list_ptr->ptr = chunk_ptr;
183 }
184 /* }}} */
185 
186 /* {{{ Disconnects from shared memory segment */
PHP_FUNCTION(shm_detach)187 PHP_FUNCTION(shm_detach)
188 {
189 	zval *shm_id;
190 	sysvshm_shm *shm_list_ptr;
191 
192 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "O", &shm_id, sysvshm_ce)) {
193 		RETURN_THROWS();
194 	}
195 
196 	shm_list_ptr = Z_SYSVSHM_P(shm_id);
197 	if (!shm_list_ptr->ptr) {
198 		zend_throw_error(NULL, "Shared memory block has already been destroyed");
199 		RETURN_THROWS();
200 	}
201 
202 	shmdt((void *) shm_list_ptr->ptr);
203 	shm_list_ptr->ptr = NULL;
204 
205 	RETURN_TRUE;
206 }
207 /* }}} */
208 
209 /* {{{ Removes shared memory from Unix systems */
PHP_FUNCTION(shm_remove)210 PHP_FUNCTION(shm_remove)
211 {
212 	zval *shm_id;
213 	sysvshm_shm *shm_list_ptr;
214 
215 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "O", &shm_id, sysvshm_ce)) {
216 		RETURN_THROWS();
217 	}
218 
219 	shm_list_ptr = Z_SYSVSHM_P(shm_id);
220 	if (!shm_list_ptr->ptr) {
221 		zend_throw_error(NULL, "Shared memory block has already been destroyed");
222 		RETURN_THROWS();
223 	}
224 
225 	if (shmctl(shm_list_ptr->id, IPC_RMID, NULL) < 0) {
226 		php_error_docref(NULL, E_WARNING, "Failed for key 0x%x, id " ZEND_LONG_FMT ": %s", shm_list_ptr->key, Z_LVAL_P(shm_id), strerror(errno));
227 		RETURN_FALSE;
228 	}
229 
230 	RETURN_TRUE;
231 }
232 /* }}} */
233 
234 /* {{{ Inserts or updates a variable in shared memory */
PHP_FUNCTION(shm_put_var)235 PHP_FUNCTION(shm_put_var)
236 {
237 	zval *shm_id, *arg_var;
238 	int ret;
239 	zend_long shm_key;
240 	sysvshm_shm *shm_list_ptr;
241 	smart_str shm_var = {0};
242 	php_serialize_data_t var_hash;
243 
244 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Olz", &shm_id, sysvshm_ce, &shm_key, &arg_var)) {
245 		RETURN_THROWS();
246 	}
247 
248 	shm_list_ptr = Z_SYSVSHM_P(shm_id);
249 	if (!shm_list_ptr->ptr) {
250 		zend_throw_error(NULL, "Shared memory block has already been destroyed");
251 		RETURN_THROWS();
252 	}
253 
254 	/* setup string-variable and serialize */
255 	PHP_VAR_SERIALIZE_INIT(var_hash);
256 	php_var_serialize(&shm_var, arg_var, &var_hash);
257 	PHP_VAR_SERIALIZE_DESTROY(var_hash);
258 
259 	if (UNEXPECTED(!shm_list_ptr->ptr)) {
260 		smart_str_free(&shm_var);
261 		zend_throw_error(NULL, "Shared memory block has been destroyed by the serialization function");
262 		RETURN_THROWS();
263 	}
264 
265 	/* insert serialized variable into shared memory */
266 	ret = php_put_shm_data(shm_list_ptr->ptr, shm_key, shm_var.s? ZSTR_VAL(shm_var.s) : NULL, shm_var.s? ZSTR_LEN(shm_var.s) : 0);
267 
268 	/* free string */
269 	smart_str_free(&shm_var);
270 
271 	if (ret == -1) {
272 		php_error_docref(NULL, E_WARNING, "Not enough shared memory left");
273 		RETURN_FALSE;
274 	}
275 	RETURN_TRUE;
276 }
277 /* }}} */
278 
279 /* {{{ Returns a variable from shared memory */
PHP_FUNCTION(shm_get_var)280 PHP_FUNCTION(shm_get_var)
281 {
282 	zval *shm_id;
283 	zend_long shm_key;
284 	sysvshm_shm *shm_list_ptr;
285 	char *shm_data;
286 	zend_long shm_varpos;
287 	sysvshm_chunk *shm_var;
288 	php_unserialize_data_t var_hash;
289 
290 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &shm_id, sysvshm_ce, &shm_key)) {
291 		RETURN_THROWS();
292 	}
293 
294 	shm_list_ptr = Z_SYSVSHM_P(shm_id);
295 	if (!shm_list_ptr->ptr) {
296 		zend_throw_error(NULL, "Shared memory block has already been destroyed");
297 		RETURN_THROWS();
298 	}
299 
300 	/* setup string-variable and serialize */
301 	/* get serialized variable from shared memory */
302 	shm_varpos = php_check_shm_data(shm_list_ptr->ptr, shm_key);
303 
304 	if (shm_varpos < 0) {
305 		php_error_docref(NULL, E_WARNING, "Variable key " ZEND_LONG_FMT " doesn't exist", shm_key);
306 		RETURN_FALSE;
307 	}
308 	shm_var = (sysvshm_chunk*) ((char *)shm_list_ptr->ptr + shm_varpos);
309 	shm_data = &shm_var->mem;
310 
311 	PHP_VAR_UNSERIALIZE_INIT(var_hash);
312 	if (php_var_unserialize(return_value, (const unsigned char **) &shm_data, (unsigned char *) shm_data + shm_var->length, &var_hash) != 1) {
313 		php_error_docref(NULL, E_WARNING, "Variable data in shared memory is corrupted");
314 		RETVAL_FALSE;
315 	}
316 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
317 }
318 /* }}} */
319 
320 /* {{{ Checks whether a specific entry exists */
PHP_FUNCTION(shm_has_var)321 PHP_FUNCTION(shm_has_var)
322 {
323 	zval *shm_id;
324 	zend_long shm_key;
325 	sysvshm_shm *shm_list_ptr;
326 
327 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &shm_id, sysvshm_ce, &shm_key)) {
328 		RETURN_THROWS();
329 	}
330 
331 	shm_list_ptr = Z_SYSVSHM_P(shm_id);
332 	if (!shm_list_ptr->ptr) {
333 		zend_throw_error(NULL, "Shared memory block has already been destroyed");
334 		RETURN_THROWS();
335 	}
336 
337 	RETURN_BOOL(php_check_shm_data(shm_list_ptr->ptr, shm_key) >= 0);
338 }
339 /* }}} */
340 
341 /* {{{ Removes variable from shared memory */
PHP_FUNCTION(shm_remove_var)342 PHP_FUNCTION(shm_remove_var)
343 {
344 	zval *shm_id;
345 	zend_long shm_key, shm_varpos;
346 	sysvshm_shm *shm_list_ptr;
347 
348 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &shm_id, sysvshm_ce, &shm_key)) {
349 		RETURN_THROWS();
350 	}
351 
352 	shm_list_ptr = Z_SYSVSHM_P(shm_id);
353 	if (!shm_list_ptr->ptr) {
354 		zend_throw_error(NULL, "Shared memory block has already been destroyed");
355 		RETURN_THROWS();
356 	}
357 
358 	shm_varpos = php_check_shm_data(shm_list_ptr->ptr, shm_key);
359 
360 	if (shm_varpos < 0) {
361 		php_error_docref(NULL, E_WARNING, "Variable key " ZEND_LONG_FMT " doesn't exist", shm_key);
362 		RETURN_FALSE;
363 	}
364 	php_remove_shm_data((shm_list_ptr->ptr), shm_varpos);
365 	RETURN_TRUE;
366 }
367 /* }}} */
368 
369 /* {{{ php_put_shm_data
370  * inserts an ascii-string into shared memory */
php_put_shm_data(sysvshm_chunk_head * ptr,zend_long key,const char * data,zend_long len)371 static int php_put_shm_data(sysvshm_chunk_head *ptr, zend_long key, const char *data, zend_long len)
372 {
373 	sysvshm_chunk *shm_var;
374 	zend_long total_size;
375 	zend_long shm_varpos;
376 
377 	total_size = ((zend_long) (len + sizeof(sysvshm_chunk) - 1) / sizeof(zend_long)) * sizeof(zend_long) + sizeof(zend_long); /* zend_long alligment */
378 
379 	if ((shm_varpos = php_check_shm_data(ptr, key)) > 0) {
380 		php_remove_shm_data(ptr, shm_varpos);
381 	}
382 
383 	if (ptr->free < total_size) {
384 		return -1; /* not enough memory */
385 	}
386 
387 	shm_var = (sysvshm_chunk *) ((char *) ptr + ptr->end);
388 	shm_var->key = key;
389 	shm_var->length = len;
390 	shm_var->next = total_size;
391 	memcpy(&(shm_var->mem), data, len);
392 	ptr->end += total_size;
393 	ptr->free -= total_size;
394 	return 0;
395 }
396 /* }}} */
397 
398 /* {{{ php_check_shm_data */
php_check_shm_data(sysvshm_chunk_head * ptr,zend_long key)399 static zend_long php_check_shm_data(sysvshm_chunk_head *ptr, zend_long key)
400 {
401 	zend_long pos;
402 	sysvshm_chunk *shm_var;
403 
404 	ZEND_ASSERT(ptr);
405 
406 	pos = ptr->start;
407 
408 	for (;;) {
409 		if (pos >= ptr->end) {
410 			return -1;
411 		}
412 		shm_var = (sysvshm_chunk*) ((char *) ptr + pos);
413 		if (shm_var->key == key) {
414 			return pos;
415 		}
416 		pos += shm_var->next;
417 
418 		if (shm_var->next <= 0 || pos < ptr->start) {
419 			return -1;
420 		}
421 	}
422 	return -1;
423 }
424 /* }}} */
425 
426 /* {{{ php_remove_shm_data */
php_remove_shm_data(sysvshm_chunk_head * ptr,zend_long shm_varpos)427 static int php_remove_shm_data(sysvshm_chunk_head *ptr, zend_long shm_varpos)
428 {
429 	sysvshm_chunk *chunk_ptr, *next_chunk_ptr;
430 	zend_long memcpy_len;
431 
432 	ZEND_ASSERT(ptr);
433 
434 	chunk_ptr = (sysvshm_chunk *) ((char *) ptr + shm_varpos);
435 	next_chunk_ptr = (sysvshm_chunk *) ((char *) ptr + shm_varpos + chunk_ptr->next);
436 
437 	memcpy_len = ptr->end-shm_varpos - chunk_ptr->next;
438 	ptr->free += chunk_ptr->next;
439 	ptr->end -= chunk_ptr->next;
440 	if (memcpy_len > 0) {
441 		memmove(chunk_ptr, next_chunk_ptr, memcpy_len);
442 	}
443 	return 0;
444 }
445 /* }}} */
446 
447 #endif /* HAVE_SYSVSHM */
448