zos: share implementation with unix (less DRY)#63
Merged
samuelkarp merged 2 commits intocontainerd:mainfrom Oct 7, 2022
Merged
zos: share implementation with unix (less DRY)#63samuelkarp merged 2 commits intocontainerd:mainfrom
samuelkarp merged 2 commits intocontainerd:mainfrom
Conversation
Member
Author
|
@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 😅 |
najohnsn
approved these changes
Feb 14, 2022
Contributor
najohnsn
left a comment
There was a problem hiding this comment.
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>
Member
Author
samuelkarp
approved these changes
Oct 7, 2022
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.