threadsafe acquiring pty

This commit is contained in:
Pavel Vorobyov
2019-09-27 17:08:28 +03:00
parent e509bffcb5
commit 7437dcc9e9
2 changed files with 13 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
)
@@ -21,6 +22,7 @@ var (
currentRemoteTmpdir string
currentDebug bool
outputFile *os.File
ptyLock *sync.Mutex
noneInterpreter string
suInterpreter string
@@ -30,6 +32,7 @@ var (
// Initialize initializes new execution pool
func Initialize(numThreads int, username string) {
pool = NewPool(numThreads)
ptyLock = new(sync.Mutex)
SetUser(username)
SetPassword("")
SetRaise(RTNone)

View File

@@ -19,24 +19,32 @@ func (w *Worker) runcmd(task *Task) int {
n int
password string
passwordSent bool
ptmx *os.File
fd *poller.FD
)
cmd := createSSHCmd(task.Hostname, task.Cmd)
cmd.Env = append(os.Environ(), environment...)
ptmx, err := pty.Start(cmd)
// threadsafe acquiring necessary file descriptors
ptyLock.Lock()
ptmx, err = pty.Start(cmd)
if err != nil {
log.Debugf("WRK[%d]: Error creating ptmx: %v", w.id, err)
ptyLock.Unlock()
return ErrTerminalError
}
defer ptmx.Close()
fd, err := poller.NewFD(int(ptmx.Fd()))
fd, err = poller.NewFD(int(ptmx.Fd()))
if err != nil {
log.Debugf("WRK[%d]: Error creating poller FD: %v", w.id, err)
ptyLock.Unlock()
return ErrTerminalError
}
defer fd.Close()
ptyLock.Unlock()
// threadsafe acquiring necessary file descriptors ends
buf := make([]byte, bufferSize)
taskForceStopped := false