blob: d7ef00ce5815f170200025db7c160d253c8e4b53 [file] [log] [blame]
Jeremy Kerr2bd05182016-03-10 16:59:43 +08001
2#include <err.h>
3#include <stdbool.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <termios.h>
8#include <unistd.h>
9#include <endian.h>
10
11#include <sys/socket.h>
12#include <sys/un.h>
13
14#include "console-server.h"
15
16struct client {
17 struct poller *poller;
18 int fd;
19};
20
21struct socket_handler {
22 struct handler handler;
23 struct console *console;
Jeremy Kerr89ea8192016-03-15 17:57:43 +080024 struct poller *poller;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080025 int sd;
26
27 struct client *clients;
28 int n_clients;
29};
30
31static struct socket_handler *to_socket_handler(struct handler *handler)
32{
33 return container_of(handler, struct socket_handler, handler);
34}
35
36static void client_close(struct socket_handler *sh, struct client *client)
37{
38 int idx;
39
40 close(client->fd);
41 if (client->poller)
42 console_unregister_poller(sh->console, client->poller);
43
44 idx = client - sh->clients;
45
46 sh->n_clients--;
47 memmove(&sh->clients[idx], &sh->clients[idx+1],
48 sizeof(*sh->clients) * (sh->n_clients - idx));
49 sh->clients = realloc(sh->clients, sizeof(sh->clients) * sh->n_clients);
50}
51
52static enum poller_ret client_poll(struct handler *handler,
53 int events, void *data)
54{
55 struct socket_handler *sh = to_socket_handler(handler);
56 struct client *client = data;
57 uint8_t buf[4096];
58 int rc;
59
60 if (!(events & POLLIN))
61 return POLLER_OK;
62
63 rc = read(client->fd, buf, sizeof(buf));
64 if (rc <= 0) {
65 client->poller = NULL;
66 client_close(sh, client);
67 return POLLER_REMOVE;
68 }
69
70 console_data_out(sh->console, buf, rc);
71
72 return POLLER_OK;
73}
74
75static void client_send_data(struct socket_handler *sh,
76 struct client *client, uint8_t *buf, size_t len)
77{
78 int rc;
79
80 rc = write_buf_to_fd(client->fd, buf, len);
81 if (rc)
82 client_close(sh, client);
83}
84
85static enum poller_ret socket_poll(struct handler *handler,
86 int events, void __attribute__((unused)) *data)
87{
88 struct socket_handler *sh = to_socket_handler(handler);
89 struct client *client;
90 int fd, n;
91
92 if (!(events & POLLIN))
93 return POLLER_OK;
94
95 fd = accept(sh->sd, NULL, NULL);
96 if (fd < 0)
97 return POLLER_OK;
98
99 n = sh->n_clients++;
100 sh->clients = realloc(sh->clients, sizeof(*client) * sh->n_clients);
101 client = &sh->clients[n];
102
103 client->fd = fd;
104 client->poller = console_register_poller(sh->console, handler,
105 client_poll, client->fd, POLLIN, client);
106
107 return POLLER_OK;
108
109}
110
111static int socket_init(struct handler *handler, struct console *console)
112{
113 struct socket_handler *sh = to_socket_handler(handler);
114 struct sockaddr_un addr;
115 int rc;
116
117 sh->console = console;
118 sh->clients = NULL;
119 sh->n_clients = 0;
120
121 sh->sd = socket(AF_UNIX, SOCK_STREAM, 0);
122 if(sh->sd < 0) {
123 warn("Can't create socket");
124 return -1;
125 }
126
127 memset(&addr, 0, sizeof(addr));
128 addr.sun_family = AF_UNIX;
129 memcpy(addr.sun_path, console_socket_path, console_socket_path_len);
130
131 rc = bind(sh->sd, (struct sockaddr *)&addr, sizeof(addr));
132 if (rc) {
133 warn("Can't bind to socket path %s",
134 console_socket_path_readable);
135 return -1;
136 }
137
138 rc = listen(sh->sd, 1);
139 if (rc) {
140 warn("Can't listen for incoming connections");
141 return -1;
142 }
143
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800144 sh->poller = console_register_poller(console, handler, socket_poll,
145 sh->sd, POLLIN, NULL);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800146
147 return 0;
148}
149
150static int socket_data(struct handler *handler, uint8_t *buf, size_t len)
151{
152 struct socket_handler *sh = to_socket_handler(handler);
153 int i;
154
155 for (i = 0; i < sh->n_clients; i++) {
156 struct client *client = &sh->clients[i];
157 client_send_data(sh, client, buf, len);
158 }
159 return 0;
160}
161
162static void socket_fini(struct handler *handler)
163{
164 struct socket_handler *sh = to_socket_handler(handler);
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800165 int i;
166
167 for (i = 0; i < sh->n_clients; i++)
168 client_close(sh, &sh->clients[i]);
169
170 if (sh->poller)
171 console_unregister_poller(sh->console, sh->poller);
172
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800173 close(sh->sd);
174}
175
176static struct socket_handler socket_handler = {
177 .handler = {
178 .name = "socket",
179 .init = socket_init,
180 .data_in = socket_data,
181 .fini = socket_fini,
182 },
183};
184
185console_register_handler(&socket_handler.handler);
186