aboutsummaryrefslogtreecommitdiffstats
path: root/src/server
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2025-01-09 20:46:50 +0100
committerbptato <nincsnevem662@gmail.com>2025-01-09 20:48:20 +0100
commit8c769fc302a04737f1060fdcc0fab16a733d94c9 (patch)
tree174bd70c150b298a65ce7cecb48406caf4adf155 /src/server
parent73f8c04970cbd0bc5a5923272ef21bf39d5c9293 (diff)
downloadchawan-8c769fc302a04737f1060fdcc0fab16a733d94c9.tar.gz
buffer: remove server socket
Now we just pass through a socket created in pager. This removes the need for a socket directory, and strengthens the buffer sandbox slightly. I've kept the ServerSocket code, because I want to add some form of RPC and communication between separate instances in the future. However, I don't expect this to be handled outside the main process, so I've removed the Capsicum-specific connectat/bindat code.
Diffstat (limited to 'src/server')
-rw-r--r--src/server/buffer.nim28
-rw-r--r--src/server/forkserver.nim123
-rw-r--r--src/server/loader.nim1
-rw-r--r--src/server/loaderiface.nim21
4 files changed, 58 insertions, 115 deletions
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 3e76d2ee..8166978c 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -105,7 +105,6 @@ type
rfd: int # file descriptor of command pipe
rootBox: BlockBox
savetask: bool
- ssock: ServerSocket
state: BufferState
tasks: array[BufferCommand, int] #TODO this should have arguments
uastyle: CSSStylesheet
@@ -181,18 +180,16 @@ proc resolve(iface: BufferInterface; id: int) =
it.p.resolve()
iface.map.del(i)
-proc newBufferInterface*(stream: SocketStream; registerFun: proc(fd: int)):
- BufferInterface =
+proc newBufferInterface*(stream: BufStream): BufferInterface =
return BufferInterface(
packetid: 1, # ids below 1 are invalid
- stream: newBufStream(stream, registerFun)
+ stream: stream
)
# After cloning a buffer, we need a new interface to the new buffer process.
# Here we create a new interface for that clone.
-proc cloneInterface*(stream: SocketStream; registerFun: proc(fd: int)):
- BufferInterface =
- let iface = newBufferInterface(stream, registerFun)
+proc cloneInterface*(stream: BufStream): BufferInterface =
+ let iface = newBufferInterface(stream)
#TODO buffered data should probably be copied here
# We have just fork'ed the buffer process inside an interface function,
# from which the new buffer is going to return as well. So we must also
@@ -960,7 +957,6 @@ proc rewind(buffer: Buffer; offset: int; unregister = true): bool =
buffer.bytesRead = offset
return true
-var gssock* {.global.}: ServerSocket
var gpstream* {.global.}: SocketStream
# Create an exact clone of the current buffer.
@@ -1019,17 +1015,12 @@ proc clone*(buffer: Buffer; newurl: URL): int {.proxy.} =
# the cache. (This also lets us skip suspend/resume in this case.)
# We ignore errors; not much we can do with them here :/
discard buffer.rewind(buffer.bytesRead, unregister = false)
- buffer.pstream.sclose()
- buffer.ssock.close()
- let ssock = newServerSocket(sockFd, buffer.loader.sockDir,
- buffer.loader.sockDirFd, myPid)
- buffer.ssock = ssock
- gssock = ssock
ps.write(char(0))
buffer.url = newurl
for it in buffer.tasks.mitems:
it = 0
- buffer.pstream = ssock.acceptSocketStream()
+ buffer.pstream.sclose()
+ buffer.pstream = newSocketStream(sockFd)
gpstream = buffer.pstream
buffer.loader.clientPid = myPid
# get key for new buffer
@@ -1963,15 +1954,11 @@ proc cleanup(buffer: Buffer) =
if gpstream != nil:
gpstream.sclose()
gpstream = nil
- if gssock != nil:
- gssock.close()
- gssock = nil
buffer.window.urandom.sclose()
proc launchBuffer*(config: BufferConfig; url: URL; attrs: WindowAttributes;
ishtml: bool; charsetStack: seq[Charset]; loader: FileLoader;
- ssock: ServerSocket; pstream: SocketStream; urandom: PosixStream;
- cacheId: int) =
+ pstream: SocketStream; urandom: PosixStream; cacheId: int) =
let factory = newCAtomFactory()
let confidence = if config.charsetOverride == CHARSET_UNKNOWN:
ccTentative
@@ -1986,7 +1973,6 @@ proc launchBuffer*(config: BufferConfig; url: URL; attrs: WindowAttributes;
needsBOMSniff: config.charsetOverride == CHARSET_UNKNOWN,
pstream: pstream,
rfd: pstream.fd,
- ssock: ssock,
url: url,
charsetStack: charsetStack,
cacheId: -1,
diff --git a/src/server/forkserver.nim b/src/server/forkserver.nim
index baa6ee7b..fafb185e 100644
--- a/src/server/forkserver.nim
+++ b/src/server/forkserver.nim
@@ -23,21 +23,17 @@ type
fcLoadConfig, fcForkBuffer, fcRemoveChild
ForkServer* = ref object
- istream: PosixStream
- ostream: PosixStream
+ stream: SocketStream
estream*: PosixStream
ForkServerContext = object
- istream: PosixStream
- ostream: PosixStream
+ stream: SocketStream
children: seq[int]
loaderPid: int
- sockDirFd: cint
- sockDir: string
loaderStream: SocketStream
proc loadConfig*(forkserver: ForkServer; config: Config): int =
- forkserver.ostream.withPacketWriter w:
+ forkserver.stream.withPacketWriter w:
w.swrite(fcLoadConfig)
w.swrite(config.display.double_width_ambiguous)
w.swrite(LoaderConfig(
@@ -45,34 +41,39 @@ proc loadConfig*(forkserver: ForkServer; config: Config): int =
w3mCGICompat: config.external.w3m_cgi_compat,
cgiDir: seq[string](config.external.cgi_dir),
tmpdir: config.external.tmpdir,
- sockdir: config.external.sockdir,
configdir: config.dir,
bookmark: config.external.bookmark
))
- var r = forkserver.istream.initPacketReader()
var process: int
- r.sread(process)
+ forkserver.stream.withPacketReader r:
+ r.sread(process)
return process
proc removeChild*(forkserver: ForkServer; pid: int) =
- forkserver.ostream.withPacketWriter w:
+ forkserver.stream.withPacketWriter w:
w.swrite(fcRemoveChild)
w.swrite(pid)
proc forkBuffer*(forkserver: ForkServer; config: BufferConfig; url: URL;
attrs: WindowAttributes; ishtml: bool; charsetStack: seq[Charset]):
- int =
- forkserver.ostream.withPacketWriter w:
+ tuple[pid: int; fd: cint] =
+ var sv {.noinit.}: array[2, cint]
+ #TODO fail gracefully
+ if socketpair(AF_UNIX, SOCK_STREAM, IPPROTO_IP, sv) != 0:
+ raise newException(Defect, "Failed to open socket pair")
+ forkserver.stream.withPacketWriter w:
w.swrite(fcForkBuffer)
w.swrite(config)
w.swrite(url)
w.swrite(attrs)
w.swrite(ishtml)
w.swrite(charsetStack)
- var r = forkserver.istream.initPacketReader()
+ w.sendAux.add(sv[1])
+ discard close(sv[1])
var bufferPid: int
- r.sread(bufferPid)
- return bufferPid
+ forkserver.stream.withPacketReader r:
+ r.sread(bufferPid)
+ return (bufferPid, sv[0])
proc trapSIGINT() =
# trap SIGINT, so e.g. an external editor receiving an interrupt in the
@@ -118,9 +119,7 @@ proc forkBuffer(ctx: var ForkServerContext; r: var BufferedReader): int =
r.sread(attrs)
r.sread(ishtml)
r.sread(charsetStack)
- var pipefd {.noinit.}: array[2, cint]
- if pipe(pipefd) == -1:
- raise newException(Defect, "Failed to open pipe.")
+ let fd = r.recvAux.pop()
stdout.flushFile()
stderr.flushFile()
let pid = fork()
@@ -132,41 +131,29 @@ proc forkBuffer(ctx: var ForkServerContext; r: var BufferedReader): int =
for i in 0 ..< ctx.children.len: ctx.children[i] = 0
ctx.children.setLen(0)
let loaderPid = ctx.loaderPid
- let sockDir = ctx.sockDir
- let sockDirFd = ctx.sockDirFd
zeroMem(addr ctx, sizeof(ctx))
- discard close(pipefd[0]) # close read
setBufferProcessTitle(url)
let pid = getCurrentProcessId()
- let ssock = newServerSocket(sockDir, sockDirFd, pid)
- let ps = newPosixStream(pipefd[1])
- ps.write(char(0))
- ps.sclose()
let urandom = newPosixStream("/dev/urandom", O_RDONLY, 0)
- let pstream = ssock.acceptSocketStream()
+ let pstream = newSocketStream(fd)
var cacheId: int
var loaderStream: SocketStream
pstream.withPacketReader r:
r.sread(cacheId)
loaderStream = newSocketStream(r.recvAux.pop())
- let loader = newFileLoader(loaderPid, pid, sockDir, sockDirFd,
- loaderStream)
- gssock = ssock
+ let loader = newFileLoader(loaderPid, pid, loaderStream)
gpstream = pstream
onSignal SIGTERM:
discard sig
if gpstream != nil:
gpstream.sclose()
gpstream = nil
- if gssock != nil:
- gssock.close()
- gssock = nil
exitnow(1)
signal(SIGPIPE, SIG_DFL)
- enterBufferSandbox(sockDir)
+ enterBufferSandbox()
try:
- launchBuffer(config, url, attrs, ishtml, charsetStack, loader,
- ssock, pstream, urandom, cacheId)
+ launchBuffer(config, url, attrs, ishtml, charsetStack, loader, pstream,
+ urandom, cacheId)
except CatchableError:
let e = getCurrentException()
# taken from system/excpt.nim
@@ -175,27 +162,20 @@ proc forkBuffer(ctx: var ForkServerContext; r: var BufferedReader): int =
stderr.write(msg)
quit(1)
doAssert false
- discard close(pipefd[1]) # close write
- let ps = newPosixStream(pipefd[0])
- let c = ps.sreadChar()
- assert c == '\0'
- ps.sclose()
ctx.children.add(pid)
return pid
-proc runForkServer(ifd, ofd: cint; loaderStream: SocketStream) =
+proc runForkServer(controlStream, loaderStream: SocketStream) =
setProcessTitle("cha forkserver")
var ctx = ForkServerContext(
- istream: newPosixStream(ifd),
- ostream: newPosixStream(ofd),
- sockDirFd: -1,
+ stream: controlStream,
loaderStream: loaderStream
)
signal(SIGCHLD, SIG_IGN)
signal(SIGPIPE, SIG_IGN)
while true:
try:
- ctx.istream.withPacketReader r:
+ ctx.stream.withPacketReader r:
var cmd: ForkCommand
r.sread(cmd)
case cmd
@@ -204,10 +184,8 @@ proc runForkServer(ifd, ofd: cint; loaderStream: SocketStream) =
var config: LoaderConfig
r.sread(isCJKAmbiguous)
r.sread(config)
- ctx.sockDir = config.sockdir
- ctx.sockDirFd = openSockDir(ctx.sockDir)
let pid = ctx.forkLoader(config)
- ctx.ostream.withPacketWriter w:
+ ctx.stream.withPacketWriter w:
w.swrite(pid)
ctx.loaderPid = pid
ctx.children.add(pid)
@@ -219,54 +197,51 @@ proc runForkServer(ifd, ofd: cint; loaderStream: SocketStream) =
ctx.children.del(i)
of fcForkBuffer:
let r = ctx.forkBuffer(r)
- ctx.ostream.withPacketWriter w:
+ ctx.stream.withPacketWriter w:
w.swrite(r)
except EOFError, ErrorBrokenPipe:
# EOF
break
- ctx.istream.sclose()
- ctx.ostream.sclose()
+ ctx.stream.sclose()
# Clean up when the main process crashed.
#TODO this seems like a bad idea; children may be out of sync here...
for child in ctx.children:
discard kill(cint(child), cint(SIGTERM))
quit(0)
-proc newForkServer*(sy: array[2, cint]): ForkServer =
- var pipeFdIn {.noinit.}: array[2, cint] # stdin in forkserver
- var pipeFdOut {.noinit.}: array[2, cint] # stdout in forkserver
+proc newForkServer*(loaderSockVec: array[2, cint]): ForkServer =
+ var sockVec {.noinit.}: array[2, cint] # stdin in forkserver
var pipeFdErr {.noinit.}: array[2, cint] # stderr in forkserver
- if pipe(pipeFdIn) == -1:
- raise newException(Defect, "Failed to open input pipe.")
- if pipe(pipeFdOut) == -1:
- raise newException(Defect, "Failed to open output pipe.")
+ if socketpair(AF_UNIX, SOCK_STREAM, IPPROTO_IP, sockVec) != 0:
+ stderr.writeLine("Failed to open fork server i/o socket")
+ quit(1)
if pipe(pipeFdErr) == -1:
- raise newException(Defect, "Failed to open error pipe.")
- stdout.flushFile()
- stderr.flushFile()
+ stderr.writeLine("Failed to open fork server error pipe")
+ quit(1)
let pid = fork()
if pid == -1:
- raise newException(Defect, "Failed to fork the fork process.")
+ stderr.writeLine("Failed to fork fork the server process")
+ quit(1)
elif pid == 0:
# child process
trapSIGINT()
closeStdin()
closeStdout()
newPosixStream(pipeFdErr[1]).moveFd(STDERR_FILENO)
- discard close(pipeFdIn[1]) # close write
- discard close(pipeFdOut[0]) # close read
discard close(pipeFdErr[0]) # close read
- discard close(sy[0])
- runForkServer(pipeFdIn[0], pipeFdOut[1], newSocketStream(sy[1]))
+ discard close(sockVec[0])
+ discard close(loaderSockVec[0])
+ let controlStream = newSocketStream(sockVec[1])
+ let loaderStream = newSocketStream(loaderSockVec[1])
+ runForkServer(controlStream, loaderStream)
doAssert false
else:
- discard close(pipeFdIn[0]) # close read
- discard close(pipeFdOut[1]) # close write
discard close(pipeFdErr[1]) # close write
- let ostream = newPosixStream(pipeFdIn[1])
- let istream = newPosixStream(pipeFdOut[0])
+ discard close(sockVec[1])
+ discard close(loaderSockVec[1])
+ let stream = newSocketStream(sockVec[0])
+ stream.setCloseOnExec()
let estream = newPosixStream(pipeFdErr[0])
+ estream.setCloseOnExec()
estream.setBlocking(false)
- for it in [ostream, istream, estream]:
- it.setCloseOnExec()
- return ForkServer(ostream: ostream, istream: istream, estream: estream)
+ return ForkServer(stream: stream, estream: estream)
diff --git a/src/server/loader.nim b/src/server/loader.nim
index 16dbc5a3..76952eab 100644
--- a/src/server/loader.nim
+++ b/src/server/loader.nim
@@ -141,7 +141,6 @@ type
uriMethodMap*: URIMethodMap
w3mCGICompat*: bool
tmpdir*: string
- sockdir*: string
configdir*: string
bookmark*: string
diff --git a/src/server/loaderiface.nim b/src/server/loaderiface.nim
index f21fbd7f..87711d8a 100644
--- a/src/server/loaderiface.nim
+++ b/src/server/loaderiface.nim
@@ -29,10 +29,6 @@ type
unregistered*: seq[int]
registerFun*: proc(fd: int)
unregisterFun*: proc(fd: int)
- # directory where we store UNIX domain sockets
- sockDir*: string
- # (FreeBSD only) fd for the socket directory so we can connectat() on it
- sockDirFd*: cint
# A mechanism to queue up new fds being added to the poll data
# inside the events iterator.
registerBlocked: bool
@@ -465,23 +461,10 @@ proc removeClient*(loader: FileLoader; pid: int) =
w.swrite(lcRemoveClient)
w.swrite(pid)
-when defined(freebsd):
- let O_DIRECTORY {.importc, header: "<fcntl.h>", noinit.}: cint
-
-# On Capsicum-capable systems, this opens path as a directory.
-# On other systems, it returns -1.
-proc openSockDir*(path: string): cint =
- when defined(freebsd):
- return newPosixStream(path, O_DIRECTORY, 0).fd
- else:
- return -1
-
-proc newFileLoader*(loaderPid, clientPid: int; sockDir: string;
- sockDirFd: cint; controlStream: SocketStream): FileLoader =
+proc newFileLoader*(loaderPid, clientPid: int; controlStream: SocketStream):
+ FileLoader =
return FileLoader(
loaderPid: loaderPid,
- sockDir: sockDir,
- sockDirFd: sockDirFd,
clientPid: clientPid,
controlStream: controlStream
)