author | viric@mandarina |
Mon, 28 Apr 2008 21:37:16 +0200 | |
changeset 89 | 2692e4742267 |
parent 82 | 5cbe47923060 |
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 |
||
74 | 41 |
void pass_winsize_to_slave() |
42 |
{ |
|
43 |
int res; |
|
44 |
res = ioctl(0, TIOCGWINSZ, (char *) &app_winsize); |
|
45 |
if (res >= 0) |
|
46 |
ioctl(app_stdin, TIOCSWINSZ, (char *) &app_winsize); |
|
47 |
} |
|
48 |
||
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
49 |
void get_term_options(struct termios *tios, struct winsize *wsize) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
50 |
{ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
51 |
int res; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
52 |
|
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
53 |
/* Try to get the config from the actual terminal's stdout */ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
54 |
res = tcgetattr(1, tios); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
55 |
if (res == -1) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
56 |
{ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
57 |
/* Create the termios structure from scratch */ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
58 |
memset(tios, 0, sizeof(*tios)); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
59 |
cfmakeraw(tios); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
60 |
} |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
61 |
|
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
62 |
/* if starting using shells' &, it doesn't get ECHO set. |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
63 |
* Let's force it. */ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
64 |
tios->c_lflag |= ECHO | ECHOE | ECHOK | ECHONL; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
65 |
/* Try to emulate something compatible with xterm */ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
66 |
tios->c_oflag |= OPOST | ONLCR; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
67 |
tios->c_lflag |= ISIG | ICANON | ECHOCTL | ECHOKE; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
68 |
tios->c_iflag |= BRKINT | IGNPAR | IMAXBEL; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
69 |
|
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
70 |
/* Get the original term's winsize */ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
71 |
res = ioctl(1, TIOCGWINSZ, wsize); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
72 |
if (res == -1) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
73 |
{ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
74 |
/* If we don't have original term, create an arbitrary winsize */ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
75 |
wsize->ws_row = 25; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
76 |
wsize->ws_col = 80; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
77 |
} |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
78 |
} |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
79 |
|
25 | 80 |
static void give_terminal(int parent[], int child[]) |
81 |
{ |
|
82 |
struct termios tios; |
|
83 |
struct winsize wsize; |
|
84 |
int res; |
|
85 |
int master, slave; |
|
86 |
||
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
87 |
get_term_options(&tios, &wsize); |
25 | 88 |
|
41 | 89 |
app_winsize = wsize; |
90 |
||
25 | 91 |
res = openpty(&master, &slave, 0, &tios, &wsize); |
92 |
if (res == -1) |
|
93 |
error("openpty"); |
|
94 |
||
95 |
parent[STDIN] = master; |
|
96 |
parent[STDOUT] = master; |
|
97 |
parent[STDERR] = master; |
|
98 |
child[STDIN] = slave; |
|
99 |
child[STDOUT] = slave; |
|
100 |
child[STDERR] = slave; |
|
101 |
set_close_on_exec(master); |
|
102 |
set_close_on_exec(slave); |
|
103 |
} |
|
104 |
||
105 |
static void give_pipes(int parent[], int child[]) |
|
106 |
{ |
|
107 |
int p_stdin[2]; /* from us to mpg321 */ |
|
108 |
int p_stdout[2]; /* from mpg321 to us */ |
|
109 |
int p_stderr[2]; /* from mpg321 to us, its stderr */ |
|
110 |
||
111 |
pipe(p_stdin); |
|
112 |
pipe(p_stdout); |
|
113 |
pipe(p_stderr); |
|
114 |
||
115 |
parent[STDIN] = p_stdin[1]; |
|
116 |
parent[STDOUT] = p_stdout[0]; |
|
117 |
parent[STDERR] = p_stderr[0]; |
|
118 |
child[STDIN] = p_stdin[0]; |
|
119 |
child[STDOUT] = p_stdout[1]; |
|
120 |
child[STDERR] = p_stderr[1]; |
|
121 |
set_close_on_exec(parent[STDIN]); |
|
122 |
set_close_on_exec(parent[STDOUT]); |
|
123 |
set_close_on_exec(parent[STDERR]); |
|
124 |
set_close_on_exec(child[STDIN]); |
|
125 |
set_close_on_exec(child[STDOUT]); |
|
126 |
set_close_on_exec(child[STDERR]); |
|
127 |
} |
|
128 |
||
23 | 129 |
int fork_app(char * const command[]) |
130 |
{ |
|
25 | 131 |
int p_parent[3]; |
132 |
int p_child[3]; |
|
23 | 133 |
int pid; |
134 |
int res; |
|
135 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
28
diff
changeset
|
136 |
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
|
137 |
{ |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
138 |
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
|
139 |
} |
26
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
140 |
else |
96920c3707b3
Unix sockets version works! Well, signals don't. :)
viric@llimona
parents:
25
diff
changeset
|
141 |
give_pipes(p_parent, p_child); |
25 | 142 |
|
143 |
/* globals */ |
|
144 |
app_stdin = p_parent[STDIN]; /* For us to write */ |
|
145 |
app_stdout = p_parent[STDOUT]; /* For us to read */ |
|
146 |
app_stderr = p_parent[STDERR]; /* For us to read */ |
|
23 | 147 |
|
148 |
pid = fork(); |
|
149 |
||
150 |
switch(pid) |
|
151 |
{ |
|
152 |
case 0: /* child */ |
|
71
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
53
diff
changeset
|
153 |
if (command_line.s_param.nohup) |
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
53
diff
changeset
|
154 |
ignore_sighup(); |
25 | 155 |
res = dup2(p_child[STDIN], 0); |
156 |
if (res == -1) error("Dup2 stdin"); |
|
23 | 157 |
|
25 | 158 |
res = dup2(p_child[STDOUT], 1); |
159 |
if (res == -1) error("Dup2 stdout"); |
|
160 |
||
161 |
res = dup2(p_child[STDERR], 2); |
|
162 |
if (res == -1) error("Dup2 stderr"); |
|
23 | 163 |
|
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
164 |
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
|
165 |
{ |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
166 |
int res; |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
167 |
res = setsid(); |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
41
diff
changeset
|
168 |
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
|
169 |
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
|
170 |
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
|
171 |
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
|
172 |
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
|
173 |
} |
23 | 174 |
execvp(command[0], command); |
175 |
||
25 | 176 |
error("Cannot execvp %s", command[0]); |
23 | 177 |
case -1: |
178 |
error("Failed fork"); |
|
179 |
default: /* parent */ |
|
25 | 180 |
close(p_child[STDIN]); |
181 |
close(p_child[STDOUT]); |
|
182 |
close(p_child[STDERR]); |
|
23 | 183 |
} |
184 |
||
185 |
return pid; |
|
186 |
} |
|
187 |