4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 #pragma ident "%Z%%M% %I% %E% SMI"
28 * write thread - read from vcc console and write to tcp client. There are one
29 * writer and multiple readers per console. The first client who connects to
30 * a console get write access.
31 * Writer thread writes vcc data to all tcp clients that connected to
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
53 /* handle for writing all clients */
54 typedef struct write_buf
{
55 uint_t sz
; /* data size */
60 * check the state of write thread. exit if no more client connects to the
64 write_chk_status(vntsd_cons_t
*consp
, int status
)
67 if ((consp
->status
& VNTSD_CONS_DELETED
) || (consp
->clientpq
== NULL
)) {
72 case VNTSD_STATUS_VCC_IO_ERR
:
73 assert(consp
->group
!= NULL
);
74 if (vntsd_vcc_err(consp
) != VNTSD_STATUS_CONTINUE
) {
78 case VNTSD_STATUS_INTR
:
87 * skip_terminal_null()
88 * scan terminal null character sequence (0x5e 0x40)
89 * return number of characters in the buf after skipping terminal null
90 * sequence. buf size must be at least sz+1.
93 skip_terminal_null(char *buf
, int sz
)
96 static int term_null_seq
= 0;
101 /* skip 0x5e previously */
104 if (buf
[0] != 0x40) {
105 /* not terminal null sequence put 0x5e back */
106 for (i
= sz
; i
> 0; i
--) {
114 /* skip terminal null sequence */
121 for (i
= 0; i
< sz
; i
++) {
128 for (i
= 0; i
< sz
; i
++) {
129 if (buf
[i
] == '\0') {
133 if (buf
[i
] == 0x5e) {
134 /* possible terminal null sequence */
136 /* last character in buffer */
143 if (buf
[i
+1] == 0x40) {
144 /* found terminal null sequence */
146 for (j
= i
; j
< sz
-i
; j
++) {
152 if (buf
[i
+1] == '\0') {
168 /* read data from vcc */
170 read_vcc(vntsd_cons_t
*consp
, char *buf
, ssize_t
*sz
)
173 *sz
= read(consp
->vcc_fd
, buf
, VNTSD_MAX_BUF_SIZE
);
175 if (errno
== EINTR
) {
176 return (VNTSD_STATUS_INTR
);
180 return (VNTSD_SUCCESS
);
182 return (VNTSD_STATUS_VCC_IO_ERR
);
187 * this function is passed as a parameter to vntsd_que_find.
188 * for each client that connected to the console, vntsd_que_find
189 * applies this function.
192 write_one_client(vntsd_client_t
*clientp
, write_buf_t
*write_buf
)
196 rv
= vntsd_write_client(clientp
, write_buf
->buf
, write_buf
->sz
);
197 if (rv
!= VNTSD_SUCCESS
) {
198 (void) mutex_lock(&clientp
->lock
);
199 clientp
->status
|= VNTSD_CLIENT_IO_ERR
;
200 assert(clientp
->cons
);
201 (void) thr_kill(clientp
->cons_tid
, NULL
);
202 (void) mutex_unlock(&clientp
->lock
);
208 /* vntsd_write_thread() */
210 vntsd_write_thread(vntsd_cons_t
*consp
)
212 char buf
[VNTSD_MAX_BUF_SIZE
+1];
215 write_buf_t write_buf
;
217 D1(stderr
, "t@%d vntsd_write@%d\n", thr_self(), consp
->vcc_fd
);
220 write_chk_status(consp
, VNTSD_SUCCESS
);
223 bzero(buf
, VNTSD_MAX_BUF_SIZE
+1);
226 rv
= read_vcc(consp
, buf
, &sz
);
228 write_chk_status(consp
, rv
);
235 if ((sz
= skip_terminal_null(buf
, sz
)) == 0) {
236 /* terminal null sequence */
244 * output data to all clients connected
248 (void) mutex_lock(&consp
->lock
);
249 (void) vntsd_que_find(consp
->clientpq
,
250 (compare_func_t
)write_one_client
, &write_buf
);
251 (void) mutex_unlock(&consp
->lock
);
253 write_chk_status(consp
, VNTSD_SUCCESS
);