Jeremy Kerr | 9326d77 | 2016-03-17 17:15:02 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 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 | */ |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 16 | |
| 17 | #include <err.h> |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 18 | #include <errno.h> |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 19 | #include <getopt.h> |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 20 | #include <stdbool.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <termios.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <sys/socket.h> |
| 29 | #include <sys/un.h> |
| 30 | |
| 31 | #include "console-server.h" |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 32 | #include "config.h" |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 33 | |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 34 | #define EXIT_ESCAPE 2 |
| 35 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 36 | enum process_rc { |
| 37 | PROCESS_OK = 0, |
| 38 | PROCESS_ERR, |
| 39 | PROCESS_EXIT, |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 40 | PROCESS_ESC, |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 41 | }; |
| 42 | |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 43 | enum esc_type { |
| 44 | ESC_TYPE_SSH, |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 45 | ESC_TYPE_STR, |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | struct ssh_esc_state { |
| 49 | uint8_t state; |
| 50 | }; |
| 51 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 52 | struct str_esc_state { |
| 53 | const uint8_t *str; |
| 54 | size_t pos; |
| 55 | }; |
| 56 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 57 | struct console_client { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 58 | int console_sd; |
| 59 | int fd_in; |
| 60 | int fd_out; |
| 61 | bool is_tty; |
| 62 | struct termios orig_termios; |
| 63 | enum esc_type esc_type; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 64 | union { |
| 65 | struct ssh_esc_state ssh; |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 66 | struct str_esc_state str; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 67 | } esc_state; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 68 | }; |
| 69 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 70 | static enum process_rc process_ssh_tty(struct console_client *client, |
| 71 | const uint8_t *buf, size_t len) |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 72 | { |
| 73 | struct ssh_esc_state *esc_state = &client->esc_state.ssh; |
| 74 | const uint8_t *out_buf = buf; |
| 75 | int rc; |
| 76 | |
| 77 | for (size_t i = 0; i < len; ++i) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 78 | switch (buf[i]) { |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 79 | case '.': |
| 80 | if (esc_state->state != '~') { |
| 81 | esc_state->state = '\0'; |
| 82 | break; |
| 83 | } |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 84 | return PROCESS_ESC; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 85 | case '~': |
| 86 | if (esc_state->state != '\r') { |
| 87 | esc_state->state = '\0'; |
| 88 | break; |
| 89 | } |
| 90 | esc_state->state = '~'; |
| 91 | /* We need to print everything to skip the tilde */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 92 | rc = write_buf_to_fd(client->console_sd, out_buf, |
| 93 | i - (out_buf - buf)); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 94 | if (rc < 0) { |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 95 | return PROCESS_ERR; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 96 | } |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 97 | out_buf = &buf[i + 1]; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 98 | break; |
| 99 | case '\r': |
| 100 | esc_state->state = '\r'; |
| 101 | break; |
| 102 | default: |
| 103 | esc_state->state = '\0'; |
| 104 | } |
| 105 | } |
| 106 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 107 | rc = write_buf_to_fd(client->console_sd, out_buf, |
| 108 | len - (out_buf - buf)); |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 109 | return rc < 0 ? PROCESS_ERR : PROCESS_OK; |
| 110 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 111 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 112 | static enum process_rc process_str_tty(struct console_client *client, |
| 113 | const uint8_t *buf, size_t len) |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 114 | { |
| 115 | struct str_esc_state *esc_state = &client->esc_state.str; |
| 116 | enum process_rc prc = PROCESS_OK; |
| 117 | size_t i; |
| 118 | |
| 119 | for (i = 0; i < len; ++i) { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 120 | if (buf[i] == esc_state->str[esc_state->pos]) { |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 121 | esc_state->pos++; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 122 | } else { |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 123 | esc_state->pos = 0; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 124 | } |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 125 | |
| 126 | if (esc_state->str[esc_state->pos] == '\0') { |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 127 | prc = PROCESS_ESC; |
William A. Kennington III | 5b16dc8 | 2019-07-26 01:18:05 -0700 | [diff] [blame] | 128 | ++i; |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 133 | if (write_buf_to_fd(client->console_sd, buf, i) < 0) { |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 134 | return PROCESS_ERR; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 135 | } |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 136 | return prc; |
| 137 | } |
| 138 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 139 | static enum process_rc process_tty(struct console_client *client) |
| 140 | { |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 141 | uint8_t buf[4096]; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 142 | ssize_t len; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 143 | |
| 144 | len = read(client->fd_in, buf, sizeof(buf)); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 145 | if (len < 0) { |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 146 | return PROCESS_ERR; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 147 | } |
| 148 | if (len == 0) { |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 149 | return PROCESS_EXIT; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 150 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 151 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 152 | switch (client->esc_type) { |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 153 | case ESC_TYPE_SSH: |
| 154 | return process_ssh_tty(client, buf, len); |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 155 | case ESC_TYPE_STR: |
| 156 | return process_str_tty(client, buf, len); |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 157 | default: |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 158 | return PROCESS_ERR; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 159 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 160 | } |
| 161 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 162 | static int process_console(struct console_client *client) |
| 163 | { |
| 164 | uint8_t buf[4096]; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 165 | ssize_t len; |
| 166 | int rc; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 167 | |
| 168 | len = read(client->console_sd, buf, sizeof(buf)); |
| 169 | if (len < 0) { |
| 170 | warn("Can't read from server"); |
| 171 | return PROCESS_ERR; |
| 172 | } |
| 173 | if (len == 0) { |
| 174 | fprintf(stderr, "Connection closed\n"); |
| 175 | return PROCESS_EXIT; |
| 176 | } |
| 177 | |
| 178 | rc = write_buf_to_fd(client->fd_out, buf, len); |
| 179 | return rc ? PROCESS_ERR : PROCESS_OK; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Setup our local file descriptors for IO: use stdin/stdout, and if we're on a |
| 184 | * TTY, put it in canonical mode |
| 185 | */ |
| 186 | static int client_tty_init(struct console_client *client) |
| 187 | { |
| 188 | struct termios termios; |
| 189 | int rc; |
| 190 | |
| 191 | client->fd_in = STDIN_FILENO; |
| 192 | client->fd_out = STDOUT_FILENO; |
| 193 | client->is_tty = isatty(client->fd_in); |
| 194 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 195 | if (!client->is_tty) { |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 196 | return 0; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 197 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 198 | |
| 199 | rc = tcgetattr(client->fd_in, &termios); |
| 200 | if (rc) { |
| 201 | warn("Can't get terminal attributes for console"); |
| 202 | return -1; |
| 203 | } |
| 204 | memcpy(&client->orig_termios, &termios, sizeof(client->orig_termios)); |
| 205 | cfmakeraw(&termios); |
| 206 | |
| 207 | rc = tcsetattr(client->fd_in, TCSANOW, &termios); |
| 208 | if (rc) { |
| 209 | warn("Can't set terminal attributes for console"); |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 216 | static int client_init(struct console_client *client, struct config *config, |
| 217 | const char *console_id) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 218 | { |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 219 | const char *resolved_id = NULL; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 220 | struct sockaddr_un addr; |
Andrew Jeffery | 6ed0e4e | 2020-02-21 14:35:39 +1030 | [diff] [blame] | 221 | socket_path_t path; |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 222 | ssize_t len; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 223 | int rc; |
| 224 | |
| 225 | client->console_sd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 226 | if (!client->console_sd) { |
| 227 | warn("Can't open socket"); |
| 228 | return -1; |
| 229 | } |
| 230 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 231 | /* Get the console id */ |
| 232 | resolved_id = config_resolve_console_id(config, console_id); |
| 233 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 234 | memset(&addr, 0, sizeof(addr)); |
| 235 | addr.sun_family = AF_UNIX; |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 236 | len = console_socket_path(addr.sun_path, resolved_id); |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 237 | if (len < 0) { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 238 | if (errno) { |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 239 | warn("Failed to configure socket: %s", strerror(errno)); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 240 | } else { |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 241 | warn("Socket name length exceeds buffer limits"); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 242 | } |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 243 | goto cleanup; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 244 | } |
| 245 | |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 246 | rc = connect(client->console_sd, (struct sockaddr *)&addr, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 247 | sizeof(addr) - sizeof(addr.sun_path) + len); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 248 | if (!rc) { |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 249 | return 0; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 250 | } |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 251 | |
Andrew Jeffery | 6ed0e4e | 2020-02-21 14:35:39 +1030 | [diff] [blame] | 252 | console_socket_path_readable(&addr, len, path); |
| 253 | warn("Can't connect to console server '@%s'", path); |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 254 | cleanup: |
| 255 | close(client->console_sd); |
| 256 | return -1; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static void client_fini(struct console_client *client) |
| 260 | { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 261 | if (client->is_tty) { |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 262 | tcsetattr(client->fd_in, TCSANOW, &client->orig_termios); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 263 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 264 | close(client->console_sd); |
| 265 | } |
| 266 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 267 | int main(int argc, char *argv[]) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 268 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 269 | struct console_client _client; |
| 270 | struct console_client *client; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 271 | struct pollfd pollfds[2]; |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 272 | enum process_rc prc = PROCESS_OK; |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 273 | const char *config_path = NULL; |
| 274 | struct config *config = NULL; |
Andrew Jeffery | 9a8f30e | 2023-05-02 13:56:02 +0930 | [diff] [blame] | 275 | const char *console_id = NULL; |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 276 | const uint8_t *esc = NULL; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 277 | int rc; |
| 278 | |
| 279 | client = &_client; |
| 280 | memset(client, 0, sizeof(*client)); |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 281 | client->esc_type = ESC_TYPE_SSH; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 282 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 283 | for (;;) { |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 284 | rc = getopt(argc, argv, "c:e:i:"); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 285 | if (rc == -1) { |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 286 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 287 | } |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 288 | |
| 289 | switch (rc) { |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 290 | case 'c': |
| 291 | if (optarg[0] == '\0') { |
| 292 | fprintf(stderr, "Config str cannot be empty\n"); |
| 293 | return EXIT_FAILURE; |
| 294 | } |
| 295 | config_path = optarg; |
| 296 | break; |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 297 | case 'e': |
| 298 | if (optarg[0] == '\0') { |
| 299 | fprintf(stderr, "Escape str cannot be empty\n"); |
| 300 | return EXIT_FAILURE; |
| 301 | } |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 302 | esc = (const uint8_t *)optarg; |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 303 | break; |
Andrew Jeffery | ddf2ab7 | 2020-02-10 12:36:09 +1030 | [diff] [blame] | 304 | case 'i': |
| 305 | if (optarg[0] == '\0') { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 306 | fprintf(stderr, |
| 307 | "Socket ID str cannot be empty\n"); |
Andrew Jeffery | ddf2ab7 | 2020-02-10 12:36:09 +1030 | [diff] [blame] | 308 | return EXIT_FAILURE; |
| 309 | } |
Andrew Jeffery | 9a8f30e | 2023-05-02 13:56:02 +0930 | [diff] [blame] | 310 | console_id = optarg; |
Andrew Jeffery | ddf2ab7 | 2020-02-10 12:36:09 +1030 | [diff] [blame] | 311 | break; |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 312 | default: |
| 313 | fprintf(stderr, |
| 314 | "Usage: %s " |
Andrew Jeffery | ddf2ab7 | 2020-02-10 12:36:09 +1030 | [diff] [blame] | 315 | "[-e <escape sequence>]" |
Andrew Jeffery | 9a8f30e | 2023-05-02 13:56:02 +0930 | [diff] [blame] | 316 | "[-i <console ID>]" |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 317 | "[-c <config>]\n", |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 318 | argv[0]); |
| 319 | return EXIT_FAILURE; |
| 320 | } |
| 321 | } |
| 322 | |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 323 | if (config_path) { |
| 324 | config = config_init(config_path); |
| 325 | if (!config) { |
| 326 | warnx("Can't read configuration, exiting."); |
| 327 | return EXIT_FAILURE; |
| 328 | } |
| 329 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 330 | if (!esc) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 331 | esc = (const uint8_t *)config_get_value( |
| 332 | config, "escape-sequence"); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 333 | } |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | if (esc) { |
| 337 | client->esc_type = ESC_TYPE_STR; |
| 338 | client->esc_state.str.str = esc; |
| 339 | } |
| 340 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 341 | rc = client_init(client, config, console_id); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 342 | if (rc) { |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 343 | goto out_config_fini; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 344 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 345 | |
| 346 | rc = client_tty_init(client); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 347 | if (rc) { |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 348 | goto out_client_fini; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 349 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 350 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 351 | for (;;) { |
| 352 | pollfds[0].fd = client->fd_in; |
| 353 | pollfds[0].events = POLLIN; |
| 354 | pollfds[1].fd = client->console_sd; |
| 355 | pollfds[1].events = POLLIN; |
| 356 | |
| 357 | rc = poll(pollfds, 2, -1); |
| 358 | if (rc < 0) { |
| 359 | warn("Poll failure"); |
| 360 | break; |
| 361 | } |
| 362 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 363 | if (pollfds[0].revents) { |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 364 | prc = process_tty(client); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 365 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 366 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 367 | if (prc == PROCESS_OK && pollfds[1].revents) { |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 368 | prc = process_console(client); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 369 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 370 | |
| 371 | rc = (prc == PROCESS_ERR) ? -1 : 0; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 372 | if (prc != PROCESS_OK) { |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 373 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 374 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 375 | } |
| 376 | |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 377 | out_client_fini: |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 378 | client_fini(client); |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 379 | |
| 380 | out_config_fini: |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 381 | if (config_path) { |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 382 | config_fini(config); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 383 | } |
Andrew Jeffery | 71e7a24 | 2020-02-12 22:58:16 +1030 | [diff] [blame] | 384 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 385 | if (prc == PROCESS_ESC) { |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 386 | return EXIT_ESCAPE; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 387 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 388 | return rc ? EXIT_FAILURE : EXIT_SUCCESS; |
| 389 | } |