xref: /titanic_44/usr/src/cmd/vntsd/write.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 #pragma ident	"%Z%%M%	%I%	%E% SMI"
26 
27 /*
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
32  * the console.
33  */
34 
35 #include <stdio.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <thread.h>
44 #include <synch.h>
45 #include <signal.h>
46 #include <assert.h>
47 #include <poll.h>
48 #include <syslog.h>
49 #include <libintl.h>
50 #include "vntsd.h"
51 #include "chars.h"
52 
53 /*
54  * check the state of write thread. exit if no more client connects to the
55  * console.
56  */
57 static void
58 write_chk_status(vntsd_cons_t *consp, int status)
59 {
60 
61 	if ((consp->status & VNTSD_CONS_DELETED) || (consp->clientpq == NULL)) {
62 		thr_exit(0);
63 	}
64 
65 	switch (status) {
66 	case VNTSD_STATUS_VCC_IO_ERR:
67 		assert(consp->group != NULL);
68 		if (vntsd_vcc_err(consp) != VNTSD_STATUS_CONTINUE) {
69 			thr_exit(0);
70 		}
71 		break;
72 	case VNTSD_STATUS_INTR:
73 		thr_exit(0);
74 	default:
75 		break;
76 
77 	}
78 }
79 
80 /*
81  * skip_terminal_null()
82  * scan terminal null character sequence (0x5e 0x40)
83  * return number of characters in the buf after skipping terminal null
84  * sequence.
85  */
86 static int
87 skip_terminal_null(char *buf, int buf_sz, int sz)
88 {
89 	int	    i, j;
90 	static int  term_null_seq = 0;
91 
92 	assert(sz >= 0);
93 
94 	if (buf_sz < sz+1) {
95 		return (-1);
96 	}
97 
98 	if (term_null_seq) {
99 		/* skip 0x5e previously */
100 		term_null_seq = 0;
101 
102 		if (buf[0] != 0x40) {
103 			/* not terminal null sequence put 0x5e back */
104 			for (i = sz; i > 0; i--) {
105 				buf[i] = buf[i-1];
106 			}
107 
108 			buf[0] = 0x5e;
109 
110 			sz++;
111 		} else {
112 			/* skip terminal null sequence */
113 			sz--;
114 
115 			if (sz == 0) {
116 				return (sz);
117 			}
118 
119 			for (i = 0; i < sz; i++) {
120 				buf[i] = buf[i+1];
121 			}
122 		}
123 	}
124 
125 	for (; ; ) {
126 		for (i = 0; i < sz; i++) {
127 			if (buf[i]  == '\0') {
128 				return (i);
129 			}
130 
131 			if (buf[i] == 0x5e) {
132 				/* possible terminal null sequence */
133 				if (i == sz -1) {
134 					/* last character in buffer */
135 					term_null_seq = 1;
136 					sz--;
137 					buf[i] = 0;
138 					return (sz);
139 				}
140 
141 				if (buf[i+1] == 0x40) {
142 					/* found terminal null sequence */
143 					sz -= 2;
144 					for (j = i; j < sz -i; j++) {
145 						buf[j] = buf[j+2];
146 					}
147 					break;
148 				}
149 
150 				if (buf[i+1] == '\0') {
151 					buf[i] = 0;
152 					term_null_seq = 1;
153 					return (i);
154 				}
155 
156 			}
157 		}
158 
159 		if (i == sz) {
160 			/* end of scan */
161 			return (sz);
162 		}
163 	}
164 }
165 
166 /* read data from vcc */
167 static int
168 read_vcc(vntsd_cons_t *consp, char *buf, ssize_t *sz)
169 {
170 	/* read from vcc */
171 	*sz = read(consp->vcc_fd, buf, VNTSD_MAX_BUF_SIZE);
172 
173 	if (errno == EINTR) {
174 		return (VNTSD_STATUS_INTR);
175 	}
176 
177 	if ((*sz > 0)) {
178 		return (VNTSD_SUCCESS);
179 	}
180 	return (VNTSD_STATUS_VCC_IO_ERR);
181 }
182 
183 static int s_sz;
184 /* write to a client */
185 static boolean_t
186 write_all_clients(vntsd_client_t *clientp, char *buf)
187 {
188 	int rv;
189 
190 	rv = vntsd_write_client(clientp, buf, s_sz);
191 	if (rv != VNTSD_SUCCESS) {
192 		(void) mutex_lock(&clientp->lock);
193 		clientp->status |= VNTSD_CLIENT_IO_ERR;
194 		assert(clientp->cons);
195 		(void) thr_kill(clientp->cons_tid, NULL);
196 		(void) mutex_unlock(&clientp->lock);
197 	}
198 	return (B_FALSE);
199 
200 }
201 
202 /* vntsd_write_thread() */
203 void*
204 vntsd_write_thread(vntsd_cons_t *consp)
205 {
206 	char		buf[VNTSD_MAX_BUF_SIZE+1];
207 	int		sz;
208 	int		rv;
209 
210 	D1(stderr, "t@%d vntsd_write@%d\n", thr_self(), consp->vcc_fd);
211 
212 	assert(consp);
213 	write_chk_status(consp, VNTSD_SUCCESS);
214 
215 	for (; ; ) {
216 		bzero(buf,  VNTSD_MAX_BUF_SIZE +1);
217 
218 		/* read data */
219 		rv = read_vcc(consp, buf, &sz);
220 
221 		write_chk_status(consp, rv);
222 
223 		if (sz <= 0) {
224 			continue;
225 		}
226 
227 		/* has data */
228 		if ((s_sz = skip_terminal_null(buf, sz+1, sz)) == 0) {
229 			/* terminal null sequence */
230 			continue;
231 		}
232 
233 		assert(s_sz > 0);
234 
235 		/*
236 		 * output data to all clients connected
237 		 * to this console
238 		 */
239 
240 		(void) mutex_lock(&consp->lock);
241 		(void) vntsd_que_find(consp->clientpq,
242 		    (compare_func_t)write_all_clients, buf);
243 		(void) mutex_unlock(&consp->lock);
244 
245 		write_chk_status(consp, VNTSD_SUCCESS);
246 
247 	}
248 
249 	/*NOTREACHED*/
250 	return (NULL);
251 }
252