Changeset 2906


Ignore:
Timestamp:
Oct 10, 2008, 12:24:51 AM (14 years ago)
Author:
Pascal Terjan
Message:
  • Cleanup my term after a grab
Location:
neercs/trunk/src
Files:
3 edited

Legend:

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

    r2785 r2906  
    4040    char fdstr[1024];
    4141    struct mytrace *parent, *child;
    42     int i, fd = 0, mode, ret;
     42    int i, fd = 0, ret;
    4343    char to_open[3];
     44    int mode[3];
    4445    struct stat stat_buf;
     46    struct termios tos;
    4547
    4648    debug("pty is %s", ptyname);
     
    5456
    5557    child = mytrace_fork(parent);
    56     mytrace_write(parent, 1, "\x1b]0;\x07", 5);
    57     mytrace_write(parent, 1, "\x1b[1000l", 7);
    58     mytrace_write(parent, 1, "\x1b[?12l\x1b[?25h", 12);
     58    mytrace_write(parent, 1, "\x1b[H\x1b[2J", 7);
    5959    mytrace_write(parent, 1, "\n[Process stolen by neercs]\n", 28);
    60     /* FIXME Reset the term */
     60    mytrace_tcgets(parent, 1, &tos);
     61    tos.c_lflag |= ICANON|ECHO;
     62    mytrace_tcsets(parent, 1, &tos);
    6163    mytrace_close(parent, 1);
    62 
    6364    mytrace_exit(parent, 0);
    6465    mytrace_detach(parent);
     
    7273        lstat(fdstr, &stat_buf);
    7374        if((stat_buf.st_mode & S_IRUSR) && (stat_buf.st_mode & S_IWUSR))
    74             mode = O_RDWR;
     75            mode[i] = O_RDWR;
    7576        else if(stat_buf.st_mode & S_IWUSR)
    76             mode = O_WRONLY;
     77            mode[i] = O_WRONLY;
    7778        else
    78             mode = O_RDONLY;
     79            mode[i] = O_RDONLY;
    7980
    8081        if(stat(fdstr, &stat_buf) < 0)
     
    128129        if(!to_open[i])
    129130            continue;
    130         fd = mytrace_open(child, ptyname, mode);
     131        fd = mytrace_open(child, ptyname, mode[i]);
    131132        if(fd < 0)
    132133        {
  • neercs/trunk/src/mytrace.c

    r2786 r2906  
    165165#define MYCALL_FORK     7
    166166#define MYCALL_EXIT     8
     167#define MYCALL_IOCTL    9
    167168
    168169#if defined __x86_64__
    169170/* from unistd_32.h on an amd64 system */
    170 int syscalls32[] = { 5, 6, 4, 63, 57, 66, 37, 2, 1 };
     171int syscalls32[] = { 5, 6, 4, 63, 57, 66, 37, 2, 1, 54 };
    171172int syscalls64[] =
    172173#else
     
    174175#endif
    175176    { SYS_open, SYS_close, SYS_write, SYS_dup2, SYS_setpgid, SYS_setsid,
    176       SYS_kill, SYS_fork, SYS_exit };
     177      SYS_kill, SYS_fork, SYS_exit, SYS_ioctl };
    177178
    178179char const *syscallnames[] =
    179180    { "open", "close", "write", "dup2", "setpgid", "setsid", "kill", "fork",
    180       "exit" };
     181      "exit", "ioctl" };
    181182
    182183struct mytrace
     
    394395    ptrace(PTRACE_SETOPTIONS, t->pid, NULL, PTRACE_O_TRACEEXIT);
    395396    return remote_syscall(t, MYCALL_EXIT, status, 0, 0);
     397#else
     398    errno = ENOSYS;
     399    return -1;
     400#endif
     401}
     402
     403int mytrace_tcgets(struct mytrace *t, int fd, struct termios *tos)
     404{
     405#if defined USE_GRAB
     406    struct user_regs_struct regs;
     407    struct termios mytos;
     408    int ret;
     409
     410    if(ptrace(PTRACE_GETREGS, t->pid, NULL, &regs) < 0)
     411    {
     412        fprintf(stderr, "PTRACE_GETREGS failed\n");
     413        return errno;
     414    }
     415
     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
     440int 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));
     463
     464    if(ret < 0)
     465    {
     466        errno = ret;
     467        return -1;
     468    }
     469
     470    return ret;
    396471#else
    397472    errno = ENOSYS;
  • neercs/trunk/src/mytrace.h

    r2516 r2906  
    1212 *  http://sam.zoy.org/wtfpl/COPYING for more details.
    1313 */
     14
     15#include <termios.h>
    1416
    1517struct mytrace;
     
    2830int mytrace_kill(struct mytrace *t, long pid, int sig);
    2931int mytrace_exit(struct mytrace *t, int status);
    30 
     32int mytrace_tcgets(struct mytrace *t, int fd, struct termios *tos);
     33int mytrace_tcsets(struct mytrace *t, int fd, struct termios *tos);
Note: See TracChangeset for help on using the changeset viewer.