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

Last change on this file since 2509 was 2509, checked in by Sam Hocevar, 13 years ago
  • Split grab.c into the grabbing heuristic and the pure ptrace interface, because we might use it for other things in the future.
  • Property svn:eol-style set to native
File size: 3.0 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#include <errno.h>
18#include <fcntl.h>
19#include <signal.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25#if defined HAVE_LINUX_KDEV_T_H
26#   include <linux/kdev_t.h>
27#   include <linux/major.h>
28#endif
29
30#include "neercs.h"
31#include "mytrace.h"
32
33int grab_process(long pid, char *ptyname, int ptyfd)
34{
35#if defined HAVE_LINUX_KDEV_T_H
36    char fdstr[1024];
37    struct mytrace *t;
38    int i, fd = 0, mode, ret;
39    char to_open[3];
40    struct stat stat_buf;
41
42    debug("pty is %s", ptyname);
43
44    t = mytrace_attach(pid);
45    if(!t)
46    {
47        fprintf(stderr, "Cannot access process %ld\n", pid);
48        return -1;
49    }
50
51    /* Look for file descriptors that are PTYs */
52    for(i = 0; i <= 2; i++)
53    {
54        snprintf(fdstr, sizeof(fdstr), "/proc/%ld/fd/%d", pid, i);
55        to_open[i] = 0;
56        lstat(fdstr, &stat_buf);
57        if((stat_buf.st_mode & S_IRUSR) && (stat_buf.st_mode & S_IWUSR))
58            mode = O_RDWR;
59        else if(stat_buf.st_mode & S_IWUSR)
60            mode = O_WRONLY;
61        else
62            mode = O_RDONLY;
63
64        if(stat(fdstr, &stat_buf) < 0)
65            continue;
66
67        if(!S_ISCHR(stat_buf.st_mode)
68            || MAJOR(stat_buf.st_rdev) != UNIX98_PTY_SLAVE_MAJOR)
69            continue;
70
71        debug("found pty %d", i);
72
73        ret = mytrace_close(t, i);
74        if(ret < 0)
75        {
76            perror("mytrace_close");
77        }
78        to_open[i] = 1;
79    }
80
81    /* Set the process's session ID */
82    debug("Running setsid on process %ld (sid=%d)", pid, getsid(pid));
83
84    ret = mytrace_setpgid(t, 0, getsid(pid));
85    if(ret < 0)
86    {
87        fprintf(stderr, "syscall setpgid failed\n");
88        mytrace_detach(t);
89        return -1;
90    }
91
92    if(ret != 0)
93    {
94        fprintf(stderr, "setpgid returned %d\n", ret);
95        mytrace_detach(t);
96        return -1;
97    }
98
99    ret = mytrace_setsid(t);
100    if(ret < 0)
101    {
102        fprintf(stderr, "syscall setsid failed\n");
103        mytrace_detach(t);
104        return -1;
105    }
106
107    debug("pid %ld has now sid %d", pid, getsid(pid));
108
109    /* Reopen PTY file descriptors */
110    for(i = 0; i <= 2; i++)
111    {
112        if(!to_open[i])
113            continue;
114        fd = mytrace_open(t, ptyname, mode);
115        if(fd < 0)
116        {
117            perror("mytrace_open");
118        }
119        ret = mytrace_dup2(t, fd, i);
120        if(ret < 0)
121        {
122            perror("mytrace_dup2");
123        }
124    }
125
126    kill(pid, SIGWINCH);
127
128    mytrace_detach(t);
129
130    return 0;
131#else
132    errno = ENOSYS;
133    return -1;
134#endif
135}
136
Note: See TracBrowser for help on using the repository browser.