1 /** 2 * Copyright © 2016 IBM Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define _GNU_SOURCE 18 19 #include <assert.h> 20 #include <err.h> 21 #include <errno.h> 22 #include <stdbool.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <termios.h> 27 #include <unistd.h> 28 #include <endian.h> 29 30 #include <sys/socket.h> 31 #include <sys/un.h> 32 #include <systemd/sd-daemon.h> 33 34 #include "console-server.h" 35 36 #define SOCKET_HANDLER_PKT_SIZE 512 37 /* Set poll() timeout to 4000 uS, or 4 mS */ 38 #define SOCKET_HANDLER_PKT_US_TIMEOUT 4000 39 40 struct client { 41 struct socket_handler *sh; 42 struct poller *poller; 43 struct ringbuffer_consumer *rbc; 44 int fd; 45 bool blocked; 46 }; 47 48 struct socket_handler { 49 struct handler handler; 50 struct console *console; 51 struct poller *poller; 52 int sd; 53 54 struct client **clients; 55 int n_clients; 56 }; 57 58 static struct timeval const socket_handler_timeout = { 59 .tv_sec = 0, 60 .tv_usec = SOCKET_HANDLER_PKT_US_TIMEOUT 61 }; 62 63 static struct socket_handler *to_socket_handler(struct handler *handler) 64 { 65 return container_of(handler, struct socket_handler, handler); 66 } 67 68 static void client_close(struct client *client) 69 { 70 struct socket_handler *sh = client->sh; 71 int idx; 72 73 close(client->fd); 74 if (client->poller) 75 console_poller_unregister(sh->console, client->poller); 76 77 if (client->rbc) 78 ringbuffer_consumer_unregister(client->rbc); 79 80 for (idx = 0; idx < sh->n_clients; idx++) 81 if (sh->clients[idx] == client) 82 break; 83 84 assert(idx < sh->n_clients); 85 86 free(client); 87 client = NULL; 88 89 sh->n_clients--; 90 memmove(&sh->clients[idx], &sh->clients[idx+1], 91 sizeof(*sh->clients) * (sh->n_clients - idx)); 92 sh->clients = realloc(sh->clients, 93 sizeof(*sh->clients) * sh->n_clients); 94 } 95 96 static void client_set_blocked(struct client *client, bool blocked) 97 { 98 int events; 99 100 if (client->blocked == blocked) 101 return; 102 103 client->blocked = blocked; 104 105 events = POLLIN; 106 if (client->blocked) 107 events |= POLLOUT; 108 109 console_poller_set_events(client->sh->console, client->poller, events); 110 } 111 112 static ssize_t send_all(struct client *client, void *buf, 113 size_t len, bool block) 114 { 115 int fd, rc, flags; 116 size_t pos; 117 118 fd = client->fd; 119 120 flags = MSG_NOSIGNAL; 121 if (!block) 122 flags |= MSG_DONTWAIT; 123 124 for (pos = 0; pos < len; pos += rc) { 125 rc = send(fd, (char *)buf + pos, len - pos, flags); 126 if (rc < 0) { 127 if (!block && (errno == EAGAIN || 128 errno == EWOULDBLOCK)) { 129 client_set_blocked(client, true); 130 break; 131 } 132 133 if (errno == EINTR) 134 continue; 135 136 return -1; 137 } 138 if (rc == 0) 139 return -1; 140 } 141 142 return pos; 143 } 144 145 /* Drain the queue to the socket and update the queue buffer. If force_len is 146 * set, send at least that many bytes from the queue, possibly while blocking 147 */ 148 static int client_drain_queue(struct client *client, size_t force_len) 149 { 150 uint8_t *buf; 151 ssize_t wlen; 152 size_t len, total_len; 153 bool block; 154 155 total_len = 0; 156 wlen = 0; 157 block = !!force_len; 158 159 /* if we're already blocked, no need for the write */ 160 if (!block && client->blocked) 161 return 0; 162 163 for (;;) { 164 len = ringbuffer_dequeue_peek(client->rbc, total_len, &buf); 165 if (!len) 166 break; 167 168 wlen = send_all(client, buf, len, block); 169 if (wlen <= 0) 170 break; 171 172 total_len += wlen; 173 174 if (force_len && total_len >= force_len) 175 break; 176 } 177 178 if (wlen < 0) 179 return -1; 180 181 if (force_len && total_len < force_len) 182 return -1; 183 184 ringbuffer_dequeue_commit(client->rbc, total_len); 185 return 0; 186 } 187 188 static enum ringbuffer_poll_ret client_ringbuffer_poll(void *arg, 189 size_t force_len) 190 { 191 struct client *client = arg; 192 int rc, len; 193 194 len = ringbuffer_len(client->rbc); 195 if (!force_len && (len < SOCKET_HANDLER_PKT_SIZE)) { 196 /* Do nothing until many small requests have accumulated, or 197 * the UART is idle for awhile (as determined by the timeout 198 * value supplied to the poll function call in console_server.c. */ 199 console_poller_set_timeout(client->sh->console, client->poller, 200 &socket_handler_timeout); 201 return RINGBUFFER_POLL_OK; 202 } 203 204 rc = client_drain_queue(client, force_len); 205 if (rc) { 206 client->rbc = NULL; 207 client_close(client); 208 return RINGBUFFER_POLL_REMOVE; 209 } 210 211 return RINGBUFFER_POLL_OK; 212 } 213 214 static enum poller_ret client_timeout(struct handler *handler __attribute__((unused)), void *data) 215 { 216 struct client *client = data; 217 int rc = 0; 218 219 if (client->blocked) { 220 /* nothing to do here, we'll call client_drain_queue when 221 * we become unblocked */ 222 return POLLER_OK; 223 } 224 225 rc = client_drain_queue(client, 0); 226 if (rc) { 227 client_close(client); 228 return POLLER_REMOVE; 229 } 230 231 return POLLER_OK; 232 } 233 234 static enum poller_ret client_poll(struct handler *handler, 235 int events, void *data) 236 { 237 struct socket_handler *sh = to_socket_handler(handler); 238 struct client *client = data; 239 uint8_t buf[4096]; 240 int rc; 241 242 if (events & POLLIN) { 243 rc = recv(client->fd, buf, sizeof(buf), MSG_DONTWAIT); 244 if (rc < 0) { 245 if (errno == EAGAIN || errno == EWOULDBLOCK) 246 return POLLER_OK; 247 else 248 goto err_close; 249 } 250 if (rc == 0) 251 goto err_close; 252 253 console_data_out(sh->console, buf, rc); 254 } 255 256 if (events & POLLOUT) { 257 client_set_blocked(client, false); 258 rc = client_drain_queue(client, 0); 259 if (rc) 260 goto err_close; 261 } 262 263 return POLLER_OK; 264 265 err_close: 266 client->poller = NULL; 267 client_close(client); 268 return POLLER_REMOVE; 269 } 270 271 static enum poller_ret socket_poll(struct handler *handler, 272 int events, void __attribute__((unused)) *data) 273 { 274 struct socket_handler *sh = to_socket_handler(handler); 275 struct client *client; 276 int fd, n; 277 278 if (!(events & POLLIN)) 279 return POLLER_OK; 280 281 fd = accept(sh->sd, NULL, NULL); 282 if (fd < 0) 283 return POLLER_OK; 284 285 client = malloc(sizeof(*client)); 286 memset(client, 0, sizeof(*client)); 287 288 client->sh = sh; 289 client->fd = fd; 290 client->poller = console_poller_register(sh->console, handler, 291 client_poll, client_timeout, client->fd, POLLIN, 292 client); 293 client->rbc = console_ringbuffer_consumer_register(sh->console, 294 client_ringbuffer_poll, client); 295 296 n = sh->n_clients++; 297 sh->clients = realloc(sh->clients, 298 sizeof(*sh->clients) * sh->n_clients); 299 sh->clients[n] = client; 300 301 return POLLER_OK; 302 303 } 304 305 static int socket_init(struct handler *handler, struct console *console, 306 struct config *config) 307 { 308 struct socket_handler *sh = to_socket_handler(handler); 309 struct sockaddr_un addr; 310 size_t addrlen; 311 ssize_t len; 312 int rc; 313 314 sh->console = console; 315 sh->clients = NULL; 316 sh->n_clients = 0; 317 318 memset(&addr, 0, sizeof(addr)); 319 addr.sun_family = AF_UNIX; 320 len = console_socket_path(&addr, config_get_value(config, "socket-id")); 321 if (len < 0) { 322 if (errno) 323 warn("Failed to configure socket: %s", strerror(errno)); 324 else 325 warn("Socket name length exceeds buffer limits"); 326 return -1; 327 } 328 329 /* Try to take a socket from systemd first */ 330 if (sd_listen_fds(0) == 1 && 331 sd_is_socket_unix(SD_LISTEN_FDS_START, SOCK_STREAM, 1, addr.sun_path, len) > 0) { 332 sh->sd = SD_LISTEN_FDS_START; 333 } else { 334 sh->sd = socket(AF_UNIX, SOCK_STREAM, 0); 335 if(sh->sd < 0) { 336 warn("Can't create socket"); 337 return -1; 338 } 339 340 addrlen = sizeof(addr) - sizeof(addr.sun_path) + len; 341 342 rc = bind(sh->sd, (struct sockaddr *)&addr, addrlen); 343 if (rc) { 344 socket_path_t name; 345 console_socket_path_readable(&addr, addrlen, name); 346 warn("Can't bind to socket path %s (terminated at first null)", 347 name); 348 goto cleanup; 349 } 350 351 rc = listen(sh->sd, 1); 352 if (rc) { 353 warn("Can't listen for incoming connections"); 354 goto cleanup; 355 } 356 } 357 358 sh->poller = console_poller_register(console, handler, socket_poll, 359 NULL, sh->sd, POLLIN, NULL); 360 361 return 0; 362 cleanup: 363 close(sh->sd); 364 return -1; 365 } 366 367 static void socket_fini(struct handler *handler) 368 { 369 struct socket_handler *sh = to_socket_handler(handler); 370 371 while (sh->n_clients) 372 client_close(sh->clients[0]); 373 374 if (sh->poller) 375 console_poller_unregister(sh->console, sh->poller); 376 377 close(sh->sd); 378 } 379 380 static struct socket_handler socket_handler = { 381 .handler = { 382 .name = "socket", 383 .init = socket_init, 384 .fini = socket_fini, 385 }, 386 }; 387 388 console_handler_register(&socket_handler.handler); 389 390