Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2023 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 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 17 | #include <assert.h> |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <err.h> |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <sys/socket.h> |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 22 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 23 | #include "config.h" |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 24 | #include "console-mux.h" |
| 25 | #include "console-server.h" |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 26 | |
| 27 | /* size of the dbus object path length */ |
| 28 | const size_t dbus_obj_path_len = 1024; |
| 29 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 30 | #define DBUS_ERR "org.openbmc.error" |
| 31 | #define DBUS_NAME "xyz.openbmc_project.Console.%s" |
| 32 | #define OBJ_NAME "/xyz/openbmc_project/console/%s" |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 33 | #define UART_INTF "xyz.openbmc_project.Console.UART" |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 34 | #define ACCESS_INTF "xyz.openbmc_project.Console.Access" |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 35 | |
| 36 | static void tty_change_baudrate(struct console *console) |
| 37 | { |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 38 | int i; |
| 39 | int rc; |
| 40 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 41 | tty_init_termios(console->server); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 42 | |
| 43 | for (i = 0; i < console->n_handlers; i++) { |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 44 | const struct handler_type *type; |
| 45 | struct handler *handler; |
| 46 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 47 | handler = console->handlers[i]; |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 48 | type = handler->type; |
| 49 | if (!type->baudrate) { |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 50 | continue; |
| 51 | } |
| 52 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 53 | rc = type->baudrate(handler, console->server->tty.uart.baud); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 54 | if (rc) { |
| 55 | warnx("Can't set terminal baudrate for handler %s", |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 56 | type->name); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 61 | static int set_baud_handler(sd_bus *bus, const char *path, |
| 62 | const char *interface, const char *property, |
| 63 | sd_bus_message *msg, void *userdata, |
| 64 | sd_bus_error *err __attribute__((unused))) |
| 65 | { |
| 66 | struct console *console = userdata; |
| 67 | uint64_t baudrate; |
| 68 | speed_t speed; |
| 69 | int r; |
| 70 | |
| 71 | if (!console) { |
| 72 | return -ENOENT; |
| 73 | } |
| 74 | |
| 75 | r = sd_bus_message_read(msg, "t", &baudrate); |
| 76 | if (r < 0 || baudrate > UINT32_MAX) { |
| 77 | return -EINVAL; |
| 78 | } |
| 79 | |
| 80 | speed = parse_int_to_baud((uint32_t)baudrate); |
| 81 | if (!speed) { |
| 82 | warnx("Invalid baud rate: '%" PRIu64 "'", baudrate); |
| 83 | return -EINVAL; |
| 84 | } |
| 85 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 86 | assert(console->server->tty.type == TTY_DEVICE_UART); |
| 87 | console->server->tty.uart.baud = speed; |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 88 | tty_change_baudrate(console); |
| 89 | |
| 90 | sd_bus_emit_properties_changed(bus, path, interface, property, NULL); |
| 91 | |
| 92 | return 1; |
| 93 | } |
| 94 | |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 95 | static int get_baud_handler(sd_bus *bus __attribute__((unused)), |
| 96 | const char *path __attribute__((unused)), |
| 97 | const char *interface __attribute__((unused)), |
| 98 | const char *property __attribute__((unused)), |
| 99 | sd_bus_message *reply, void *userdata, |
| 100 | sd_bus_error *error __attribute__((unused))) |
| 101 | { |
| 102 | struct console *console = userdata; |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 103 | struct console_server *server = console->server; |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 104 | uint64_t baudrate; |
| 105 | int r; |
| 106 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 107 | assert(server->tty.type == TTY_DEVICE_UART); |
| 108 | baudrate = parse_baud_to_int(server->tty.uart.baud); |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 109 | if (!baudrate) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 110 | warnx("Invalid baud rate: '%d'", server->tty.uart.baud); |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | r = sd_bus_message_append(reply, "t", baudrate); |
| 114 | |
| 115 | return r; |
| 116 | } |
| 117 | |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 118 | static int method_connect(sd_bus_message *msg, void *userdata, |
| 119 | sd_bus_error *err) |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 120 | { |
| 121 | struct console *console = userdata; |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 122 | int rc; |
| 123 | int socket_fd = -1; |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 124 | |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 125 | if (!console) { |
| 126 | warnx("Internal error: Console pointer is null"); |
| 127 | sd_bus_error_set_const(err, DBUS_ERR, "Internal error"); |
| 128 | return sd_bus_reply_method_error(msg, err); |
| 129 | } |
| 130 | |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 131 | console_mux_activate(console); |
| 132 | |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 133 | /* Register the consumer. */ |
| 134 | socket_fd = dbus_create_socket_consumer(console); |
| 135 | if (socket_fd < 0) { |
| 136 | rc = socket_fd; |
| 137 | warnx("Failed to create socket consumer: %s", strerror(rc)); |
| 138 | sd_bus_error_set_const(err, DBUS_ERR, |
| 139 | "Failed to create socket consumer"); |
| 140 | return sd_bus_reply_method_error(msg, err); |
| 141 | } |
| 142 | |
| 143 | rc = sd_bus_reply_method_return(msg, "h", socket_fd); |
| 144 | |
| 145 | /* Close the our end */ |
| 146 | close(socket_fd); |
| 147 | |
| 148 | return rc; |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 151 | static const sd_bus_vtable console_uart_vtable[] = { |
| 152 | SD_BUS_VTABLE_START(0), |
| 153 | SD_BUS_WRITABLE_PROPERTY("Baud", "t", get_baud_handler, |
| 154 | set_baud_handler, 0, |
| 155 | SD_BUS_VTABLE_UNPRIVILEGED | |
| 156 | SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 157 | SD_BUS_VTABLE_END, |
| 158 | }; |
| 159 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 160 | static const sd_bus_vtable console_access_vtable[] = { |
| 161 | SD_BUS_VTABLE_START(0), |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 162 | SD_BUS_METHOD("Connect", SD_BUS_NO_ARGS, "h", method_connect, |
| 163 | SD_BUS_VTABLE_UNPRIVILEGED), |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 164 | SD_BUS_VTABLE_END, |
| 165 | }; |
| 166 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 167 | int dbus_server_init(struct console_server *server) |
| 168 | { |
| 169 | int r; |
| 170 | int fd; |
| 171 | r = sd_bus_default(&server->bus); |
| 172 | if (r < 0) { |
| 173 | warnx("Failed to connect to bus: %s", strerror(-r)); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | fd = sd_bus_get_fd(server->bus); |
| 178 | if (fd < 0) { |
| 179 | warnx("Couldn't get the bus file descriptor"); |
| 180 | sd_bus_unref(server->bus); |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | const ssize_t index = console_server_request_pollfd(server, fd, POLLIN); |
| 185 | if (index < 0) { |
| 186 | warnx("Error: failed to allocate pollfd"); |
| 187 | sd_bus_unref(server->bus); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | server->dbus_pollfd_index = index; |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | void dbus_server_fini(struct console_server *server) |
| 196 | { |
| 197 | if (server->dbus_pollfd_index < server->capacity_pollfds) { |
| 198 | console_server_release_pollfd(server, |
| 199 | server->dbus_pollfd_index); |
| 200 | server->dbus_pollfd_index = SIZE_MAX; |
| 201 | } |
| 202 | |
| 203 | sd_bus_unref(server->bus); |
| 204 | } |
| 205 | |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 206 | int dbus_init(struct console *console, |
| 207 | struct config *config __attribute__((unused))) |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 208 | { |
| 209 | char obj_name[dbus_obj_path_len]; |
| 210 | char dbus_name[dbus_obj_path_len]; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 211 | int r; |
| 212 | size_t bytes; |
| 213 | |
| 214 | if (!console) { |
| 215 | warnx("Couldn't get valid console"); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 216 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 219 | /* Register support console interface */ |
| 220 | bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME, |
| 221 | console->console_id); |
| 222 | if (bytes >= dbus_obj_path_len) { |
| 223 | warnx("Console id '%s' is too long. There is no enough space in the buffer.", |
| 224 | console->console_id); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 225 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 228 | if (console->server->tty.type == TTY_DEVICE_UART) { |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 229 | /* Register UART interface */ |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 230 | r = sd_bus_add_object_vtable(console->server->bus, NULL, |
| 231 | obj_name, UART_INTF, |
| 232 | console_uart_vtable, console); |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 233 | if (r < 0) { |
| 234 | warnx("Failed to register UART interface: %s", |
| 235 | strerror(-r)); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 236 | return -1; |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 237 | } |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* Register access interface */ |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 241 | r = sd_bus_add_object_vtable(console->server->bus, NULL, obj_name, |
| 242 | ACCESS_INTF, console_access_vtable, |
| 243 | console); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 244 | if (r < 0) { |
| 245 | warnx("Failed to issue method call: %s", strerror(-r)); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 246 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME, |
| 250 | console->console_id); |
| 251 | if (bytes >= dbus_obj_path_len) { |
| 252 | warnx("Console id '%s' is too long. There is no enough space in the buffer.", |
| 253 | console->console_id); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 254 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 255 | } |
| 256 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 257 | /* Finally register the bus name */ |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 258 | r = sd_bus_request_name(console->server->bus, dbus_name, |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 259 | SD_BUS_NAME_ALLOW_REPLACEMENT | |
| 260 | SD_BUS_NAME_REPLACE_EXISTING); |
| 261 | if (r < 0) { |
| 262 | warnx("Failed to acquire service name: %s", strerror(-r)); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 263 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 264 | } |
| 265 | |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 266 | return 0; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 267 | } |