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