Skip to content

Refactor union semun in ext/sysvsem #13473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions ext/sysvsem/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,9 @@ PHP_ARG_ENABLE([sysvsem],
[Enable System V semaphore support])])

if test "$PHP_SYSVSEM" != "no"; then
PHP_NEW_EXTENSION(sysvsem, sysvsem.c, $ext_shared)
AC_DEFINE(HAVE_SYSVSEM, 1, [ ])
AC_CACHE_CHECK(for union semun,php_cv_semun,
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
]], [[union semun x;]])],[
php_cv_semun=yes
],[
php_cv_semun=no
])
)
if test "$php_cv_semun" = "yes"; then
AC_DEFINE(HAVE_SEMUN, 1, [ ])
else
AC_DEFINE(HAVE_SEMUN, 0, [ ])
fi
PHP_NEW_EXTENSION(sysvsem, sysvsem.c, $ext_shared)
AC_DEFINE(HAVE_SYSVSEM, 1, [ ])
AC_CHECK_TYPES([union semun],,,[#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>])
fi
36 changes: 1 addition & 35 deletions ext/sysvsem/sysvsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,13 @@
#include "php_sysvsem.h"
#include "ext/standard/info.h"

#if !HAVE_SEMUN

#ifndef HAVE_UNION_SEMUN
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short int *array; /* array for GETALL, SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
};

#undef HAVE_SEMUN
#define HAVE_SEMUN 1

#endif

/* {{{ sysvsem_module_entry */
Expand Down Expand Up @@ -173,12 +168,6 @@ PHP_MINFO_FUNCTION(sysvsem)
}
/* }}} */

#define SETVAL_WANTS_PTR

#if defined(_AIX)
#undef SETVAL_WANTS_PTR
#endif

/* {{{ 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)
{
Expand Down Expand Up @@ -247,24 +236,11 @@ PHP_FUNCTION(sem_get)
/* If we are the only user, then take this opportunity to set the max. */

if (count == 1) {
#if HAVE_SEMUN
/* This is correct for Linux which has union semun. */
union semun semarg;
semarg.val = max_acquire;
if (semctl(semid, SYSVSEM_SEM, SETVAL, semarg) == -1) {
php_error_docref(NULL, E_WARNING, "Failed for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
}
#elif defined(SETVAL_WANTS_PTR)
/* This is correct for Solaris 2.6 which does not have union semun. */
if (semctl(semid, SYSVSEM_SEM, SETVAL, &max_acquire) == -1) {
php_error_docref(NULL, E_WARNING, "Failed for key 0x%lx: %s", key, strerror(errno));
}
#else
/* This works for i.e. AIX */
if (semctl(semid, SYSVSEM_SEM, SETVAL, max_acquire) == -1) {
php_error_docref(NULL, E_WARNING, "Failed for key 0x%lx: %s", key, strerror(errno));
}
#endif
}

/* Set semaphore 1 back to zero. */
Expand Down Expand Up @@ -357,32 +333,22 @@ PHP_FUNCTION(sem_remove)
{
zval *arg_id;
sysvsem_sem *sem_ptr;
#if HAVE_SEMUN
union semun un;
struct semid_ds buf;
#endif

if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &arg_id, sysvsem_ce) == FAILURE) {
RETURN_THROWS();
}

sem_ptr = Z_SYSVSEM_P(arg_id);

#if HAVE_SEMUN
un.buf = &buf;
if (semctl(sem_ptr->semid, 0, IPC_STAT, un) < 0) {
#else
if (semctl(sem_ptr->semid, 0, IPC_STAT, NULL) < 0) {
#endif
php_error_docref(NULL, E_WARNING, "SysV semaphore for key 0x%x does not (any longer) exist", sem_ptr->key);
RETURN_FALSE;
}

#if HAVE_SEMUN
if (semctl(sem_ptr->semid, 0, IPC_RMID, un) < 0) {
#else
if (semctl(sem_ptr->semid, 0, IPC_RMID, NULL) < 0) {
#endif
php_error_docref(NULL, E_WARNING, "Failed for SysV semaphore for key 0x%x: %s", sem_ptr->key, strerror(errno));
RETURN_FALSE;
}
Expand Down