From a86d053f734a6226e56de0e23607148b6834e95c Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Wed, 31 Jul 2019 00:10:25 +0300 Subject: [PATCH] Created extension skeleton. Added functions to open, close and unlink named semaphore --- config.m4 | 7 +++ posix_semaphores.c | 137 +++++++++++++++++++++++++++++++++++++++++++++ posix_semaphores.h | 17 ++++++ 3 files changed, 161 insertions(+) create mode 100644 config.m4 create mode 100644 posix_semaphores.c create mode 100644 posix_semaphores.h diff --git a/config.m4 b/config.m4 new file mode 100644 index 0000000..ebe5eb7 --- /dev/null +++ b/config.m4 @@ -0,0 +1,7 @@ +PHP_ARG_ENABLE(posix_semaphores, whether to enable POSIX semaphores support, +[ --enable-posix-semaphores Enable POSIX semaphores support]) + +if test "$PHP_POSIX_SEMAPHORES" = "yes"; then + AC_DEFINE(HAVE_POSIX_SEMAPHORES, 1, [Whether you have POSIX semaphores]) + PHP_NEW_EXTENSION(posix_semaphores, posix_semaphores.c, $ext_shared) +fi \ No newline at end of file diff --git a/posix_semaphores.c b/posix_semaphores.c new file mode 100644 index 0000000..3edab09 --- /dev/null +++ b/posix_semaphores.c @@ -0,0 +1,137 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#include "posix_semaphores.h" + +#include +#include +#include +#include + +static zend_function_entry posix_semaphores_functions[] = { + PHP_FE(posix_sem_open, NULL) + PHP_FE(posix_sem_close, NULL) + PHP_FE(posix_sem_unlink, NULL) + {NULL, NULL, NULL} +}; + +zend_module_entry posix_semaphores_module_entry = { +#if ZEND_MODULE_API_NO >= 20010901 + STANDARD_MODULE_HEADER, +#endif + PHP_POSIX_SEMAPHORES_EXTNAME, + posix_semaphores_functions, + PHP_MINIT(posix_semaphores), + PHP_MSHUTDOWN(posix_semaphores), + NULL, + NULL, + NULL, +#if ZEND_MODULE_API_NO >= 20010901 + PHP_POSIX_SEMAPHORES_VERSION, +#endif + STANDARD_MODULE_PROPERTIES +}; + +#ifdef COMPILE_DL_POSIX_SEMAPHORES + ZEND_GET_MODULE(posix_semaphores) +#endif + +// Struct to store semaphore pointer. Will be passed as resource to PHP +// and back +typedef struct _php_posix_semaphore { + sem_t *sem; +} php_posix_semaphore; + +static void php_posix_semaphore_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + php_posix_semaphore *s = (php_posix_semaphore*)rsrc->ptr; + if (s) + { + if (s->sem) + { + sem_close(s->sem); + s->sem = NULL; + } + } +} + +#define PHP_POSIX_SEMAPHORES_RES_NAME "POSIX semaphore" + +// list entry identifier +int le_posix_semaphore; + +PHP_MINIT_FUNCTION(posix_semaphores) +{ + le_posix_semaphore = zend_register_list_destructors_ex(php_posix_semaphore_dtor, NULL, PHP_POSIX_SEMAPHORES_RES_NAME, module_number); + return SUCCESS; +} + +PHP_MSHUTDOWN_FUNCTION(posix_semaphores) +{ + return SUCCESS; +} + +/* +* MODULE FUNCTIONS +*/ + +PHP_FUNCTION(posix_sem_open) +{ + char *name; + int name_length; + long flags; + long mode = 0666; + long value = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|ll", &name, &name_length, &flags, &mode, &value) == FAILURE) + { + RETURN_NULL(); + } + sem_t* sem = sem_open(name, flags, mode, value); + if (sem == SEM_FAILED) + { + printf("sem_open failed, errno = %d flags = %d O_CREAT = %d", errno, flags, O_CREAT); + RETURN_NULL(); + } + php_posix_semaphore *semaphore = emalloc(sizeof(php_posix_semaphore)); + semaphore->sem = sem; + ZEND_REGISTER_RESOURCE(return_value, semaphore, le_posix_semaphore); +} + +PHP_FUNCTION(posix_sem_close) +{ + php_posix_semaphore *semaphore; + zval *zsemaphore; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zsemaphore) == FAILURE) + { + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE(semaphore, php_posix_semaphore*, &zsemaphore, -1, PHP_POSIX_SEMAPHORES_RES_NAME, le_posix_semaphore); + if (semaphore->sem) + { + if (sem_close(semaphore->sem) == -1) + { + RETURN_FALSE; + } + semaphore->sem = NULL; + } + + RETURN_TRUE; +} + +PHP_FUNCTION(posix_sem_unlink) +{ + char *name; + int name_length; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_length) == FAILURE) + { + RETURN_NULL(); + } + if (sem_unlink(name) == -1) + { + RETURN_FALSE; + } + RETURN_TRUE; +} \ No newline at end of file diff --git a/posix_semaphores.h b/posix_semaphores.h new file mode 100644 index 0000000..721b1d3 --- /dev/null +++ b/posix_semaphores.h @@ -0,0 +1,17 @@ +#ifndef POSIX_SEMAPHORES_H +#define POSIX_SEMAPHORES_H 1 + +#define PHP_POSIX_SEMAPHORES_VERSION "1.0" +#define PHP_POSIX_SEMAPHORES_EXTNAME "posix_semaphores" + +PHP_MINIT_FUNCTION(posix_semaphores); +PHP_MSHUTDOWN_FUNCTION(posix_semaphores); + +PHP_FUNCTION(posix_sem_open); +PHP_FUNCTION(posix_sem_close); +PHP_FUNCTION(posix_sem_unlink); + +extern zend_module_entry posix_semaphores_module_entry; +#define phpext_posix_semaphores_ptr &posix_semaphores_module_entry; + +#endif