about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-28 16:17:47 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-28 16:17:47 +0100
commit444793a6ac7b1359a88a47b39c4c2e90c1fe5236 (patch)
tree8099401278486dfac07c8b1b0c3619def863b1f0
parent6716b3a0013949dc8a0d51e39ae4724755305762 (diff)
downloadchawan-444793a6ac7b1359a88a47b39c4c2e90c1fe5236.tar.gz
fflush() before forks
seems like a good idea, especially because CGI uses stdout as the IPC
mechanism
-rw-r--r--src/loader/cgi.nim2
-rw-r--r--src/local/pager.nim17
-rw-r--r--src/server/forkserver.nim6
3 files changed, 19 insertions, 6 deletions
diff --git a/src/loader/cgi.nim b/src/loader/cgi.nim
index 3e3ec4fc..10809767 100644
--- a/src/loader/cgi.nim
+++ b/src/loader/cgi.nim
@@ -188,6 +188,8 @@ proc loadCGI*(handle: LoaderHandle; request: Request; cgiDir: seq[string];
     contentLen = request.body.get.len
   elif request.multipart.isSome:
     contentLen = request.multipart.get.calcLength()
+  stdout.flushFile()
+  stderr.flushFile()
   let pid = fork()
   if pid == -1:
     handle.sendResult(ERROR_FAIL_SETUP_CGI)
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 29e817f9..6e9ac0dd 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -1256,6 +1256,11 @@ type CheckMailcapResult = object
   ishtml: bool
   found: bool
 
+template myFork(): cint =
+  stdout.flushFile()
+  stderr.flushFile()
+  fork()
+
 # Pipe output of an x-ansioutput mailcap command to the text/x-ansi handler.
 proc ansiDecode(pager: Pager; url: URL; ishtml: var bool; fdin: cint): cint =
   let entry = pager.config.external.mailcap.getMailcapEntry("text/x-ansi", "",
@@ -1269,7 +1274,7 @@ proc ansiDecode(pager: Pager; url: URL; ishtml: var bool; fdin: cint): cint =
   if pipe(pipefdOutAnsi) == -1:
     pager.alert("Error: failed to open pipe")
     return
-  case fork()
+  case myFork()
   of -1:
     pager.alert("Error: failed to fork ANSI decoder process")
     discard close(pipefdOutAnsi[0])
@@ -1293,7 +1298,7 @@ proc ansiDecode(pager: Pager; url: URL; ishtml: var bool; fdin: cint): cint =
 # needsterminal is ignored.
 proc runMailcapReadPipe(pager: Pager; stream: SocketStream; cmd: string;
     pipefdOut: array[2, cint]): int =
-  let pid = fork()
+  let pid = myFork()
   if pid == -1:
     pager.alert("Error: failed to fork mailcap read process")
     return -1
@@ -1315,7 +1320,7 @@ proc runMailcapWritePipe(pager: Pager; stream: SocketStream;
     needsterminal: bool; cmd: string) =
   if needsterminal:
     pager.term.quit()
-  let pid = fork()
+  let pid = myFork()
   if pid == -1:
     pager.alert("Error: failed to fork mailcap write process")
   elif pid == 0:
@@ -1356,7 +1361,7 @@ proc writeToFile(istream: SocketStream; outpath: string): bool =
 # needsterminal is ignored.
 proc runMailcapReadFile(pager: Pager; stream: SocketStream;
     cmd, outpath: string; pipefdOut: array[2, cint]): int =
-  let pid = fork()
+  let pid = myFork()
   if pid == 0:
     # child process
     discard close(pipefdOut[0])
@@ -1388,7 +1393,7 @@ proc runMailcapWriteFile(pager: Pager; stream: SocketStream;
       pager.term.restart()
   else:
     # don't block
-    let pid = fork()
+    let pid = myFork()
     if pid == 0:
       # child process
       closeStdin()
@@ -1411,7 +1416,7 @@ proc filterBuffer(pager: Pager; stream: SocketStream; cmd: string;
   if pipe(pipefd_out) == -1:
     pager.alert("Error: failed to open pipe")
     return CheckMailcapResult(connect: false, fdout: -1)
-  let pid = fork()
+  let pid = myFork()
   if pid == -1:
     pager.alert("Error: failed to fork buffer filter process")
     return CheckMailcapResult(connect: false, fdout: -1)
diff --git a/src/server/forkserver.nim b/src/server/forkserver.nim
index c1bbdedb..d972958a 100644
--- a/src/server/forkserver.nim
+++ b/src/server/forkserver.nim
@@ -85,6 +85,8 @@ proc forkLoader(ctx: var ForkServerContext, config: LoaderConfig): int =
   var pipefd: array[2, cint]
   if pipe(pipefd) == -1:
     raise newException(Defect, "Failed to open pipe.")
+  stdout.flushFile()
+  stderr.flushFile()
   let pid = fork()
   if pid == 0:
     # child process
@@ -132,6 +134,8 @@ proc forkBuffer(ctx: var ForkServerContext; r: var BufferedReader): int =
   var pipefd: array[2, cint]
   if pipe(pipefd) == -1:
     raise newException(Defect, "Failed to open pipe.")
+  stdout.flushFile()
+  stderr.flushFile()
   let pid = fork()
   if pid == -1:
     raise newException(Defect, "Failed to fork process.")
@@ -248,6 +252,8 @@ proc newForkServer*(): ForkServer =
     raise newException(Defect, "Failed to open output pipe.")
   if pipe(pipefd_err) == -1:
     raise newException(Defect, "Failed to open error pipe.")
+  stdout.flushFile()
+  stderr.flushFile()
   let pid = fork()
   if pid == -1:
     raise newException(Defect, "Failed to fork the fork process.")