author | Lluís Batlle <viric@viric.name> |
Thu, 20 Mar 2014 16:33:27 +0100 | |
changeset 97 | eea77d5a624c |
parent 95 | 13360d8af313 |
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 <stdio.h> |
8 |
#include <stdlib.h> |
|
9 |
#include <sys/types.h> |
|
10 |
#include <sys/select.h> |
|
11 |
#include <unistd.h> |
|
12 |
#include <errno.h> |
|
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
13 |
#include <signal.h> |
23 | 14 |
#include <string.h> |
94
330324fc7c20
Adding a feature: save all stdin/stdout traffic of the server into a file.
viric <viriketo@gmail.com>
parents:
86
diff
changeset
|
15 |
#include <fcntl.h> |
23 | 16 |
|
17 |
#include "main.h" |
|
18 |
#include "handlers.h" |
|
19 |
||
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
20 |
/* signals.c */ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
21 |
extern int child_died; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
22 |
|
94
330324fc7c20
Adding a feature: save all stdin/stdout traffic of the server into a file.
viric <viriketo@gmail.com>
parents:
86
diff
changeset
|
23 |
/* app_control.c */ |
330324fc7c20
Adding a feature: save all stdin/stdout traffic of the server into a file.
viric <viriketo@gmail.com>
parents:
86
diff
changeset
|
24 |
extern int savefile; |
330324fc7c20
Adding a feature: save all stdin/stdout traffic of the server into a file.
viric <viriketo@gmail.com>
parents:
86
diff
changeset
|
25 |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
26 |
/*extern*/ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
27 |
void fdset_dump(fd_set *set, int maxfd); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
28 |
|
23 | 29 |
static void loop() |
30 |
{ |
|
31 |
fd_set read_set; |
|
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
32 |
sigset_t sigempty,signoalarm; |
23 | 33 |
int maxfd; |
34 |
int stdin_opened; |
|
35 |
int res; |
|
36 |
||
41 | 37 |
if (command_line.s_param.nohup) |
38 |
stdin_opened = 0; |
|
39 |
else |
|
40 |
stdin_opened = 1; |
|
41 |
||
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
42 |
/* Prepare the sigset for blocking alarm */ |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
43 |
sigemptyset(&signoalarm); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
44 |
sigaddset(&signoalarm, SIGALRM); |
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
45 |
sigemptyset(&sigempty); |
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
46 |
|
23 | 47 |
do |
48 |
{ |
|
49 |
FD_ZERO(&read_set); |
|
50 |
||
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
51 |
maxfd = -1; |
23 | 52 |
if (stdin_opened) |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
53 |
{ |
23 | 54 |
FD_SET(0, &read_set); |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
55 |
maxfd = 0; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
56 |
} |
23 | 57 |
|
58 |
app_control_prepare_read_fdset(&read_set, &maxfd); |
|
33 | 59 |
if (command_line.s_param.serve_unix) |
60 |
s_unix_prepare_read_fdset(&read_set, &maxfd); |
|
61 |
if (command_line.s_param.serve_tcp) |
|
62 |
s_tcp_prepare_read_fdset(&read_set, &maxfd); |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
63 |
#ifdef linux |
44 | 64 |
if (command_line.s_param.serve_eth) |
65 |
s_eth_prepare_read_fdset(&read_set, &maxfd); |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
66 |
#endif /* linux */ |
23 | 67 |
|
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
68 |
/* Prepare checking for timeout */ |
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
69 |
sigprocmask(SIG_BLOCK, &signoalarm, 0); |
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
70 |
|
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
71 |
#ifdef linux |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
72 |
if (command_line.s_param.serve_eth) |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
73 |
/* This will check if the timeout occurred, |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
74 |
* and will not do anything otherwise. */ |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
75 |
eth_proto_process_timeouts(); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
76 |
#endif |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
77 |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
78 |
if (maxfd == -1) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
79 |
error("No fds in select"); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
80 |
|
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
81 |
debugmsg("Blocking in pselect"); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
82 |
if (child_died == 1) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
83 |
break; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
84 |
|
23 | 85 |
/* Will block */ |
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
86 |
res = pselect(maxfd + 1, &read_set, 0, 0, 0, &sigempty); |
66
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
87 |
#ifdef linux |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
88 |
if (command_line.s_param.serve_eth) |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
89 |
eth_proto_process_timeouts(); |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
90 |
#endif |
23 | 91 |
if (res == -1) |
92 |
{ |
|
93 |
if (errno == EINTR) |
|
84
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
94 |
{ |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
95 |
dump_line("Signal received in server select()\n"); |
23 | 96 |
continue; |
84
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
97 |
} |
23 | 98 |
else |
99 |
error("Error in select()"); |
|
100 |
} |
|
101 |
||
102 |
res = app_control_process_read_fdset(&read_set); |
|
103 |
if (res == -1) /* app_stdout and app_stderr closed */ |
|
104 |
break; |
|
105 |
||
41 | 106 |
if (stdin_opened && FD_ISSET(0, &read_set)) |
23 | 107 |
{ |
108 |
res = read(0, stream_buffer, stream_buffer_size); |
|
32
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
29
diff
changeset
|
109 |
/* if res is 0, the fcall will close app_stdin */ |
df110f784648
Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents:
29
diff
changeset
|
110 |
app_control_local_send_to_stdin(stream_buffer, res); |
23 | 111 |
if (res == 0) |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
112 |
{ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
113 |
debugmsg("Closing stdin\n"); |
23 | 114 |
stdin_opened = 0; |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
115 |
} |
23 | 116 |
} |
117 |
||
33 | 118 |
if (command_line.s_param.serve_unix) |
119 |
s_unix_process_read_fdset(&read_set); |
|
120 |
if (command_line.s_param.serve_tcp) |
|
121 |
s_tcp_process_read_fdset(&read_set); |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
122 |
#ifdef linux |
44 | 123 |
if (command_line.s_param.serve_eth) |
124 |
s_eth_process_read_fdset(&read_set); |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
125 |
#endif /* linux */ |
23 | 126 |
} while(1); |
127 |
} |
|
128 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
129 |
int server() |
23 | 130 |
{ |
131 |
int child; |
|
132 |
||
133 |
command_line.is_server = 1; |
|
134 |
||
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
135 |
app_control_start(); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
136 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
137 |
/* in raw mode, the signals for Control-C, ... will be generated by the |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
138 |
* slave pty. The master will receive the key codes. */ |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
139 |
if (command_line.s_param.serve_unix) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
140 |
s_unix_update_served(command_line.s_param.max_served); |
23 | 141 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
142 |
if (command_line.s_param.serve_tcp) |
33 | 143 |
s_tcp_update_served(command_line.s_param.max_served); |
23 | 144 |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
145 |
#ifdef linux |
44 | 146 |
if (command_line.s_param.serve_eth) |
147 |
s_eth_init(); |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
148 |
#endif /* linux */ |
44 | 149 |
|
94
330324fc7c20
Adding a feature: save all stdin/stdout traffic of the server into a file.
viric <viriketo@gmail.com>
parents:
86
diff
changeset
|
150 |
if (command_line.s_param.savefile) |
330324fc7c20
Adding a feature: save all stdin/stdout traffic of the server into a file.
viric <viriketo@gmail.com>
parents:
86
diff
changeset
|
151 |
{ |
95
13360d8af313
Fixing a problem on savefile (stdin was not saved well, and the file not
viric <viriketo@gmail.com>
parents:
94
diff
changeset
|
152 |
savefile = open(command_line.s_param.savefile, |
13360d8af313
Fixing a problem on savefile (stdin was not saved well, and the file not
viric <viriketo@gmail.com>
parents:
94
diff
changeset
|
153 |
O_CREAT | O_TRUNC | O_RDWR, 0666); |
94
330324fc7c20
Adding a feature: save all stdin/stdout traffic of the server into a file.
viric <viriketo@gmail.com>
parents:
86
diff
changeset
|
154 |
} |
330324fc7c20
Adding a feature: save all stdin/stdout traffic of the server into a file.
viric <viriketo@gmail.com>
parents:
86
diff
changeset
|
155 |
|
40 | 156 |
child = fork_app(command_line.s_param.command); |
157 |
||
41 | 158 |
if (command_line.s_param.nohup) |
159 |
{ |
|
71
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
67
diff
changeset
|
160 |
ignore_sighup(); |
41 | 161 |
close(0); |
162 |
close(1); |
|
163 |
close(2); |
|
164 |
} else |
|
165 |
if (command_line.s_param.run_in_subterminal) |
|
166 |
prepare_user_terminal(); |
|
40 | 167 |
|
168 |
install_signal_forwarders(child); |
|
169 |
||
23 | 170 |
loop(); |
171 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
172 |
if (command_line.s_param.serve_unix) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
173 |
s_unix_shutdown(); |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
174 |
|
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
175 |
if (command_line.s_param.serve_tcp) |
33 | 176 |
s_tcp_shutdown(); |
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
177 |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
178 |
#ifdef linux |
44 | 179 |
if (command_line.s_param.serve_eth) |
180 |
s_eth_shutdown(); |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
181 |
#endif /* linux */ |
44 | 182 |
|
41 | 183 |
if (!command_line.s_param.nohup && |
184 |
command_line.s_param.run_in_subterminal) |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
185 |
restore_user_terminal(); |
23 | 186 |
|
187 |
return 0; |
|
188 |
} |