about summary refs log tree commit diff stats
path: root/src/io/serversocket.nim
blob: abb3491a3ce16055e991c37e41b43a68e737bd3b (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import std/nativesockets
import std/net
import std/os

when defined(posix):
  import std/posix

type ServerSocket* = ref object
  sock*: Socket
  path*: string
  dfd: int

const SocketPathPrefix = "cha_sock_"
proc getSocketName*(pid: int): string =
  SocketPathPrefix & $pid

func getFd*(ssock: ServerSocket): SocketHandle =
  return ssock.sock.getFd()

proc getSocketPath*(socketDir: string; pid: int): string =
  socketDir / getSocketName(pid)

# The way stdlib does bindUnix is utterly broken at least on FreeBSD.
# It seems that just writing it in C is the easiest solution.
{.compile: "bind_unix.c".}
proc bind_unix_from_c(fd: cint; path: cstring; pathlen: cint): cint
  {.importc.}

when defined(freebsd):
  # capsicum stuff
  proc unlinkat(dfd: cint; path: cstring; flag: cint): cint
    {.importc, header: "<unistd.h>".}
  proc bindat_unix_from_c(dfd, sock: cint; path: cstring; pathlen: cint): cint
    {.importc.}

proc initServerSocket*(fd: SocketHandle; sockDir: string; pid, sockDirFd: int):
    ServerSocket =
  let sock = newSocket(fd, Domain.AF_UNIX, SockType.SOCK_STREAM,
    Protocol.IPPROTO_IP, buffered = false)
  let path = getSocketPath(sockDir, pid)
  return ServerSocket(sock: sock, path: path, dfd: sockDirFd)

proc initServerSocket*(sockDir: string; sockDirFd, pid: int; blocking = true):
    ServerSocket =
  let sock = newSocket(Domain.AF_UNIX, SockType.SOCK_STREAM,
    Protocol.IPPROTO_IP, buffered = false)
  if not blocking:
    sock.getFd().setBlocking(false)
  let path = getSocketPath(sockDir, pid)
  if sockDirFd == -1:
    discard tryRemoveFile(path)
    if bind_unix_from_c(cint(sock.getFd()), cstring(path), cint(path.len)) != 0:
      raiseOSError(osLastError())
  else:
    when defined(freebsd):
      let name = getSocketName(pid)
      discard unlinkat(cint(sockDirFd), cstring(name), 0)
      if bindat_unix_from_c(cint(sockDirFd), cint(sock.getFd()), cstring(name),
          cint(name.len)) != 0:
        raiseOSError(osLastError())
    else:
      # shouldn't have sockDirFd on other architectures
      doAssert false
  listen(sock)
  return ServerSocket(sock: sock, path: path, dfd: sockDirFd)

proc close*(ssock: ServerSocket; unlink = true) =
  close(ssock.sock)
  if unlink:
    when defined(freebsd):
      if ssock.dfd != -1:
        discard unlinkat(cint(ssock.dfd), cstring(ssock.path), 0)
        return
    discard tryRemoveFile(ssock.path)