-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipc.c
302 lines (263 loc) · 6.34 KB
/
ipc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
########################################################################
# This file is part of the minicom communications package for WRAMP.
#
# Copyright 1991-1995 Miquel van Smoorenburg.
# Copyright (C) 2019 The University of Waikato, Hamilton, New Zealand.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
########################################################################
*/
#include "port.h"
#include "remote.h"
#ifndef _SELECT
static int tokeyserv, fromkeyserv; /* File desciptors of ipc pipes */
static int keypid; /* pid of keyserv */
static jmp_buf ackjmp; /* To jump to after ACK signal */
static int waiting = 0; /* Do we expect an ACK signal? */
static int keypress = -1; /* A key was pressed. */
/*
* We got the "ksigio" signal. This means that CTRL-A was pressed in
* the main terminal loop, or any key if requested by keyserv(KSIGIO).
*/
/*ARGSUSED*/
static void ksigio(dummy)
int dummy;
{
unsigned char c[8];
int n;
signal(HELLO, ksigio);
while ((n = read(fromkeyserv, c, 8)) < 0 && errno == EINTR)
errno = 0;
keypress = n ? c[n - 1] : -1;
}
/*
* After we have sent a signal to "keyserv", we wait for it to signal
* us back. Otherwise "keyserv" would be swamped with signals and
* die ungracefully....
*/
static void sigack(dummy)
int dummy;
{
signal(ACK, sigack);
if (waiting) longjmp(ackjmp, 1);
printf("sigack: unexpected ACK signal &^%%$!! (pardon my French)\r\n");
}
/*
* Install the keyserv process. This involves setting up the communications
* channels (pipes) and forking the child process.
*/
static void kinstall()
{
char mpid[8];
int pipe1[2], pipe2[2];
char buf[2];
char prog[128];
if (pipe(pipe1) < 0 || pipe(pipe2) < 0)
leave("out of file descriptors\n");
tokeyserv = pipe1[1];
fromkeyserv = pipe2[0];
/* Set signal handler */
signal(HELLO, ksigio);
signal(ACK, sigack);
sprintf(mpid, "%d", (int)getpid());
switch(keypid = fork()) {
case -1:
leave("could not fork.\n");
break;
case 0: /* Child */
/* Set up fd #1 : stdout */
dup2(portfd, 1);
close(portfd);
/* Set up fd #3 : remote ---> keyserv */
dup2(pipe1[0], 3);
/* Set up fd #4 : remote <--- keyserv */
dup2(pipe2[1], 4);
/* Close unused file descriptors */
close(pipe1[1]);
close(pipe2[0]);
sprintf(prog, "%s/keyserv", LIBDIR);
execl(prog, "keyserv", mpid, (char *)NULL);
exit(0);
default: /* Parent */
if (setjmp(ackjmp) == 0) {
#ifdef DEBUG
printf("keyserv has PID %d\r\n", keypid);
#endif
sleep(2); /* Wait for keyserv to initialize */
waiting = 1;
buf[0] = KSTOP;
write(tokeyserv, buf, 2);
if (kill(keypid, HELLO) < 0) {
leave("could not exec keyserv\n");
}
/* Do nothing 'till ACK signal */
while(1) pause();
}
waiting = 0;
/* close unused pipes */
close(pipe1[0]);
close(pipe2[1]);
break;
}
}
/*
* Install / tell /de-install "keyserv" program.
*/
int keyboard(cmd, arg)
int cmd, arg;
{
char ch[2];
int pid, stt;
static int lastcmd = -1;
int c;
lastcmd = cmd;
if (cmd == KINSTALL) {
kinstall();
return(0);
}
if (cmd == KUNINSTALL) {
close(fromkeyserv);
close(tokeyserv);
(void) kill(keypid, SIGKILL);
pid = m_wait(&stt);
keypid = 0;
return(0);
}
if (cmd == KGETKEY) {
if (keypress >= 0)
/* Return the command key from keyserv. */
c = keypress;
else {
/* Just read it. */
read(0, ch, 1);
c = ch[0];
}
keypress = -1;
return(c);
}
if (cmd == KSETESC) {
/* Store this because the code expects it. */
escape = arg;
}
/* Do nothing if keyserv doesn't run yet. */
if (keypid == 0) return(0);
if (setjmp(ackjmp) == 0) {
waiting = 1;
ch[0] = cmd;
ch[1] = arg;
write(tokeyserv, ch, 2);
(void) kill(keypid, HELLO);
/* Do nothing 'till ACK signal */
while(1) pause();
}
waiting = 0;
return(0);
}
/* Dummy sigalarm handler. */
static void dummy()
{
}
/* Wait for I/O to happen. We might get interrupted by keyserv. */
int check_io(fd1, fd2, tmout, buf, buflen)
int fd1;
int fd2;
int tmout;
char *buf;
int *buflen;
{
int n;
int x = 0;
/* OK, set the alarm if needed. */
signal(SIGALRM, dummy);
if (tmout) alarm((tmout + 500) / 1000);
/* We do a read on the first fd, the second one is always stdin. */
if (keypress < 0) {
if (fd1 >= 0) {
/* Read gets interrupted by keypress or alarm. */
n = read(fd1, buf, 127);
buf[n > 0 ? n : 0] = 0;
if (buflen) *buflen = n;
if (n > 0) x |= 1;
} else
/* Wait for keypress or alarm. */
pause();
}
alarm(0);
if (keypress >= 0) x |= 2;
return(x);
}
#else /* _SELECT */
/* Check if there is IO pending. */
int check_io(fd1, fd2, tmout, buf, buflen)
int fd1;
int fd2;
int tmout;
char *buf;
int *buflen;
{
int n = 0, i;
struct timeval tv;
fd_set fds;
extern int io_pending; /* wkeys.c */
tv.tv_sec = tmout / 1000;
tv.tv_usec = (tmout % 1000) * 1000L;
i = fd1;
if (fd2 > fd1) i = fd2;
FD_ZERO(&fds);
if (fd1 >= 0) FD_SET(fd1, &fds); else fd1 = 0;
if (fd2 >= 0) FD_SET(fd2, &fds); else fd2 = 0;
if (fd2 == 0 && io_pending)
n = 2;
else if (select(i+1, &fds, NULL, NULL, &tv) > 0)
n = 1 * (FD_ISSET(fd1, &fds) > 0) + 2 * (FD_ISSET(fd2, &fds) > 0);
/* If there is data put it in the buffer. */
if (buf) {
i = 0;
if ((n & 1) == 1) i = read(fd1, buf, 127);
buf[i > 0 ? i : 0] = 0;
if (buflen) *buflen = i;
}
return(n);
}
int keyboard(cmd, arg)
int cmd, arg;
{
switch(cmd) {
case KSTART:
case KSTOP:
break;
case KSIGIO:
break;
case KGETKEY:
return(wxgetch());
case KSETESC:
escape = arg;
break;
case KSETBS:
vt_set(-1, -1, NULL, -1, arg, -1, -1);
break;
case KCURST:
vt_set(-1, -1, NULL, -1, -1, -1, NORMAL);
break;
case KCURAPP:
vt_set(-1, -1, NULL, -1, -1, -1, APPL);
break;
default:
/* The rest is only meaningful if a keyserv runs. */
break;
}
return(0);
}
#endif /* _SELECT */