Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions cmd/breakpoint/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"fmt"
"os"
"os/exec"
"syscall"
"time"

"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/emptypb"
"namespacelabs.dev/breakpoint/api/private/v1"
v1 "namespacelabs.dev/breakpoint/api/private/v1"
"namespacelabs.dev/breakpoint/pkg/bcontrol"
"namespacelabs.dev/breakpoint/pkg/execbackground"
"namespacelabs.dev/breakpoint/pkg/waiter"
)

Expand All @@ -35,9 +35,7 @@ func newStartCmd() *cobra.Command {

procArgs := []string{"wait", "--config", *configPath}
proc := exec.Command(os.Args[0], procArgs...)
proc.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
}
execbackground.SetCreateSession(proc)

if err := proc.Start(); err != nil {
return fmt.Errorf("failed to start background process: %w", err)
Expand Down
14 changes: 14 additions & 0 deletions pkg/execbackground/bg_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !windows

package execbackground

import (
"os/exec"
"syscall"
)

func SetCreateSession(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
}
}
9 changes: 9 additions & 0 deletions pkg/execbackground/bg_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build windows

package execbackground

import "os/exec"

func SetCreateSession(cmd *exec.Cmd) {
panic("not supported")
}
44 changes: 44 additions & 0 deletions pkg/sshd/pty_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//go:build !windows

package sshd

import (
"fmt"
"io"
"os"
"os/exec"
"syscall"
"unsafe"

"github.com/creack/pty"
"github.com/gliderlabs/ssh"
)

func handlePty(session io.ReadWriter, ptyReq ssh.Pty, winCh <-chan ssh.Window, cmd *exec.Cmd) error {
cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
ptyFile, err := pty.Start(cmd)
if err != nil {
return err
}

defer ptyFile.Close()

go syncWinSize(ptyFile, winCh)
go func() {
_, _ = io.Copy(ptyFile, session) // stdin
}()
_, _ = io.Copy(session, ptyFile) // stdout

return nil
}

func syncWinSize(ptyFile *os.File, winCh <-chan ssh.Window) {
for win := range winCh {
setWinsize(ptyFile, win.Width, win.Height)
}
}

func setWinsize(f *os.File, w, h int) {
syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ),
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
}
15 changes: 15 additions & 0 deletions pkg/sshd/pty_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build windows

package sshd

import (
"errors"
"io"
"os/exec"

"github.com/gliderlabs/ssh"
)

func handlePty(session io.ReadWriter, ptyReq ssh.Pty, winCh <-chan ssh.Window, cmd *exec.Cmd) error {
return errors.New("pty not supported in windows")
}
13 changes: 1 addition & 12 deletions pkg/sshd/sshd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os/exec"
"time"

"github.com/creack/pty"
"github.com/gliderlabs/ssh"
"github.com/rs/zerolog"
"go.uber.org/atomic"
Expand Down Expand Up @@ -96,21 +95,11 @@ func MakeServer(ctx context.Context, opts SSHServerOpts) (*SSHServer, error) {
opts.InteractiveMOTD(session)
}

cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
ptyFile, err := pty.Start(cmd)
if err != nil {
if err := handlePty(session, ptyReq, winCh, cmd); err != nil {
sessionLog.Err(err).Msg("pty start failed")
session.Exit(1)
return
}

defer ptyFile.Close()

go syncWinSize(ptyFile, winCh)
go func() {
_, _ = io.Copy(ptyFile, session) // stdin
}()
_, _ = io.Copy(session, ptyFile) // stdout
} else {
cmd.Stdout = session
cmd.Stderr = session
Expand Down
20 changes: 0 additions & 20 deletions pkg/sshd/winresize.go

This file was deleted.