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