summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/include/skalibs/webipc.h1
-rw-r--r--src/libstddjb/ipc_bind_reuse.c9
-rw-r--r--src/libstddjb/ipc_bind_reuse_lock.c28
3 files changed, 31 insertions, 7 deletions
diff --git a/src/include/skalibs/webipc.h b/src/include/skalibs/webipc.h
index eba6f6b..2a7e95e 100644
--- a/src/include/skalibs/webipc.h
+++ b/src/include/skalibs/webipc.h
@@ -40,6 +40,7 @@ extern int ipc_pair_internal (int *, unsigned int) ;
extern int ipc_bind (int, char const *) ;
extern int ipc_bind_reuse (int, char const *) ;
+extern int ipc_bind_reuse_lock (int, char const *, int *) ;
extern int ipc_listen (int, int) ;
#define ipc_accept(s, path, len, trunc) ipc_accept_internal(s, path, len, (trunc), 0)
diff --git a/src/libstddjb/ipc_bind_reuse.c b/src/libstddjb/ipc_bind_reuse.c
index 31db745..ce5c7a4 100644
--- a/src/libstddjb/ipc_bind_reuse.c
+++ b/src/libstddjb/ipc_bind_reuse.c
@@ -1,14 +1,9 @@
/* ISC license. */
-#include <skalibs/nonposix.h>
-#include <sys/socket.h>
-#include <unistd.h>
#include <skalibs/webipc.h>
int ipc_bind_reuse (int s, char const *p)
{
- unsigned int opt = 1 ;
- setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) ;
- unlink(p) ;
- return ipc_bind(s, p) ;
+ int fdlock ;
+ return ipc_bind_reuse_lock(s, p, &fdlock) ;
}
diff --git a/src/libstddjb/ipc_bind_reuse_lock.c b/src/libstddjb/ipc_bind_reuse_lock.c
new file mode 100644
index 0000000..aede744
--- /dev/null
+++ b/src/libstddjb/ipc_bind_reuse_lock.c
@@ -0,0 +1,28 @@
+/* ISC license. */
+
+#include <skalibs/nonposix.h>
+
+#include <string.h>
+#include <unistd.h>
+#include <sys/socket.h>
+
+#include <skalibs/djbunix.h>
+#include <skalibs/webipc.h>
+
+int ipc_bind_reuse_lock (int s, char const *p, int *fdlock)
+{
+ unsigned int opt = 1 ;
+ size_t len = strlen(p) ;
+ int fd ;
+ char lockname[len + 6] ;
+ memcpy(lockname, p, len) ;
+ memcpy(lockname + len, ".lock", 6) ;
+ fd = openc_create(lockname) ;
+ if (fd < 0) return -1 ;
+ if (lock_exnb(fd) < 0) return -1 ;
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) ;
+ unlink(p) ;
+ if (ipc_bind(s, p) < 0) return -1 ;
+ *fdlock = fd ;
+ return 0 ;
+}