-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix docker exec stdout EOF missing (#93)
- Loading branch information
Showing
14 changed files
with
282 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0. | ||
*/ | ||
|
||
package handlers | ||
|
||
import ( | ||
fixout2 "github.com/traas-stack/holoinsight-agent/cmd/containerhelper/handlers/fixout" | ||
"github.com/traas-stack/holoinsight-agent/cmd/containerhelper/model" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
// fixOutHandler will run another process and encode the stdout/stderr of that process into fixOutHandler's stdout. | ||
func fixOutHandler(action string, resp *model.Resp) error { | ||
// build cmd | ||
cmd := exec.Command(os.Args[2], os.Args[3:]...) | ||
cmd.Stdin = os.Stdin | ||
stdoutr, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return err | ||
} | ||
stderrr, err := cmd.StderrPipe() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
errChan := make(chan error, 2) | ||
// encode cmd's stdout and stderr into os.Stdout | ||
go fixout2.CopyStream(fixout2.StdoutFd, stdoutr, errChan) | ||
go fixout2.CopyStream(fixout2.StderrFd, stderrr, errChan) | ||
|
||
wait := 2 | ||
loop: | ||
for { | ||
select { | ||
case <-errChan: | ||
wait-- | ||
if wait == 0 { | ||
cmd.Wait() | ||
// done | ||
break loop | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0. | ||
*/ | ||
|
||
package fixout | ||
|
||
const ( | ||
StdoutFd = 1 | ||
StderrFd = 2 | ||
bufSize = 4096 | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0. | ||
*/ | ||
|
||
package fixout | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
func Decode(hr *io.PipeReader, stdoutW io.WriteCloser, stderrW io.WriteCloser) error { | ||
var err error | ||
activeFdCount := 2 | ||
for activeFdCount > 0 { | ||
var fd byte | ||
var size int16 | ||
if err = binary.Read(hr, binary.LittleEndian, &fd); err != nil { | ||
break | ||
} | ||
if err = binary.Read(hr, binary.LittleEndian, &size); err != nil { | ||
break | ||
} | ||
if size == -1 { | ||
activeFdCount-- | ||
switch fd { | ||
case StdoutFd: | ||
stdoutW.Close() | ||
case StderrFd: | ||
stderrW.Close() | ||
} | ||
continue | ||
} | ||
switch fd { | ||
case StdoutFd: | ||
_, err = io.CopyN(stdoutW, hr, int64(size)) | ||
case StderrFd: | ||
_, err = io.CopyN(stderrW, hr, int64(size)) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0. | ||
*/ | ||
|
||
package fixout | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
"os" | ||
"sync" | ||
) | ||
|
||
var ( | ||
writeLock sync.Mutex | ||
) | ||
|
||
func write(fd int, payload []byte) error { | ||
writeLock.Lock() | ||
defer writeLock.Unlock() | ||
|
||
// fd 1 byte | ||
// size 2 bytes | ||
// payload <size> bytes (optional) | ||
if err := binary.Write(os.Stdout, binary.LittleEndian, byte(fd)); err != nil { | ||
return err | ||
} | ||
if err := binary.Write(os.Stdout, binary.LittleEndian, int16(len(payload))); err != nil { | ||
return err | ||
} | ||
return binary.Write(os.Stdout, binary.LittleEndian, payload) | ||
} | ||
|
||
func writeClose(fd int) error { | ||
writeLock.Lock() | ||
defer writeLock.Unlock() | ||
|
||
// fd 1 byte | ||
// -1 2 bytes (const) | ||
if err := binary.Write(os.Stdout, binary.LittleEndian, byte(fd)); err != nil { | ||
return err | ||
} | ||
return binary.Write(os.Stdout, binary.LittleEndian, int16(-1)) | ||
} | ||
|
||
// copyStream reads bytes from in, and encodes bytes into os.Stdout | ||
func CopyStream(fd int, in io.Reader, errChan chan error) { | ||
buf := make([]byte, bufSize) | ||
for { | ||
n, err := in.Read(buf) | ||
var err2 error | ||
if n > 0 { | ||
err2 = write(fd, buf[:n]) | ||
} | ||
if err == io.EOF { | ||
if err2 == nil { | ||
err2 = writeClose(fd) | ||
} | ||
} | ||
if err == nil { | ||
err = err2 | ||
} | ||
if err != nil { | ||
errChan <- err | ||
if err != io.EOF { | ||
io.Copy(io.Discard, in) | ||
} | ||
break | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.