github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/debug/term.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     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  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package debug
    18  
    19  import (
    20  	"io"
    21  	"os"
    22  
    23  	"github.com/moby/term"
    24  	"github.com/sirupsen/logrus"
    25  )
    26  
    27  // SafeFunc is a function to be invoked by TTY.
    28  type SafeFunc func() error
    29  
    30  // TTY helps invoke a function and preserve the state of the terminal, even if the process is
    31  // terminated during execution. It also provides support for terminal resizing for remote command
    32  // attachment.
    33  type TTY struct {
    34  	// In is a reader representing stdin. It is a required field.
    35  	In io.Reader
    36  	// Out is a writer representing stdout. It must be set to support terminal resizing. It is an
    37  	// optional field.
    38  	Out io.Writer
    39  	// Raw is true if the terminal should be set raw.
    40  	Raw bool
    41  	// TryDev indicates the TTY should try to open /dev/tty if the provided input
    42  	// is not a file descriptor.
    43  	TryDev bool
    44  	// Parent is an optional interrupt handler provided to this function - if provided
    45  	// it will be invoked after the terminal state is restored. If it is not provided,
    46  	// a signal received during the TTY will result in os.Exit(0) being invoked.
    47  	Parent *Handler
    48  
    49  	// sizeQueue is set after a call to MonitorSize() and is used to monitor SIGWINCH signals when the
    50  	// user's terminal resizes.
    51  	sizeQueue *sizeQueue
    52  }
    53  
    54  // IsTerminalIn returns true if t.In is a terminal. Does not check /dev/tty
    55  // even if TryDev is set.
    56  func (t TTY) IsTerminalIn() bool {
    57  	return IsTerminal(t.In)
    58  }
    59  
    60  // IsTerminalOut returns true if t.Out is a terminal. Does not check /dev/tty
    61  // even if TryDev is set.
    62  func (t TTY) IsTerminalOut() bool {
    63  	return IsTerminal(t.Out)
    64  }
    65  
    66  // IsTerminal returns whether the passed object is a terminal or not
    67  func IsTerminal(i interface{}) bool {
    68  	_, terminal := term.GetFdInfo(i)
    69  	return terminal
    70  }
    71  
    72  // Safe invokes the provided function and will attempt to ensure that when the
    73  // function returns (or a termination signal is sent) that the terminal state
    74  // is reset to the condition it was in prior to the function being invoked.
    75  // If Raw is true the terminal will be put into raw mode prior to calling the function.
    76  func (t TTY) Safe(fn SafeFunc) error {
    77  	// If the input file descriptor is not a TTY and TryDev is true, the /dev/tty file
    78  	// will be opened (if available).
    79  	inFd, isTerminal := term.GetFdInfo(t.In)
    80  
    81  	if !isTerminal && t.TryDev {
    82  		if f, err := os.Open("/dev/tty"); err == nil {
    83  			defer func() {
    84  				if err := f.Close(); err != nil {
    85  					logrus.Fatal("failed to close file")
    86  				}
    87  			}()
    88  			inFd = f.Fd()
    89  			isTerminal = term.IsTerminal(inFd)
    90  		}
    91  	}
    92  
    93  	if !isTerminal {
    94  		return fn()
    95  	}
    96  
    97  	var state *term.State
    98  	var err error
    99  	if t.Raw {
   100  		state, err = term.MakeRaw(inFd)
   101  	} else {
   102  		state, err = term.SaveState(inFd)
   103  	}
   104  	if err != nil {
   105  		return err
   106  	}
   107  	return Chain(t.Parent, func() {
   108  		if t.sizeQueue != nil {
   109  			t.sizeQueue.stop()
   110  		}
   111  
   112  		if err := term.RestoreTerminal(inFd, state); err != nil {
   113  			return
   114  		}
   115  	}).Run(fn)
   116  }