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

Last change on this file since 3116 was 3116, checked in by Pascal Terjan, 12 years ago
  • Don't exit the process if exec suceeded
  • Property svn:eol-style set to native
File size: 3.4 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    ret = mytrace_exec(parent, "/usr/bin/reset");
58    if(ret < 0)
59        mytrace_exit(parent, 0);
60    mytrace_detach(parent);
61    mytrace_write(child, 1, "\x1b[H\x1b[2J", 7);
62    mytrace_write(child, 1, "\n[Process stolen by neercs]\n", 28);
63    pid = mytrace_getpid(child);
64
65    /* Look for file descriptors that are PTYs */
66    for(i = 0; i <= 2; i++)
67    {
68        snprintf(fdstr, sizeof(fdstr), "/proc/%ld/fd/%d", pid, i);
69        to_open[i] = 0;
70        lstat(fdstr, &stat_buf);
71        if((stat_buf.st_mode & S_IRUSR) && (stat_buf.st_mode & S_IWUSR))
72            mode[i] = O_RDWR;
73        else if(stat_buf.st_mode & S_IWUSR)
74            mode[i] = O_WRONLY;
75        else
76            mode[i] = O_RDONLY;
77
78        if(stat(fdstr, &stat_buf) < 0)
79            continue;
80
81        if(!S_ISCHR(stat_buf.st_mode)
82            || MAJOR(stat_buf.st_rdev) != UNIX98_PTY_SLAVE_MAJOR)
83            continue;
84
85        debug("found pty %d for pid %d", i, pid);
86
87        ret = mytrace_close(child, i);
88        if(ret < 0)
89        {
90            perror("mytrace_close");
91        }
92        to_open[i] = 1;
93    }
94
95    /* Set the process's session ID */
96    debug("Running setsid on process %ld (sid=%d)", pid, getsid(pid));
97
98    ret = mytrace_setpgid(child, 0, getsid(pid));
99    if(ret < 0)
100    {
101        fprintf(stderr, "syscall setpgid failed\n");
102        mytrace_detach(child);
103        return -1;
104    }
105
106    if(ret != 0)
107    {
108        fprintf(stderr, "setpgid returned %d\n", ret);
109        mytrace_detach(child);
110        return -1;
111    }
112
113    ret = mytrace_setsid(child);
114    if(ret < 0)
115    {
116        fprintf(stderr, "syscall setsid failed\n");
117        mytrace_detach(child);
118        return -1;
119    }
120
121    debug("pid %ld has now sid %d", pid, getsid(pid));
122
123    /* Reopen PTY file descriptors */
124    for(i = 0; i <= 2; i++)
125    {
126        if(!to_open[i])
127            continue;
128        fd = mytrace_open(child, ptyname, mode[i]);
129        if(fd < 0)
130        {
131            perror("mytrace_open");
132        }
133        ret = mytrace_dup2(child, fd, i);
134        if(ret < 0)
135        {
136            perror("mytrace_dup2");
137        }
138    }
139
140    kill(pid, SIGWINCH);
141    mytrace_detach(child);
142
143    return 0;
144#else
145    errno = ENOSYS;
146    return -1;
147#endif
148}
149
Note: See TracBrowser for help on using the repository browser.