1 //===-- TTYState.cpp --------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Created by Greg Clayton on 3/26/07. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "TTYState.h" 14 #include <fcntl.h> 15 #include <sys/signal.h> 16 #include <unistd.h> 17 18 TTYState::TTYState() 19 : m_fd(-1), m_tflags(-1), m_ttystateErr(-1), m_processGroup(-1) {} 20 21 TTYState::~TTYState() {} 22 23 bool TTYState::GetTTYState(int fd, bool saveProcessGroup) { 24 if (fd >= 0 && ::isatty(fd)) { 25 m_fd = fd; 26 m_tflags = fcntl(fd, F_GETFL, 0); 27 m_ttystateErr = tcgetattr(fd, &m_ttystate); 28 if (saveProcessGroup) 29 m_processGroup = tcgetpgrp(0); 30 else 31 m_processGroup = -1; 32 } else { 33 m_fd = -1; 34 m_tflags = -1; 35 m_ttystateErr = -1; 36 m_processGroup = -1; 37 } 38 return m_ttystateErr == 0; 39 } 40 41 bool TTYState::SetTTYState() const { 42 int result = 0; 43 if (IsValid()) { 44 if (TFlagsValid()) 45 result = fcntl(m_fd, F_SETFL, m_tflags); 46 47 if (TTYStateValid()) 48 result = tcsetattr(m_fd, TCSANOW, &m_ttystate); 49 50 if (ProcessGroupValid()) { 51 // Save the original signal handler. 52 void (*saved_sigttou_callback)(int) = NULL; 53 saved_sigttou_callback = (void (*)(int))signal(SIGTTOU, SIG_IGN); 54 // Set the process group 55 result = tcsetpgrp(m_fd, m_processGroup); 56 // Restore the original signal handler. 57 signal(SIGTTOU, saved_sigttou_callback); 58 } 59 return true; 60 } 61 return false; 62 } 63 64 TTYStateSwitcher::TTYStateSwitcher() : m_currentState(~0) {} 65 66 TTYStateSwitcher::~TTYStateSwitcher() {} 67 68 bool TTYStateSwitcher::GetState(uint32_t idx, int fd, bool saveProcessGroup) { 69 if (ValidStateIndex(idx)) 70 return m_ttystates[idx].GetTTYState(fd, saveProcessGroup); 71 return false; 72 } 73 74 bool TTYStateSwitcher::SetState(uint32_t idx) const { 75 if (!ValidStateIndex(idx)) 76 return false; 77 78 // See if we already are in this state? 79 if (ValidStateIndex(m_currentState) && (idx == m_currentState) && 80 m_ttystates[idx].IsValid()) 81 return true; 82 83 // Set the state to match the index passed in and only update the 84 // current state if there are no errors. 85 if (m_ttystates[idx].SetTTYState()) { 86 m_currentState = idx; 87 return true; 88 } 89 90 // We failed to set the state. The tty state was invalid or not 91 // initialized. 92 return false; 93 } 94