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