diff options
author | bptato <nincsnevem662@gmail.com> | 2023-09-23 11:50:36 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-09-23 13:16:26 +0200 |
commit | d7c2c30c924c051c36dd30290f4853e960325886 (patch) | |
tree | c7f71efdc56fac11918a229f1a6028ba694acd10 /src/local/container.nim | |
parent | 860a1e77e40469753dbef080ee6fcc24f82dc847 (diff) | |
download | chawan-d7c2c30c924c051c36dd30290f4853e960325886.tar.gz |
fix devnull dup in place of stdin
The previous solution was ok, but it could leak an fd...
Diffstat (limited to 'src/local/container.nim')
-rw-r--r-- | src/local/container.nim | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/src/local/container.nim b/src/local/container.nim index 2f4d0044..8b961e03 100644 --- a/src/local/container.nim +++ b/src/local/container.nim @@ -983,13 +983,16 @@ proc setStream*(container: Container, stream: Stream) = container.iface = newBufferInterface(stream) if container.source.t == LOAD_PIPE: container.iface.passFd(container.source.fd).then(proc() = - discard close(container.source.fd) if container.source.fd == 0: - # We have just closed stdin. + # We are closing stdin. # Leaving the stdin fileno open to grab is a bad idea. - # (Yes, I've just got bitten by this: dup2(0, 0). Ouch.) let devnull = open("/dev/null", O_RDONLY) - discard dup2(devnull, 0) + doAssert devnull != -1 + if devnull != 0: + discard dup2(devnull, 0) + discard close(devnull) + else: + discard close(container.source.fd) ) stream.flush() container.load() |