make port map thread safe

This patch has been in Fedora and RHEL for a while. It adds a mutex
to protect the port map from concurrent thread accesses.

Original patch from Stefan Berger <stefanb@us.ibm.com>.
Modified to use configure.in to check for libpthread
This commit is contained in:
Thomas Graf 2011-08-11 15:17:56 +02:00
parent b5d081d1c9
commit 569bec5b5c
2 changed files with 15 additions and 2 deletions

View file

@ -46,6 +46,7 @@ AC_ARG_ENABLE([cli],
AM_CONDITIONAL([ENABLE_CLI], [test "$enable_cli" = "yes"])
AC_CHECK_LIB([m], [pow], [], AC_MSG_ERROR([libm is required]))
AC_CHECK_LIB([pthread], [pthread_mutex_lock], [], AC_MSG_ERROR([libpthread is required]))
AC_CONFIG_FILES([Makefile doc/Doxyfile doc/Makefile lib/Makefile
include/Makefile src/Makefile src/lib/Makefile man/Makefile

View file

@ -15,6 +15,8 @@
* @{
*/
#include <pthread.h>
#include <netlink-local.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
@ -43,12 +45,15 @@ static void __init init_default_cb(void)
}
static uint32_t used_ports_map[32];
static pthread_mutex_t port_map_mutex = PTHREAD_MUTEX_INITIALIZER;
static uint32_t generate_local_port(void)
{
int i, n;
uint32_t pid = getpid() & 0x3FFFFF;
pthread_mutex_lock(&port_map_mutex);
for (i = 0; i < 32; i++) {
if (used_ports_map[i] == 0xFFFFFFFF)
continue;
@ -62,11 +67,15 @@ static uint32_t generate_local_port(void)
/* PID_MAX_LIMIT is currently at 2^22, leaving 10 bit
* to, i.e. 1024 unique ports per application. */
return pid + (n << 22);
pthread_mutex_unlock(&port_map_mutex);
return pid + (n << 22);
}
}
pthread_mutex_unlock(&port_map_mutex);
/* Out of sockets in our own PID namespace, what to do? FIXME */
return UINT_MAX;
}
@ -79,7 +88,10 @@ static void release_local_port(uint32_t port)
return;
nr = port >> 22;
used_ports_map[nr / 32] &= ~(1 << nr % 32);
pthread_mutex_lock(&port_map_mutex);
used_ports_map[nr / 32] &= ~(1 << (nr % 32));
pthread_mutex_unlock(&port_map_mutex);
}
/**