Index: include/memalloc.h
===================================================================
--- include/memalloc.h	(revision 65488)
+++ include/memalloc.h	(revision 65489)
@@ -46,8 +46,7 @@
 }
 zbx_mem_info_t;
 
-void	zbx_mem_create(zbx_mem_info_t **info, key_t shm_key, int lock_name, zbx_uint64_t size,
-		const char *descr, const char *param, int allow_oom);
+void	zbx_mem_create(zbx_mem_info_t **info, zbx_uint64_t size, const char *descr, const char *param, int allow_oom);
 void	zbx_mem_destroy(zbx_mem_info_t *info);
 
 #define	zbx_mem_malloc(info, old, size) __zbx_mem_malloc(__FILE__, __LINE__, info, old, size)
Index: include/ipc.h
===================================================================
--- include/ipc.h	(revision 65488)
+++ include/ipc.h	(revision 65489)
@@ -28,16 +28,9 @@
 
 #define ZBX_NONEXISTENT_SHMID		(-1)
 
-#define ZBX_IPC_CONFIG_ID		'g'
-#define ZBX_IPC_HISTORY_ID		'h'
-#define ZBX_IPC_HISTORY_INDEX_ID	'H'
-#define ZBX_IPC_TREND_ID		't'
-#define ZBX_IPC_STRPOOL_ID		's'
 #define ZBX_IPC_COLLECTOR_ID		'l'
 #define ZBX_IPC_COLLECTOR_DISKSTAT	'm'
 #define ZBX_IPC_SELFMON_ID		'S'
-#define ZBX_IPC_VALUECACHE_ID		'v'
-#define ZBX_IPC_VMWARE_ID		'w'
 #define ZBX_IPC_COLLECTOR_PROC_ID	'p'
 
 key_t	zbx_ftok(char *path, int id);
Index: include/mutexs.h
===================================================================
--- include/mutexs.h	(revision 65488)
+++ include/mutexs.h	(revision 65489)
@@ -37,7 +37,6 @@
 
 #	define ZBX_MUTEX_NAME		int
 
-#	define ZBX_NO_MUTEX		-1
 #	define ZBX_MUTEX_LOG		0
 #	define ZBX_MUTEX_CACHE		1
 #	define ZBX_MUTEX_TRENDS		2
Index: include/dbcache.h
===================================================================
--- include/dbcache.h	(revision 65488)
+++ include/dbcache.h	(revision 65489)
@@ -47,7 +47,6 @@
 
 #define ZBX_EXPAND_MACROS		1
 
-extern char	*CONFIG_FILE;
 extern int	CONFIG_TIMEOUT;
 
 extern zbx_uint64_t	CONFIG_CONF_CACHE_SIZE;
Index: src/zabbix_proxy/Makefile.am
===================================================================
--- src/zabbix_proxy/Makefile.am	(revision 65488)
+++ src/zabbix_proxy/Makefile.am	(revision 65489)
@@ -38,6 +38,7 @@
 	$(top_srcdir)/src/libs/zbxmemory/libzbxmemory.a \
 	$(top_srcdir)/src/libs/zbxregexp/libzbxregexp.a \
 	$(top_srcdir)/src/libs/zbxalgo/libzbxalgo.a \
+	$(top_srcdir)/src/libs/zbxself/libzbxself.a \
 	$(top_srcdir)/src/libs/zbxnix/libzbxnix.a \
 	$(top_srcdir)/src/libs/zbxsys/libzbxsys.a \
 	$(top_srcdir)/src/libs/zbxconf/libzbxconf.a \
@@ -47,7 +48,6 @@
 	$(top_srcdir)/src/libs/zbxcommshigh/libzbxcommshigh.a \
 	$(top_srcdir)/src/libs/zbxjson/libzbxjson.a \
 	$(top_srcdir)/src/libs/zbxexec/libzbxexec.a \
-	$(top_srcdir)/src/libs/zbxself/libzbxself.a \
 	$(top_srcdir)/src/libs/zbxicmpping/libzbxicmpping.a \
 	$(top_srcdir)/src/libs/zbxdbupgrade/libzbxdbupgrade.a \
 	$(top_srcdir)/src/libs/zbxdbhigh/libzbxdbhigh.a \
Index: src/zabbix_server/vmware/vmware.c
===================================================================
--- src/zabbix_server/vmware/vmware.c	(revision 65488)
+++ src/zabbix_server/vmware/vmware.c	(revision 65489)
@@ -63,7 +63,6 @@
  * performance data updates.
  */
 
