Skip to content

Commit e301e16

Browse files
committed
Revert "syscall: use Ctty before fd shuffle"
This reverts commit 103b5b6. Reason for revert: Breaks valid existing programs. Updates golang#29458 Change-Id: I7ace4ae404cf2a8b0e15e646663c50115f74b758 Reviewed-on: https://go-review.googlesource.com/c/go/+/183939 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Greg Thelen <gthelen@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent 998a989 commit e301e16

File tree

5 files changed

+69
-80
lines changed

5 files changed

+69
-80
lines changed

src/os/signal/signal_cgo_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,6 @@ func TestTerminalSignal(t *testing.T) {
101101
Ctty: int(slave.Fd()),
102102
}
103103

104-
// Test ctty management by sending enough child fd to overlap the
105-
// parent's fd intended for child's ctty.
106-
for 2+len(cmd.ExtraFiles) < cmd.SysProcAttr.Ctty {
107-
dummy, err := os.Open(os.DevNull)
108-
if err != nil {
109-
t.Fatal(err)
110-
}
111-
defer dummy.Close()
112-
cmd.ExtraFiles = append(cmd.ExtraFiles, dummy)
113-
}
114-
115104
if err := cmd.Start(); err != nil {
116105
t.Fatal(err)
117106
}

src/syscall/exec_bsd.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,6 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
162162
}
163163
}
164164

165-
// Detach fd 0 from tty
166-
if sys.Noctty {
167-
_, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCNOTTY), 0)
168-
if err1 != 0 {
169-
goto childerror
170-
}
171-
}
172-
173-
// Set the controlling TTY to Ctty
174-
if sys.Setctty {
175-
_, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
176-
if err1 != 0 {
177-
goto childerror
178-
}
179-
}
180-
181165
// Pass 1: look for fd[i] < i and move those up above len(fd)
182166
// so that pass 2 won't stomp on an fd it needs later.
183167
if pipe < nextfd {
@@ -235,6 +219,22 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
235219
RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
236220
}
237221

222+
// Detach fd 0 from tty
223+
if sys.Noctty {
224+
_, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCNOTTY), 0)
225+
if err1 != 0 {
226+
goto childerror
227+
}
228+
}
229+
230+
// Set the controlling TTY to Ctty
231+
if sys.Setctty {
232+
_, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
233+
if err1 != 0 {
234+
goto childerror
235+
}
236+
}
237+
238238
// Time to exec.
239239
_, _, err1 = RawSyscall(SYS_EXECVE,
240240
uintptr(unsafe.Pointer(argv0)),

src/syscall/exec_darwin.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,6 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
160160
}
161161
}
162162

163-
// Detach fd 0 from tty
164-
if sys.Noctty {
165-
_, _, err1 = rawSyscall(funcPC(libc_ioctl_trampoline), 0, uintptr(TIOCNOTTY), 0)
166-
if err1 != 0 {
167-
goto childerror
168-
}
169-
}
170-
171-
// Set the controlling TTY to Ctty
172-
if sys.Setctty {
173-
_, _, err1 = rawSyscall(funcPC(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
174-
if err1 != 0 {
175-
goto childerror
176-
}
177-
}
178-
179163
// Pass 1: look for fd[i] < i and move those up above len(fd)
180164
// so that pass 2 won't stomp on an fd it needs later.
181165
if pipe < nextfd {
@@ -233,6 +217,22 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
233217
rawSyscall(funcPC(libc_close_trampoline), uintptr(i), 0, 0)
234218
}
235219

220+
// Detach fd 0 from tty
221+
if sys.Noctty {
222+
_, _, err1 = rawSyscall(funcPC(libc_ioctl_trampoline), 0, uintptr(TIOCNOTTY), 0)
223+
if err1 != 0 {
224+
goto childerror
225+
}
226+
}
227+
228+
// Set the controlling TTY to Ctty
229+
if sys.Setctty {
230+
_, _, err1 = rawSyscall(funcPC(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
231+
if err1 != 0 {
232+
goto childerror
233+
}
234+
}
235+
236236
// Time to exec.
237237
_, _, err1 = rawSyscall(funcPC(libc_execve_trampoline),
238238
uintptr(unsafe.Pointer(argv0)),

src/syscall/exec_libc.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,6 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
180180
}
181181
}
182182

183-
// Detach fd 0 from tty
184-
if sys.Noctty {
185-
err1 = ioctl(0, uintptr(TIOCNOTTY), 0)
186-
if err1 != 0 {
187-
goto childerror
188-
}
189-
}
190-
191-
// Set the controlling TTY to Ctty
192-
if sys.Setctty {
193-
// On AIX, TIOCSCTTY is undefined
194-
if TIOCSCTTY == 0 {
195-
err1 = ENOSYS
196-
goto childerror
197-
}
198-
err1 = ioctl(uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
199-
if err1 != 0 {
200-
goto childerror
201-
}
202-
}
203-
204183
// Pass 1: look for fd[i] < i and move those up above len(fd)
205184
// so that pass 2 won't stomp on an fd it needs later.
206185
if pipe < nextfd {
@@ -261,6 +240,27 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
261240
close(uintptr(i))
262241
}
263242

243+
// Detach fd 0 from tty
244+
if sys.Noctty {
245+
err1 = ioctl(0, uintptr(TIOCNOTTY), 0)
246+
if err1 != 0 {
247+
goto childerror
248+
}
249+
}
250+
251+
// Set the controlling TTY to Ctty
252+
if sys.Setctty {
253+
// On AIX, TIOCSCTTY is undefined
254+
if TIOCSCTTY == 0 {
255+
err1 = ENOSYS
256+
goto childerror
257+
}
258+
err1 = ioctl(uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
259+
if err1 != 0 {
260+
goto childerror
261+
}
262+
}
263+
264264
// Time to exec.
265265
err1 = execve(
266266
uintptr(unsafe.Pointer(argv0)),

src/syscall/exec_linux.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -431,22 +431,6 @@ func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, att
431431
}
432432
}
433433

434-
// Detach fd 0 from tty
435-
if sys.Noctty {
436-
_, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCNOTTY), 0)
437-
if err1 != 0 {
438-
goto childerror
439-
}
440-
}
441-
442-
// Set the controlling TTY to Ctty
443-
if sys.Setctty {
444-
_, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSCTTY), 1)
445-
if err1 != 0 {
446-
goto childerror
447-
}
448-
}
449-
450434
// Pass 1: look for fd[i] < i and move those up above len(fd)
451435
// so that pass 2 won't stomp on an fd it needs later.
452436
if pipe < nextfd {
@@ -504,6 +488,22 @@ func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, att
504488
RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
505489
}
506490

491+
// Detach fd 0 from tty
492+
if sys.Noctty {
493+
_, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCNOTTY), 0)
494+
if err1 != 0 {
495+
goto childerror
496+
}
497+
}
498+
499+
// Set the controlling TTY to Ctty
500+
if sys.Setctty {
501+
_, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSCTTY), 1)
502+
if err1 != 0 {
503+
goto childerror
504+
}
505+
}
506+
507507
// Enable tracing if requested.
508508
// Do this right before exec so that we don't unnecessarily trace the runtime
509509
// setting up after the fork. See issue #21428.

0 commit comments

Comments
 (0)