Skip to content

Commit

Permalink
check if ttyname is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas.with@st.ovgu.de committed Apr 19, 2023
1 parent 6d7cf19 commit 508a39b
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/slave/slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,18 @@ int Slave::start_gdb() const
const int pid = fork();
if (0 == pid)
{
char *pts = strdup(ttyname(STDOUT_FILENO));
char *pts = nullptr;
int fds[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
for (int i = 0; i < 3; ++i)
{
pts = ttyname(fds[i]);
if (pts)
{
pts = strdup(pts);
break;
}
}

const int tty_fd = open(m_tty_gdb.c_str(), O_RDWR);

// connect I/O of GDB to PTY
Expand Down Expand Up @@ -168,12 +179,15 @@ int Slave::start_gdb() const
// Only reached if exec fails
close(tty_fd);

const int std_fd = open(pts, O_RDWR);
dup2(std_fd, STDIN_FILENO);
dup2(std_fd, STDOUT_FILENO);
dup2(std_fd, STDERR_FILENO);
if (pts)
{
const int std_fd = open(pts, O_RDWR);
dup2(std_fd, STDIN_FILENO);
dup2(std_fd, STDOUT_FILENO);
dup2(std_fd, STDERR_FILENO);

free(pts);
free(pts);
}

fprintf(stderr,
"Error starting gdb.\n"
Expand Down

0 comments on commit 508a39b

Please sign in to comment.