resizeevents.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // +build !windows
  2. /*
  3. Copyright 2016 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package term
  15. import (
  16. "os"
  17. "os/signal"
  18. "golang.org/x/sys/unix"
  19. "k8s.io/apimachinery/pkg/util/runtime"
  20. "k8s.io/client-go/tools/remotecommand"
  21. )
  22. // monitorResizeEvents spawns a goroutine that waits for SIGWINCH signals (these indicate the
  23. // terminal has resized). After receiving a SIGWINCH, this gets the terminal size and tries to send
  24. // it to the resizeEvents channel. The goroutine stops when the stop channel is closed.
  25. func monitorResizeEvents(fd uintptr, resizeEvents chan<- remotecommand.TerminalSize, stop chan struct{}) {
  26. go func() {
  27. defer runtime.HandleCrash()
  28. winch := make(chan os.Signal, 1)
  29. signal.Notify(winch, unix.SIGWINCH)
  30. defer signal.Stop(winch)
  31. for {
  32. select {
  33. case <-winch:
  34. size := GetSize(fd)
  35. if size == nil {
  36. return
  37. }
  38. // try to send size
  39. select {
  40. case resizeEvents <- *size:
  41. // success
  42. default:
  43. // not sent
  44. }
  45. case <-stop:
  46. return
  47. }
  48. }
  49. }()
  50. }