author | viric@mandarina |
Mon, 28 Apr 2008 21:37:16 +0200 | |
changeset 89 | 2692e4742267 |
parent 86 | c972d3312fbd |
child 94 | 330324fc7c20 |
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> |
15 |
||
16 |
#include "main.h" |
|
17 |
#include "handlers.h" |
|
18 |
||
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
19 |
/* signals.c */ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
20 |
extern int child_died; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
21 |
|
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
22 |
/*extern*/ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
23 |
void fdset_dump(fd_set *set, int maxfd); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
24 |
|
23 | 25 |
static void loop() |
26 |
{ |
|
27 |
fd_set read_set; |
|
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
28 |
sigset_t sigempty,signoalarm; |
23 | 29 |
int maxfd; |
30 |
int stdin_opened; |
|
31 |
int res; |
|
32 |
||
41 | 33 |
if (command_line.s_param.nohup) |
34 |
stdin_opened = 0; |
|
35 |
else |
|
36 |
stdin_opened = 1; |
|
37 |
||
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
38 |
/* 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
|
39 |
sigemptyset(&signoalarm); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
40 |
sigaddset(&signoalarm, SIGALRM); |
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
41 |
sigemptyset(&sigempty); |
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
42 |
|
23 | 43 |
do |
44 |
{ |
|
45 |
FD_ZERO(&read_set); |
|
46 |
||
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
47 |
maxfd = -1; |
23 | 48 |
if (stdin_opened) |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
49 |
{ |
23 | 50 |
FD_SET(0, &read_set); |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
51 |
maxfd = 0; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
52 |
} |
23 | 53 |
|
54 |
app_control_prepare_read_fdset(&read_set, &maxfd); |
|
33 | 55 |
if (command_line.s_param.serve_unix) |
56 |
s_unix_prepare_read_fdset(&read_set, &maxfd); |
|
57 |
if (command_line.s_param.serve_tcp) |
|
58 |
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
|
59 |
#ifdef linux |
44 | 60 |
if (command_line.s_param.serve_eth) |
61 |
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
|
62 |
#endif /* linux */ |
23 | 63 |
|
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
64 |
/* Prepare checking for timeout */ |
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
65 |
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
|
66 |
|
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
67 |
#ifdef linux |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
68 |
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
|
69 |
/* 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
|
70 |
* 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
|
71 |
eth_proto_process_timeouts(); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
72 |
#endif |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
73 |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
74 |
if (maxfd == -1) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
75 |
error("No fds in select"); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
76 |
|
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
77 |
debugmsg("Blocking in pselect"); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
78 |
if (child_died == 1) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
79 |
break; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
80 |
|
23 | 81 |
/* Will block */ |
86
c972d3312fbd
Better variable names: sigold to sigempty
lbatlle@npdl268.bpo.hp.com
parents:
84
diff
changeset
|
82 |
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
|
83 |
#ifdef linux |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
84 |
if (command_line.s_param.serve_eth) |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
85 |
eth_proto_process_timeouts(); |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
86 |
#endif |
23 | 87 |
if (res == -1) |
88 |
{ |
|
89 |
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
|
90 |
{ |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
91 |
dump_line("Signal received in server select()\n"); |
23 | 92 |
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
|
93 |
} |
23 | 94 |
else |
95 |
error("Error in select()"); |
|
96 |
} |
|
97 |
||
98 |
res = app_control_process_read_fdset(&read_set); |
|
99 |
if (res == -1) /* app_stdout and app_stderr closed */ |
|
100 |
break; |
|
101 |
||
41 | 102 |
if (stdin_opened && FD_ISSET(0, &read_set)) |
23 | 103 |
{ |
104 |
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
|
105 |
/* 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
|
106 |
app_control_local_send_to_stdin(stream_buffer, res); |
23 | 107 |
if (res == 0) |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
108 |
{ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
109 |
debugmsg("Closing stdin\n"); |
23 | 110 |
stdin_opened = 0; |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
71
diff
changeset
|
111 |
} |
23 | 112 |
} |
113 |
||
33 | 114 |
if (command_line.s_param.serve_unix) |
115 |
s_unix_process_read_fdset(&read_set); |
|
116 |
if (command_line.s_param.serve_tcp) |
|
117 |
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
|
118 |
#ifdef linux |
44 | 119 |
if (command_line.s_param.serve_eth) |
120 |
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
|
121 |
#endif /* linux */ |
23 | 122 |
} while(1); |
123 |
} |
|
124 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
125 |
int server() |
23 | 126 |
{ |
127 |
int child; |
|
128 |
||
129 |
command_line.is_server = 1; |
|
130 |
||
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
131 |
app_control_start(); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
132 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
133 |
/* 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
|
134 |
* 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
|
135 |
if (command_line.s_param.serve_unix) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
136 |
s_unix_update_served(command_line.s_param.max_served); |
23 | 137 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
138 |
if (command_line.s_param.serve_tcp) |
33 | 139 |
s_tcp_update_served(command_line.s_param.max_served); |
23 | 140 |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
141 |
#ifdef linux |
44 | 142 |
if (command_line.s_param.serve_eth) |
143 |
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
|
144 |
#endif /* linux */ |
44 | 145 |
|
40 | 146 |
child = fork_app(command_line.s_param.command); |
147 |
||
41 | 148 |
if (command_line.s_param.nohup) |
149 |
{ |
|
71
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
67
diff
changeset
|
150 |
ignore_sighup(); |
41 | 151 |
close(0); |
152 |
close(1); |
|
153 |
close(2); |
|
154 |
} else |
|
155 |
if (command_line.s_param.run_in_subterminal) |
|
156 |
prepare_user_terminal(); |
|
40 | 157 |
|
158 |
install_signal_forwarders(child); |
|
159 |
||
23 | 160 |
loop(); |
161 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
162 |
if (command_line.s_param.serve_unix) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
163 |
s_unix_shutdown(); |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
164 |
|
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
165 |
if (command_line.s_param.serve_tcp) |
33 | 166 |
s_tcp_shutdown(); |
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
167 |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
168 |
#ifdef linux |
44 | 169 |
if (command_line.s_param.serve_eth) |
170 |
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
|
171 |
#endif /* linux */ |
44 | 172 |
|
41 | 173 |
if (!command_line.s_param.nohup && |
174 |
command_line.s_param.run_in_subterminal) |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
175 |
restore_user_terminal(); |
23 | 176 |
|
177 |
return 0; |
|
178 |
} |