Source
zabbix_log(LOG_LEVEL_TRACE, "cannot remove semaphore set %d: %s", ZBX_SEM_LIST_ID, zbx_strerror(errno));
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
typedef struct
{
pthread_mutex_t mutexes[ZBX_MUTEX_COUNT];
pthread_rwlock_t rwlocks[ZBX_RWLOCK_COUNT];
}
zbx_shared_lock_t;
static zbx_shared_lock_t *shared_lock;
static int shm_id, locks_disabled;
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 */
};
/* HAVE_SEMUN */
static int ZBX_SEM_LIST_ID = -1;
static unsigned char mutexes;
/******************************************************************************
* *
* Purpose: if pthread mutexes and read-write locks can be shared between *
* processes then create them, otherwise fallback to System V *
* semaphore operations *
* *
* Parameters: error - [OUT] dynamically allocated memory with error message. *
* *
* Return value: SUCCEED if mutexes successfully created, otherwise FAIL *
* *
******************************************************************************/
int zbx_locks_create(char **error)
{
int i;
pthread_mutexattr_t mta;
pthread_rwlockattr_t rwa;
if (-1 == (shm_id = shmget(IPC_PRIVATE, ZBX_SIZE_T_ALIGN8(sizeof(zbx_shared_lock_t)),
IPC_CREAT | IPC_EXCL | 0600)))
{
*error = zbx_dsprintf(*error, "cannot allocate shared memory for locks");
return FAIL;
}
if ((void *)(-1) == (shared_lock = (zbx_shared_lock_t *)shmat(shm_id, NULL, 0)))
{
*error = zbx_dsprintf(*error, "cannot attach shared memory for locks: %s", zbx_strerror(errno));
return FAIL;
}
memset(shared_lock, 0, sizeof(zbx_shared_lock_t));
/* immediately mark the new shared memory for destruction after attaching to it */
if (-1 == shmctl(shm_id, IPC_RMID, 0))
{
*error = zbx_dsprintf(*error, "cannot mark the new shared memory for destruction: %s",
zbx_strerror(errno));
return FAIL;
}