xref: /titanic_41/usr/src/cmd/vntsd/read.c (revision 28cdc3d776761766afeb198769d1b70ed7e0f2e1)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*
29  * read thread - Read from tcp client and write to vcc driver. There  are one
30  * writer and multiple readers per console. The first client who connects to
31  * a console get write access. An error message is returned to readers if they
32  * attemp to input commands. Read thread accepts special daemon commands from
33  * all clients.
34  */
35 
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 <ctype.h>
48 #include <syslog.h>
49 #include <libintl.h>
50 #include "vntsd.h"
51 #include "chars.h"
52 
53 /* write_vcc()  - write to vcc virtual console */
54 static int
55 write_vcc(vntsd_client_t *clientp, char c)
56 {
57 	int	n;
58 
59 
60 	assert(clientp);
61 	assert(clientp->cons);
62 
63 	if (c == 0) {
64 		return (VNTSD_SUCCESS);
65 	}
66 	n = write(clientp->cons->vcc_fd, &c, 1);
67 
68 	if (n < 0) {
69 		/* write error */
70 		if (errno == EINTR) {
71 			return (vntsd_cons_chk_intr(clientp));
72 		}
73 
74 		return (VNTSD_STATUS_VCC_IO_ERR);
75 	}
76 
77 	assert(n != 0);
78 	return (VNTSD_SUCCESS);
79 
80 }
81 
82 /*
83  * acquire_writer() the client is going to be writer.
84  * insert the client to the head of the console client queue.
85  */
86 static int
87 acquire_writer(vntsd_client_t *clientp)
88 {
89 	vntsd_cons_t	    *consp;
90 	vntsd_client_t	    *writerp;
91 	int		    rv;
92 
93 	D1(stderr, "t@%d:acuire_writer :client@%d\n", thr_self(),
94 	    clientp->sockfd);
95 
96 	assert(clientp != NULL);
97 	consp = clientp->cons;
98 
99 	assert(consp);
100 
101 	(void) mutex_lock(&consp->lock);
102 
103 	assert(consp->clientpq != NULL);
104 	if (consp->clientpq->handle == clientp) {
105 		/* clientp is a writer already */
106 		(void) mutex_unlock(&consp->lock);
107 		return (VNTSD_SUCCESS);
108 	}
109 
110 	/* current writer */
111 	writerp = (vntsd_client_t *)(consp->clientpq->handle);
112 
113 	(void) mutex_lock(&writerp->lock);
114 
115 	rv = vntsd_que_rm(&(consp->clientpq), clientp);
116 	assert(rv == VNTSD_SUCCESS);
117 
118 	(void) mutex_lock(&clientp->lock);
119 
120 	/* move client to be first in the console queue */
121 	consp->clientpq->handle = clientp;
122 
123 	/* move previous writer to be the second in the queue */
124 	rv =  vntsd_que_insert_after(consp->clientpq, clientp, writerp);
125 
126 	(void) mutex_unlock(&consp->lock);
127 	(void) mutex_unlock(&writerp->lock);
128 	(void) mutex_unlock(&clientp->lock);
129 
130 	if (rv != VNTSD_SUCCESS) {
131 		return (rv);
132 	}
133 
134 	/* write warning message to the writer */
135 
136 	if ((rv = vntsd_write_line(writerp,
137 	    gettext("Warning: Console connection forced into read-only mode")))
138 	    != VNTSD_SUCCESS) {
139 		return (rv);
140 	}
141 
142 	return (VNTSD_SUCCESS);
143 }
144 
145 /* interrupt handler */
146 int
147 vntsd_cons_chk_intr(vntsd_client_t *clientp)
148 {
149 
150 	if (clientp->status & VNTSD_CLIENT_TIMEOUT) {
151 		return (VNTSD_STATUS_CLIENT_QUIT);
152 	}
153 	if (clientp->status & VNTSD_CLIENT_CONS_DELETED) {
154 		return (VNTSD_STATUS_RESELECT_CONS);
155 	}
156 
157 	if (clientp->status & VNTSD_CLIENT_IO_ERR) {
158 		return (VNTSD_STATUS_CLIENT_QUIT);
159 	}
160 	return (VNTSD_STATUS_CONTINUE);
161 }
162 
163 /* read from client */
164 static int
165 read_char(vntsd_client_t *clientp, char *c)
166 {
167 	int	    rv;
168 
169 	for (; ; ) {
170 
171 		rv = vntsd_read_data(clientp, c);
172 
173 		switch (rv) {
174 		case VNTSD_STATUS_CONTINUE:
175 			break;
176 
177 		case VNTSD_STATUS_ACQUIRE_WRITER:
178 			rv = acquire_writer(clientp);
179 			if (rv != VNTSD_SUCCESS) {
180 				return (rv);
181 			}
182 			break;
183 		default:
184 			return (rv);
185 		}
186 	}
187 }
188 
189 /* vntsd_read worker */
190 int
191 vntsd_read(vntsd_client_t *clientp)
192 {
193 	char		c;
194 	int		rv;
195 
196 
197 	assert(clientp);
198 	D3(stderr, "t@%d vntsd_read@%d\n", thr_self(), clientp->sockfd);
199 
200 	for (; ; ) {
201 
202 		/* client input */
203 		rv = read_char(clientp, &c);
204 
205 		if (rv == VNTSD_STATUS_INTR) {
206 			rv = vntsd_cons_chk_intr(clientp);
207 		}
208 
209 		if (rv != VNTSD_SUCCESS) {
210 			return (rv);
211 		}
212 
213 		assert(clientp->cons);
214 		if (clientp->cons->clientpq->handle != clientp) {
215 			/* reader - print  error message */
216 			if ((c != CR) && (c != LF)) {
217 				rv = vntsd_write_line(clientp,
218 				    gettext(VNTSD_NO_WRITE_ACCESS_MSG));
219 
220 				/* check errors and may exit */
221 				if (rv == VNTSD_STATUS_INTR) {
222 					rv = vntsd_cons_chk_intr(clientp);
223 				}
224 
225 				if (rv != VNTSD_SUCCESS) {
226 					return (rv);
227 				}
228 
229 			}
230 
231 			continue;
232 		}
233 
234 		rv = vntsd_ctrl_cmd(clientp, c);
235 
236 		switch (rv) {
237 		case VNTSD_STATUS_CONTINUE:
238 			continue;
239 			break;
240 		case VNTSD_STATUS_INTR:
241 			rv = vntsd_cons_chk_intr(clientp);
242 			if (rv != VNTSD_SUCCESS) {
243 				return (rv);
244 			}
245 			break;
246 		case VNTSD_SUCCESS:
247 			break;
248 		default:
249 			return (rv);
250 		}
251 
252 		/* write to vcc */
253 		rv = write_vcc(clientp, c);
254 		if (rv == VNTSD_STATUS_INTR) {
255 			rv = vntsd_cons_chk_intr(clientp);
256 		}
257 		if (rv != VNTSD_SUCCESS) {
258 			return (rv);
259 		}
260 
261 	}
262 
263 	/*NOTREACHED*/
264 	return (NULL);
265 }
266