forked from OSchip/llvm-project
[lldb/debugserver] Rename Master/Slave to Primary/Secondary (NFC)
This commit is contained in:
parent
6752786d65
commit
0965b59bf4
|
@ -1154,7 +1154,7 @@
|
|||
2008-02-14 Jason Molenda (jmolenda@apple.com)
|
||||
|
||||
* MachProcess.cpp: (MachProcess::SBForkChildForPTraceDebugging):
|
||||
Set mode bits on slave side of pty.
|
||||
Set mode bits on secondary side of pty.
|
||||
|
||||
2008-02-12 Greg Clayton <gclayton@apple.com>
|
||||
|
||||
|
|
|
@ -3255,9 +3255,9 @@ pid_t MachProcess::PosixSpawnChildForPTraceDebugging(
|
|||
if (file_actions_valid) {
|
||||
if (stdin_path == NULL && stdout_path == NULL && stderr_path == NULL &&
|
||||
!no_stdio) {
|
||||
pty_error = pty.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY);
|
||||
pty_error = pty.OpenFirstAvailablePrimary(O_RDWR | O_NOCTTY);
|
||||
if (pty_error == PseudoTerminal::success) {
|
||||
stdin_path = stdout_path = stderr_path = pty.SlaveName();
|
||||
stdin_path = stdout_path = stderr_path = pty.SecondaryName();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3332,8 +3332,8 @@ pid_t MachProcess::PosixSpawnChildForPTraceDebugging(
|
|||
|
||||
if (pty_error == 0) {
|
||||
if (process != NULL) {
|
||||
int master_fd = pty.ReleaseMasterFD();
|
||||
process->SetChildFileDescriptors(master_fd, master_fd, master_fd);
|
||||
int primary_fd = pty.ReleasePrimaryFD();
|
||||
process->SetChildFileDescriptors(primary_fd, primary_fd, primary_fd);
|
||||
}
|
||||
}
|
||||
::posix_spawnattr_destroy(&attr);
|
||||
|
@ -3430,10 +3430,10 @@ pid_t MachProcess::ForkChildForPTraceDebugging(const char *path,
|
|||
::setpgid(pid, pid); // Set the child process group to match its pid
|
||||
|
||||
if (process != NULL) {
|
||||
// Release our master pty file descriptor so the pty class doesn't
|
||||
// Release our primary pty file descriptor so the pty class doesn't
|
||||
// close it and so we can continue to use it in our STDIO thread
|
||||
int master_fd = pty.ReleaseMasterFD();
|
||||
process->SetChildFileDescriptors(master_fd, master_fd, master_fd);
|
||||
int primary_fd = pty.ReleasePrimaryFD();
|
||||
process->SetChildFileDescriptors(primary_fd, primary_fd, primary_fd);
|
||||
}
|
||||
}
|
||||
return pid;
|
||||
|
@ -3606,15 +3606,15 @@ pid_t MachProcess::SBForkChildForPTraceDebugging(
|
|||
PseudoTerminal pty;
|
||||
if (!no_stdio) {
|
||||
PseudoTerminal::Status pty_err =
|
||||
pty.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY);
|
||||
pty.OpenFirstAvailablePrimary(O_RDWR | O_NOCTTY);
|
||||
if (pty_err == PseudoTerminal::success) {
|
||||
const char *slave_name = pty.SlaveName();
|
||||
const char *secondary_name = pty.SecondaryName();
|
||||
DNBLogThreadedIf(LOG_PROCESS,
|
||||
"%s() successfully opened master pty, slave is %s",
|
||||
__FUNCTION__, slave_name);
|
||||
if (slave_name && slave_name[0]) {
|
||||
::chmod(slave_name, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
stdio_path.SetFileSystemRepresentation(slave_name);
|
||||
"%s() successfully opened primary pty, secondary is %s",
|
||||
__FUNCTION__, secondary_name);
|
||||
if (secondary_name && secondary_name[0]) {
|
||||
::chmod(secondary_name, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
stdio_path.SetFileSystemRepresentation(secondary_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3671,10 +3671,10 @@ pid_t MachProcess::SBForkChildForPTraceDebugging(
|
|||
CFRelease(bundleIDCFStr);
|
||||
if (pid_found) {
|
||||
if (process != NULL) {
|
||||
// Release our master pty file descriptor so the pty class doesn't
|
||||
// Release our primary pty file descriptor so the pty class doesn't
|
||||
// close it and so we can continue to use it in our STDIO thread
|
||||
int master_fd = pty.ReleaseMasterFD();
|
||||
process->SetChildFileDescriptors(master_fd, master_fd, master_fd);
|
||||
int primary_fd = pty.ReleasePrimaryFD();
|
||||
process->SetChildFileDescriptors(primary_fd, primary_fd, primary_fd);
|
||||
}
|
||||
DNBLogThreadedIf(LOG_PROCESS, "%s() => pid = %4.4x", __FUNCTION__, pid);
|
||||
} else {
|
||||
|
@ -3807,17 +3807,17 @@ pid_t MachProcess::BoardServiceForkChildForPTraceDebugging(
|
|||
PseudoTerminal pty;
|
||||
if (!no_stdio) {
|
||||
PseudoTerminal::Status pty_err =
|
||||
pty.OpenFirstAvailableMaster(O_RDWR | O_NOCTTY);
|
||||
pty.OpenFirstAvailablePrimary(O_RDWR | O_NOCTTY);
|
||||
if (pty_err == PseudoTerminal::success) {
|
||||
const char *slave_name = pty.SlaveName();
|
||||
const char *secondary_name = pty.SecondaryName();
|
||||
DNBLogThreadedIf(LOG_PROCESS,
|
||||
"%s() successfully opened master pty, slave is %s",
|
||||
__FUNCTION__, slave_name);
|
||||
if (slave_name && slave_name[0]) {
|
||||
::chmod(slave_name, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
"%s() successfully opened primary pty, secondary is %s",
|
||||
__FUNCTION__, secondary_name);
|
||||
if (secondary_name && secondary_name[0]) {
|
||||
::chmod(secondary_name, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
stdio_path = [file_manager
|
||||
stringWithFileSystemRepresentation:slave_name
|
||||
length:strlen(slave_name)];
|
||||
stringWithFileSystemRepresentation:secondary_name
|
||||
length:strlen(secondary_name)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3866,8 +3866,8 @@ pid_t MachProcess::BoardServiceForkChildForPTraceDebugging(
|
|||
#endif
|
||||
|
||||
if (success) {
|
||||
int master_fd = pty.ReleaseMasterFD();
|
||||
SetChildFileDescriptors(master_fd, master_fd, master_fd);
|
||||
int primary_fd = pty.ReleasePrimaryFD();
|
||||
SetChildFileDescriptors(primary_fd, primary_fd, primary_fd);
|
||||
CFString::UTF8(bundleIDCFStr, m_bundle_id);
|
||||
}
|
||||
|
||||
|
|
|
@ -17,119 +17,119 @@
|
|||
|
||||
// PseudoTerminal constructor
|
||||
PseudoTerminal::PseudoTerminal()
|
||||
: m_master_fd(invalid_fd), m_slave_fd(invalid_fd) {}
|
||||
: m_primary_fd(invalid_fd), m_secondary_fd(invalid_fd) {}
|
||||
|
||||
// Destructor
|
||||
// The master and slave file descriptors will get closed if they are
|
||||
// valid. Call the ReleaseMasterFD()/ReleaseSlaveFD() member functions
|
||||
// The primary and secondary file descriptors will get closed if they are
|
||||
// valid. Call the ReleasePrimaryFD()/ReleaseSecondaryFD() member functions
|
||||
// to release any file descriptors that are needed beyond the lifespan
|
||||
// of this object.
|
||||
PseudoTerminal::~PseudoTerminal() {
|
||||
CloseMaster();
|
||||
CloseSlave();
|
||||
ClosePrimary();
|
||||
CloseSecondary();
|
||||
}
|
||||
|
||||
// Close the master file descriptor if it is valid.
|
||||
void PseudoTerminal::CloseMaster() {
|
||||
if (m_master_fd > 0) {
|
||||
::close(m_master_fd);
|
||||
m_master_fd = invalid_fd;
|
||||
// Close the primary file descriptor if it is valid.
|
||||
void PseudoTerminal::ClosePrimary() {
|
||||
if (m_primary_fd > 0) {
|
||||
::close(m_primary_fd);
|
||||
m_primary_fd = invalid_fd;
|
||||
}
|
||||
}
|
||||
|
||||
// Close the slave file descriptor if it is valid.
|
||||
void PseudoTerminal::CloseSlave() {
|
||||
if (m_slave_fd > 0) {
|
||||
::close(m_slave_fd);
|
||||
m_slave_fd = invalid_fd;
|
||||
// Close the secondary file descriptor if it is valid.
|
||||
void PseudoTerminal::CloseSecondary() {
|
||||
if (m_secondary_fd > 0) {
|
||||
::close(m_secondary_fd);
|
||||
m_secondary_fd = invalid_fd;
|
||||
}
|
||||
}
|
||||
|
||||
// Open the first available pseudo terminal with OFLAG as the
|
||||
// permissions. The file descriptor is store in the m_master_fd member
|
||||
// variable and can be accessed via the MasterFD() or ReleaseMasterFD()
|
||||
// permissions. The file descriptor is store in the m_primary_fd member
|
||||
// variable and can be accessed via the PrimaryFD() or ReleasePrimaryFD()
|
||||
// accessors.
|
||||
//
|
||||
// Suggested value for oflag is O_RDWR|O_NOCTTY
|
||||
//
|
||||
// RETURNS:
|
||||
// Zero when successful, non-zero indicating an error occurred.
|
||||
PseudoTerminal::Status PseudoTerminal::OpenFirstAvailableMaster(int oflag) {
|
||||
// Open the master side of a pseudo terminal
|
||||
m_master_fd = ::posix_openpt(oflag);
|
||||
if (m_master_fd < 0) {
|
||||
PseudoTerminal::Status PseudoTerminal::OpenFirstAvailablePrimary(int oflag) {
|
||||
// Open the primary side of a pseudo terminal
|
||||
m_primary_fd = ::posix_openpt(oflag);
|
||||
if (m_primary_fd < 0) {
|
||||
return err_posix_openpt_failed;
|
||||
}
|
||||
|
||||
// Grant access to the slave pseudo terminal
|
||||
if (::grantpt(m_master_fd) < 0) {
|
||||
CloseMaster();
|
||||
// Grant access to the secondary pseudo terminal
|
||||
if (::grantpt(m_primary_fd) < 0) {
|
||||
ClosePrimary();
|
||||
return err_grantpt_failed;
|
||||
}
|
||||
|
||||
// Clear the lock flag on the slave pseudo terminal
|
||||
if (::unlockpt(m_master_fd) < 0) {
|
||||
CloseMaster();
|
||||
// Clear the lock flag on the secondary pseudo terminal
|
||||
if (::unlockpt(m_primary_fd) < 0) {
|
||||
ClosePrimary();
|
||||
return err_unlockpt_failed;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Open the slave pseudo terminal for the current master pseudo
|
||||
// terminal. A master pseudo terminal should already be valid prior to
|
||||
// calling this function (see PseudoTerminal::OpenFirstAvailableMaster()).
|
||||
// The file descriptor is stored in the m_slave_fd member variable and
|
||||
// can be accessed via the SlaveFD() or ReleaseSlaveFD() accessors.
|
||||
// Open the secondary pseudo terminal for the current primary pseudo
|
||||
// terminal. A primary pseudo terminal should already be valid prior to
|
||||
// calling this function (see PseudoTerminal::OpenFirstAvailablePrimary()).
|
||||
// The file descriptor is stored in the m_secondary_fd member variable and
|
||||
// can be accessed via the SecondaryFD() or ReleaseSecondaryFD() accessors.
|
||||
//
|
||||
// RETURNS:
|
||||
// Zero when successful, non-zero indicating an error occurred.
|
||||
PseudoTerminal::Status PseudoTerminal::OpenSlave(int oflag) {
|
||||
CloseSlave();
|
||||
PseudoTerminal::Status PseudoTerminal::OpenSecondary(int oflag) {
|
||||
CloseSecondary();
|
||||
|
||||
// Open the master side of a pseudo terminal
|
||||
const char *slave_name = SlaveName();
|
||||
// Open the primary side of a pseudo terminal
|
||||
const char *secondary_name = SecondaryName();
|
||||
|
||||
if (slave_name == NULL)
|
||||
if (secondary_name == NULL)
|
||||
return err_ptsname_failed;
|
||||
|
||||
m_slave_fd = ::open(slave_name, oflag);
|
||||
m_secondary_fd = ::open(secondary_name, oflag);
|
||||
|
||||
if (m_slave_fd < 0)
|
||||
return err_open_slave_failed;
|
||||
if (m_secondary_fd < 0)
|
||||
return err_open_secondary_failed;
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Get the name of the slave pseudo terminal. A master pseudo terminal
|
||||
// Get the name of the secondary pseudo terminal. A primary pseudo terminal
|
||||
// should already be valid prior to calling this function (see
|
||||
// PseudoTerminal::OpenFirstAvailableMaster()).
|
||||
// PseudoTerminal::OpenFirstAvailablePrimary()).
|
||||
//
|
||||
// RETURNS:
|
||||
// NULL if no valid master pseudo terminal or if ptsname() fails.
|
||||
// The name of the slave pseudo terminal as a NULL terminated C string
|
||||
// NULL if no valid primary pseudo terminal or if ptsname() fails.
|
||||
// The name of the secondary pseudo terminal as a NULL terminated C string
|
||||
// that comes from static memory, so a copy of the string should be
|
||||
// made as subsequent calls can change this value.
|
||||
const char *PseudoTerminal::SlaveName() const {
|
||||
if (m_master_fd < 0)
|
||||
const char *PseudoTerminal::SecondaryName() const {
|
||||
if (m_primary_fd < 0)
|
||||
return NULL;
|
||||
return ::ptsname(m_master_fd);
|
||||
return ::ptsname(m_primary_fd);
|
||||
}
|
||||
|
||||
// Fork a child process that and have its stdio routed to a pseudo
|
||||
// terminal.
|
||||
//
|
||||
// In the parent process when a valid pid is returned, the master file
|
||||
// In the parent process when a valid pid is returned, the primary file
|
||||
// descriptor can be used as a read/write access to stdio of the
|
||||
// child process.
|
||||
//
|
||||
// In the child process the stdin/stdout/stderr will already be routed
|
||||
// to the slave pseudo terminal and the master file descriptor will be
|
||||
// to the secondary pseudo terminal and the primary file descriptor will be
|
||||
// closed as it is no longer needed by the child process.
|
||||
//
|
||||
// This class will close the file descriptors for the master/slave
|
||||
// when the destructor is called, so be sure to call ReleaseMasterFD()
|
||||
// or ReleaseSlaveFD() if any file descriptors are going to be used
|
||||
// This class will close the file descriptors for the primary/secondary
|
||||
// when the destructor is called, so be sure to call ReleasePrimaryFD()
|
||||
// or ReleaseSecondaryFD() if any file descriptors are going to be used
|
||||
// past the lifespan of this object.
|
||||
//
|
||||
// RETURNS:
|
||||
|
@ -138,10 +138,10 @@ const char *PseudoTerminal::SlaveName() const {
|
|||
|
||||
pid_t PseudoTerminal::Fork(PseudoTerminal::Status &error) {
|
||||
pid_t pid = invalid_pid;
|
||||
error = OpenFirstAvailableMaster(O_RDWR | O_NOCTTY);
|
||||
error = OpenFirstAvailablePrimary(O_RDWR | O_NOCTTY);
|
||||
|
||||
if (error == 0) {
|
||||
// Successfully opened our master pseudo terminal
|
||||
// Successfully opened our primary pseudo terminal
|
||||
|
||||
pid = ::fork();
|
||||
if (pid < 0) {
|
||||
|
@ -151,23 +151,23 @@ pid_t PseudoTerminal::Fork(PseudoTerminal::Status &error) {
|
|||
// Child Process
|
||||
::setsid();
|
||||
|
||||
error = OpenSlave(O_RDWR);
|
||||
error = OpenSecondary(O_RDWR);
|
||||
if (error == 0) {
|
||||
// Successfully opened slave
|
||||
// We are done with the master in the child process so lets close it
|
||||
CloseMaster();
|
||||
// Successfully opened secondary
|
||||
// We are done with the primary in the child process so lets close it
|
||||
ClosePrimary();
|
||||
|
||||
#if defined(TIOCSCTTY)
|
||||
// Acquire the controlling terminal
|
||||
if (::ioctl(m_slave_fd, TIOCSCTTY, (char *)0) < 0)
|
||||
if (::ioctl(m_secondary_fd, TIOCSCTTY, (char *)0) < 0)
|
||||
error = err_failed_to_acquire_controlling_terminal;
|
||||
#endif
|
||||
// Duplicate all stdio file descriptors to the slave pseudo terminal
|
||||
if (::dup2(m_slave_fd, STDIN_FILENO) != STDIN_FILENO)
|
||||
// Duplicate all stdio file descriptors to the secondary pseudo terminal
|
||||
if (::dup2(m_secondary_fd, STDIN_FILENO) != STDIN_FILENO)
|
||||
error = error ? error : err_dup2_failed_on_stdin;
|
||||
if (::dup2(m_slave_fd, STDOUT_FILENO) != STDOUT_FILENO)
|
||||
if (::dup2(m_secondary_fd, STDOUT_FILENO) != STDOUT_FILENO)
|
||||
error = error ? error : err_dup2_failed_on_stdout;
|
||||
if (::dup2(m_slave_fd, STDERR_FILENO) != STDERR_FILENO)
|
||||
if (::dup2(m_secondary_fd, STDERR_FILENO) != STDERR_FILENO)
|
||||
error = error ? error : err_dup2_failed_on_stderr;
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -27,7 +27,7 @@ public:
|
|||
err_grantpt_failed = -3,
|
||||
err_unlockpt_failed = -4,
|
||||
err_ptsname_failed = -5,
|
||||
err_open_slave_failed = -6,
|
||||
err_open_secondary_failed = -6,
|
||||
err_fork_failed = -7,
|
||||
err_setsid_failed = -8,
|
||||
err_failed_to_acquire_controlling_terminal = -9,
|
||||
|
@ -39,37 +39,37 @@ public:
|
|||
PseudoTerminal();
|
||||
~PseudoTerminal();
|
||||
|
||||
void CloseMaster();
|
||||
void CloseSlave();
|
||||
Status OpenFirstAvailableMaster(int oflag);
|
||||
Status OpenSlave(int oflag);
|
||||
int MasterFD() const { return m_master_fd; }
|
||||
int SlaveFD() const { return m_slave_fd; }
|
||||
int ReleaseMasterFD() {
|
||||
// Release ownership of the master pseudo terminal file
|
||||
void ClosePrimary();
|
||||
void CloseSecondary();
|
||||
Status OpenFirstAvailablePrimary(int oflag);
|
||||
Status OpenSecondary(int oflag);
|
||||
int PrimaryFD() const { return m_primary_fd; }
|
||||
int SecondaryFD() const { return m_secondary_fd; }
|
||||
int ReleasePrimaryFD() {
|
||||
// Release ownership of the primary pseudo terminal file
|
||||
// descriptor without closing it. (the destructor for this
|
||||
// class will close it otherwise!)
|
||||
int fd = m_master_fd;
|
||||
m_master_fd = invalid_fd;
|
||||
int fd = m_primary_fd;
|
||||
m_primary_fd = invalid_fd;
|
||||
return fd;
|
||||
}
|
||||
int ReleaseSlaveFD() {
|
||||
// Release ownership of the slave pseudo terminal file
|
||||
int ReleaseSecondaryFD() {
|
||||
// Release ownership of the secondary pseudo terminal file
|
||||
// descriptor without closing it (the destructor for this
|
||||
// class will close it otherwise!)
|
||||
int fd = m_slave_fd;
|
||||
m_slave_fd = invalid_fd;
|
||||
int fd = m_secondary_fd;
|
||||
m_secondary_fd = invalid_fd;
|
||||
return fd;
|
||||
}
|
||||
|
||||
const char *SlaveName() const;
|
||||
const char *SecondaryName() const;
|
||||
|
||||
pid_t Fork(Status &error);
|
||||
|
||||
protected:
|
||||
// Classes that inherit from PseudoTerminal can see and modify these
|
||||
int m_master_fd;
|
||||
int m_slave_fd;
|
||||
int m_primary_fd;
|
||||
int m_secondary_fd;
|
||||
|
||||
private:
|
||||
PseudoTerminal(const PseudoTerminal &rhs) = delete;
|
||||
|
|
Loading…
Reference in New Issue