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

Last change on this file since 2512 was 2512, checked in by sam, 6 years ago
  • Add missing headers and _XOPEN_SOURCE definition to grab.c.
  • Property svn:eol-style set to native
File size: 3.1 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 *t;
42    int i, fd = 0, mode, ret;
43    char to_open[3];
44    struct stat stat_buf;
45
46    debug("pty is %s", ptyname);
47
48    t = mytrace_attach(pid);
49    if(!t)
50    {
51        fprintf(stderr, "Cannot access process %ld\n", pid);
52        return -1;
53    }
54
55    /* Look for file descriptors that are PTYs */
56    for(i = 0; i <= 2; i++)
57    {
58        snprintf(fdstr, sizeof(fdstr), "/proc/%ld/fd/%d", pid, i);
59        to_open[i] = 0;
60        lstat(fdstr, &stat_buf);
61        if((stat_buf.st_mode & S_IRUSR) && (stat_buf.st_mode & S_IWUSR))
62            mode = O_RDWR;
63        else if(stat_buf.st_mode & S_IWUSR)
64            mode = O_WRONLY;
65        else
66            mode = O_RDONLY;
67
68        if(stat(fdstr, &stat_buf) < 0)
69            continue;
70
71        if(!S_ISCHR(stat_buf.st_mode)
72            || MAJOR(stat_buf.st_rdev) != UNIX98_PTY_SLAVE_MAJOR)
73            continue;
74
75        debug("found pty %d", i);
76
77        ret = mytrace_close(t, i);
78        if(ret < 0)
79        {
80            perror("mytrace_close");
81        }
82        to_open[i] = 1;
83    }
84
85    /* Set the process's session ID */
86    debug("Running setsid on process %ld (sid=%d)", pid, getsid(pid));
87
88    ret = mytrace_setpgid(t, 0, getsid(pid));
89    if(ret < 0)
90    {
91        fprintf(stderr, "syscall setpgid failed\n");
92        mytrace_detach(t);
93        return -1;
94    }
95
96    if(ret != 0)
97    {
98        fprintf(stderr, "setpgid returned %d\n", ret);
99        mytrace_detach(t);
100        return -1;
101    }
102
103    ret = mytrace_setsid(t);
104    if(ret < 0)
105    {
106        fprintf(stderr, "syscall setsid failed\n");
107        mytrace_detach(t);
108        return -1;
109    }
110
111    debug("pid %ld has now sid %d", pid, getsid(pid));
112
113    /* Reopen PTY file descriptors */
114    for(i = 0; i <= 2; i++)
115    {
116        if(!to_open[i])
117            continue;
118        fd = mytrace_open(t, ptyname, mode);
119        if(fd < 0)
120        {
121            perror("mytrace_open");
122        }
123        ret = mytrace_dup2(t, fd, i);
124        if(ret < 0)
125        {
126            perror("mytrace_dup2");
127        }
128    }
129
130    kill(pid, SIGWINCH);
131
132    mytrace_detach(t);
133
134    return 0;
135#else
136    errno = ENOSYS;
137    return -1;
138#endif
139}
140
Note: See TracBrowser for help on using the repository browser.