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
|
/*
* 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
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* write thread - read from vcc console and write to tcp client. There are one
* writer and multiple readers per console. The first client who connects to
* a console get write access.
* Writer thread writes vcc data to all tcp clients that connected to
* the console.
*/
#include <stdio.h>
#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 <poll.h>
#include <syslog.h>
#include <libintl.h>
#include "vntsd.h"
#include "chars.h"
/* handle for writing all clients */
typedef struct write_buf {
uint_t sz; /* data size */
char *buf;
} write_buf_t;
/*
* check the state of write thread. exit if no more client connects to the
* console.
*/
static void
write_chk_status(vntsd_cons_t *consp, int status)
{
if ((consp->status & VNTSD_CONS_DELETED) || (consp->clientpq == NULL)) {
thr_exit(0);
}
switch (status) {
case VNTSD_STATUS_VCC_IO_ERR:
assert(consp->group != NULL);
if (vntsd_vcc_err(consp) != VNTSD_STATUS_CONTINUE) {
thr_exit(0);
}
break;
case VNTSD_STATUS_INTR:
thr_exit(0);
default:
break;
}
}
/*
* skip_terminal_null()
* scan terminal null character sequence (0x5e 0x40)
* return number of characters in the buf after skipping terminal null
* sequence. buf size must be at least sz+1.
*/
static int
skip_terminal_null(char *buf, int sz)
{
int i, j;
static int term_null_seq = 0;
assert(sz >= 0);
if (term_null_seq) {
/* skip 0x5e previously */
term_null_seq = 0;
if (buf[0] != 0x40) {
/* not terminal null sequence put 0x5e back */
for (i = sz; i > 0; i--) {
buf[i] = buf[i-1];
}
buf[0] = 0x5e;
sz++;
} else {
/* skip terminal null sequence */
sz--;
if (sz == 0) {
return (sz);
}
for (i = 0; i < sz; i++) {
buf[i] = buf[i+1];
}
}
}
for (; ; ) {
for (i = 0; i < sz; i++) {
if (buf[i] == '\0') {
return (i);
}
if (buf[i] == 0x5e) {
/* possible terminal null sequence */
if (i == sz -1) {
/* last character in buffer */
term_null_seq = 1;
sz--;
buf[i] = 0;
return (sz);
}
if (buf[i+1] == 0x40) {
/* found terminal null sequence */
sz -= 2;
for (j = i; j < sz -i; j++) {
buf[j] = buf[j+2];
}
break;
}
if (buf[i+1] == '\0') {
buf[i] = 0;
term_null_seq = 1;
return (i);
}
}
}
if (i == sz) {
/* end of scan */
return (sz);
}
}
}
/* read data from vcc */
static int
read_vcc(vntsd_cons_t *consp, char *buf, ssize_t *sz)
{
/* read from vcc */
*sz = read(consp->vcc_fd, buf, VNTSD_MAX_BUF_SIZE);
if (errno == EINTR) {
return (VNTSD_STATUS_INTR);
}
if ((*sz > 0)) {
return (VNTSD_SUCCESS);
}
return (VNTSD_STATUS_VCC_IO_ERR);
}
/*
* write to a client
* this function is passed as a parameter to vntsd_que_find.
* for each client that connected to the console, vntsd_que_find
* applies this function.
*/
static boolean_t
write_one_client(vntsd_client_t *clientp, write_buf_t *write_buf)
{
int rv;
rv = vntsd_write_client(clientp, write_buf->buf, write_buf->sz);
if (rv != VNTSD_SUCCESS) {
(void) mutex_lock(&clientp->lock);
clientp->status |= VNTSD_CLIENT_IO_ERR;
assert(clientp->cons);
(void) thr_kill(clientp->cons_tid, NULL);
(void) mutex_unlock(&clientp->lock);
}
return (B_FALSE);
}
/* vntsd_write_thread() */
void*
vntsd_write_thread(vntsd_cons_t *consp)
{
char buf[VNTSD_MAX_BUF_SIZE+1];
int sz;
int rv;
write_buf_t write_buf;
D1(stderr, "t@%d vntsd_write@%d\n", thr_self(), consp->vcc_fd);
assert(consp);
write_chk_status(consp, VNTSD_SUCCESS);
for (; ; ) {
bzero(buf, VNTSD_MAX_BUF_SIZE +1);
/* read data */
rv = read_vcc(consp, buf, &sz);
write_chk_status(consp, rv);
if (sz <= 0) {
continue;
}
/* has data */
if ((sz = skip_terminal_null(buf, sz)) == 0) {
/* terminal null sequence */
continue;
}
write_buf.sz = sz;
write_buf.buf = buf;
/*
* output data to all clients connected
* to this console
*/
(void) mutex_lock(&consp->lock);
(void) vntsd_que_find(consp->clientpq,
(compare_func_t)write_one_client, &write_buf);
(void) mutex_unlock(&consp->lock);
write_chk_status(consp, VNTSD_SUCCESS);
}
/*NOTREACHED*/
return (NULL);
}
|