Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 1 | /* Copyright 2018 IBM Corp. |
| 2 | * |
| 3 | * Author: Jeremy Kerr <jk@ozlabs.org> |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 6 | * use this file except in compliance with the License. You may obtain a copy |
| 7 | * of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | */ |
Ed Tanous | 9ac5231 | 2023-07-26 12:18:47 -0700 | [diff] [blame] | 17 | // NOLINTBEGIN |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 18 | #define _GNU_SOURCE |
| 19 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 20 | #include "config.h" |
| 21 | |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 22 | #include <dirent.h> |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 23 | #include <err.h> |
| 24 | #include <errno.h> |
| 25 | #include <fcntl.h> |
Jeremy Kerr | 6f9c433 | 2018-08-03 17:11:15 +0800 | [diff] [blame] | 26 | #include <getopt.h> |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 27 | #include <json.h> |
| 28 | #include <libudev.h> |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 29 | #include <limits.h> |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | #include <stdbool.h> |
| 32 | #include <stdint.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 36 | #include <sys/poll.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/stat.h> |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 39 | #include <sys/types.h> |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 40 | #include <sys/un.h> |
| 41 | #include <sys/wait.h> |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 42 | #include <unistd.h> |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 43 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 44 | struct config |
| 45 | { |
| 46 | char* name; |
| 47 | bool is_default; |
| 48 | char* nbd_device; |
| 49 | struct json_object* metadata; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 50 | }; |
| 51 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 52 | struct ctx |
| 53 | { |
| 54 | int sock; |
| 55 | int sock_client; |
| 56 | int signal_pipe[2]; |
| 57 | char* sock_path; |
| 58 | pid_t nbd_client_pid; |
| 59 | pid_t state_hook_pid; |
| 60 | int nbd_timeout; |
| 61 | dev_t nbd_devno; |
| 62 | uint8_t* buf; |
| 63 | size_t bufsize; |
| 64 | struct config* configs; |
| 65 | int n_configs; |
| 66 | struct config* default_config; |
| 67 | struct config* config; |
| 68 | struct udev* udev; |
| 69 | struct udev_monitor* monitor; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 70 | }; |
| 71 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 72 | static const char* conf_path = SYSCONFDIR "/nbd-proxy/config.json"; |
| 73 | static const char* state_hook_path = SYSCONFDIR "/nbd-proxy/state"; |
| 74 | static const char* sockpath_tmpl = RUNSTATEDIR "/nbd.%d.sock"; |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 75 | |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 76 | static const size_t bufsize = 0x20000; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 77 | static const int nbd_timeout_default = 30; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 78 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 79 | static int open_nbd_socket(struct ctx* ctx) |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 80 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 81 | struct sockaddr_un addr; |
| 82 | char* path; |
| 83 | int sd, rc; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 84 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 85 | rc = asprintf(&path, sockpath_tmpl, getpid()); |
| 86 | if (rc < 0) |
| 87 | return -1; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 88 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 89 | sd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 90 | if (sd < 0) |
| 91 | { |
| 92 | warn("can't create socket"); |
| 93 | goto err_free; |
| 94 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 95 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 96 | rc = fchmod(sd, S_IRUSR | S_IWUSR); |
| 97 | if (rc) |
| 98 | { |
| 99 | warn("can't set permissions on socket"); |
| 100 | goto err_close; |
| 101 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 102 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 103 | addr.sun_family = AF_UNIX; |
| 104 | strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 105 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 106 | rc = bind(sd, (struct sockaddr*)&addr, sizeof(addr)); |
| 107 | if (rc) |
| 108 | { |
| 109 | warn("can't bind to path %s", path); |
| 110 | goto err_close; |
| 111 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 112 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 113 | rc = listen(sd, 1); |
| 114 | if (rc) |
| 115 | { |
| 116 | warn("can't listen on socket %s", path); |
| 117 | goto err_unlink; |
| 118 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 119 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 120 | ctx->sock = sd; |
| 121 | ctx->sock_path = path; |
| 122 | return 0; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 123 | |
| 124 | err_unlink: |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 125 | unlink(path); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 126 | err_close: |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 127 | close(sd); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 128 | err_free: |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 129 | free(path); |
| 130 | return -1; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 131 | } |
| 132 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 133 | static int start_nbd_client(struct ctx* ctx) |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 134 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 135 | pid_t pid; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 136 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 137 | pid = fork(); |
| 138 | if (pid < 0) |
| 139 | { |
| 140 | warn("can't create client process"); |
| 141 | return -1; |
| 142 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 143 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 144 | /* child process: run nbd-client in non-fork mode */ |
| 145 | if (pid == 0) |
| 146 | { |
| 147 | char timeout_str[10]; |
| 148 | int fd; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 149 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 150 | snprintf(timeout_str, sizeof(timeout_str), "%d", ctx->nbd_timeout); |
Jeremy Kerr | 09295f2 | 2018-07-27 16:29:38 +0800 | [diff] [blame] | 151 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 152 | fd = open("/dev/null", O_RDWR | O_CLOEXEC); |
| 153 | if (fd < 0) |
| 154 | err(EXIT_FAILURE, "can't open /dev/null"); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 155 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 156 | dup2(fd, STDIN_FILENO); |
| 157 | dup2(fd, STDOUT_FILENO); |
| 158 | close(fd); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 159 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 160 | execlp("nbd-client", "nbd-client", "-u", ctx->sock_path, "-n", "-L", |
| 161 | "-t", timeout_str, ctx->config->nbd_device, NULL); |
| 162 | err(EXIT_FAILURE, "can't start ndb client"); |
| 163 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 164 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 165 | ctx->nbd_client_pid = pid; |
| 166 | return 0; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 167 | } |
| 168 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 169 | static void stop_nbd_client(struct ctx* ctx) |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 170 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 171 | int rc; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 172 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 173 | if (!ctx->nbd_client_pid) |
| 174 | return; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 175 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 176 | rc = kill(ctx->nbd_client_pid, SIGTERM); |
| 177 | if (rc) |
| 178 | return; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 179 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 180 | waitpid(ctx->nbd_client_pid, NULL, 0); |
| 181 | ctx->nbd_client_pid = 0; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 184 | static int copy_fd(struct ctx* ctx, int fd_in, int fd_out) |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 185 | { |
| 186 | #ifdef HAVE_SPLICE |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 187 | int rc; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 188 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 189 | rc = splice(fd_in, NULL, fd_out, NULL, ctx->bufsize, 0); |
| 190 | if (rc < 0) |
| 191 | warn("splice"); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 192 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 193 | return rc; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 194 | #else |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 195 | size_t len, pos; |
| 196 | ssize_t rc; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 197 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 198 | for (;;) |
| 199 | { |
| 200 | errno = 0; |
| 201 | rc = read(fd_in, ctx->buf, ctx->bufsize); |
| 202 | if (rc < 0) |
| 203 | { |
| 204 | if (errno == EINTR) |
| 205 | continue; |
| 206 | warn("read failure"); |
| 207 | return -1; |
| 208 | } |
| 209 | if (rc == 0) |
| 210 | return 0; |
| 211 | break; |
| 212 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 213 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 214 | len = rc; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 215 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 216 | for (pos = 0; pos < len;) |
| 217 | { |
| 218 | errno = 0; |
| 219 | rc = write(fd_out, ctx->buf + pos, len - pos); |
| 220 | if (rc < 0) |
| 221 | { |
| 222 | if (errno == EINTR) |
| 223 | continue; |
| 224 | warn("write failure"); |
| 225 | return -1; |
| 226 | } |
| 227 | if (rc == 0) |
| 228 | break; |
| 229 | pos += rc; |
| 230 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 231 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 232 | return pos; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 233 | #endif |
| 234 | } |
| 235 | |
| 236 | static int signal_pipe_fd = -1; |
| 237 | |
| 238 | static void signal_handler(int signal) |
| 239 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 240 | int rc; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 241 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 242 | rc = write(signal_pipe_fd, &signal, sizeof(signal)); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 243 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 244 | /* not a lot we can do here but exit... */ |
| 245 | if (rc != sizeof(signal)) |
| 246 | exit(EXIT_FAILURE); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 247 | } |
| 248 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 249 | static int setup_signals(struct ctx* ctx) |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 250 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 251 | struct sigaction sa; |
| 252 | int rc; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 253 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 254 | rc = pipe2(ctx->signal_pipe, O_CLOEXEC); |
| 255 | if (rc) |
| 256 | { |
| 257 | warn("cant setup signal pipe"); |
| 258 | return -1; |
| 259 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 260 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 261 | signal_pipe_fd = ctx->signal_pipe[1]; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 262 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 263 | memset(&sa, 0, sizeof(sa)); |
| 264 | sa.sa_handler = signal_handler; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 265 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 266 | sigaction(SIGINT, &sa, NULL); |
| 267 | sigaction(SIGTERM, &sa, NULL); |
| 268 | sigaction(SIGCHLD, &sa, NULL); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 269 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 270 | return 0; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 271 | } |
| 272 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 273 | static void cleanup_signals(struct ctx* ctx) |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 274 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 275 | struct sigaction sa; |
| 276 | memset(&sa, 0, sizeof(sa)); |
| 277 | sa.sa_handler = SIG_DFL; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 278 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 279 | sigaction(SIGINT, &sa, NULL); |
| 280 | sigaction(SIGTERM, &sa, NULL); |
| 281 | sigaction(SIGCHLD, &sa, NULL); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 282 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 283 | close(ctx->signal_pipe[0]); |
| 284 | close(ctx->signal_pipe[1]); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 285 | } |
| 286 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 287 | static void process_sigchld(struct ctx* ctx, bool* exit) |
Jeremy Kerr | fa1d375 | 2018-08-13 13:13:16 +0800 | [diff] [blame] | 288 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 289 | int status; |
| 290 | pid_t pid; |
Jeremy Kerr | fa1d375 | 2018-08-13 13:13:16 +0800 | [diff] [blame] | 291 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 292 | for (;;) |
| 293 | { |
| 294 | pid = waitpid(-1, &status, WNOHANG); |
| 295 | if (pid == 0) |
| 296 | break; |
Jeremy Kerr | fa1d375 | 2018-08-13 13:13:16 +0800 | [diff] [blame] | 297 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 298 | if (pid == ctx->nbd_client_pid) |
| 299 | { |
| 300 | warnx("nbd client stopped (%s: %d); exiting", |
| 301 | WIFEXITED(status) ? "rc" : "sig", |
| 302 | WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)); |
| 303 | ctx->nbd_client_pid = 0; |
| 304 | *exit = true; |
| 305 | } |
| 306 | else if (pid == ctx->state_hook_pid) |
| 307 | { |
| 308 | if (!WIFEXITED(status) || WEXITSTATUS(status)) |
| 309 | { |
| 310 | warnx("state hook failed (%s: %d); exiting", |
| 311 | WIFEXITED(status) ? "rc" : "sig", |
| 312 | WIFEXITED(status) ? WEXITSTATUS(status) |
| 313 | : WTERMSIG(status)); |
| 314 | *exit = true; |
| 315 | } |
| 316 | ctx->state_hook_pid = 0; |
| 317 | } |
| 318 | } |
Jeremy Kerr | fa1d375 | 2018-08-13 13:13:16 +0800 | [diff] [blame] | 319 | } |
| 320 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 321 | static int process_signal_pipe(struct ctx* ctx, bool* exit) |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 322 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 323 | int buf, rc; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 324 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 325 | rc = read(ctx->signal_pipe[0], &buf, sizeof(buf)); |
| 326 | if (rc != sizeof(buf)) |
| 327 | return -1; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 328 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 329 | *exit = false; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 330 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 331 | switch (buf) |
| 332 | { |
| 333 | case SIGCHLD: |
| 334 | process_sigchld(ctx, exit); |
| 335 | break; |
| 336 | case SIGINT: |
| 337 | case SIGTERM: |
| 338 | *exit = true; |
| 339 | break; |
| 340 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 341 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 342 | return 0; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 343 | } |
| 344 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 345 | static int wait_for_nbd_client(struct ctx* ctx) |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 346 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 347 | struct pollfd pollfds[2]; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 348 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 349 | pollfds[0].fd = ctx->sock; |
| 350 | pollfds[0].events = POLLIN; |
| 351 | pollfds[1].fd = ctx->signal_pipe[0]; |
| 352 | pollfds[1].events = POLLIN; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 353 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 354 | for (;;) |
| 355 | { |
| 356 | errno = 0; |
Ed Tanous | 9ac5231 | 2023-07-26 12:18:47 -0700 | [diff] [blame] | 357 | int rc; |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 358 | rc = poll(pollfds, 2, -1); |
| 359 | if (rc < 0) |
| 360 | { |
| 361 | if (errno == EINTR) |
| 362 | continue; |
| 363 | warn("poll failed"); |
| 364 | return -1; |
| 365 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 366 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 367 | if (pollfds[0].revents) |
| 368 | { |
| 369 | rc = accept4(ctx->sock, NULL, NULL, SOCK_CLOEXEC); |
| 370 | if (rc < 0) |
| 371 | { |
| 372 | warn("can't create connection"); |
| 373 | return -1; |
| 374 | } |
| 375 | ctx->sock_client = rc; |
| 376 | break; |
| 377 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 378 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 379 | if (pollfds[1].revents) |
| 380 | { |
| 381 | bool exit; |
| 382 | rc = process_signal_pipe(ctx, &exit); |
| 383 | if (rc || exit) |
| 384 | return -1; |
| 385 | } |
| 386 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 387 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 388 | return 0; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 389 | } |
| 390 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 391 | static int run_state_hook(struct ctx* ctx, const char* action, bool wait) |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 392 | { |
Ed Tanous | 9ac5231 | 2023-07-26 12:18:47 -0700 | [diff] [blame] | 393 | int status, rc; |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 394 | pid_t pid; |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 395 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 396 | /* if the hook isn't present or executable, that's not necessarily |
| 397 | * an error condition */ |
| 398 | if (access(state_hook_path, X_OK)) |
| 399 | return 0; |
Jeremy Kerr | a87af84 | 2018-08-13 11:48:23 +0800 | [diff] [blame] | 400 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 401 | pid = fork(); |
| 402 | if (pid < 0) |
| 403 | { |
| 404 | warn("can't fork to execute hook %s", state_hook_path); |
| 405 | return -1; |
| 406 | } |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 407 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 408 | if (!pid) |
| 409 | { |
| 410 | const char* argv0; |
Jeremy Kerr | a87af84 | 2018-08-13 11:48:23 +0800 | [diff] [blame] | 411 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 412 | argv0 = strchr(state_hook_path, '/'); |
| 413 | if (!argv0) |
| 414 | argv0 = state_hook_path; |
Jeremy Kerr | a87af84 | 2018-08-13 11:48:23 +0800 | [diff] [blame] | 415 | |
Ed Tanous | 9ac5231 | 2023-07-26 12:18:47 -0700 | [diff] [blame] | 416 | int fd; |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 417 | fd = open("/dev/null", O_RDWR | O_CLOEXEC); |
| 418 | if (fd < 0) |
| 419 | exit(EXIT_FAILURE); |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 420 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 421 | dup2(fd, STDIN_FILENO); |
| 422 | dup2(fd, STDOUT_FILENO); |
| 423 | execl(state_hook_path, argv0, action, ctx->config->name, NULL); |
| 424 | exit(EXIT_FAILURE); |
| 425 | } |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 426 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 427 | if (!wait) |
| 428 | { |
| 429 | ctx->state_hook_pid = pid; |
| 430 | return 0; |
| 431 | } |
Jeremy Kerr | fa1d375 | 2018-08-13 13:13:16 +0800 | [diff] [blame] | 432 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 433 | rc = waitpid(pid, &status, 0); |
| 434 | if (rc < 0) |
| 435 | { |
| 436 | warn("wait"); |
| 437 | return -1; |
| 438 | } |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 439 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 440 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 441 | { |
| 442 | warnx("hook %s failed", state_hook_path); |
| 443 | return -1; |
| 444 | } |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 445 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 446 | return 0; |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 447 | } |
| 448 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 449 | static int udev_init(struct ctx* ctx) |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 450 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 451 | int rc; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 452 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 453 | ctx->udev = udev_new(); |
Ed Tanous | ba2a636 | 2023-07-26 12:16:09 -0700 | [diff] [blame] | 454 | if (!ctx->udev) |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 455 | { |
| 456 | warn("can't create udev object"); |
| 457 | return -1; |
| 458 | } |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 459 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 460 | ctx->monitor = udev_monitor_new_from_netlink(ctx->udev, "kernel"); |
| 461 | if (!ctx->monitor) |
| 462 | { |
| 463 | warn("can't create udev monitor"); |
| 464 | goto out_unref_udev; |
| 465 | } |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 466 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 467 | rc = udev_monitor_filter_add_match_subsystem_devtype(ctx->monitor, "block", |
| 468 | "disk"); |
| 469 | if (rc) |
| 470 | { |
| 471 | warn("can't create udev monitor filter"); |
| 472 | goto out_unref_monitor; |
| 473 | } |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 474 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 475 | rc = udev_monitor_enable_receiving(ctx->monitor); |
| 476 | if (rc) |
| 477 | { |
| 478 | warn("can't start udev monitor"); |
| 479 | goto out_unref_monitor; |
| 480 | } |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 481 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 482 | return 0; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 483 | |
| 484 | out_unref_monitor: |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 485 | udev_monitor_unref(ctx->monitor); |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 486 | out_unref_udev: |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 487 | udev_unref(ctx->udev); |
| 488 | return -1; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 489 | } |
| 490 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 491 | static void udev_free(struct ctx* ctx) |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 492 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 493 | udev_monitor_unref(ctx->monitor); |
| 494 | udev_unref(ctx->udev); |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* Check for the change event on our nbd device, signifying that the kernel |
| 498 | * has finished initialising the block device. Once we see the event, we run |
| 499 | * the "start" state hook, and close the udev monitor. |
| 500 | * |
| 501 | * Returns: |
| 502 | * 0 if no processing was performed |
| 503 | * -1 on state hook error (and the nbd session should be closed) |
| 504 | */ |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 505 | static int udev_process(struct ctx* ctx) |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 506 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 507 | struct udev_device* dev; |
| 508 | bool action_is_change; |
| 509 | dev_t devno; |
| 510 | int rc; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 511 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 512 | dev = udev_monitor_receive_device(ctx->monitor); |
| 513 | if (!dev) |
| 514 | return 0; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 515 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 516 | devno = udev_device_get_devnum(dev); |
| 517 | action_is_change = !strcmp(udev_device_get_action(dev), "change"); |
| 518 | udev_device_unref(dev); |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 519 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 520 | if (devno != ctx->nbd_devno) |
| 521 | return 0; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 522 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 523 | if (!action_is_change) |
| 524 | return 0; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 525 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 526 | udev_monitor_unref(ctx->monitor); |
| 527 | udev_unref(ctx->udev); |
| 528 | ctx->monitor = NULL; |
| 529 | ctx->udev = NULL; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 530 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 531 | rc = run_state_hook(ctx, "start", false); |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 532 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 533 | return rc; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 534 | } |
| 535 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 536 | static int run_proxy(struct ctx* ctx) |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 537 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 538 | struct pollfd pollfds[4]; |
| 539 | bool exit = false; |
| 540 | int rc, n_fd; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 541 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 542 | /* main proxy: forward data between stdio & socket */ |
| 543 | pollfds[0].fd = ctx->sock_client; |
| 544 | pollfds[0].events = POLLIN; |
| 545 | pollfds[1].fd = STDIN_FILENO; |
| 546 | pollfds[1].events = POLLIN; |
| 547 | pollfds[2].fd = ctx->signal_pipe[0]; |
| 548 | pollfds[2].events = POLLIN; |
| 549 | pollfds[3].fd = udev_monitor_get_fd(ctx->monitor); |
| 550 | pollfds[3].events = POLLIN; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 551 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 552 | n_fd = 4; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 553 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 554 | for (;;) |
| 555 | { |
| 556 | errno = 0; |
| 557 | rc = poll(pollfds, n_fd, -1); |
| 558 | if (rc < 0) |
| 559 | { |
| 560 | if (errno == EINTR) |
| 561 | continue; |
| 562 | warn("poll failed"); |
| 563 | break; |
| 564 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 565 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 566 | if (pollfds[0].revents) |
| 567 | { |
| 568 | rc = copy_fd(ctx, ctx->sock_client, STDOUT_FILENO); |
| 569 | if (rc <= 0) |
| 570 | break; |
| 571 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 572 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 573 | if (pollfds[1].revents) |
| 574 | { |
| 575 | rc = copy_fd(ctx, STDIN_FILENO, ctx->sock_client); |
| 576 | if (rc <= 0) |
| 577 | break; |
| 578 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 579 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 580 | if (pollfds[2].revents) |
| 581 | { |
| 582 | rc = process_signal_pipe(ctx, &exit); |
| 583 | if (rc || exit) |
| 584 | break; |
| 585 | } |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 586 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 587 | if (pollfds[3].revents) |
| 588 | { |
| 589 | rc = udev_process(ctx); |
| 590 | if (rc) |
| 591 | break; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 592 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 593 | /* udev_process may have closed the udev connection, |
| 594 | * in which case we can stop polling on its fd */ |
| 595 | if (!ctx->udev) |
| 596 | { |
| 597 | pollfds[3].fd = 0; |
| 598 | pollfds[3].revents = 0; |
| 599 | n_fd = 3; |
| 600 | } |
| 601 | } |
| 602 | } |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 603 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 604 | return rc ? -1 : 0; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 605 | } |
| 606 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 607 | static void print_metadata(struct ctx* ctx) |
Jeremy Kerr | 13bb28f | 2018-08-09 10:41:10 +0800 | [diff] [blame] | 608 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 609 | struct json_object* md; |
| 610 | int i; |
Jeremy Kerr | 13bb28f | 2018-08-09 10:41:10 +0800 | [diff] [blame] | 611 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 612 | md = json_object_new_object(); |
Jeremy Kerr | 13bb28f | 2018-08-09 10:41:10 +0800 | [diff] [blame] | 613 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 614 | for (i = 0; i < ctx->n_configs; i++) |
| 615 | { |
| 616 | struct config* config = &ctx->configs[i]; |
| 617 | json_object_object_add(md, config->name, |
| 618 | json_object_get(config->metadata)); |
| 619 | } |
Jeremy Kerr | 13bb28f | 2018-08-09 10:41:10 +0800 | [diff] [blame] | 620 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 621 | puts(json_object_get_string(md)); |
Jeremy Kerr | 13bb28f | 2018-08-09 10:41:10 +0800 | [diff] [blame] | 622 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 623 | json_object_put(md); |
Jeremy Kerr | 13bb28f | 2018-08-09 10:41:10 +0800 | [diff] [blame] | 624 | } |
| 625 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 626 | static void config_free_one(struct config* config) |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 627 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 628 | if (config->metadata) |
| 629 | json_object_put(config->metadata); |
| 630 | free(config->nbd_device); |
| 631 | free(config->name); |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 632 | } |
| 633 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 634 | static int config_parse_one(struct config* config, const char* name, |
| 635 | json_object* obj) |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 636 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 637 | struct json_object *tmp, *meta; |
| 638 | json_bool jrc; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 639 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 640 | jrc = json_object_object_get_ex(obj, "nbd-device", &tmp); |
| 641 | if (!jrc) |
| 642 | { |
| 643 | warnx("config %s doesn't specify a nbd-device", name); |
| 644 | return -1; |
| 645 | } |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 646 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 647 | if (!json_object_is_type(tmp, json_type_string)) |
| 648 | { |
| 649 | warnx("config %s has invalid nbd-device", name); |
| 650 | return -1; |
| 651 | } |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 652 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 653 | config->nbd_device = strdup(json_object_get_string(tmp)); |
| 654 | config->name = strdup(name); |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 655 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 656 | jrc = json_object_object_get_ex(obj, "default", &tmp); |
| 657 | config->is_default = jrc && json_object_get_boolean(tmp); |
Jeremy Kerr | 0f1a49c | 2018-08-03 15:41:38 +0800 | [diff] [blame] | 658 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 659 | jrc = json_object_object_get_ex(obj, "metadata", &meta); |
| 660 | if (jrc && json_object_is_type(meta, json_type_object)) |
| 661 | config->metadata = json_object_get(meta); |
| 662 | else |
| 663 | config->metadata = NULL; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 664 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 665 | return 0; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 666 | } |
| 667 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 668 | static void config_free(struct ctx* ctx) |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 669 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 670 | int i; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 671 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 672 | for (i = 0; i < ctx->n_configs; i++) |
| 673 | config_free_one(&ctx->configs[i]); |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 674 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 675 | free(ctx->configs); |
| 676 | ctx->n_configs = 0; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 677 | } |
| 678 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 679 | static int config_init(struct ctx* ctx) |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 680 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 681 | struct json_object *obj, *tmp; |
| 682 | json_bool jrc; |
| 683 | int i, rc; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 684 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 685 | /* apply defaults */ |
| 686 | ctx->nbd_timeout = nbd_timeout_default; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 687 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 688 | obj = json_object_from_file(conf_path); |
| 689 | if (!obj) |
| 690 | { |
| 691 | warnx("can't read configuration from %s\n", conf_path); |
| 692 | return -1; |
| 693 | } |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 694 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 695 | /* global configuration */ |
| 696 | jrc = json_object_object_get_ex(obj, "timeout", &tmp); |
| 697 | if (jrc) |
| 698 | { |
| 699 | errno = 0; |
| 700 | ctx->nbd_timeout = json_object_get_int(tmp); |
| 701 | if (ctx->nbd_timeout == 0 && errno) |
| 702 | { |
| 703 | warnx("can't parse timeout value"); |
| 704 | goto err_free; |
| 705 | } |
| 706 | } |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 707 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 708 | /* per-config configuration */ |
| 709 | jrc = json_object_object_get_ex(obj, "configurations", &tmp); |
| 710 | if (!jrc) |
| 711 | { |
| 712 | warnx("no configurations specified"); |
| 713 | goto err_free; |
| 714 | } |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 715 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 716 | if (!json_object_is_type(tmp, json_type_object)) |
| 717 | { |
| 718 | warnx("invalid configurations format"); |
| 719 | goto err_free; |
| 720 | } |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 721 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 722 | ctx->n_configs = json_object_object_length(tmp); |
| 723 | ctx->configs = calloc(ctx->n_configs, sizeof(*ctx->configs)); |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 724 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 725 | i = 0; |
| 726 | json_object_object_foreach(tmp, name, config_json) |
| 727 | { |
| 728 | struct config* config = &ctx->configs[i]; |
Jeremy Kerr | 0f1a49c | 2018-08-03 15:41:38 +0800 | [diff] [blame] | 729 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 730 | rc = config_parse_one(config, name, config_json); |
| 731 | if (rc) |
| 732 | goto err_free; |
Jeremy Kerr | 0f1a49c | 2018-08-03 15:41:38 +0800 | [diff] [blame] | 733 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 734 | if (config->is_default) |
| 735 | { |
| 736 | if (ctx->default_config) |
| 737 | { |
| 738 | warn("multiple configs flagged as default"); |
| 739 | goto err_free; |
| 740 | } |
| 741 | ctx->default_config = config; |
| 742 | } |
| 743 | i++; |
| 744 | } |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 745 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 746 | json_object_put(obj); |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 747 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 748 | if (ctx->n_configs == 1) |
| 749 | ctx->default_config = &ctx->configs[0]; |
Jeremy Kerr | 0f1a49c | 2018-08-03 15:41:38 +0800 | [diff] [blame] | 750 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 751 | return 0; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 752 | |
| 753 | err_free: |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 754 | warnx("failed to load config from %s", conf_path); |
| 755 | json_object_put(obj); |
| 756 | return -1; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 757 | } |
| 758 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 759 | static int config_select(struct ctx* ctx, const char* name) |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 760 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 761 | struct config* config; |
| 762 | struct stat statbuf; |
Ed Tanous | 9ac5231 | 2023-07-26 12:18:47 -0700 | [diff] [blame] | 763 | int rc; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 764 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 765 | config = NULL; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 766 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 767 | if (!name) |
| 768 | { |
| 769 | /* no config specified: use the default */ |
| 770 | if (!ctx->default_config) |
| 771 | { |
| 772 | warnx("no config specified, and no default"); |
| 773 | return -1; |
| 774 | } |
| 775 | config = ctx->default_config; |
| 776 | } |
| 777 | else |
| 778 | { |
| 779 | /* find a matching config... */ |
Ed Tanous | 9ac5231 | 2023-07-26 12:18:47 -0700 | [diff] [blame] | 780 | for (int i = 0; i < ctx->n_configs; i++) |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 781 | { |
| 782 | if (!strcmp(ctx->configs[i].name, name)) |
| 783 | { |
| 784 | config = &ctx->configs[i]; |
| 785 | break; |
| 786 | } |
| 787 | } |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 788 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 789 | if (!config) |
| 790 | { |
| 791 | warnx("no such configuration '%s'", name); |
| 792 | return -1; |
| 793 | } |
| 794 | } |
Jeremy Kerr | 0f1a49c | 2018-08-03 15:41:38 +0800 | [diff] [blame] | 795 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 796 | /* check that the device exists */ |
| 797 | rc = stat(config->nbd_device, &statbuf); |
| 798 | if (rc) |
| 799 | { |
| 800 | warn("can't stat nbd device %s", config->nbd_device); |
| 801 | return -1; |
| 802 | } |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 803 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 804 | if (!S_ISBLK(statbuf.st_mode)) |
| 805 | { |
| 806 | warn("specified nbd path %s isn't a block device", config->nbd_device); |
| 807 | return -1; |
| 808 | } |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 809 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 810 | /* ... and apply it */ |
| 811 | ctx->config = config; |
| 812 | ctx->nbd_devno = statbuf.st_rdev; |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 813 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 814 | return 0; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 815 | } |
| 816 | |
Jeremy Kerr | 6f9c433 | 2018-08-03 17:11:15 +0800 | [diff] [blame] | 817 | static const struct option options[] = { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 818 | {.name = "help", .val = 'h'}, |
| 819 | {.name = "metadata", .val = 'm'}, |
| 820 | {0}, |
Jeremy Kerr | 6f9c433 | 2018-08-03 17:11:15 +0800 | [diff] [blame] | 821 | }; |
| 822 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 823 | enum action |
Jeremy Kerr | 6f9c433 | 2018-08-03 17:11:15 +0800 | [diff] [blame] | 824 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 825 | ACTION_PROXY, |
| 826 | ACTION_METADATA, |
| 827 | }; |
| 828 | |
| 829 | static void print_usage(const char* progname) |
| 830 | { |
| 831 | fprintf(stderr, "usage:\n"); |
| 832 | fprintf(stderr, "\t%s [configuration]\n", progname); |
| 833 | fprintf(stderr, "\t%s --metadata\n", progname); |
Jeremy Kerr | 6f9c433 | 2018-08-03 17:11:15 +0800 | [diff] [blame] | 834 | } |
| 835 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 836 | int main(int argc, char** argv) |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 837 | { |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 838 | enum action action = ACTION_PROXY; |
| 839 | const char* config_name; |
| 840 | struct ctx _ctx, *ctx; |
| 841 | int rc; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 842 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 843 | config_name = NULL; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 844 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 845 | for (;;) |
| 846 | { |
| 847 | int c = getopt_long(argc, argv, "h", options, NULL); |
| 848 | if (c == -1) |
| 849 | break; |
Jeremy Kerr | 6f9c433 | 2018-08-03 17:11:15 +0800 | [diff] [blame] | 850 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 851 | switch (c) |
| 852 | { |
| 853 | case 'm': |
| 854 | action = ACTION_METADATA; |
| 855 | break; |
| 856 | case 'h': |
| 857 | case '?': |
| 858 | print_usage(argv[0]); |
| 859 | return c == 'h' ? EXIT_SUCCESS : EXIT_FAILURE; |
| 860 | } |
| 861 | } |
Jeremy Kerr | 6f9c433 | 2018-08-03 17:11:15 +0800 | [diff] [blame] | 862 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 863 | if (optind < argc) |
| 864 | config_name = argv[optind]; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 865 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 866 | ctx = &_ctx; |
| 867 | memset(ctx, 0, sizeof(*ctx)); |
| 868 | ctx->bufsize = bufsize; |
| 869 | ctx->buf = malloc(ctx->bufsize); |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 870 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 871 | rc = config_init(ctx); |
| 872 | if (rc) |
| 873 | goto out_free; |
Jeremy Kerr | 1952735 | 2018-08-03 15:04:38 +0800 | [diff] [blame] | 874 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 875 | if (action == ACTION_METADATA) |
| 876 | { |
| 877 | print_metadata(ctx); |
| 878 | goto out_free; |
| 879 | } |
Jeremy Kerr | 13bb28f | 2018-08-09 10:41:10 +0800 | [diff] [blame] | 880 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 881 | rc = config_select(ctx, config_name); |
| 882 | if (rc) |
| 883 | goto out_free; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 884 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 885 | rc = open_nbd_socket(ctx); |
| 886 | if (rc) |
| 887 | goto out_free; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 888 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 889 | rc = setup_signals(ctx); |
| 890 | if (rc) |
| 891 | goto out_close; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 892 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 893 | rc = start_nbd_client(ctx); |
| 894 | if (rc) |
| 895 | goto out_stop_client; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 896 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 897 | rc = wait_for_nbd_client(ctx); |
| 898 | if (rc) |
| 899 | goto out_stop_client; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 900 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 901 | rc = udev_init(ctx); |
| 902 | if (rc) |
| 903 | goto out_stop_client; |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 904 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 905 | rc = run_proxy(ctx); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 906 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 907 | if (ctx->udev) |
| 908 | udev_free(ctx); |
Jeremy Kerr | 4ac9246 | 2018-08-10 11:16:29 +0800 | [diff] [blame] | 909 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 910 | run_state_hook(ctx, "stop", true); |
Jeremy Kerr | c6134c1 | 2018-08-09 13:03:33 +0800 | [diff] [blame] | 911 | |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 912 | out_stop_client: |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 913 | /* we cleanup signals before stopping the client, because we |
| 914 | * no longer care about SIGCHLD from the stopping nbd-client |
| 915 | * process. stop_nbd_client will be a no-op if the client hasn't |
| 916 | * been started. */ |
| 917 | cleanup_signals(ctx); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 918 | |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 919 | stop_nbd_client(ctx); |
| 920 | close(ctx->sock_client); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 921 | |
| 922 | out_close: |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 923 | if (ctx->sock_path) |
| 924 | { |
| 925 | unlink(ctx->sock_path); |
| 926 | free(ctx->sock_path); |
| 927 | } |
| 928 | close(ctx->sock); |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 929 | out_free: |
Ed Tanous | 4136ad7 | 2023-07-26 11:15:56 -0700 | [diff] [blame] | 930 | config_free(ctx); |
| 931 | free(ctx->buf); |
| 932 | return rc ? EXIT_FAILURE : EXIT_SUCCESS; |
Jeremy Kerr | f403c42 | 2018-07-26 12:14:56 +0800 | [diff] [blame] | 933 | } |
Ed Tanous | 9ac5231 | 2023-07-26 12:18:47 -0700 | [diff] [blame] | 934 | |
| 935 | // NOLINTEND |