@@ -65,52 +65,32 @@ void PseudoTerminal::CloseSecondaryFileDescriptor() {
65
65
}
66
66
}
67
67
68
- // Open the first available pseudo terminal with OFLAG as the permissions. The
69
- // file descriptor is stored in this object and can be accessed with the
70
- // PrimaryFileDescriptor() accessor. The ownership of the primary file
71
- // descriptor can be released using the ReleasePrimaryFileDescriptor() accessor.
72
- // If this object has a valid primary files descriptor when its destructor is
73
- // called, it will close the primary file descriptor, therefore clients must
74
- // call ReleasePrimaryFileDescriptor() if they wish to use the primary file
75
- // descriptor after this object is out of scope or destroyed.
76
- //
77
- // RETURNS:
78
- // True when successful, false indicating an error occurred.
79
- bool PseudoTerminal::OpenFirstAvailablePrimary (int oflag, char *error_str,
80
- size_t error_len) {
81
- if (error_str)
82
- error_str[0 ] = ' \0 ' ;
83
-
68
+ llvm::Error PseudoTerminal::OpenFirstAvailablePrimary (int oflag) {
84
69
#if LLDB_ENABLE_POSIX
85
70
// Open the primary side of a pseudo terminal
86
71
m_primary_fd = ::posix_openpt (oflag);
87
72
if (m_primary_fd < 0 ) {
88
- if (error_str)
89
- ErrnoToStr (error_str, error_len);
90
- return false ;
73
+ return llvm::errorCodeToError (
74
+ std::error_code (errno, std::generic_category ()));
91
75
}
92
76
93
77
// Grant access to the secondary pseudo terminal
94
78
if (::grantpt (m_primary_fd) < 0 ) {
95
- if (error_str)
96
- ErrnoToStr (error_str, error_len);
79
+ std::error_code EC (errno, std::generic_category ());
97
80
ClosePrimaryFileDescriptor ();
98
- return false ;
81
+ return llvm::errorCodeToError (EC) ;
99
82
}
100
83
101
84
// Clear the lock flag on the secondary pseudo terminal
102
85
if (::unlockpt (m_primary_fd) < 0 ) {
103
- if (error_str)
104
- ErrnoToStr (error_str, error_len);
86
+ std::error_code EC (errno, std::generic_category ());
105
87
ClosePrimaryFileDescriptor ();
106
- return false ;
88
+ return llvm::errorCodeToError (EC) ;
107
89
}
108
90
109
- return true ;
91
+ return llvm::Error::success () ;
110
92
#else
111
- if (error_str)
112
- ::snprintf (error_str, error_len, " %s" , " pseudo terminal not supported" );
113
- return false ;
93
+ return llvm::errorCodeToError (llvm::errc::not_supported);
114
94
#endif
115
95
}
116
96
@@ -180,54 +160,53 @@ lldb::pid_t PseudoTerminal::Fork(char *error_str, size_t error_len) {
180
160
error_str[0 ] = ' \0 ' ;
181
161
pid_t pid = LLDB_INVALID_PROCESS_ID;
182
162
#if LLDB_ENABLE_POSIX
183
- int flags = O_RDWR;
184
- flags |= O_CLOEXEC ;
185
- if ( OpenFirstAvailablePrimary (flags, error_str, error_len)) {
186
- // Successfully opened our primary pseudo terminal
163
+ if (llvm::Error Err = OpenFirstAvailablePrimary ( O_RDWR | O_CLOEXEC)) {
164
+ snprintf (error_str, error_len, " %s " , toString ( std::move (Err)). c_str ()) ;
165
+ return LLDB_INVALID_PROCESS_ID;
166
+ }
187
167
188
- pid = ::fork ();
189
- if (pid < 0 ) {
190
- // Fork failed
191
- if (error_str)
192
- ErrnoToStr (error_str, error_len);
193
- } else if (pid == 0 ) {
194
- // Child Process
195
- ::setsid ();
168
+ pid = ::fork ();
169
+ if (pid < 0 ) {
170
+ // Fork failed
171
+ if (error_str)
172
+ ErrnoToStr (error_str, error_len);
173
+ } else if (pid == 0 ) {
174
+ // Child Process
175
+ ::setsid ();
196
176
197
- if (OpenSecondary (O_RDWR, error_str, error_len)) {
198
- // Successfully opened secondary
177
+ if (OpenSecondary (O_RDWR, error_str, error_len)) {
178
+ // Successfully opened secondary
199
179
200
- // Primary FD should have O_CLOEXEC set, but let's close it just in
201
- // case...
202
- ClosePrimaryFileDescriptor ();
180
+ // Primary FD should have O_CLOEXEC set, but let's close it just in
181
+ // case...
182
+ ClosePrimaryFileDescriptor ();
203
183
204
184
#if defined(TIOCSCTTY)
205
- // Acquire the controlling terminal
206
- if (::ioctl (m_secondary_fd, TIOCSCTTY, (char *)0 ) < 0 ) {
207
- if (error_str)
208
- ErrnoToStr (error_str, error_len);
209
- }
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
+ }
210
190
#endif
211
- // Duplicate all stdio file descriptors to the secondary pseudo terminal
212
- if (::dup2 (m_secondary_fd, STDIN_FILENO) != STDIN_FILENO) {
213
- if (error_str && !error_str[0 ])
214
- ErrnoToStr (error_str, error_len);
215
- }
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
+ }
216
196
217
- if (::dup2 (m_secondary_fd, STDOUT_FILENO) != STDOUT_FILENO) {
218
- if (error_str && !error_str[0 ])
219
- ErrnoToStr (error_str, error_len);
220
- }
197
+ if (::dup2 (m_secondary_fd, STDOUT_FILENO) != STDOUT_FILENO) {
198
+ if (error_str && !error_str[0 ])
199
+ ErrnoToStr (error_str, error_len);
200
+ }
221
201
222
- if (::dup2 (m_secondary_fd, STDERR_FILENO) != STDERR_FILENO) {
223
- if (error_str && !error_str[0 ])
224
- ErrnoToStr (error_str, error_len);
225
- }
202
+ if (::dup2 (m_secondary_fd, STDERR_FILENO) != STDERR_FILENO) {
203
+ if (error_str && !error_str[0 ])
204
+ ErrnoToStr (error_str, error_len);
226
205
}
227
- } else {
228
- // Parent Process
229
- // Do nothing and let the pid get returned!
230
206
}
207
+ } else {
208
+ // Parent Process
209
+ // Do nothing and let the pid get returned!
231
210
}
232
211
#endif
233
212
return pid;
0 commit comments