From e637915823ab643216f44759a6cd495a05042d15 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 4 Dec 2015 15:39:37 -0500 Subject: [PATCH] add a lock around the socket creating/deleting. --- src/zm_stream.cpp | 22 ++++++++++++++++++++++ src/zm_stream.h | 3 +++ 2 files changed, 25 insertions(+) diff --git a/src/zm_stream.cpp b/src/zm_stream.cpp index 30a420bb1..4e9a4da10 100644 --- a/src/zm_stream.cpp +++ b/src/zm_stream.cpp @@ -286,6 +286,23 @@ void StreamBase::openComms() { if ( connkey > 0 ) { + + snprintf( sock_path_lock, sizeof(sock_path_lock), "%s/zms-%06d.lock", config.path_socks, connkey); + + lock_fd = open(sock_path_lock, O_CREAT|O_WRONLY, S_IRUSR | S_IWUSR); + if (lock_fd <= 0 || flock(lock_fd, LOCK_SH|LOCK_NB) != 0) + { + Error("Unable to lock sock lock file %s: %s", sock_path_lock, strerror(errno) ); + + close(lock_fd); + lock_fd = 0; + } + else + { + Debug( 1, "We have obtained a read lock on %s fd: %d", sock_path_lock, lock_fd); + } + + sd = socket( AF_UNIX, SOCK_DGRAM, 0 ); if ( sd < 0 ) { @@ -321,6 +338,11 @@ void StreamBase::closeComms() { unlink( loc_sock_path ); } + if (lock_fd > 0) + { + loc_sock_path[0] = '\0'; + close(lock_fd); //close it rather than unlock it incase it got deleted. + } } } diff --git a/src/zm_stream.h b/src/zm_stream.h index ef442ab8e..9936688a8 100644 --- a/src/zm_stream.h +++ b/src/zm_stream.h @@ -78,6 +78,8 @@ protected: struct sockaddr_un loc_addr; char rem_sock_path[PATH_MAX]; struct sockaddr_un rem_addr; + char sock_path_lock[PATH_MAX]; + int lock_fd; protected: bool paused; @@ -127,6 +129,7 @@ public: connkey = 0; sd = -1; + lock_fd = 0; memset( &loc_sock_path, 0, sizeof(loc_sock_path) ); memset( &loc_addr, 0, sizeof(loc_addr) ); memset( &rem_sock_path, 0, sizeof(rem_sock_path) );