-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zos: share implementation with unix (less DRY) #63
Conversation
@estesp @najohnsn PTAL if this makes sense to do. I noticed that the Happy to either drop the last commit (if the first implementation is clearer), or to squash both commits. Or of course to just close this PR if we don't think it's worth the effort 😅 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for bringing this to my attention! Looks great!
Testing on z/OS passed.
The zos and "unix" implementations were identical, except for the NewPTY() function. This patch extracts that function to non-exported implementations for "zos" and "unix", so that all other bits can be shared. For reference; this was the diff between both files before: diff --git a/console_unix.go b/console_zos.go index 78f70c2..96ccb6f 100644 --- a/console_unix.go +++ b/console_zos.go @@ -17,6 +17,9 @@ package console import ( + "fmt" + "os" + "golang.org/x/sys/unix" ) @@ -24,16 +27,20 @@ import ( // 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) { - f, err := openpt() - if err != nil { - return nil, "", err - } - slave, err := ptsname(f) - if err != nil { - return nil, "", err - } - if err := unlockpt(f); err != nil { - return nil, "", err + 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 { Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Follow-up to the previous commit; instead of using separate implementations for `newPty()` as a whole, create implementations for `openpt()`, `ptsname()` and a stub for `unlockpt()`. This seems slightly more consistent with other implementations, which already had os/arch specific implementations for these functions. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
zos: share implementation with unix
The zos and "unix" implementations were identical, except for the
NewPTY()
function.This patch extracts that function to non-exported implementations for "zos" and "unix", so that all other bits can be shared.
For reference; this was the diff between both files before:
zos: further reconcile implementation with other unices
Follow-up to the previous commit; instead of using separate implementations for
newPty()
as a whole, create implementations foropenpt()
,ptsname()
and a stub forunlockpt()
.This seems slightly more consistent with other implementations, which already had os/arch specific implementations for these functions.