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 | |
| 17 | #include <errno.h> |
| 18 | #include <err.h> |
| 19 | |
| 20 | #include "console-server.h" |
| 21 | |
| 22 | /* size of the dbus object path length */ |
| 23 | const size_t dbus_obj_path_len = 1024; |
| 24 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 25 | #define DBUS_ERR "org.openbmc.error" |
| 26 | #define DBUS_NAME "xyz.openbmc_project.Console.%s" |
| 27 | #define OBJ_NAME "/xyz/openbmc_project/console/%s" |
| 28 | #define TTY_INTF "xyz.openbmc_project.console" |
| 29 | #define ACCESS_INTF "xyz.openbmc_project.Console.Access" |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 30 | |
| 31 | static void tty_change_baudrate(struct console *console) |
| 32 | { |
| 33 | struct handler *handler; |
| 34 | int i; |
| 35 | int rc; |
| 36 | |
| 37 | tty_init_termios(console); |
| 38 | |
| 39 | for (i = 0; i < console->n_handlers; i++) { |
| 40 | handler = console->handlers[i]; |
| 41 | if (!handler->baudrate) { |
| 42 | continue; |
| 43 | } |
| 44 | |
| 45 | rc = handler->baudrate(handler, console->tty_baud); |
| 46 | if (rc) { |
| 47 | warnx("Can't set terminal baudrate for handler %s", |
| 48 | handler->name); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static int method_set_baud_rate(sd_bus_message *msg, void *userdata, |
| 54 | sd_bus_error *err) |
| 55 | { |
| 56 | struct console *console = userdata; |
| 57 | uint32_t baudrate; |
| 58 | speed_t speed; |
| 59 | int r; |
| 60 | |
| 61 | if (!console) { |
| 62 | sd_bus_error_set_const(err, DBUS_ERR, "Internal error"); |
| 63 | return sd_bus_reply_method_return(msg, "x", 0); |
| 64 | } |
| 65 | |
| 66 | r = sd_bus_message_read(msg, "u", &baudrate); |
| 67 | if (r < 0) { |
| 68 | sd_bus_error_set_const(err, DBUS_ERR, "Bad message"); |
| 69 | return sd_bus_reply_method_return(msg, "x", -EINVAL); |
| 70 | } |
| 71 | |
| 72 | speed = parse_int_to_baud(baudrate); |
| 73 | if (!speed) { |
| 74 | warnx("Invalid baud rate: '%u'", baudrate); |
| 75 | return sd_bus_reply_method_return(msg, "x", -EINVAL); |
| 76 | } |
| 77 | |
| 78 | console->tty_baud = speed; |
| 79 | tty_change_baudrate(console); |
| 80 | |
| 81 | return sd_bus_reply_method_return(msg, "x", r); |
| 82 | } |
| 83 | |
| 84 | static int get_handler(sd_bus *bus __attribute__((unused)), |
| 85 | const char *path __attribute__((unused)), |
| 86 | const char *interface __attribute__((unused)), |
| 87 | const char *property __attribute__((unused)), |
| 88 | sd_bus_message *reply, void *userdata, |
| 89 | sd_bus_error *error __attribute__((unused))) |
| 90 | { |
| 91 | struct console *console = userdata; |
| 92 | uint32_t baudrate; |
| 93 | int r; |
| 94 | |
| 95 | baudrate = parse_baud_to_int(console->tty_baud); |
| 96 | if (!baudrate) { |
| 97 | warnx("Invalid baud rate: '%d'", console->tty_baud); |
| 98 | } |
| 99 | |
| 100 | r = sd_bus_message_append(reply, "u", baudrate); |
| 101 | |
| 102 | return r; |
| 103 | } |
| 104 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 105 | static int get_socket_name(sd_bus *bus __attribute__((unused)), |
| 106 | const char *path __attribute__((unused)), |
| 107 | const char *interface __attribute__((unused)), |
| 108 | const char *property __attribute__((unused)), |
| 109 | sd_bus_message *reply, void *userdata, |
| 110 | sd_bus_error *error __attribute__((unused))) |
| 111 | { |
| 112 | struct console *console = userdata; |
| 113 | |
| 114 | /* The abstract socket name starts with null character hence we need to |
| 115 | * send it as a byte stream instead of regular string. |
| 116 | */ |
| 117 | return sd_bus_message_append_array(reply, 'y', console->socket_name, |
| 118 | console->socket_name_len); |
| 119 | } |
| 120 | |
| 121 | static const sd_bus_vtable console_tty_vtable[] = { |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 122 | SD_BUS_VTABLE_START(0), |
| 123 | SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate, |
| 124 | SD_BUS_VTABLE_UNPRIVILEGED), |
| 125 | SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0), |
| 126 | SD_BUS_VTABLE_END, |
| 127 | }; |
| 128 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 129 | static const sd_bus_vtable console_access_vtable[] = { |
| 130 | SD_BUS_VTABLE_START(0), |
| 131 | SD_BUS_PROPERTY("SocketName", "ay", get_socket_name, 0, 0), |
| 132 | SD_BUS_VTABLE_END, |
| 133 | }; |
| 134 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 135 | void dbus_init(struct console *console, |
| 136 | struct config *config __attribute__((unused))) |
| 137 | { |
| 138 | char obj_name[dbus_obj_path_len]; |
| 139 | char dbus_name[dbus_obj_path_len]; |
| 140 | int dbus_poller = 0; |
| 141 | int fd; |
| 142 | int r; |
| 143 | size_t bytes; |
| 144 | |
| 145 | if (!console) { |
| 146 | warnx("Couldn't get valid console"); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | r = sd_bus_default_system(&console->bus); |
| 151 | if (r < 0) { |
| 152 | warnx("Failed to connect to system bus: %s", strerror(-r)); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | /* Register support console interface */ |
| 157 | bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME, |
| 158 | console->console_id); |
| 159 | if (bytes >= dbus_obj_path_len) { |
| 160 | warnx("Console id '%s' is too long. There is no enough space in the buffer.", |
| 161 | console->console_id); |
| 162 | return; |
| 163 | } |
| 164 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 165 | /* Register tty interface */ |
| 166 | r = sd_bus_add_object_vtable(console->bus, NULL, obj_name, TTY_INTF, |
| 167 | console_tty_vtable, console); |
| 168 | if (r < 0) { |
| 169 | warnx("Failed to issue method call: %s", strerror(-r)); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | /* Register access interface */ |
| 174 | r = sd_bus_add_object_vtable(console->bus, NULL, obj_name, ACCESS_INTF, |
| 175 | console_access_vtable, console); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 176 | if (r < 0) { |
| 177 | warnx("Failed to issue method call: %s", strerror(-r)); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME, |
| 182 | console->console_id); |
| 183 | if (bytes >= dbus_obj_path_len) { |
| 184 | warnx("Console id '%s' is too long. There is no enough space in the buffer.", |
| 185 | console->console_id); |
| 186 | return; |
| 187 | } |
| 188 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 189 | /* Finally register the bus name */ |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 190 | r = sd_bus_request_name(console->bus, dbus_name, |
| 191 | SD_BUS_NAME_ALLOW_REPLACEMENT | |
| 192 | SD_BUS_NAME_REPLACE_EXISTING); |
| 193 | if (r < 0) { |
| 194 | warnx("Failed to acquire service name: %s", strerror(-r)); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | fd = sd_bus_get_fd(console->bus); |
| 199 | if (fd < 0) { |
| 200 | warnx("Couldn't get the bus file descriptor"); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | dbus_poller = POLLFD_DBUS; |
| 205 | |
| 206 | console->pollfds[dbus_poller].fd = fd; |
| 207 | console->pollfds[dbus_poller].events = POLLIN; |
| 208 | } |