From 129b15eea7a790bff0a83d1668b8d666fe0e6f35 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Fri, 20 May 2016 07:34:41 +0200 Subject: ipcpd: shim-udp: read fd on client side There could be a theoretical race condition that the server sends a message before the FD is set on the client side, which is resolved. --- src/ipcpd/shim-udp/main.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/ipcpd/shim-udp') diff --git a/src/ipcpd/shim-udp/main.c b/src/ipcpd/shim-udp/main.c index a75f3ce3..3ef783c4 100644 --- a/src/ipcpd/shim-udp/main.c +++ b/src/ipcpd/shim-udp/main.c @@ -1124,6 +1124,7 @@ static int ipcp_udp_flow_alloc(pid_t n_pid, int fd; struct hostent * h; uint32_t ip_addr = 0; + bool fd_wait = true; #ifdef CONFIG_OUROBOROS_ENABLE_DNS uint32_t dns_addr = 0; #endif @@ -1220,8 +1221,20 @@ static int ipcp_udp_flow_alloc(pid_t n_pid, rw_lock_rdlock(&_ipcp->state_lock); rw_lock_wrlock(&_ap_instance->flows_lock); + pthread_mutex_lock(&_ap_instance->fd_set_lock); + + _ap_instance->fd_set_sync = true; FD_SET(fd, &shim_data(_ipcp)->flow_fd_s); + pthread_mutex_unlock(&_ap_instance->fd_set_lock); + + while (fd_wait) { + sched_yield(); + pthread_mutex_lock(&_ap_instance->fd_set_lock); + fd_wait = _ap_instance->fd_set_sync; + pthread_mutex_unlock(&_ap_instance->fd_set_lock); + } + _ap_instance->flows[fd].port_id = port_id; _ap_instance->flows[fd].state = FLOW_PENDING; _ap_instance->flows[fd].rb = rb; -- cgit v1.2.3