summaryrefslogtreecommitdiff
path: root/src/libstddjb/ipc_bind_reuse_lock.c
blob: aede744255d952939af1d9eab7cad67024383b74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 ;
}