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