-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathconsole-dbus.c
267 lines (224 loc) · 6.68 KB
/
console-dbus.c
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
/**
* Copyright © 2023 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.
*/
#include <assert.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include <sys/socket.h>
#include "config.h"
#include "console-mux.h"
#include "console-server.h"
/* size of the dbus object path length */
const size_t dbus_obj_path_len = 1024;
#define DBUS_ERR "org.openbmc.error"
#define DBUS_NAME "xyz.openbmc_project.Console.%s"
#define OBJ_NAME "/xyz/openbmc_project/console/%s"
#define UART_INTF "xyz.openbmc_project.Console.UART"
#define ACCESS_INTF "xyz.openbmc_project.Console.Access"
static void tty_change_baudrate(struct console *console)
{
int i;
int rc;
tty_init_termios(console->server);
for (i = 0; i < console->n_handlers; i++) {
const struct handler_type *type;
struct handler *handler;
handler = console->handlers[i];
type = handler->type;
if (!type->baudrate) {
continue;
}
rc = type->baudrate(handler, console->server->tty.uart.baud);
if (rc) {
warnx("Can't set terminal baudrate for handler %s",
type->name);
}
}
}
static int set_baud_handler(sd_bus *bus, const char *path,
const char *interface, const char *property,
sd_bus_message *msg, void *userdata,
sd_bus_error *err __attribute__((unused)))
{
struct console *console = userdata;
uint64_t baudrate;
speed_t speed;
int r;
if (!console) {
return -ENOENT;
}
r = sd_bus_message_read(msg, "t", &baudrate);
if (r < 0 || baudrate > UINT32_MAX) {
return -EINVAL;
}
speed = parse_int_to_baud((uint32_t)baudrate);
if (!speed) {
warnx("Invalid baud rate: '%" PRIu64 "'", baudrate);
return -EINVAL;
}
assert(console->server->tty.type == TTY_DEVICE_UART);
console->server->tty.uart.baud = speed;
tty_change_baudrate(console);
sd_bus_emit_properties_changed(bus, path, interface, property, NULL);
return 1;
}
static int get_baud_handler(sd_bus *bus __attribute__((unused)),
const char *path __attribute__((unused)),
const char *interface __attribute__((unused)),
const char *property __attribute__((unused)),
sd_bus_message *reply, void *userdata,
sd_bus_error *error __attribute__((unused)))
{
struct console *console = userdata;
struct console_server *server = console->server;
uint64_t baudrate;
int r;
assert(server->tty.type == TTY_DEVICE_UART);
baudrate = parse_baud_to_int(server->tty.uart.baud);
if (!baudrate) {
warnx("Invalid baud rate: '%d'", server->tty.uart.baud);
}
r = sd_bus_message_append(reply, "t", baudrate);
return r;
}
static int method_connect(sd_bus_message *msg, void *userdata,
sd_bus_error *err)
{
struct console *console = userdata;
int rc;
int socket_fd = -1;
if (!console) {
warnx("Internal error: Console pointer is null");
sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
return sd_bus_reply_method_error(msg, err);
}
console_mux_activate(console);
/* Register the consumer. */
socket_fd = dbus_create_socket_consumer(console);
if (socket_fd < 0) {
rc = socket_fd;
warnx("Failed to create socket consumer: %s", strerror(rc));
sd_bus_error_set_const(err, DBUS_ERR,
"Failed to create socket consumer");
return sd_bus_reply_method_error(msg, err);
}
rc = sd_bus_reply_method_return(msg, "h", socket_fd);
/* Close the our end */
close(socket_fd);
return rc;
}
static const sd_bus_vtable console_uart_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_WRITABLE_PROPERTY("Baud", "t", get_baud_handler,
set_baud_handler, 0,
SD_BUS_VTABLE_UNPRIVILEGED |
SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_VTABLE_END,
};
static const sd_bus_vtable console_access_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("Connect", SD_BUS_NO_ARGS, "h", method_connect,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END,
};
int dbus_server_init(struct console_server *server)
{
int r;
int fd;
r = sd_bus_default(&server->bus);
if (r < 0) {
warnx("Failed to connect to bus: %s", strerror(-r));
return -1;
}
fd = sd_bus_get_fd(server->bus);
if (fd < 0) {
warnx("Couldn't get the bus file descriptor");
sd_bus_unref(server->bus);
return -1;
}
const ssize_t index = console_server_request_pollfd(server, fd, POLLIN);
if (index < 0) {
warnx("Error: failed to allocate pollfd");
sd_bus_unref(server->bus);
return -1;
}
server->dbus_pollfd_index = index;
return 0;
}
void dbus_server_fini(struct console_server *server)
{
if (server->dbus_pollfd_index < server->capacity_pollfds) {
console_server_release_pollfd(server,
server->dbus_pollfd_index);
server->dbus_pollfd_index = SIZE_MAX;
}
sd_bus_unref(server->bus);
}
int dbus_init(struct console *console,
struct config *config __attribute__((unused)))
{
char obj_name[dbus_obj_path_len];
char dbus_name[dbus_obj_path_len];
int r;
size_t bytes;
if (!console) {
warnx("Couldn't get valid console");
return -1;
}
/* Register support console interface */
bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME,
console->console_id);
if (bytes >= dbus_obj_path_len) {
warnx("Console id '%s' is too long. There is no enough space in the buffer.",
console->console_id);
return -1;
}
if (console->server->tty.type == TTY_DEVICE_UART) {
/* Register UART interface */
r = sd_bus_add_object_vtable(console->server->bus, NULL,
obj_name, UART_INTF,
console_uart_vtable, console);
if (r < 0) {
warnx("Failed to register UART interface: %s",
strerror(-r));
return -1;
}
}
/* Register access interface */
r = sd_bus_add_object_vtable(console->server->bus, NULL, obj_name,
ACCESS_INTF, console_access_vtable,
console);
if (r < 0) {
warnx("Failed to issue method call: %s", strerror(-r));
return -1;
}
bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME,
console->console_id);
if (bytes >= dbus_obj_path_len) {
warnx("Console id '%s' is too long. There is no enough space in the buffer.",
console->console_id);
return -1;
}
/* Finally register the bus name */
r = sd_bus_request_name(console->server->bus, dbus_name,
SD_BUS_NAME_ALLOW_REPLACEMENT |
SD_BUS_NAME_REPLACE_EXISTING);
if (r < 0) {
warnx("Failed to acquire service name: %s", strerror(-r));
return -1;
}
return 0;
}