-extern char		*CONFIG_FILE;
 extern int		CONFIG_VMWARE_FREQUENCY;
 extern int		CONFIG_VMWARE_PERF_FREQUENCY;
 extern zbx_uint64_t	CONFIG_VMWARE_CACHE_SIZE;
@@ -4087,7 +4086,6 @@
 {
 	const char	*__function_name = "zbx_vmware_init";
 
-	key_t		shm_key;
 	zbx_uint64_t	size_reserved;
 
 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
@@ -4094,18 +4092,11 @@
 
 	zbx_mutex_create(&vmware_lock, ZBX_MUTEX_VMWARE);
 
-	if (-1 == (shm_key = zbx_ftok(CONFIG_FILE, ZBX_IPC_VMWARE_ID)))
-	{
-		zabbix_log(LOG_LEVEL_CRIT, "cannot create IPC key for vmware cache");
-		exit(EXIT_FAILURE);
-	}
-
 	size_reserved = zbx_mem_required_size(1, "vmware cache size", "VMwareCacheSize");
 
 	CONFIG_VMWARE_CACHE_SIZE -= size_reserved;
 
-	zbx_mem_create(&vmware_mem, shm_key, ZBX_NO_MUTEX, CONFIG_VMWARE_CACHE_SIZE, "vmware cache size",
-			"VMwareCacheSize", 0);
+	zbx_mem_create(&vmware_mem, CONFIG_VMWARE_CACHE_SIZE, "vmware cache size", "VMwareCacheSize", 0);
 
 	vmware = __vm_mem_malloc_func(NULL, sizeof(zbx_vmware_t));
 	memset(vmware, 0, sizeof(zbx_vmware_t));
Index: src/zabbix_server/Makefile.am
===================================================================
--- src/zabbix_server/Makefile.am	(revision 65488)
+++ src/zabbix_server/Makefile.am	(revision 65489)
@@ -53,6 +53,7 @@
 	$(top_srcdir)/src/libs/zbxmemory/libzbxmemory.a \
 	$(top_srcdir)/src/libs/zbxregexp/libzbxregexp.a \
 	$(top_srcdir)/src/libs/zbxalgo/libzbxalgo.a \
+	$(top_srcdir)/src/libs/zbxself/libzbxself.a \
 	$(top_srcdir)/src/libs/zbxnix/libzbxnix.a \
 	$(top_srcdir)/src/libs/zbxsys/libzbxsys.a \
 	$(top_srcdir)/src/libs/zbxconf/libzbxconf.a \
@@ -63,7 +64,6 @@
 	$(top_srcdir)/src/libs/zbxcommshigh/libzbxcommshigh.a \
 	$(top_srcdir)/src/libs/zbxjson/libzbxjson.a \
 	$(top_srcdir)/src/libs/zbxexec/libzbxexec.a \
-	$(top_srcdir)/src/libs/zbxself/libzbxself.a \
 	$(top_srcdir)/src/libs/zbxicmpping/libzbxicmpping.a \
 	$(top_srcdir)/src/libs/zbxdbupgrade/libzbxdbupgrade.a \
 	$(top_srcdir)/src/libs/zbxdbhigh/libzbxdbhigh.a \
Index: src/libs/zbxmemory/strpool.c
===================================================================
--- src/libs/zbxmemory/strpool.c	(revision 65488)
+++ src/libs/zbxmemory/strpool.c	(revision 65489)
@@ -26,8 +26,6 @@
 
 #include "strpool.h"
 
-extern char		*CONFIG_FILE;
-
 static zbx_strpool_t	strpool;
 
 static zbx_hash_t	__strpool_hash_func(const void *data);
@@ -58,18 +56,10 @@
 {
 	const char	*__function_name = "zbx_strpool_create";
 
-	key_t		shm_key;
-
 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
 
-	if (-1 == (shm_key = zbx_ftok(CONFIG_FILE, ZBX_IPC_STRPOOL_ID)))
-	{
-		zabbix_log(LOG_LEVEL_CRIT, "cannot create IPC key for string pool");
-		exit(EXIT_FAILURE);
-	}
+	zbx_mem_create(&strpool.mem_info, size, "string pool", "CacheSize", 0);
 
-	zbx_mem_create(&strpool.mem_info, shm_key, ZBX_NO_MUTEX, size, "string pool", "CacheSize", 0);
-
 	strpool.hashset = __strpool_mem_malloc_func(NULL, sizeof(zbx_hashset_t));
 	zbx_hashset_create_ext(strpool.hashset, INIT_HASHSET_SIZE,
 				__strpool_hash_func, __strpool_compare_func, NULL,
Index: src/libs/zbxmemory/memalloc.c
===================================================================
--- src/libs/zbxmemory/memalloc.c	(revision 65488)
+++ src/libs/zbxmemory/memalloc.c	(revision 65489)
@@ -19,11 +19,12 @@
 
 #include "common.h"
 #include "mutexs.h"
-#include "ipc.h"
 #include "log.h"
 
 #include "memalloc.h"
 
+extern char	*CONFIG_FILE;
+
 /******************************************************************************
  *                                                                            *
  *                     Some information on memory layout                      *
@@ -540,13 +541,19 @@
 
 /* public memory interface */
 
-void	zbx_mem_create(zbx_mem_info_t **info, key_t shm_key, int lock_name, zbx_uint64_t size,
-		const char *descr, const char *param, int allow_oom)
+void	zbx_mem_create(zbx_mem_info_t **info, zbx_uint64_t size, const char *descr, const char *param, int allow_oom)
 {
-	const char	*__function_name = "zbx_mem_create";
+	const char		*__function_name = "zbx_mem_create";
 
-	int		shm_id, index;
-	void		*base;
+	const char		proj_ids[] = "abcdefghijknoqrstuvwxy"		/* 'l', 'm', 'S', 'p', 'z' are used */
+						"ABCDEFGHIJKLMNOPQRTUVWXYZ";	/* to allocate memory segments and  */
+										/* semaphores in other places, see  */
+										/* ipc.h and mutexs.c for details   */
+	static const char	*proj_id = NULL;
+	key_t			key;
+	int			shm_id, index;
+	struct shmid_ds		shmid_ds;
+	void			*base;
 
 	descr = ZBX_NULL2STR(descr);
 	param = ZBX_NULL2STR(param);
@@ -570,12 +577,75 @@
 		exit(EXIT_FAILURE);
 	}
 
-	if (-1 == (shm_id = zbx_shmget(shm_key, size)))
+	if (NULL == proj_id)
 	{
-		zabbix_log(LOG_LEVEL_CRIT, "cannot allocate shared memory for %s", descr);
-		exit(EXIT_FAILURE);
+		/* do the cleanup */
+
+		for (proj_id = proj_ids; '\0' != *proj_id; proj_id++)
+		{
+			if (-1 == (key = ftok(CONFIG_FILE, *proj_id)))
+			{
+				zbx_error("cannot create IPC key for path [%s]: %s", CONFIG_FILE, zbx_strerror(errno));
+				exit(EXIT_FAILURE);
+			}
+
+			if (-1 == (shm_id = shmget(key, 0, 0600)))
+				continue;	/* either we don't have permissions or segment does not exist */
+
+			zabbix_log(LOG_LEVEL_DEBUG, "attempting to remove existing shm_id:%d", shm_id);
+
+			if (-1 != shmctl(shm_id, IPC_STAT, &shmid_ds))
+			{
+				zabbix_log(LOG_LEVEL_DEBUG, "segment size:" ZBX_FS_SIZE_T " last attach time:%d"
+						" last detach time:%d last change time: %d PID of creator:%d"
+						" PID of last shmat()/shmdt():%d number of current attaches:%d"
+						" UID of owner:%d GID of owner:%d UID of creator GID of creator:%d",
+						(zbx_fs_size_t)shmid_ds.shm_segsz, (int)shmid_ds.shm_atime,
+						(int)shmid_ds.shm_dtime, (int)shmid_ds.shm_ctime, (int)shmid_ds.shm_cpid,
+						(int)shmid_ds.shm_lpid, (int)shmid_ds.shm_nattch,
+						(int)shmid_ds.shm_perm.uid, (int)shmid_ds.shm_perm.gid,
+						(int)shmid_ds.shm_perm.cuid, (int)shmid_ds.shm_perm.cgid);
+			}
+			else
+				zabbix_log(LOG_LEVEL_DEBUG, "no stats available: %s", zbx_strerror(errno));
+
+			if (-1 != shmctl(shm_id, IPC_RMID, NULL))
+				zabbix_log(LOG_LEVEL_DEBUG, "shm_id:%d successfully marked for deletion", shm_id);
+			else
+				zabbix_log(LOG_LEVEL_DEBUG, "deletion attempt failed: %s", zbx_strerror(errno));
+		}
+
+		proj_id = proj_ids;
 	}
 
+	for (;;)
+	{
+		if ('\0' == *proj_id)
+		{
+			zbx_error("ran out of possible ids for IPC key creation, please remove unused shared memory"
+					" segments manually");
+			exit(EXIT_FAILURE);
+		}
+
+		if (-1 == (key = ftok(CONFIG_FILE, *proj_id++)))
+		{
+			zbx_error("cannot create IPC key for path [%s]: %s", CONFIG_FILE, zbx_strerror(errno));
+			exit(EXIT_FAILURE);
+		}
+
+		if (-1 == (shm_id = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600)))
+		{
+			if (EEXIST == errno)
+				continue;
+
+			zbx_error("cannot allocate shared memory of size " ZBX_FS_SIZE_T " for %s: %s",
+					(zbx_fs_size_t)size, descr, zbx_strerror(errno));
+			exit(EXIT_FAILURE);
+		}
+
+		break;
+	}
+
 	if ((void *)(-1) == (base = shmat(shm_id, NULL, 0)))
 	{
 		zabbix_log(LOG_LEVEL_CRIT, "cannot attach shared memory for %s: %s", descr, zbx_strerror(errno));
@@ -608,21 +678,9 @@
 
 	(*info)->allow_oom = allow_oom;
 
-	/* allocate mutex */
+	/* do not allocate mutex */
+	(*info)->use_lock = 0;
 
-	if (ZBX_NO_MUTEX != lock_name)
-	{
-		(*info)->use_lock = 1;
-
-		if (FAIL == zbx_mutex_create_force(&((*info)->mem_lock), lock_name))
-		{
-			zabbix_log(LOG_LEVEL_CRIT, "cannot create mutex for %s", descr);
-			exit(EXIT_FAILURE);
-		}
-	}
-	else
-		(*info)->use_lock = 0;
-
 	/* prepare shared memory for further allocation by creating one big chunk */
 	(*info)->lo_bound = ALIGN8(base);
 	(*info)->hi_bound = ALIGN8((char *)base + size - 8);
Index: src/libs/zbxdbcache/valuecache.c
===================================================================
--- src/libs/zbxdbcache/valuecache.c	(revision 65488)
+++ src/libs/zbxdbcache/valuecache.c	(revision 65489)
@@ -2758,7 +2758,6 @@
 void	zbx_vc_init(void)
 {
 	const char	*__function_name = "zbx_vc_init";
-	key_t		shm_key;
 	zbx_uint64_t	size_reserved;
 
 	if (0 == CONFIG_VALUE_CACHE_SIZE)
@@ -2766,12 +2765,6 @@
 
 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
 
-	if (-1 == (shm_key = zbx_ftok(CONFIG_FILE, ZBX_IPC_VALUECACHE_ID)))
-	{
-		zabbix_log(LOG_LEVEL_CRIT, "cannot create IPC key for value cache");
-		exit(EXIT_FAILURE);
-	}
-
 	if (FAIL == zbx_mutex_create_force(&vc_lock, ZBX_MUTEX_VALUECACHE))
 	{
 		zbx_error("cannot create mutex for value cache");
@@ -2780,8 +2773,7 @@
 
 	size_reserved = zbx_mem_required_size(1, "value cache size", "ValueCacheSize");
 
-	zbx_mem_create(&vc_mem, shm_key, ZBX_NO_MUTEX, CONFIG_VALUE_CACHE_SIZE,
-			"value cache size", "ValueCacheSize", 1);
+	zbx_mem_create(&vc_mem, CONFIG_VALUE_CACHE_SIZE, "value cache size", "ValueCacheSize", 1);
 
 	CONFIG_VALUE_CACHE_SIZE -= size_reserved;
 
Index: src/libs/zbxdbcache/dbcache.c
===================================================================
--- src/libs/zbxdbcache/dbcache.c	(revision 65488)
+++ src/libs/zbxdbcache/dbcache.c	(revision 65489)
@@ -3233,17 +3233,10 @@
 static void	init_trend_cache()
 {
 	const char	*__function_name = "init_trend_cache";
-	key_t		trend_shm_key;
 	size_t		sz;
 
 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
 
-	if (-1 == (trend_shm_key = zbx_ftok(CONFIG_FILE, ZBX_IPC_TREND_ID)))
-	{
-		zabbix_log(LOG_LEVEL_CRIT, "cannot create IPC key for trend cache");
-		exit(EXIT_FAILURE);
-	}
-
 	if (FAIL == zbx_mutex_create_force(&trends_lock, ZBX_MUTEX_TRENDS))
 	{
 		zbx_error("cannot create mutex for trend cache");
@@ -3251,8 +3244,7 @@
 	}
 
 	sz = zbx_mem_required_size(1, "trend cache", "TrendCacheSize");
-	zbx_mem_create(&trend_mem, trend_shm_key, ZBX_NO_MUTEX, CONFIG_TRENDS_CACHE_SIZE,
-			"trend cache", "TrendCacheSize", 0);
+	zbx_mem_create(&trend_mem, CONFIG_TRENDS_CACHE_SIZE, "trend cache", "TrendCacheSize", 0);
 	CONFIG_TRENDS_CACHE_SIZE -= sz;
 
 	cache->trends_num = 0;
@@ -3283,17 +3275,9 @@
 void	init_database_cache()
 {
 	const char	*__function_name = "init_database_cache";
-	key_t		hc_shm_key, hc_index_shm_key;
 
 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
 
-	if (-1 == (hc_shm_key = zbx_ftok(CONFIG_FILE, ZBX_IPC_HISTORY_ID)) ||
-			-1 == (hc_index_shm_key = zbx_ftok(CONFIG_FILE, ZBX_IPC_HISTORY_INDEX_ID)))
-	{
-		zabbix_log(LOG_LEVEL_CRIT, "cannot create IPC keys for history cache");
-		exit(EXIT_FAILURE);
-	}
-
 	if (FAIL == zbx_mutex_create_force(&cache_lock, ZBX_MUTEX_CACHE))
 	{
 		zbx_error("cannot create mutex for history cache");
@@ -3307,12 +3291,10 @@
 	}
 
 	/* history cache */
-	zbx_mem_create(&hc_mem, hc_shm_key, ZBX_NO_MUTEX, CONFIG_HISTORY_CACHE_SIZE, "history cache",
-			"HistoryCacheSize", 1);
+	zbx_mem_create(&hc_mem, CONFIG_HISTORY_CACHE_SIZE, "history cache", "HistoryCacheSize", 1);
 
 	/* history index cache */
-	zbx_mem_create(&hc_index_mem, hc_index_shm_key, ZBX_NO_MUTEX, CONFIG_HISTORY_INDEX_CACHE_SIZE,
-			"history index cache", "HistoryIndexCacheSize", 0);
+	zbx_mem_create(&hc_index_mem, CONFIG_HISTORY_INDEX_CACHE_SIZE, "history index cache", "HistoryIndexCacheSize", 0);
 
 	cache = (ZBX_DC_CACHE *)__hc_index_mem_malloc_func(NULL, sizeof(ZBX_DC_CACHE));
 	memset(cache, 0, sizeof(ZBX_DC_CACHE));
Index: src/libs/zbxdbcache/dbconfig.c
===================================================================
--- src/libs/zbxdbcache/dbconfig.c	(revision 65488)
+++ src/libs/zbxdbcache/dbconfig.c	(revision 65489)
@@ -4417,7 +4417,6 @@
 	const char	*__function_name = "init_configuration_cache";
 
 	int		i;
-	key_t		shm_key;
 	size_t		config_size;
 	size_t		strpool_size;
 
@@ -4426,12 +4425,6 @@
 	strpool_size = (size_t)(CONFIG_CONF_CACHE_SIZE * 0.15);
 	config_size = CONFIG_CONF_CACHE_SIZE - strpool_size;
 
-	if (-1 == (shm_key = zbx_ftok(CONFIG_FILE, ZBX_IPC_CONFIG_ID)))
-	{
-		zbx_error("Can't create IPC key for configuration cache");
-		exit(EXIT_FAILURE);
-	}
-
 	if (FAIL == zbx_mutex_create_force(&config_lock, ZBX_MUTEX_CONFIG))
 	{
 		zbx_error("Unable to create mutex for configuration cache");
@@ -4438,7 +4431,7 @@
 		exit(EXIT_FAILURE);
 	}
 
-	zbx_mem_create(&config_mem, shm_key, ZBX_NO_MUTEX, config_size, "configuration cache", "CacheSize", 0);
+	zbx_mem_create(&config_mem, config_size, "configuration cache", "CacheSize", 0);
 
 	config = __config_mem_malloc_func(NULL, sizeof(ZBX_DC_CONFIG) +
 			CONFIG_TIMER_FORKS * sizeof(zbx_vector_ptr_t));
