Skip to content

Commit

Permalink
Merge pull request #46 from najohnsn/master
Browse files Browse the repository at this point in the history
Add support for z/OS
  • Loading branch information
estesp authored Apr 12, 2021
2 parents 2f1e3d2 + 7097449 commit 2298a9c
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 10 deletions.
19 changes: 10 additions & 9 deletions console_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux solaris freebsd
// +build linux solaris zos freebsd

/*
Copyright The containerd Authors.
Expand All @@ -20,7 +20,6 @@ package console

import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -69,11 +68,6 @@ func TestConsolePty(t *testing.T) {

iteration := 10

cmd := exec.Command("sh", "-c", fmt.Sprintf("for x in `seq 1 %d`; do echo -n test; done", iteration))
cmd.Stdin = slave
cmd.Stdout = slave
cmd.Stderr = slave

var (
b bytes.Buffer
wg sync.WaitGroup
Expand All @@ -84,8 +78,15 @@ func TestConsolePty(t *testing.T) {
wg.Done()
}()

if err := cmd.Run(); err != nil {
t.Fatal(err)
for i := 0; i < iteration; i++ {
cmd := exec.Command("sh", "-c", "printf test")
cmd.Stdin = slave
cmd.Stdout = slave
cmd.Stderr = slave

if err := cmd.Run(); err != nil {
t.Fatal(err)
}
}
slave.Close()
wg.Wait()
Expand Down
163 changes: 163 additions & 0 deletions console_zos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// +build zos

/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package console

import (
"fmt"
"os"

"golang.org/x/sys/unix"
)

// NewPty creates a new pty pair
// The master is returned as the first console and a string
// with the path to the pty slave is returned as the second
func NewPty() (Console, string, error) {
var f File
var err error
var slave string
for i := 0;; i++ {
ptyp := fmt.Sprintf("/dev/ptyp%04d", i)
f, err = os.OpenFile(ptyp, os.O_RDWR, 0600)
if err == nil {
slave = fmt.Sprintf("/dev/ttyp%04d", i)
break
}
if os.IsNotExist(err) {
return nil, "", err
}
// else probably Resource Busy
}
m, err := newMaster(f)
if err != nil {
return nil, "", err
}
return m, slave, nil
}

type master struct {
f File
original *unix.Termios
}

func (m *master) Read(b []byte) (int, error) {
return m.f.Read(b)
}

func (m *master) Write(b []byte) (int, error) {
return m.f.Write(b)
}

func (m *master) Close() error {
return m.f.Close()
}

func (m *master) Resize(ws WinSize) error {
return tcswinsz(m.f.Fd(), ws)
}

func (m *master) ResizeFrom(c Console) error {
ws, err := c.Size()
if err != nil {
return err
}
return m.Resize(ws)
}

func (m *master) Reset() error {
if m.original == nil {
return nil
}
return tcset(m.f.Fd(), m.original)
}

func (m *master) getCurrent() (unix.Termios, error) {
var termios unix.Termios
if err := tcget(m.f.Fd(), &termios); err != nil {
return unix.Termios{}, err
}
return termios, nil
}

func (m *master) SetRaw() error {
rawState, err := m.getCurrent()
if err != nil {
return err
}
rawState = cfmakeraw(rawState)
rawState.Oflag = rawState.Oflag | unix.OPOST
return tcset(m.f.Fd(), &rawState)
}

func (m *master) DisableEcho() error {
rawState, err := m.getCurrent()
if err != nil {
return err
}
rawState.Lflag = rawState.Lflag &^ unix.ECHO
return tcset(m.f.Fd(), &rawState)
}

func (m *master) Size() (WinSize, error) {
return tcgwinsz(m.f.Fd())
}

func (m *master) Fd() uintptr {
return m.f.Fd()
}

func (m *master) Name() string {
return m.f.Name()
}

// checkConsole checks if the provided file is a console
func checkConsole(f File) error {
var termios unix.Termios
if tcget(f.Fd(), &termios) != nil {
return ErrNotAConsole
}
return nil
}

func newMaster(f File) (Console, error) {
m := &master{
f: f,
}
t, err := m.getCurrent()
if err != nil {
return nil, err
}
m.original = &t
return m, nil
}

// ClearONLCR sets the necessary tty_ioctl(4)s to ensure that a pty pair
// created by us acts normally. In particular, a not-very-well-known default of
// Linux unix98 ptys is that they have +onlcr by default. While this isn't a
// problem for terminal emulators, because we relay data from the terminal we
// also relay that funky line discipline.
func ClearONLCR(fd uintptr) error {
return setONLCR(fd, false)
}

// SetONLCR sets the necessary tty_ioctl(4)s to ensure that a pty pair
// created by us acts as intended for a terminal emulator.
func SetONLCR(fd uintptr) error {
return setONLCR(fd, true)
}
2 changes: 1 addition & 1 deletion tc_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build darwin freebsd linux netbsd openbsd solaris
// +build darwin freebsd linux netbsd openbsd solaris zos

/*
Copyright The containerd Authors.
Expand Down
26 changes: 26 additions & 0 deletions tc_zos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package console

import (
"golang.org/x/sys/unix"
)

const (
cmdTcGet = unix.TCGETS
cmdTcSet = unix.TCSETS
)

0 comments on commit 2298a9c

Please sign in to comment.