author | viric@llimona |
Thu, 27 Sep 2007 00:25:54 +0200 | |
changeset 58 | 2cf8c513d18f |
parent 53 | 07500c5c53cb |
child 66 | b2469563a1dc |
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> |
|
13 |
#include <string.h> |
|
14 |
||
15 |
#include "main.h" |
|
16 |
#include "handlers.h" |
|
17 |
||
18 |
static void loop() |
|
19 |
{ |
|
20 |
fd_set read_set; |
|
21 |
int maxfd; |
|
22 |
int stdin_opened; |
|
23 |
int res; |
|
24 |
||
41 | 25 |
if (command_line.s_param.nohup) |
26 |
stdin_opened = 0; |
|
27 |
else |
|
28 |
stdin_opened = 1; |
|
29 |
||
23 | 30 |
do |
31 |
{ |
|
32 |
FD_ZERO(&read_set); |
|
33 |
||
34 |
if (stdin_opened) |
|
35 |
FD_SET(0, &read_set); |
|
36 |
maxfd = 0; |
|
37 |
||
38 |
app_control_prepare_read_fdset(&read_set, &maxfd); |
|
33 | 39 |
if (command_line.s_param.serve_unix) |
40 |
s_unix_prepare_read_fdset(&read_set, &maxfd); |
|
41 |
if (command_line.s_param.serve_tcp) |
|
42 |
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
|
43 |
#ifdef linux |
44 | 44 |
if (command_line.s_param.serve_eth) |
45 |
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
|
46 |
#endif /* linux */ |
23 | 47 |
|
48 |
/* Will block */ |
|
49 |
res = select(maxfd + 1, &read_set, 0, 0, 0); |
|
50 |
if (res == -1) |
|
51 |
{ |
|
52 |
if (errno == EINTR) |
|
53 |
continue; |
|
54 |
else |
|
55 |
error("Error in select()"); |
|
56 |
} |
|
57 |
||
58 |
res = app_control_process_read_fdset(&read_set); |
|
59 |
if (res == -1) /* app_stdout and app_stderr closed */ |
|
60 |
break; |
|
61 |
||
41 | 62 |
if (stdin_opened && FD_ISSET(0, &read_set)) |
23 | 63 |
{ |
64 |
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
|
65 |
/* 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
|
66 |
app_control_local_send_to_stdin(stream_buffer, res); |
23 | 67 |
if (res == 0) |
68 |
stdin_opened = 0; |
|
69 |
} |
|
70 |
||
33 | 71 |
if (command_line.s_param.serve_unix) |
72 |
s_unix_process_read_fdset(&read_set); |
|
73 |
if (command_line.s_param.serve_tcp) |
|
74 |
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
|
75 |
#ifdef linux |
44 | 76 |
if (command_line.s_param.serve_eth) |
77 |
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
|
78 |
#endif /* linux */ |
23 | 79 |
} while(1); |
80 |
} |
|
81 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
82 |
int server() |
23 | 83 |
{ |
84 |
int child; |
|
85 |
||
86 |
command_line.is_server = 1; |
|
87 |
||
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
88 |
app_control_start(); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
33
diff
changeset
|
89 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
90 |
/* 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
|
91 |
* 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
|
92 |
if (command_line.s_param.serve_unix) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
93 |
s_unix_update_served(command_line.s_param.max_served); |
23 | 94 |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
95 |
if (command_line.s_param.serve_tcp) |
33 | 96 |
s_tcp_update_served(command_line.s_param.max_served); |
23 | 97 |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
98 |
#ifdef linux |
44 | 99 |
if (command_line.s_param.serve_eth) |
100 |
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
|
101 |
#endif /* linux */ |
44 | 102 |
|
40 | 103 |
child = fork_app(command_line.s_param.command); |
104 |
||
41 | 105 |
if (command_line.s_param.nohup) |
106 |
{ |
|
107 |
close(0); |
|
108 |
close(1); |
|
109 |
close(2); |
|
110 |
} else |
|
111 |
if (command_line.s_param.run_in_subterminal) |
|
112 |
prepare_user_terminal(); |
|
40 | 113 |
|
114 |
install_signal_forwarders(child); |
|
115 |
||
23 | 116 |
loop(); |
117 |
||
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
118 |
if (command_line.s_param.serve_unix) |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
119 |
s_unix_shutdown(); |
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
120 |
|
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
121 |
if (command_line.s_param.serve_tcp) |
33 | 122 |
s_tcp_shutdown(); |
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
123 |
|
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
44
diff
changeset
|
124 |
#ifdef linux |
44 | 125 |
if (command_line.s_param.serve_eth) |
126 |
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
|
127 |
#endif /* linux */ |
44 | 128 |
|
41 | 129 |
if (!command_line.s_param.nohup && |
130 |
command_line.s_param.run_in_subterminal) |
|
29
91286c3ecebc
Added getopt, and some things got based on parameters.
viric@llimona
parents:
26
diff
changeset
|
131 |
restore_user_terminal(); |
23 | 132 |
|
133 |
return 0; |
|
134 |
} |