process_windows.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package libcontainerd
  2. import (
  3. "io"
  4. "github.com/Microsoft/hcsshim"
  5. )
  6. // process keeps the state for both main container process and exec process.
  7. type process struct {
  8. processCommon
  9. // Platform specific fields are below here.
  10. // commandLine is to support returning summary information for docker top
  11. commandLine string
  12. hcsProcess hcsshim.Process
  13. }
  14. func openReaderFromPipe(p io.ReadCloser) io.Reader {
  15. r, w := io.Pipe()
  16. go func() {
  17. if _, err := io.Copy(w, p); err != nil {
  18. r.CloseWithError(err)
  19. }
  20. w.Close()
  21. p.Close()
  22. }()
  23. return r
  24. }
  25. // fixStdinBackspaceBehavior works around a bug in Windows before build 14350
  26. // where it interpreted DEL as VK_DELETE instead of as VK_BACK. This replaces
  27. // DEL with BS to work around this.
  28. func fixStdinBackspaceBehavior(w io.WriteCloser, osversion string, tty bool) io.WriteCloser {
  29. if !tty {
  30. return w
  31. }
  32. if build := buildFromVersion(osversion); build == 0 || build >= 14350 {
  33. return w
  34. }
  35. return &delToBsWriter{w}
  36. }
  37. type delToBsWriter struct {
  38. io.WriteCloser
  39. }
  40. func (w *delToBsWriter) Write(b []byte) (int, error) {
  41. const (
  42. backspace = 0x8
  43. del = 0x7f
  44. )
  45. bc := make([]byte, len(b))
  46. for i, c := range b {
  47. if c == del {
  48. bc[i] = backspace
  49. } else {
  50. bc[i] = c
  51. }
  52. }
  53. return w.WriteCloser.Write(bc)
  54. }
  55. type stdInCloser struct {
  56. io.WriteCloser
  57. hcsshim.Process
  58. }
  59. func createStdInCloser(pipe io.WriteCloser, process hcsshim.Process) *stdInCloser {
  60. return &stdInCloser{
  61. WriteCloser: pipe,
  62. Process: process,
  63. }
  64. }
  65. func (stdin *stdInCloser) Close() error {
  66. if err := stdin.WriteCloser.Close(); err != nil {
  67. return err
  68. }
  69. return stdin.Process.CloseStdin()
  70. }
  71. func (stdin *stdInCloser) Write(p []byte) (n int, err error) {
  72. return stdin.WriteCloser.Write(p)
  73. }