Ignore:
Timestamp:
Oct 18, 2008, 11:54:52 PM (13 years ago)
Author:
Pascal Terjan
Message:
  • Run /usr/bin/reset in target process after a grab
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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)
Note: See TracChangeset for help on using the changeset viewer.