-
Notifications
You must be signed in to change notification settings - Fork 24
/
console-server.h
287 lines (225 loc) · 7.78 KB
/
console-server.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Copyright © 2016 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <poll.h>
#include <stdbool.h>
#include <stdint.h>
#include <termios.h> /* for speed_t */
#include <time.h>
#include <systemd/sd-bus.h>
#include <sys/time.h>
#include <sys/un.h>
struct console;
struct config;
/* Handler API.
*
* Console data handlers: these implement the functions that process
* data coming out of the main tty device.
*
* Handlers are registered at link time using the console_handler_register()
* macro. We call each handler's ->init() function at startup, and ->fini() at
* exit.
*
* Handlers will almost always want to register a ringbuffer consumer, which
* provides data coming from the tty. Use cosole_register_ringbuffer_consumer()
* for this. To send data to the tty, use console_data_out().
*
* If a handler needs to monitor a separate file descriptor for events, use the
* poller API, through console_poller_register().
*/
struct handler;
struct handler_type {
const char *name;
struct handler *(*init)(const struct handler_type *type,
struct console *console, struct config *config);
void (*fini)(struct handler *handler);
int (*baudrate)(struct handler *handler, speed_t baudrate);
void (*deselect)(struct handler *handler);
};
struct handler {
const struct handler_type *type;
};
/* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
#define __handler_name(n) __handler_##n
#define _handler_name(n) __handler_name(n)
#ifndef __clang__
#define handler_type_check(h) BUILD_ASSERT_OR_ZERO((h)->init && (h)->fini)
#else
/* clang doesn't seem to be able to constify the type ops */
#define handler_type_check(h) 0
#endif
#define console_handler_register(h) \
static const __attribute__((section("handlers"))) \
__attribute__((used)) struct handler_type * \
_handler_name(__COUNTER__) = (h) + handler_type_check(h)
/* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
int console_data_out(struct console *console, const uint8_t *data, size_t len);
enum poller_ret {
POLLER_OK = 0,
POLLER_REMOVE,
POLLER_EXIT,
};
typedef char(socket_path_t)[sizeof(((struct sockaddr_un *)NULL)->sun_path)];
typedef enum poller_ret (*poller_event_fn_t)(struct handler *handler,
int revents, void *data);
typedef enum poller_ret (*poller_timeout_fn_t)(struct handler *handler,
void *data);
enum tty_device {
TTY_DEVICE_UNDEFINED = 0,
TTY_DEVICE_VUART,
TTY_DEVICE_UART,
TTY_DEVICE_PTY,
};
struct console_server {
struct {
const char *kname;
char *dev;
int fd;
enum tty_device type;
union {
struct {
char *sysfs_devnode;
int sirq;
uint16_t lpc_addr;
} vuart;
struct {
speed_t baud;
} uart;
};
} tty;
// All the pollfds are stored here,
// so 'poll' can operate on them.
// The other 'pollfd*' are just pointers to this array.
struct pollfd *pollfds;
size_t capacity_pollfds;
// index into pollfds
size_t tty_pollfd_index;
struct config *config;
// the currently active console
struct console *active;
struct console **consoles;
size_t n_consoles;
// index into (struct console_server)->pollfds
size_t dbus_pollfd_index;
struct sd_bus *bus;
// may be NULL in case there is no mux
struct console_mux *mux;
};
struct console {
// point back to the console server
// which we are a member of
struct console_server *server;
const char *console_id;
/* Socket name starts with null character hence we need length */
socket_path_t socket_name;
ssize_t socket_name_len;
struct ringbuffer *rb;
struct handler **handlers;
long n_handlers;
struct poller **pollers;
long n_pollers;
// values to configure the mux
unsigned long mux_index;
};
/* poller API */
struct poller {
struct handler *handler;
void *data;
poller_event_fn_t event_fn;
poller_timeout_fn_t timeout_fn;
struct timeval timeout;
bool remove;
// index into (struct console_server)->pollfds
size_t pollfd_index;
};
struct poller *console_poller_register(struct console *console,
struct handler *handler,
poller_event_fn_t poller_fn,
poller_timeout_fn_t timeout_fn, int fd,
int events, void *data);
void console_poller_unregister(struct console *console, struct poller *poller);
void console_poller_set_events(struct console *console, struct poller *poller,
int events);
void console_poller_set_timeout(struct console *console, struct poller *poller,
const struct timeval *tv);
/* ringbuffer API */
enum ringbuffer_poll_ret {
RINGBUFFER_POLL_OK = 0,
RINGBUFFER_POLL_REMOVE,
};
typedef enum ringbuffer_poll_ret (*ringbuffer_poll_fn_t)(void *data,
size_t force_len);
struct ringbuffer_consumer;
struct ringbuffer {
uint8_t *buf;
size_t size;
size_t tail;
struct ringbuffer_consumer **consumers;
int n_consumers;
};
struct ringbuffer_consumer {
struct ringbuffer *rb;
ringbuffer_poll_fn_t poll_fn;
void *poll_data;
size_t pos;
};
struct ringbuffer *ringbuffer_init(size_t size);
void ringbuffer_fini(struct ringbuffer *rb);
struct ringbuffer_consumer *
ringbuffer_consumer_register(struct ringbuffer *rb,
ringbuffer_poll_fn_t poll_fn, void *data);
void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc);
int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len);
size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
uint8_t **data);
int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len);
size_t ringbuffer_len(struct ringbuffer_consumer *rbc);
/* console wrapper around ringbuffer consumer registration */
struct ringbuffer_consumer *
console_ringbuffer_consumer_register(struct console *console,
ringbuffer_poll_fn_t poll_fn, void *data);
/* Console server API */
void tty_init_termios(struct console_server *server);
/* socket paths */
ssize_t console_socket_path(socket_path_t path, const char *id);
ssize_t console_socket_path_readable(const struct sockaddr_un *addr,
size_t addrlen, socket_path_t path);
/* utils */
int write_buf_to_fd(int fd, const uint8_t *buf, size_t len);
/* console_server dbus */
int dbus_server_init(struct console_server *server);
void dbus_server_fini(struct console_server *server);
/* console-dbus API */
int dbus_init(struct console *console,
struct config *config __attribute__((unused)));
/* socket-handler API */
int dbus_create_socket_consumer(struct console *console);
#ifndef offsetof
#define offsetof(type, member) ((unsigned long)&((type *)NULL)->member)
#endif
#define container_of(ptr, type, member) \
((type *)((void *)((ptr) - offsetof(type, member))))
#define BUILD_ASSERT(c) \
do { \
char __c[(c) ? 1 : -1] __attribute__((unused)); \
} while (0)
#define BUILD_ASSERT_OR_ZERO(c) (sizeof(char[(c) ? 1 : -1]) - 1)
// returns the index of that pollfd in server->pollfds
// we cannot return a pointer because 'realloc' may move server->pollfds
ssize_t console_server_request_pollfd(struct console_server *server, int fd,
short int events);
int console_server_release_pollfd(struct console_server *server,
size_t pollfd_index);