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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* read thread - Read from tcp client and write to vcc driver. There are one
* writer and multiple readers per console. The first client who connects to
* a console get write access. An error message is returned to readers if they
* attemp to input commands. Read thread accepts special daemon commands from
* all clients.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <thread.h>
#include <synch.h>
#include <signal.h>
#include <assert.h>
#include <ctype.h>
#include <syslog.h>
#include <libintl.h>
#include "vntsd.h"
#include "chars.h"
/* write_vcc() - write to vcc virtual console */
static int
write_vcc(vntsd_client_t *clientp, char c)
{
int n;
assert(clientp);
assert(clientp->cons);
n = write(clientp->cons->vcc_fd, &c, 1);
if (n < 0) {
/* write error */
if (errno == EINTR) {
return (vntsd_cons_chk_intr(clientp));
}
return (VNTSD_STATUS_VCC_IO_ERR);
}
assert(n != 0);
return (VNTSD_SUCCESS);
}
/*
* acquire_writer() the client is going to be writer.
* insert the client to the head of the console client queue.
*/
static int
acquire_writer(vntsd_client_t *clientp)
{
vntsd_cons_t *consp;
vntsd_client_t *writerp;
int rv;
D1(stderr, "t@%d:acuire_writer :client@%d\n", thr_self(),
clientp->sockfd);
assert(clientp != NULL);
consp = clientp->cons;
assert(consp);
(void) mutex_lock(&consp->lock);
assert(consp->clientpq != NULL);
if (consp->clientpq->handle == clientp) {
/* clientp is a writer already */
(void) mutex_unlock(&consp->lock);
return (VNTSD_SUCCESS);
}
/* current writer */
writerp = (vntsd_client_t *)(consp->clientpq->handle);
(void) mutex_lock(&writerp->lock);
rv = vntsd_que_rm(&(consp->clientpq), clientp);
assert(rv == VNTSD_SUCCESS);
(void) mutex_lock(&clientp->lock);
/* move client to be first in the console queue */
consp->clientpq->handle = clientp;
/* move previous writer to be the second in the queue */
rv = vntsd_que_insert_after(consp->clientpq, clientp, writerp);
(void) mutex_unlock(&consp->lock);
(void) mutex_unlock(&writerp->lock);
(void) mutex_unlock(&clientp->lock);
if (rv != VNTSD_SUCCESS) {
return (rv);
}
/* write warning message to the writer */
if ((rv = vntsd_write_line(writerp,
gettext("Warning: Console connection forced into read-only mode")))
!= VNTSD_SUCCESS) {
return (rv);
}
return (VNTSD_SUCCESS);
}
/* interrupt handler */
int
vntsd_cons_chk_intr(vntsd_client_t *clientp)
{
if (clientp->status & VNTSD_CLIENT_TIMEOUT) {
return (VNTSD_STATUS_CLIENT_QUIT);
}
if (clientp->status & VNTSD_CLIENT_CONS_DELETED) {
return (VNTSD_STATUS_RESELECT_CONS);
}
if (clientp->status & VNTSD_CLIENT_IO_ERR) {
return (VNTSD_STATUS_CLIENT_QUIT);
}
return (VNTSD_STATUS_CONTINUE);
}
/* read from client */
static int
read_char(vntsd_client_t *clientp, char *c)
{
int rv;
for (; ; ) {
rv = vntsd_read_data(clientp, c);
switch (rv) {
case VNTSD_STATUS_ACQUIRE_WRITER:
clientp->prev_char = 0;
rv = acquire_writer(clientp);
if (rv != VNTSD_SUCCESS) {
return (rv);
}
break;
case VNTSD_SUCCESS:
/*
* Based on telnet protocol, when an <eol> is entered,
* vntsd receives <0x0d,0x00>. However, console expects
* <0xd> only. We need to filter out <0x00>.
*/
if (clientp->prev_char == 0xd && *c == 0) {
clientp->prev_char = *c;
break;
}
clientp->prev_char = *c;
return (rv);
default:
assert(rv != VNTSD_STATUS_CONTINUE);
clientp->prev_char = 0;
return (rv);
}
}
}
/* vntsd_read worker */
int
vntsd_read(vntsd_client_t *clientp)
{
char c;
int rv;
assert(clientp);
D3(stderr, "t@%d vntsd_read@%d\n", thr_self(), clientp->sockfd);
for (; ; ) {
/* client input */
rv = read_char(clientp, &c);
if (rv == VNTSD_STATUS_INTR) {
rv = vntsd_cons_chk_intr(clientp);
}
if (rv != VNTSD_SUCCESS) {
return (rv);
}
assert(clientp->cons);
/*
* Only keyboard inputs from first connection to a
* guest console should be accepted. Check to see if
* this client is the first connection in console
* queue
*/
if (clientp->cons->clientpq->handle != clientp) {
/*
* Since this console connection is not the first
* connection in the console queue,
* it is operating in 'reader'
* mode, print warning and ignore the input.
*/
rv = vntsd_write_line(clientp,
gettext(VNTSD_NO_WRITE_ACCESS_MSG));
/* check errors and interrupts */
if (rv == VNTSD_STATUS_INTR) {
rv = vntsd_cons_chk_intr(clientp);
}
if (rv != VNTSD_SUCCESS) {
return (rv);
}
continue;
}
rv = vntsd_ctrl_cmd(clientp, c);
switch (rv) {
case VNTSD_STATUS_CONTINUE:
continue;
break;
case VNTSD_STATUS_INTR:
rv = vntsd_cons_chk_intr(clientp);
if (rv != VNTSD_SUCCESS) {
return (rv);
}
break;
case VNTSD_SUCCESS:
break;
default:
return (rv);
}
/* write to vcc */
rv = write_vcc(clientp, c);
if (rv == VNTSD_STATUS_INTR) {
rv = vntsd_cons_chk_intr(clientp);
}
if (rv != VNTSD_SUCCESS) {
return (rv);
}
}
/*NOTREACHED*/
return (NULL);
}
|