Source
zbx_error("[file:'%s',line:%d] read-write lock unlock failed: %s", filename, line, zbx_strerror(errno));
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
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))