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 2007 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 n = write(clientp->cons->vcc_fd, &c, 1); 64 65 if (n < 0) { 66 /* write error */ 67 if (errno == EINTR) { 68 return (vntsd_cons_chk_intr(clientp)); 69 } 70 71 return (VNTSD_STATUS_VCC_IO_ERR); 72 } 73 74 assert(n != 0); 75 return (VNTSD_SUCCESS); 76 77 } 78 79 /* 80 * acquire_writer() the client is going to be writer. 81 * insert the client to the head of the console client queue. 82 */ 83 static int 84 acquire_writer(vntsd_client_t *clientp) 85 { 86 vntsd_cons_t *consp; 87 vntsd_client_t *writerp; 88 int rv; 89 90 D1(stderr, "t@%d:acuire_writer :client@%d\n", thr_self(), 91 clientp->sockfd); 92 93 assert(clientp != NULL); 94 consp = clientp->cons; 95 96 assert(consp); 97 98 (void) mutex_lock(&consp->lock); 99 100 assert(consp->clientpq != NULL); 101 if (consp->clientpq->handle == clientp) { 102 /* clientp is a writer already */ 103 (void) mutex_unlock(&consp->lock); 104 return (VNTSD_SUCCESS); 105 } 106 107 /* current writer */ 108 writerp = (vntsd_client_t *)(consp->clientpq->handle); 109 110 (void) mutex_lock(&writerp->lock); 111 112 rv = vntsd_que_rm(&(consp->clientpq), clientp); 113 assert(rv == VNTSD_SUCCESS); 114 115 (void) mutex_lock(&clientp->lock); 116 117 /* move client to be first in the console queue */ 118 consp->clientpq->handle = clientp; 119 120 /* move previous writer to be the second in the queue */ 121 rv = vntsd_que_insert_after(consp->clientpq, clientp, writerp); 122 123 (void) mutex_unlock(&consp->lock); 124 (void) mutex_unlock(&writerp->lock); 125 (void) mutex_unlock(&clientp->lock); 126 127 if (rv != VNTSD_SUCCESS) { 128 return (rv); 129 } 130 131 /* write warning message to the writer */ 132 133 if ((rv = vntsd_write_line(writerp, 134 gettext("Warning: Console connection forced into read-only mode"))) 135 != VNTSD_SUCCESS) { 136 return (rv); 137 } 138 139 return (VNTSD_SUCCESS); 140 } 141 142 /* interrupt handler */ 143 int 144 vntsd_cons_chk_intr(vntsd_client_t *clientp) 145 { 146 147 if (clientp->status & VNTSD_CLIENT_TIMEOUT) { 148 return (VNTSD_STATUS_CLIENT_QUIT); 149 } 150 if (clientp->status & VNTSD_CLIENT_CONS_DELETED) { 151 return (VNTSD_STATUS_RESELECT_CONS); 152 } 153 154 if (clientp->status & VNTSD_CLIENT_IO_ERR) { 155 return (VNTSD_STATUS_CLIENT_QUIT); 156 } 157 return (VNTSD_STATUS_CONTINUE); 158 } 159 160 /* read from client */ 161 static int 162 read_char(vntsd_client_t *clientp, char *c) 163 { 164 int rv; 165 166 for (; ; ) { 167 168 rv = vntsd_read_data(clientp, c); 169 170 switch (rv) { 171 case VNTSD_STATUS_CONTINUE: 172 break; 173 174 case VNTSD_STATUS_ACQUIRE_WRITER: 175 rv = acquire_writer(clientp); 176 if (rv != VNTSD_SUCCESS) { 177 return (rv); 178 } 179 break; 180 default: 181 return (rv); 182 } 183 } 184 } 185 186 /* vntsd_read worker */ 187 int 188 vntsd_read(vntsd_client_t *clientp) 189 { 190 char c; 191 int rv; 192 193 194 assert(clientp); 195 D3(stderr, "t@%d vntsd_read@%d\n", thr_self(), clientp->sockfd); 196 197 for (; ; ) { 198 199 /* client input */ 200 rv = read_char(clientp, &c); 201 202 if (rv == VNTSD_STATUS_INTR) { 203 rv = vntsd_cons_chk_intr(clientp); 204 } 205 206 if (rv != VNTSD_SUCCESS) { 207 return (rv); 208 } 209 210 assert(clientp->cons); 211 if (clientp->cons->clientpq->handle != clientp) { 212 /* reader - print error message */ 213 if ((c != CR) && (c != LF)) { 214 rv = vntsd_write_line(clientp, 215 gettext(VNTSD_NO_WRITE_ACCESS_MSG)); 216 217 /* check errors and may exit */ 218 if (rv == VNTSD_STATUS_INTR) { 219 rv = vntsd_cons_chk_intr(clientp); 220 } 221 222 if (rv != VNTSD_SUCCESS) { 223 return (rv); 224 } 225 226 } 227 228 continue; 229 } 230 231 rv = vntsd_ctrl_cmd(clientp, c); 232 233 switch (rv) { 234 case VNTSD_STATUS_CONTINUE: 235 continue; 236 break; 237 case VNTSD_STATUS_INTR: 238 rv = vntsd_cons_chk_intr(clientp); 239 if (rv != VNTSD_SUCCESS) { 240 return (rv); 241 } 242 break; 243 case VNTSD_SUCCESS: 244 break; 245 default: 246 return (rv); 247 } 248 249 /* write to vcc */ 250 rv = write_vcc(clientp, c); 251 if (rv == VNTSD_STATUS_INTR) { 252 rv = vntsd_cons_chk_intr(clientp); 253 } 254 if (rv != VNTSD_SUCCESS) { 255 return (rv); 256 } 257 258 } 259 260 /*NOTREACHED*/ 261 return (NULL); 262 } 263