author | viric@mandarina |
Mon, 28 Apr 2008 21:42:48 +0200 | |
changeset 92 | faf9db07c037 |
parent 84 | 150a622f26ea |
permissions | -rw-r--r-- |
12 | 1 |
/* |
53 | 2 |
Terminal Mixer - multi-point multi-user access to terminal applications |
12 | 3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
4 |
||
5 |
Please find the license in the provided COPYING file. |
|
6 |
*/ |
|
7 | 7 |
#include <signal.h> |
21 | 8 |
#include <string.h> |
60 | 9 |
#include <unistd.h> |
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
10 |
#include <sys/types.h> |
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
11 |
#include <sys/wait.h> |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
12 |
#include <assert.h> |
7 | 13 |
|
14 |
#include "main.h" |
|
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
15 |
#include "handlers.h" |
7 | 16 |
|
17 |
static int child; |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
18 |
int child_died; |
60 | 19 |
static char old_alarm_handler_set; |
20 |
static struct sigaction old_alarm_handler; |
|
7 | 21 |
|
66
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
22 |
static int timeout_timed_out; |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
23 |
|
7 | 24 |
static void forward_signals_to_child_handler(int val) |
25 |
{ |
|
26 |
kill(child, val); |
|
27 |
} |
|
28 |
||
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
29 |
static void sig_child_died(int val) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
30 |
{ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
31 |
int status; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
32 |
int res; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
33 |
|
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
34 |
assert(child_died == 0); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
35 |
|
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
36 |
res = wait(&status); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
37 |
|
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
38 |
if (WIFEXITED(status) || WIFSIGNALED(status)) |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
39 |
{ |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
40 |
debugmsg("Child exited"); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
41 |
child_died = 1; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
42 |
} |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
43 |
debugmsg("Child handler"); |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
44 |
} |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
45 |
|
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
46 |
static void update_window_size(int val) |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
47 |
{ |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
48 |
char *xterm_str; |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
49 |
int len; |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
50 |
|
74 | 51 |
pass_winsize_to_slave(); |
52 |
||
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
53 |
kill(child, val); |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
54 |
|
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
55 |
/* Resend the xterm string */ |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
56 |
xterm_str = get_xterm_resize_string(); |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
57 |
len = strlen(xterm_str); |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
58 |
if (command_line.s_param.serve_unix) |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
59 |
s_unix_send_to_connected(xterm_str, len); |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
60 |
if (command_line.s_param.serve_tcp) |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
61 |
s_tcp_send_to_connected(xterm_str, len); |
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
47
diff
changeset
|
62 |
#ifdef linux |
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
63 |
if (command_line.s_param.serve_eth) |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
64 |
s_eth_send_to_connected(xterm_str, len); |
49
1cead94cfd99
Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents:
47
diff
changeset
|
65 |
#endif /* linux */ |
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
66 |
} |
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
67 |
|
21 | 68 |
void install_signal_forwarders(int _child) |
7 | 69 |
{ |
74 | 70 |
struct sigaction act; |
7 | 71 |
|
74 | 72 |
child = _child; |
7 | 73 |
|
74 | 74 |
act.sa_handler = forward_signals_to_child_handler; |
75 |
sigemptyset(&act.sa_mask); |
|
76 |
act.sa_flags = 0; |
|
7 | 77 |
|
74 | 78 |
sigaction(SIGTERM, &act, 0); |
79 |
sigaction(SIGINT, &act, 0); |
|
47
8dcc45d8f3e5
xterm resizes sent on SIGWINCH. new terminal applications start a new session.
lbatlle@npdl268.bpo.hp.com
parents:
21
diff
changeset
|
80 |
|
74 | 81 |
act.sa_handler = update_window_size; |
82 |
sigaction(SIGWINCH, &act, 0); |
|
82
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
83 |
|
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
84 |
act.sa_handler = sig_child_died; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
85 |
act.sa_flags = SA_NOCLDSTOP; |
5cbe47923060
Added a terminal-creation capability.
lbatlle@npdl268.bpo.hp.com
parents:
74
diff
changeset
|
86 |
sigaction(SIGCHLD, &act, 0); |
7 | 87 |
} |
60 | 88 |
|
89 |
static void alarm_handler(int x) |
|
90 |
{ |
|
66
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
91 |
timeout_timed_out = 1; |
84
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
92 |
dump_line("Timeout timed out."); |
60 | 93 |
} |
94 |
||
95 |
void program_timeout(int secs) |
|
96 |
{ |
|
74 | 97 |
struct sigaction act; |
98 |
struct sigaction *old; |
|
84
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
99 |
int res; |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
100 |
|
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
101 |
dump_line("Programming timeout for %i seconds.\n", secs); |
60 | 102 |
|
74 | 103 |
sigemptyset(&act.sa_mask); |
104 |
act.sa_flags = 0; |
|
105 |
act.sa_handler = alarm_handler; |
|
60 | 106 |
|
74 | 107 |
if (old_alarm_handler_set) |
108 |
old = 0; |
|
109 |
else |
|
110 |
old = &old_alarm_handler; |
|
60 | 111 |
|
84
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
112 |
res = sigaction(SIGALRM, &act, old); |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
113 |
if (res == -1) |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
114 |
error("Sigaction SIGALRM failed"); |
60 | 115 |
|
74 | 116 |
old_alarm_handler_set = 1; |
117 |
timeout_timed_out = 0; |
|
60 | 118 |
|
74 | 119 |
alarm(secs); |
60 | 120 |
} |
121 |
||
66
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
122 |
int did_timeout_happen() |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
123 |
{ |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
124 |
return timeout_timed_out; |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
125 |
} |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
126 |
|
60 | 127 |
void unprogram_timeout() |
128 |
{ |
|
129 |
alarm(0); |
|
130 |
||
84
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
131 |
dump_line("Unprogramming timeout"); |
150a622f26ea
Fixing eth_proto bugs. There was no ACK working until now (SIGALARM problems).
lbatlle@npdl268.bpo.hp.com
parents:
82
diff
changeset
|
132 |
|
66
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
133 |
timeout_timed_out = 0; |
b2469563a1dc
Reliable ethernet protocol. I still need pselect instead of select.
viric@mandarina
parents:
60
diff
changeset
|
134 |
|
60 | 135 |
if (old_alarm_handler_set) |
136 |
{ |
|
137 |
sigaction(SIGALRM, &old_alarm_handler, 0); |
|
138 |
old_alarm_handler_set = 0; |
|
139 |
} |
|
140 |
} |
|
71
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
66
diff
changeset
|
141 |
|
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
66
diff
changeset
|
142 |
void ignore_sighup() |
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
66
diff
changeset
|
143 |
{ |
74 | 144 |
struct sigaction act; |
71
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
66
diff
changeset
|
145 |
|
74 | 146 |
act.sa_handler = SIG_IGN; |
147 |
sigemptyset(&act.sa_mask); |
|
148 |
act.sa_flags = 0; |
|
71
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
66
diff
changeset
|
149 |
|
74 | 150 |
sigaction(SIGHUP, &act, 0); |
71
c209487034d7
True nohup-like behaviour. Programs ignore to SIGHUP.
viric@llimona
parents:
66
diff
changeset
|
151 |
} |