Skip to content

Commit ed3e4af

Browse files
oridb0intro
authored andcommitted
syscall/plan9: remove spooky fd action at a distance
Change Plan 9 fork/exec to use the O_CLOEXEC file descriptor, instead of relying on spooky at a distance. Historically, Plan 9 has set the O_CLOEXEC flag on the underlying channels in the kernel, rather than the file descriptors -- if two fds pointed at a single channel, as with dup, changing the flags on one of them would be observable on the other. The per-Chan semantics are ok, if unexpected, when a chan is only handled within a single process, but this isn't always the case. Forked processes share Chans, but even more of a problem is the interaction between /srv and OCEXEC, which can lead to unexectedly closed file descriptors in completely unrelated proceses. For example: func exists() bool { // If some other thread execs here, // we don't want to leak the fd, so // open it O_CLOEXEC fd := Open("/srv/foo", O_CLOEXEC) if fd != -1 { Close(fd) return true } return false } would close the connection to any file descriptor (maybe even for the root fs) in ALL other processes that have it open if an exec were to happen(!), which is quite undesriable. As a result, 9front will be changing this behavior for the next release. Go is the only code observed so far that relies on this behavior on purpose, and It's easy to make the code work with both semantics: simply using the file descriptor that was opened with O_CEXEC instead of throwing it away. So we do that here. Fixes #43524 Change-Id: I4887f5c934a5e63e5e6c1bb59878a325abc928d3 GitHub-Last-Rev: 96bb21b GitHub-Pull-Request: #43533 Reviewed-on: https://go-review.googlesource.com/c/go/+/281833 Reviewed-by: David du Colombier <[email protected]> Reviewed-by: Richard Miller <[email protected]> Reviewed-by: Jacob Moody <[email protected]> Run-TryBot: David du Colombier <[email protected]> Trust: Ian Lance Taylor <[email protected]>
1 parent 724d072 commit ed3e4af

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/syscall/exec_plan9.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,15 @@ func cexecPipe(p []int) error {
320320
return e
321321
}
322322

323-
fd, e := Open("#d/"+itoa(p[1]), O_CLOEXEC)
323+
fd, e := Open("#d/"+itoa(p[1]), O_RDWR|O_CLOEXEC)
324324
if e != nil {
325325
Close(p[0])
326326
Close(p[1])
327327
return e
328328
}
329329

330-
Close(fd)
330+
Close(p[1])
331+
p[1] = fd
331332
return nil
332333
}
333334

0 commit comments

Comments
 (0)