source: neercs/trunk/src/grab.c @ 2999

Last change on this file since 2999 was 2999, checked in by Pascal Terjan, 12 years ago
  • Run /usr/bin/reset in target process after a grab
  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2008 Pascal Terjan
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
16
17#define _XOPEN_SOURCE 500 /* getsid() */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <signal.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28
29#if defined HAVE_LINUX_KDEV_T_H
30#   include <linux/kdev_t.h>
31#   include <linux/major.h>
32#endif
33
34#include "neercs.h"
35#include "mytrace.h"
36
37int grab_process(long pid, char *ptyname, int ptyfd)
38{
39#if defined HAVE_LINUX_KDEV_T_H
40    char fdstr[1024];
41    struct mytrace *parent, *child;
42    int i, fd = 0, ret;
43    char to_open[3];
44    int mode[3];
45    struct stat stat_buf;
46
47    debug("pty is %s", ptyname);
48
49    parent = mytrace_attach(pid);
50    if(!parent)
51    {
52        fprintf(stderr, "Cannot access process %ld\n", pid);
53        return -1;
54    }
55
56    child = mytrace_fork(parent);
57    mytrace_exec(parent, "/usr/bin/reset");
58    mytrace_exit(parent, 0); /* In case the execve failed */
59    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);
62    pid = mytrace_getpid(child);
63
64    /* Look for file descriptors that are PTYs */
65    for(i = 0; i <= 2; i++)
66    {
67        snprintf(fdstr, sizeof(fdstr), "/proc/%ld/fd/%d", pid, i);
68        to_open[i] = 0;
69        lstat(fdstr, &stat_buf);
70        if((stat_buf.st_mode & S_IRUSR) && (stat_buf.st_mode & S_IWUSR))
71            mode[i] = O_RDWR;
72        else if(stat_buf.st_mode & S_IWUSR)
73            mode[i] = O_WRONLY;
74        else
75            mode[i] = O_RDONLY;
76
77        if(stat(fdstr, &stat_buf) < 0)
78            continue;
79
80        if(!S_ISCHR(stat_buf.st_mode)
81            || MAJOR(stat_buf.st_rdev) != UNIX98_PTY_SLAVE_MAJOR)
82            continue;
83
84        debug("found pty %d for pid %d", i, pid);
85
86        ret = mytrace_close(child, i);
87        if(ret < 0)
88        {
89            perror("mytrace_close");
90        }
91        to_open[i] = 1;
92    }
93
94    /* Set the process's session ID */
95    debug("Running setsid on process %ld (sid=%d)", pid, getsid(pid));
96
97    ret = mytrace_setpgid(child, 0, getsid(pid));
98    if(ret < 0)
99    {
100        fprintf(stderr, "syscall setpgid failed\n");
101        mytrace_detach(child);
102        return -1;
103    }
104
105    if(ret != 0)
106    {
107        fprintf(stderr, "setpgid returned %d\n", ret);
108        mytrace_detach(child);
109        return -1;
110    }
111
112    ret = mytrace_setsid(child);
113    if(ret < 0)
114    {
115        fprintf(stderr, "syscall setsid failed\n");
116        mytrace_detach(child);
117        return -1;
118    }
119
120    debug("pid %ld has now sid %d", pid, getsid(pid));
121
122    /* Reopen PTY file descriptors */
123    for(i = 0; i <= 2; i++)
124    {
125        if(!to_open[i])
126            continue;
127        fd = mytrace_open(child, ptyname, mode[i]);
128        if(fd < 0)
129        {
130            perror("mytrace_open");
131        }
132        ret = mytrace_dup2(child, fd, i);
133        if(ret < 0)
134        {
135            perror("mytrace_dup2");
136        }
137    }
138
139    kill(pid, SIGWINCH);
140    mytrace_detach(child);
141
142    return 0;
143#else
144    errno = ENOSYS;
145    return -1;
146#endif
147}
148
Note: See TracBrowser for help on using the repository browser.