author | viric@llimona |
Thu, 27 Sep 2007 00:25:54 +0200 | |
changeset 58 | 2cf8c513d18f |
parent 53 | 07500c5c53cb |
child 71 | c209487034d7 |
permissions | -rw-r--r-- |
53 | 1 |
/* |
2 |
Terminal Mixer - multi-point multi-user access to terminal applications |
|
3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
|
4 |
||
5 |
Please find the license in the provided COPYING file. |
|
6 |
*/ |
|
23 | 7 |
#include <sys/types.h> |
8 |
#include <unistd.h> |
|
9 |
#include <errno.h> |
|
25 | 10 |
#include <fcntl.h> |
23 | 11 |
#include <string.h> |
12 |
#include <sys/select.h> |
|
25 | 13 |
#include <sys/ioctl.h> |
14 |
#include <termios.h> |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
47
diff
changeset
|
15 |
#ifdef __APPLE__ |
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
47
diff
changeset
|
16 |
#include <util.h> |
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
47
diff
changeset
|
17 |
#else |
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
47
diff
changeset
|
18 |
#include <pty.h> |
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
47
diff
changeset
|
19 |
#endif |
23 | 20 |
|
21 |
#include "main.h" |
|
22 |
||
41 | 23 |
|
24 |
struct winsize app_winsize; /* for xterm.c */ |
|
25 |
||
25 | 26 |
enum |
27 |
{ |
|
28 |
STDIN, |
|
29 |
STDOUT, |
|
30 |
STDERR |
|
31 |
}; |
|
32 |
||
33 |
static void set_close_on_exec(int fd) |
|
34 |
{ |
|
35 |
int res; |
|
36 |
res = fcntl(fd, F_SETFD, FD_CLOEXEC); |
|
37 |
if (res == -1) |
|
38 |
error("Set close-on-exec failed"); |
|
39 |
} |
|
40 |
||
41 |
static void give_terminal(int parent[], int child[]) |
|
42 |
{ |
|
43 |
struct termios tios; |
|
44 |
struct winsize wsize; |
|
45 |
int res; |
|
46 |
int master, slave; |
|
47 |
||
28
b73712b8370d
use the termios calls to stdout instead of stdin.
viric@llimona
parents:
26
diff
changeset
|
48 |
res = tcgetattr(1, &tios); |
25 | 49 |
if (res == -1) |
50 |
error("tcgetatttr"); |
|
51 |
||
41 | 52 |
/* if starting using shells' &, it doesn't get ECHO set. |
53 |
* Let's force it. */ |
|
54 |
tios.c_lflag |= ECHO | ECHOE | ECHOK | ECHONL; |
|
55 |
||
28
b73712b8370d
use the termios calls to stdout instead of stdin.
viric@llimona
parents:
26
diff
changeset
|
56 |
res = ioctl(1, TIOCGWINSZ, &wsize); |
25 | 57 |
if (res == -1) |
58 |
error("ioctl TIOCGWINSZ"); |
|
59 |
||
41 | 60 |
app_winsize = wsize; |
61 |
||
25 | 62 |
res = openpty(&master, &slave, 0, &tios, &wsize); |
63 |
if (res == -1) |
|
64 |
error("openpty"); |
|
65 |
||
66 |
parent[STDIN] = master; |
|
67 |
parent[STDOUT] = master; |
|
68 |
parent[STDERR] = master; |
|
69 |
child[STDIN] = slave; |
|
70 |
child[STDOUT] = slave; |
|
71 |
child[STDERR] = slave; |
|
72 |
set_close_on_exec(master); |
|
73 |
set_close_on_exec(slave); |
|
74 |
} |
|
75 |
||
76 |
static void give_pipes(int parent[], int child[]) |
|
77 |
{ |
|
78 |
int p_stdin[2]; /* from us to mpg321 */ |
|
79 |
int p_stdout[2]; /* from mpg321 to us */ |
|
80 |
int p_stderr[2]; /* from mpg321 to us, its stderr */ |
|
81 |
||
82 |
pipe(p_stdin); |
|
83 |
pipe(p_stdout); |
|
84 |
pipe(p_stderr); |
|
85 |
||
86 |
parent[STDIN] = p_stdin[1]; |
|
87 |
parent[STDOUT] = p_stdout[0]; |
|
88 |
parent[STDERR] = p_stderr[0]; |
|
89 |
child[STDIN] = p_stdin[0]; |
|
90 |
child[STDOUT] = p_stdout[1]; |
|
91 |
child[STDERR] = p_stderr[1]; |
|
92 |
set_close_on_exec(parent[STDIN]); |
|
93 |
set_close_on_exec(parent[STDOUT]); |
|
94 |
set_close_on_exec(parent[STDERR]); |
|
95 |
set_close_on_exec(child[STDIN]); |
|
96 |
set_close_on_exec(child[STDOUT]); |
|
97 |
set_close_on_exec(child[STDERR]); |
|
98 |
} |
|
99 |
||
23 | 100 |
int fork_app(char * const command[]) |
101 |
{ |
|
25 | 102 |
int p_parent[3]; |
103 |
int p_child[3]; |
|
23 | 104 |
int pid; |
105 |
int res; |
|
106 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
28
diff
changeset
|
107 |
if (command_line.s_param.run_in_subterminal) |
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
108 |
{ |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
109 |
give_terminal(p_parent, p_child); |
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
110 |
} |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
111 |
else |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
112 |
give_pipes(p_parent, p_child); |
25 | 113 |
|
114 |
/* globals */ |
|
115 |
app_stdin = p_parent[STDIN]; /* For us to write */ |
|
116 |
app_stdout = p_parent[STDOUT]; /* For us to read */ |
|
117 |
app_stderr = p_parent[STDERR]; /* For us to read */ |
|
23 | 118 |
|
119 |
pid = fork(); |
|
120 |
||
121 |
switch(pid) |
|
122 |
{ |
|
123 |
case 0: /* child */ |
|
25 | 124 |
res = dup2(p_child[STDIN], 0); |
125 |
if (res == -1) error("Dup2 stdin"); |
|
23 | 126 |
|
25 | 127 |
res = dup2(p_child[STDOUT], 1); |
128 |
if (res == -1) error("Dup2 stdout"); |
|
129 |
||
130 |
res = dup2(p_child[STDERR], 2); |
|
131 |
if (res == -1) error("Dup2 stderr"); |
|
23 | 132 |
|
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
133 |
if (command_line.s_param.run_in_subterminal) |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
134 |
{ |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
135 |
int res; |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
136 |
res = setsid(); |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
137 |
if (res < 0) |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
138 |
error("failed setsid()"); |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
139 |
res = ioctl(0, TIOCSCTTY, 0); |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
140 |
if (res < 0) |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
141 |
error("failed ioctl(0,TIOCSCTTY,0)"); |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
142 |
} |
23 | 143 |
execvp(command[0], command); |
144 |
||
25 | 145 |
error("Cannot execvp %s", command[0]); |
23 | 146 |
case -1: |
147 |
error("Failed fork"); |
|
148 |
default: /* parent */ |
|
25 | 149 |
close(p_child[STDIN]); |
150 |
close(p_child[STDOUT]); |
|
151 |
close(p_child[STDERR]); |
|
23 | 152 |
} |
153 |
||
154 |
return pid; |
|
155 |
} |
|
156 |