Permalink
Browse files

Add a -s option to force attaching stdio.

  • Loading branch information...
1 parent d51bfc2 commit 9ce252618ed4791d9b7e6218f7b9f7c121768078 @nelhage committed Feb 19, 2011
Showing with 40 additions and 15 deletions.
  1. +25 −9 attach.c
  2. +14 −5 reptyr.c
  3. +1 −1 reptyr.h
View
@@ -259,10 +259,9 @@ int copy_tty_state(pid_t pid, const char *pty) {
if (!isatty(fd)) {
err = ENOTTY;
- error("Target is not connected to a terminal.");
goto out;
}
-
+
if (tcgetattr(fd, &tio) < 0) {
err = errno;
goto out;
@@ -279,16 +278,21 @@ int copy_tty_state(pid_t pid, const char *pty) {
return -err;
}
-int attach_child(pid_t pid, const char *pty) {
+int attach_child(pid_t pid, const char *pty, int force_stdio) {
struct ptrace_child child;
unsigned long scratch_page = -1;
int *child_tty_fds = NULL, n_fds, child_fd;
int i;
int err = 0;
long page_size = sysconf(_SC_PAGE_SIZE);
- if ((err = copy_tty_state(pid, pty)) < 0)
- return -err;
+ if ((err = copy_tty_state(pid, pty)) < 0) {
+ if (err == -ENOTTY && !force_stdio) {
+ error("Target is not connected to a terminal.\n"
+ " Use -s to force attaching anyways.");
+ return -err;
+ }
+ }
kill(pid, SIGTSTP);
wait_for_stop(pid);
@@ -318,10 +322,22 @@ int attach_child(pid_t pid, const char *pty) {
debug("Allocated scratch page: %lx", scratch_page);
- child_tty_fds = get_child_tty_fds(&child, &n_fds);
- if (!child_tty_fds) {
- err = child.error;
- goto out_unmap;
+ if (force_stdio) {
+ child_tty_fds = malloc(3 * sizeof(int));
+ if (!child_tty_fds) {
+ err = ENOMEM;
+ goto out_unmap;
+ }
+ n_fds = 3;
+ child_tty_fds[0] = 0;
+ child_tty_fds[1] = 1;
+ child_tty_fds[2] = 2;
+ } else {
+ child_tty_fds = get_child_tty_fds(&child, &n_fds);
+ if (!child_tty_fds) {
+ err = child.error;
+ goto out_unmap;
+ }
}
if (ptrace_memcpy_to_child(&child, scratch_page, pty, strlen(pty)+1)) {
View
@@ -139,7 +139,10 @@ void do_proxy(int pty) {
}
void usage(char *me) {
- fprintf(stderr, "Usage: %s [-l | PID]\n", me);
+ fprintf(stderr, "Usage: %s [-s] PID\n", me);
+ fprintf(stderr, " %s -l\n", me);
+ fprintf(stderr, " -l Create a new pty pair and print the name of the slave.\n");
+ fprintf(stderr, " -s Attach fds 0-2 on the target, even if it is not attached to a tty.\n");
}
void check_yama_ptrace_scope(void) {
@@ -165,20 +168,26 @@ int main(int argc, char **argv) {
struct termios saved_termios;
struct sigaction act;
int pty;
+ int arg = 1;
int do_attach = 1;
+ int force_stdio = 0;
if (argc < 2) {
usage(argv[0]);
return 2;
}
- if(argv[1][0] == '-') {
- switch(argv[1][1]) {
+ if(argv[arg][0] == '-') {
+ switch(argv[arg][1]) {
case 'h':
usage(argv[0]);
return 0;
case 'l':
do_attach = 0;
break;
+ case 's':
+ arg++;
+ force_stdio = 1;
+ break;
case 'v':
printf("This is reptyr version %s.\n", REPTYR_VERSION);
printf(" by Nelson Elhage <nelhage@nelhage.com>\n");
@@ -198,9 +207,9 @@ int main(int argc, char **argv) {
die("Unable to unlockpt: %m");
if (do_attach) {
- pid_t child = atoi(argv[1]);
+ pid_t child = atoi(argv[arg]);
int err;
- if ((err = attach_child(child, ptsname(pty)))) {
+ if ((err = attach_child(child, ptsname(pty), force_stdio))) {
fprintf(stderr, "Unable to attach to pid %d: %s\n", child, strerror(err));
if (err == EPERM) {
check_yama_ptrace_scope();
View
@@ -22,7 +22,7 @@
#define REPTYR_VERSION "0.1"
-int attach_child(pid_t pid, const char *pty);
+int attach_child(pid_t pid, const char *pty, int force_stdio);
void die(const char *msg, ...);
void debug(const char *msg, ...);
void error(const char *msg, ...);

0 comments on commit 9ce2526

Please sign in to comment.