Changeset 2999 for neercs/trunk


Ignore:
Timestamp:
Oct 18, 2008, 11:54:52 PM (12 years ago)
Author:
Pascal Terjan
Message:
  • Run /usr/bin/reset in target process after a grab
Location:
neercs/trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/src/grab.c

    r2906 r2999  
    4444    int mode[3];
    4545    struct stat stat_buf;
    46     struct termios tos;
    4746
    4847    debug("pty is %s", ptyname);
     
    5655
    5756    child = mytrace_fork(parent);
    58     mytrace_write(parent, 1, "\x1b[H\x1b[2J", 7);
    59     mytrace_write(parent, 1, "\n[Process stolen by neercs]\n", 28);
    60     mytrace_tcgets(parent, 1, &tos);
    61     tos.c_lflag |= ICANON|ECHO;
    62     mytrace_tcsets(parent, 1, &tos);
    63     mytrace_close(parent, 1);
    64     mytrace_exit(parent, 0);
     57    mytrace_exec(parent, "/usr/bin/reset");
     58    mytrace_exit(parent, 0); /* In case the execve failed */
    6559    mytrace_detach(parent);
     60    mytrace_write(child, 1, "\x1b[H\x1b[2J", 7);
     61    mytrace_write(child, 1, "\n[Process stolen by neercs]\n", 28);
    6662    pid = mytrace_getpid(child);
    6763
  • neercs/trunk/src/mytrace.c

    r2906 r2999  
    1818#include <errno.h>
    1919#include <fcntl.h>
     20#include <limits.h>
    2021#include <stdio.h>
    2122#include <stdlib.h>
     
    2324
    2425#if defined USE_GRAB
    25 #   include <sys/ioctl.h>
    2626#   include <sys/ptrace.h>
    2727#   include <sys/stat.h>
     
    103103
    104104int mytrace_exit(struct mytrace *t, int status)
     105{
     106    errno = ENOSYS;
     107    return -1;
     108}
     109
     110int mytrace_exec(struct mytrace *t, char const *command)
    105111{
    106112    errno = ENOSYS;
     
    165171#define MYCALL_FORK     7
    166172#define MYCALL_EXIT     8
    167 #define MYCALL_IOCTL    9
     173#define MYCALL_EXECVE   9
    168174
    169175#if defined __x86_64__
    170176/* from unistd_32.h on an amd64 system */
    171 int syscalls32[] = { 5, 6, 4, 63, 57, 66, 37, 2, 1, 54 };
     177int syscalls32[] = { 5, 6, 4, 63, 57, 66, 37, 2, 1, 11 };
    172178int syscalls64[] =
    173179#else
     
    175181#endif
    176182    { SYS_open, SYS_close, SYS_write, SYS_dup2, SYS_setpgid, SYS_setsid,
    177       SYS_kill, SYS_fork, SYS_exit, SYS_ioctl };
     183      SYS_kill, SYS_fork, SYS_exit, SYS_execve };
    178184
    179185char const *syscallnames[] =
    180186    { "open", "close", "write", "dup2", "setpgid", "setsid", "kill", "fork",
    181       "exit", "ioctl" };
     187      "exit", "execve" };
    182188
    183189struct mytrace
     
    401407}
    402408
    403 int mytrace_tcgets(struct mytrace *t, int fd, struct termios *tos)
     409int mytrace_exec(struct mytrace *t, char const *command)
    404410{
    405411#if defined USE_GRAB
    406412    struct user_regs_struct regs;
    407     struct termios mytos;
    408     int ret;
     413    char *env;
     414    char envpath[PATH_MAX+1];
     415    ssize_t envsize = 32*1024;
     416    int ret, fd, l;
     417    ssize_t r;
    409418
    410419    if(ptrace(PTRACE_GETREGS, t->pid, NULL, &regs) < 0)
     
    414423    }
    415424
    416     /* Backup the data that we will use */
    417     if(memcpy_from_target(t, (char *)&mytos, regs.RSP, sizeof(struct termios)) < 0)
    418         return -1;
    419 
    420     ret = remote_syscall(t, MYCALL_IOCTL, fd, TCGETS, regs.RSP);
    421 
    422     memcpy_from_target(t, (char *)tos, regs.RSP, sizeof(struct termios));
    423 
    424     /* Restore the data */
    425     memcpy_into_target(t, regs.RSP, (char *)&mytos, sizeof(struct termios));
    426 
    427     if(ret < 0)
    428     {
    429         errno = ret;
    430         return -1;
    431     }
    432 
    433     return ret;
    434 #else
    435     errno = ENOSYS;
    436     return -1;
    437 #endif
    438 }
    439 
    440 int mytrace_tcsets(struct mytrace *t, int fd, struct termios *tos)
    441 {
    442 #if defined USE_GRAB
    443     struct user_regs_struct regs;
    444     struct termios mytos;
    445     int ret;
    446 
    447     if(ptrace(PTRACE_GETREGS, t->pid, NULL, &regs) < 0)
    448     {
    449         fprintf(stderr, "PTRACE_GETREGS failed\n");
    450         return errno;
    451     }
    452 
    453     /* Backup the data that we will use */
    454     if(memcpy_from_target(t, (char *)&mytos, regs.RSP, sizeof(struct termios)) < 0)
    455         return -1;
    456 
    457     memcpy_into_target(t, regs.RSP, (char *)tos, sizeof(struct termios));
    458 
    459     ret = remote_syscall(t, MYCALL_IOCTL, fd, TCSETS, regs.RSP);
    460 
    461     /* Restore the data */
    462     memcpy_into_target(t, regs.RSP, (char *)&mytos, sizeof(struct termios));
     425    env = malloc(envsize);
     426    if (!env)
     427        return -1;
     428
     429    snprintf(envpath, PATH_MAX, "/proc/%d/environ", t->pid);
     430
     431    fd = open(envpath, O_RDONLY);
     432    r = read(fd, env, envsize);
     433    close(fd);
     434    if (r == -1)
     435        return -1;
     436    while (r == envsize)
     437    {
     438        free(env);
     439        env = malloc(envsize);
     440        if (!env)
     441            return -1;
     442        fd = open(envpath, O_RDONLY);
     443        r = read(fd, env, envsize);
     444        close(fd);
     445        if (r == -1)
     446            return -1;
     447    }
     448    l = strlen(command)+1;
     449    memcpy_into_target(t, regs.RSP, command, l);
     450    memcpy_into_target(t, regs.RSP+l, env, envsize);
     451    free(env);
     452    ret = remote_syscall(t, MYCALL_EXECVE, regs.RSP, 0, regs.RSP+l);
    463453
    464454    if(ret < 0)
  • neercs/trunk/src/mytrace.h

    r2906 r2999  
    1212 *  http://sam.zoy.org/wtfpl/COPYING for more details.
    1313 */
    14 
    15 #include <termios.h>
    1614
    1715struct mytrace;
     
    3028int mytrace_kill(struct mytrace *t, long pid, int sig);
    3129int mytrace_exit(struct mytrace *t, int status);
    32 int mytrace_tcgets(struct mytrace *t, int fd, struct termios *tos);
    33 int mytrace_tcsets(struct mytrace *t, int fd, struct termios *tos);
     30int mytrace_exec(struct mytrace *t, char const *command);
Note: See TracChangeset for help on using the changeset viewer.