resizeevents.go 2.1 KB

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