Skip to content

Commit daae4a8

Browse files
committed
[lldb] Modernize PseudoTerminal::OpenSecondary
1 parent 8a548bc commit daae4a8

File tree

5 files changed

+38
-69
lines changed

5 files changed

+38
-69
lines changed

lldb/include/lldb/Host/PseudoTerminal.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,10 @@ class PseudoTerminal {
148148
/// \param[in] oflag
149149
/// Flags to use when calling \c open(\a oflag).
150150
///
151-
/// \param[out] error_str
152-
/// An pointer to an error that can describe any errors that
153-
/// occur. This can be NULL if no error status is desired.
154-
///
155-
/// \return
156-
/// \b true when the primary files descriptor is
157-
/// successfully opened.
158-
/// \b false if anything goes wrong.
159-
///
160151
/// \see PseudoTerminal::OpenFirstAvailablePrimary() @see
161152
/// PseudoTerminal::GetSecondaryFileDescriptor() @see
162153
/// PseudoTerminal::ReleaseSecondaryFileDescriptor()
163-
bool OpenSecondary(int oflag, char *error_str, size_t error_len);
154+
llvm::Error OpenSecondary(int oflag);
164155

165156
/// Release the primary file descriptor.
166157
///

lldb/source/Host/common/PseudoTerminal.cpp

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,16 @@ llvm::Error PseudoTerminal::OpenFirstAvailablePrimary(int oflag) {
9494
#endif
9595
}
9696

97-
// Open the secondary pseudo terminal for the current primary pseudo terminal. A
98-
// primary pseudo terminal should already be valid prior to calling this
99-
// function (see OpenFirstAvailablePrimary()). The file descriptor is stored
100-
// this object's member variables and can be accessed via the
101-
// GetSecondaryFileDescriptor(), or released using the
102-
// ReleaseSecondaryFileDescriptor() member function.
103-
//
104-
// RETURNS:
105-
// True when successful, false indicating an error occurred.
106-
bool PseudoTerminal::OpenSecondary(int oflag, char *error_str,
107-
size_t error_len) {
108-
if (error_str)
109-
error_str[0] = '\0';
110-
97+
llvm::Error PseudoTerminal::OpenSecondary(int oflag) {
11198
CloseSecondaryFileDescriptor();
11299

113100
std::string name = GetSecondaryName();
114101
m_secondary_fd = llvm::sys::RetryAfterSignal(-1, ::open, name.c_str(), oflag);
115-
if (m_secondary_fd < 0) {
116-
if (error_str)
117-
ErrnoToStr(error_str, error_len);
118-
return false;
119-
}
102+
if (m_secondary_fd >= 0)
103+
return llvm::Error::success();
120104

121-
return true;
105+
return llvm::errorCodeToError(
106+
std::error_code(errno, std::generic_category()));
122107
}
123108

124109
std::string PseudoTerminal::GetSecondaryName() const {
@@ -174,35 +159,36 @@ lldb::pid_t PseudoTerminal::Fork(char *error_str, size_t error_len) {
174159
// Child Process
175160
::setsid();
176161

177-
if (OpenSecondary(O_RDWR, error_str, error_len)) {
178-
// Successfully opened secondary
162+
if (llvm::Error Err = OpenSecondary(O_RDWR)) {
163+
snprintf(error_str, error_len, "%s", toString(std::move(Err)).c_str());
164+
return LLDB_INVALID_PROCESS_ID;
165+
}
179166

180-
// Primary FD should have O_CLOEXEC set, but let's close it just in
181-
// case...
182-
ClosePrimaryFileDescriptor();
167+
// Primary FD should have O_CLOEXEC set, but let's close it just in
168+
// case...
169+
ClosePrimaryFileDescriptor();
183170

184171
#if defined(TIOCSCTTY)
185-
// Acquire the controlling terminal
186-
if (::ioctl(m_secondary_fd, TIOCSCTTY, (char *)0) < 0) {
187-
if (error_str)
188-
ErrnoToStr(error_str, error_len);
189-
}
172+
// Acquire the controlling terminal
173+
if (::ioctl(m_secondary_fd, TIOCSCTTY, (char *)0) < 0) {
174+
if (error_str)
175+
ErrnoToStr(error_str, error_len);
176+
}
190177
#endif
191-
// Duplicate all stdio file descriptors to the secondary pseudo terminal
192-
if (::dup2(m_secondary_fd, STDIN_FILENO) != STDIN_FILENO) {
193-
if (error_str && !error_str[0])
194-
ErrnoToStr(error_str, error_len);
195-
}
196-
197-
if (::dup2(m_secondary_fd, STDOUT_FILENO) != STDOUT_FILENO) {
198-
if (error_str && !error_str[0])
199-
ErrnoToStr(error_str, error_len);
200-
}
201-
202-
if (::dup2(m_secondary_fd, STDERR_FILENO) != STDERR_FILENO) {
203-
if (error_str && !error_str[0])
204-
ErrnoToStr(error_str, error_len);
205-
}
178+
// Duplicate all stdio file descriptors to the secondary pseudo terminal
179+
if (::dup2(m_secondary_fd, STDIN_FILENO) != STDIN_FILENO) {
180+
if (error_str && !error_str[0])
181+
ErrnoToStr(error_str, error_len);
182+
}
183+
184+
if (::dup2(m_secondary_fd, STDOUT_FILENO) != STDOUT_FILENO) {
185+
if (error_str && !error_str[0])
186+
ErrnoToStr(error_str, error_len);
187+
}
188+
189+
if (::dup2(m_secondary_fd, STDERR_FILENO) != STDERR_FILENO) {
190+
if (error_str && !error_str[0])
191+
ErrnoToStr(error_str, error_len);
206192
}
207193
} else {
208194
// Parent Process

lldb/source/Plugins/Platform/MacOSX/objcxx/PlatformiOSSimulatorCoreSimulatorSupport.mm

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,12 @@ static Status HandleFileAction(ProcessLaunchInfo &launch_info,
411411
if (file_spec == secondary_spec) {
412412
int secondary_fd =
413413
launch_info.GetPTY().GetSecondaryFileDescriptor();
414-
if (secondary_fd == PseudoTerminal::invalid_fd)
415-
secondary_fd =
416-
launch_info.GetPTY().OpenSecondary(O_RDWR, nullptr, 0);
417414
if (secondary_fd == PseudoTerminal::invalid_fd) {
418-
std::string secondary_path = secondary_spec.GetPath();
419-
error.SetErrorStringWithFormat(
420-
"unable to open secondary pty '%s'", secondary_path.c_str());
421-
return error; // Failure
415+
if (llvm::Error Err = launch_info.GetPTY().OpenSecondary(O_RDWR))
416+
return Status(std::move(Err));
422417
}
418+
secondary_fd = launch_info.GetPTY().GetSecondaryFileDescriptor();
419+
assert(secondary_fd != PseudoTerminal::invalid_fd);
423420
[options setValue:[NSNumber numberWithInteger:secondary_fd]
424421
forKey:key];
425422
return error; // Success

lldb/unittests/Editline/EditlineTest.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,7 @@ EditlineAdapter::EditlineAdapter()
107107
_pty_master_fd = _pty.GetPrimaryFileDescriptor();
108108

109109
// Open the corresponding secondary pty.
110-
char error_string[256];
111-
error_string[0] = '\0';
112-
if (!_pty.OpenSecondary(O_RDWR, error_string, sizeof(error_string))) {
113-
fprintf(stderr, "failed to open secondary pty: '%s'\n", error_string);
114-
return;
115-
}
110+
EXPECT_THAT_ERROR(_pty.OpenSecondary(O_RDWR), llvm::Succeeded());
116111
_pty_secondary_fd = _pty.GetSecondaryFileDescriptor();
117112

118113
_el_secondary_file.reset(new FilePointer(fdopen(_pty_secondary_fd, "rw")));

lldb/unittests/Host/MainLoopTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ TEST_F(MainLoopTest, DetectsEOF) {
103103

104104
PseudoTerminal term;
105105
ASSERT_THAT_ERROR(term.OpenFirstAvailablePrimary(O_RDWR), llvm::Succeeded());
106-
ASSERT_TRUE(term.OpenSecondary(O_RDWR | O_NOCTTY, nullptr, 0));
106+
ASSERT_THAT_ERROR(term.OpenSecondary(O_RDWR | O_NOCTTY), llvm::Succeeded());
107107
auto conn = std::make_unique<ConnectionFileDescriptor>(
108108
term.ReleasePrimaryFileDescriptor(), true);
109109

0 commit comments

Comments
 (0)