author | viric@llimona |
Mon, 08 Oct 2007 12:59:27 +0200 | |
changeset 79 | 7d316733d4b1 |
parent 71 | c209487034d7 |
child 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 <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 |
||
19 |
static void loop() |
|
20 |
{ |
|
21 |
fd_set read_set; |
|
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
22 |
sigset_t sigold,signoalarm; |
23 | 23 |
int maxfd; |
24 |
int stdin_opened; |
|
25 |
int res; |
|
26 |
||
41 | 27 |
if (command_line.s_param.nohup) |
28 |
stdin_opened = 0; |
|
29 |
else |
|
30 |
stdin_opened = 1; |
|
31 |
||
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
32 |
/* 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
|
33 |
sigemptyset(&signoalarm); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
34 |
sigaddset(&signoalarm, SIGALRM); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
35 |
|
23 | 36 |
do |
37 |
{ |
|
38 |
FD_ZERO(&read_set); |
|
39 |
||
40 |
if (stdin_opened) |
|
41 |
FD_SET(0, &read_set); |
|
42 |
maxfd = 0; |
|
43 |
||
44 |
app_control_prepare_read_fdset(&read_set, &maxfd); |
|
33 | 45 |
if (command_line.s_param.serve_unix) |
46 |
s_unix_prepare_read_fdset(&read_set, &maxfd); |
|
47 |
if (command_line.s_param.serve_tcp) |
|
48 |
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
|
49 |
#ifdef linux |
44 | 50 |
if (command_line.s_param.serve_eth) |
51 |
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
|
52 |
#endif /* linux */ |
23 | 53 |
|
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
54 |
/* Prepare checking for timeout */ |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
55 |
sigprocmask(SIG_BLOCK, &signoalarm, &sigold); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
56 |
|
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
57 |
#ifdef linux |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
58 |
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
|
59 |
/* 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
|
60 |
* 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
|
61 |
eth_proto_process_timeouts(); |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
62 |
#endif |
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
63 |
|
23 | 64 |
/* Will block */ |
67
d7405e4f12e1
The timeout should be always handled. Now we use pselect in the loops.
viric@mandarina
parents:
66
diff
changeset
|
65 |
res = pselect(maxfd + 1, &read_set, 0, 0, 0, &sigold); |
66
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
66 |
#ifdef linux |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
67 |
if (command_line.s_param.serve_eth) |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
68 |
eth_proto_process_timeouts(); |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
53
diff
changeset
|
69 |
#endif |
23 | 70 |
if (res == -1) |
71 |
{ |
|
72 |
if (errno == EINTR) |
|
73 |
continue; |
|
74 |
else |
|
75 |
error("Error in select()"); |
|
76 |
} |
|
77 |
||
78 |
res = app_control_process_read_fdset(&read_set); |
|
79 |
if (res == -1) /* app_stdout and app_stderr closed */ |
|
80 |
break; |
|
81 |
||
41 | 82 |
if (stdin_opened && FD_ISSET(0, &read_set)) |
23 | 83 |
{ |
84 |
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
|
85 |
/* 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
|
86 |
app_control_local_send_to_stdin(stream_buffer, res); |
23 | 87 |
if (res == 0) |
88 |
stdin_opened = 0; |
|
89 |
} |
|
90 |
||
33 | 91 |
if (command_line.s_param.serve_unix) |
92 |
s_unix_process_read_fdset(&read_set); |
|
93 |
if (command_line.s_param.serve_tcp) |
|
94 |
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
|
95 |
#ifdef linux |
44 | 96 |
if (command_line.s_param.serve_eth) |
97 |
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
|
98 |
#endif /* linux */ |
23 | 99 |
} while(1); |
100 |
} |
|
101 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
102 |
int server() |
23 | 103 |
{ |
104 |
int child; |
|
105 |
||
106 |
command_line.is_server = 1; |
|
107 |
||
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
108 |
app_control_start(); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
109 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
110 |
/* 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
|
111 |
* 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
|
112 |
if (command_line.s_param.serve_unix) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
113 |
s_unix_update_served(command_line.s_param.max_served); |
23 | 114 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
115 |
if (command_line.s_param.serve_tcp) |
33 | 116 |
s_tcp_update_served(command_line.s_param.max_served); |
23 | 117 |
|
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_init(); |
|
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 */ |
44 | 122 |
|
40 | 123 |
child = fork_app(command_line.s_param.command); |
124 |
||
41 | 125 |
if (command_line.s_param.nohup) |
126 |
{ |
|
71
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
67
diff
changeset
|
127 |
ignore_sighup(); |
41 | 128 |
close(0); |
129 |
close(1); |
|
130 |
close(2); |
|
131 |
} else |
|
132 |
if (command_line.s_param.run_in_subterminal) |
|
133 |
prepare_user_terminal(); |
|
40 | 134 |
|
135 |
install_signal_forwarders(child); |
|
136 |
||
23 | 137 |
loop(); |
138 |
||
29
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_shutdown(); |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
141 |
|
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_shutdown(); |
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
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_shutdown(); |
|
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 |
|
41 | 150 |
if (!command_line.s_param.nohup && |
151 |
command_line.s_param.run_in_subterminal) |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
152 |
restore_user_terminal(); |
23 | 153 |
|
154 |
return 0; |
|
155 |
} |