forked from TrenchBoot/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty_linux.go
62 lines (53 loc) · 1.36 KB
/
tty_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2012-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"log"
"os"
"os/signal"
"unsafe"
"golang.org/x/sys/unix"
)
var (
ttypgrp int
ttyf *os.File
)
// tty does whatever needs to be done to set up a tty for GOOS.
func tty() {
var err error
sigs := make(chan os.Signal, 512)
signal.Notify(sigs, os.Interrupt)
signal.Ignore(unix.SIGTTOU)
go func() {
for i := range sigs {
fmt.Println(i)
}
}()
// N.B. We can continue to use this file, in the foreground function,
// but the runtime closes it on exec for us.
ttyf, err = os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil {
log.Printf("rush: Can't open a console; no job control in this session")
return
}
// Get the current pgrp, and the pgrp on the tty.
// get current pgrp
ttypgrp, err = unix.IoctlGetInt(int(ttyf.Fd()), unix.TIOCGPGRP)
if err != nil {
log.Printf("Can't get foreground: %v", err)
ttyf.Close()
ttyf = nil
ttypgrp = 0
}
}
func foreground() {
// Place process group in foreground.
if ttypgrp != 0 {
_, _, errno := unix.RawSyscall(unix.SYS_IOCTL, ttyf.Fd(), uintptr(unix.TIOCSPGRP), uintptr(unsafe.Pointer(&ttypgrp)))
if errno != 0 {
log.Printf("rush pid %v: Can't set foreground to %v: %v", os.Getpid(), ttypgrp, errno)
}
}
}