1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #ifndef ZABBIX_IPC_H
21 #define ZABBIX_IPC_H
22 
23 #if defined(_WINDOWS)
24 #	error "This module allowed only for Unix OS"
25 #endif
26 
27 #include "mutexs.h"
28 
29 #define ZBX_NONEXISTENT_SHMID		(-1)
30 
31 int	zbx_shm_create(size_t size);
32 int	zbx_shm_destroy(int shmid);
33 
34 /* data copying callback function prototype */
35 typedef void (*zbx_shm_copy_func_t)(void *dst, size_t size_dst, const void *src);
36 
37 /* dynamic shared memory data structure */
38 typedef struct
39 {
40 	/* shared memory segment identifier */
41 	int			shmid;
42 
43 	/* allocated size */
44 	size_t			size;
45 
46 	/* callback function to copy data after shared memory reallocation */
47 	zbx_shm_copy_func_t	copy_func;
48 
49 	zbx_mutex_t		lock;
50 }
51 zbx_dshm_t;
52 
53 /* local process reference to dynamic shared memory data */
54 typedef struct
55 {
56 	/* shared memory segment identifier */
57 	int	shmid;
58 
59 	/* shared memory base address */
60 	void	*addr;
61 }
62 zbx_dshm_ref_t;
63 
64 int	zbx_dshm_create(zbx_dshm_t *shm, size_t shm_size, zbx_mutex_name_t mutex,
65 		zbx_shm_copy_func_t copy_func, char **errmsg);
66 
67 int	zbx_dshm_destroy(zbx_dshm_t *shm, char **errmsg);
68 
69 int	zbx_dshm_realloc(zbx_dshm_t *shm, size_t size, char **errmsg);
70 
71 int	zbx_dshm_validate_ref(const zbx_dshm_t *shm, zbx_dshm_ref_t *shm_ref, char **errmsg);
72 
73 void	zbx_dshm_lock(zbx_dshm_t *shm);
74 void	zbx_dshm_unlock(zbx_dshm_t *shm);
75 
76 #endif
77