-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Linux specific code for Pts to
pts_linux.go
(#221)
This allows us to build and test our code on macOS for local development. With this change, `make test` works on macOs.
- Loading branch information
Showing
4 changed files
with
64 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package system | ||
|
||
// OsPts is the mock Darwin implementation of the Pts interface. | ||
type OsPts struct{} | ||
|
||
func NewOsPts() Pts { | ||
panic("not supported on darwin") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package system | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// OsPts is the Linux implementation of the Pts interface. | ||
type OsPts struct{} | ||
|
||
func NewOsPts() Pts { | ||
return &OsPts{} | ||
} | ||
|
||
// Create a new pseudo terminal (pts). Returns a ReaderCloser for the master device and a pts number | ||
func (p *OsPts) NewPts() (io.ReadCloser, int, error) { | ||
ptmxPath := os.Getenv(PtmxPathEnv) | ||
if ptmxPath == "" { | ||
ptmxPath = DefaultPtmxPath | ||
} | ||
ptsMaster, err := os.Open(ptmxPath) | ||
if err != nil { | ||
return nil, 0, fmt.Errorf("Failed to open tty: %w", err) | ||
} | ||
success := false | ||
defer func() { | ||
if !success { | ||
ptsMaster.Close() | ||
} | ||
}() | ||
// grantpt ioctl to allow mount-s3 process access to the pts | ||
var n uintptr // dummy int for ioctl | ||
if err = unix.IoctlSetInt(int(ptsMaster.Fd()), unix.TIOCGPTN, int(uintptr(unsafe.Pointer(&n)))); err != nil { | ||
return nil, 0, fmt.Errorf("Failed grantpt: %w", err) | ||
} | ||
n = 0 | ||
// unlockpt ioctl | ||
err = unix.IoctlSetInt(int(ptsMaster.Fd()), unix.TIOCSPTLCK, int(uintptr(unsafe.Pointer(&n)))) | ||
if err != nil { | ||
return nil, 0, fmt.Errorf("Failed unlockpt: %w", err) | ||
} | ||
// ptsname ioctl to get pts path for systemd | ||
ptsN, err := unix.IoctlGetInt(int(ptsMaster.Fd()), unix.TIOCGPTN) | ||
if err != nil { | ||
return nil, 0, fmt.Errorf("Failed to get ptsname: %w", err) | ||
} | ||
|
||
success = true | ||
return ptsMaster, ptsN, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build linux | ||
|
||
package system_test | ||
|
||
import ( | ||
|