| 1 | /* |
| 2 | * Privileged helper to handle persistent reservation commands for QEMU |
| 3 | * |
| 4 | * Copyright (C) 2017 Red Hat, Inc. <pbonzini@redhat.com> |
| 5 | * |
| 6 | * Author: Paolo Bonzini <pbonzini@redhat.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; under version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "qemu/osdep.h" |
| 22 | #include <getopt.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <linux/dm-ioctl.h> |
| 25 | #include <scsi/sg.h> |
| 26 | |
| 27 | #ifdef CONFIG_LIBCAP_NG |
| 28 | #include <cap-ng.h> |
| 29 | #endif |
| 30 | #include <pwd.h> |
| 31 | #include <grp.h> |
| 32 | |
| 33 | #ifdef CONFIG_MPATH |
| 34 | #include <libudev.h> |
| 35 | #include <mpath_cmd.h> |
| 36 | #include <mpath_persist.h> |
| 37 | #endif |
| 38 | |
| 39 | #include "qemu/help-texts.h" |
| 40 | #include "qapi/error.h" |
| 41 | #include "qemu/cutils.h" |
| 42 | #include "qemu/main-loop.h" |
| 43 | #include "qemu/module.h" |
| 44 | #include "qemu/error-report.h" |
| 45 | #include "qemu/config-file.h" |
| 46 | #include "qemu/bswap.h" |
| 47 | #include "qemu/log.h" |
| 48 | #include "qemu/systemd.h" |
| 49 | #include "qapi/util.h" |
| 50 | #include "qobject/qstring.h" |
| 51 | #include "io/channel-socket.h" |
| 52 | #include "trace/control.h" |
| 53 | #include "qemu-version.h" |
| 54 | |
| 55 | #include "block/thread-pool.h" |
| 56 | |
| 57 | #include "scsi/constants.h" |
| 58 | #include "scsi/utils.h" |
| 59 | #include "pr-helper.h" |
| 60 | |
| 61 | #define PR_OUT_FIXED_PARAM_SIZE 24 |
| 62 | |
| 63 | static char *socket_path; |
| 64 | static char *pidfile; |
| 65 | static enum { RUNNING, TERMINATE, TERMINATING } state; |
| 66 | static QIOChannelSocket *server_ioc; |
| 67 | static int server_watch; |
| 68 | static int num_active_sockets = 1; |
| 69 | static int noisy; |
| 70 | static int verbose; |
| 71 | |
| 72 | #ifdef CONFIG_LIBCAP_NG |
| 73 | static int uid = -1; |
| 74 | static int gid = -1; |
| 75 | #endif |
| 76 | |
| 77 | static void compute_default_paths(void) |
| 78 | { |
| 79 | g_autofree char *state = qemu_get_local_state_dir(); |
| 80 | |
| 81 | socket_path = g_build_filename(state, "run", "qemu-pr-helper.sock", NULL); |
| 82 | pidfile = g_build_filename(state, "run", "qemu-pr-helper.pid", NULL); |
| 83 | } |
| 84 | |
| 85 | static void usage(const char *name) |
| 86 | { |
| 87 | (printf) ( |
| 88 | "Usage: %s [OPTIONS] FILE\n" |
| 89 | "Persistent Reservation helper program for QEMU\n" |
| 90 | "\n" |
| 91 | " -h, --help display this help and exit\n" |
| 92 | " -V, --version output version information and exit\n" |
| 93 | "\n" |
| 94 | " -d, --daemon run in the background\n" |
| 95 | " -f, --pidfile=PATH PID file when running as a daemon\n" |
| 96 | " (default '%s')\n" |
| 97 | " -k, --socket=PATH path to the unix socket\n" |
| 98 | " (default '%s')\n" |
| 99 | " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 100 | " specify tracing options\n" |
| 101 | #ifdef CONFIG_LIBCAP_NG |
| 102 | " -u, --user=USER user to drop privileges to\n" |
| 103 | " -g, --group=GROUP group to drop privileges to\n" |
| 104 | #endif |
| 105 | "\n" |
| 106 | QEMU_HELP_BOTTOM "\n" |
| 107 | , name, pidfile, socket_path); |
| 108 | } |
| 109 | |
| 110 | static void version(const char *name) |
| 111 | { |
| 112 | printf( |
| 113 | "%s " QEMU_FULL_VERSION "\n" |
| 114 | "Written by Paolo Bonzini.\n" |
| 115 | "\n" |
| 116 | QEMU_COPYRIGHT "\n" |
| 117 | "This is free software; see the source for copying conditions. There is NO\n" |
| 118 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" |
| 119 | , name); |
| 120 | } |
| 121 | |
| 122 | /* SG_IO support */ |
| 123 | |
| 124 | typedef struct PRHelperSGIOData { |
| 125 | int fd; |
| 126 | const uint8_t *cdb; |
| 127 | uint8_t *sense; |
| 128 | uint8_t *buf; |
| 129 | int sz; /* input/output */ |
| 130 | int dir; |
| 131 | } PRHelperSGIOData; |
| 132 | |
| 133 | static int do_sgio_worker(void *opaque) |
| 134 | { |
| 135 | PRHelperSGIOData *data = opaque; |
| 136 | struct sg_io_hdr io_hdr; |
| 137 | int ret; |
| 138 | int status; |
| 139 | SCSISense sense_code; |
| 140 | |
| 141 | memset(data->sense, 0, PR_HELPER_SENSE_SIZE); |
| 142 | memset(&io_hdr, 0, sizeof(io_hdr)); |
| 143 | io_hdr.interface_id = 'S'; |
| 144 | io_hdr.cmd_len = PR_HELPER_CDB_SIZE; |
| 145 | io_hdr.cmdp = (uint8_t *)data->cdb; |
| 146 | io_hdr.sbp = data->sense; |
| 147 | io_hdr.mx_sb_len = PR_HELPER_SENSE_SIZE; |
| 148 | io_hdr.timeout = 1; |
| 149 | io_hdr.dxfer_direction = data->dir; |
| 150 | io_hdr.dxferp = (char *)data->buf; |
| 151 | io_hdr.dxfer_len = data->sz; |
| 152 | ret = ioctl(data->fd, SG_IO, &io_hdr); |
| 153 | |
| 154 | if (ret < 0) { |
| 155 | status = scsi_sense_from_errno(errno, &sense_code); |
| 156 | if (status == CHECK_CONDITION) { |
| 157 | scsi_build_sense(data->sense, sense_code); |
| 158 | } |
| 159 | } else if (io_hdr.host_status != SCSI_HOST_OK) { |
| 160 | status = scsi_sense_from_host_status(io_hdr.host_status, &sense_code); |
| 161 | if (status == CHECK_CONDITION) { |
| 162 | scsi_build_sense(data->sense, sense_code); |
| 163 | } |
| 164 | } else if (io_hdr.driver_status & SG_ERR_DRIVER_TIMEOUT) { |
| 165 | status = BUSY; |
| 166 | } else { |
| 167 | status = io_hdr.status; |
| 168 | } |
| 169 | |
| 170 | if (status == GOOD) { |
| 171 | data->sz -= io_hdr.resid; |
| 172 | } else { |
| 173 | data->sz = 0; |
| 174 | } |
| 175 | |
| 176 | return status; |
| 177 | } |
| 178 | |
| 179 | static int coroutine_fn do_sgio(int fd, const uint8_t *cdb, uint8_t *sense, |
| 180 | uint8_t *buf, int *sz, int dir) |
| 181 | { |
| 182 | int r; |
| 183 | |
| 184 | PRHelperSGIOData data = { |
| 185 | .fd = fd, |
| 186 | .cdb = cdb, |
| 187 | .sense = sense, |
| 188 | .buf = buf, |
| 189 | .sz = *sz, |
| 190 | .dir = dir, |
| 191 | }; |
| 192 | |
| 193 | r = thread_pool_submit_co(do_sgio_worker, &data); |
| 194 | *sz = data.sz; |
| 195 | return r; |
| 196 | } |
| 197 | |
| 198 | /* Device mapper interface */ |
| 199 | |
| 200 | #ifdef CONFIG_MPATH |
| 201 | #define CONTROL_PATH "/dev/mapper/control" |
| 202 | |
| 203 | typedef struct DMData { |
| 204 | struct dm_ioctl dm; |
| 205 | uint8_t data[1024]; |
| 206 | } DMData; |
| 207 | |
| 208 | static int control_fd; |
| 209 | |
| 210 | static void *dm_ioctl(int ioc, struct dm_ioctl *dm) |
| 211 | { |
| 212 | static DMData d; |
| 213 | memcpy(&d.dm, dm, sizeof(d.dm)); |
| 214 | QEMU_BUILD_BUG_ON(sizeof(d.data) < sizeof(struct dm_target_spec)); |
| 215 | |
| 216 | d.dm.version[0] = DM_VERSION_MAJOR; |
| 217 | d.dm.version[1] = 0; |
| 218 | d.dm.version[2] = 0; |
| 219 | d.dm.data_size = 1024; |
| 220 | d.dm.data_start = offsetof(DMData, data); |
| 221 | if (ioctl(control_fd, ioc, &d) < 0) { |
| 222 | return NULL; |
| 223 | } |
| 224 | memcpy(dm, &d.dm, sizeof(d.dm)); |
| 225 | return &d.data; |
| 226 | } |
| 227 | |
| 228 | static void *dm_dev_ioctl(int fd, int ioc, struct dm_ioctl *dm) |
| 229 | { |
| 230 | struct stat st; |
| 231 | int r; |
| 232 | |
| 233 | r = fstat(fd, &st); |
| 234 | if (r < 0) { |
| 235 | perror("fstat"); |
| 236 | exit(1); |
| 237 | } |
| 238 | |
| 239 | dm->dev = st.st_rdev; |
| 240 | return dm_ioctl(ioc, dm); |
| 241 | } |
| 242 | |
| 243 | static void dm_init(void) |
| 244 | { |
| 245 | control_fd = open(CONTROL_PATH, O_RDWR); |
| 246 | if (control_fd < 0) { |
| 247 | perror("Cannot open " CONTROL_PATH); |
| 248 | exit(1); |
| 249 | } |
| 250 | struct dm_ioctl dm = { }; |
| 251 | if (!dm_ioctl(DM_VERSION, &dm)) { |
| 252 | perror("ioctl"); |
| 253 | exit(1); |
| 254 | } |
| 255 | if (dm.version[0] != DM_VERSION_MAJOR) { |
| 256 | fprintf(stderr, "Unsupported device mapper interface"); |
| 257 | exit(1); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* Variables required by libmultipath and libmpathpersist. */ |
| 262 | QEMU_BUILD_BUG_ON(PR_HELPER_DATA_SIZE > MPATH_MAX_PARAM_LEN); |
| 263 | static struct config *multipath_conf; |
| 264 | unsigned mpath_mx_alloc_len = PR_HELPER_DATA_SIZE; |
| 265 | int logsink; |
| 266 | struct udev *udev; |
| 267 | |
| 268 | extern struct config *get_multipath_config(void); |
| 269 | struct config *get_multipath_config(void) |
| 270 | { |
| 271 | return multipath_conf; |
| 272 | } |
| 273 | |
| 274 | extern void put_multipath_config(struct config *conf); |
| 275 | void put_multipath_config(struct config *conf) |
| 276 | { |
| 277 | } |
| 278 | |
| 279 | static void multipath_pr_init(void) |
| 280 | { |
| 281 | udev = udev_new(); |
| 282 | multipath_conf = mpath_lib_init(); |
| 283 | } |
| 284 | |
| 285 | static int is_mpath(int fd) |
| 286 | { |
| 287 | struct dm_ioctl dm = { .flags = DM_NOFLUSH_FLAG }; |
| 288 | struct dm_target_spec *tgt; |
| 289 | |
| 290 | tgt = dm_dev_ioctl(fd, DM_TABLE_STATUS, &dm); |
| 291 | if (!tgt) { |
| 292 | if (errno == ENXIO) { |
| 293 | return 0; |
| 294 | } |
| 295 | perror("ioctl"); |
| 296 | exit(EXIT_FAILURE); |
| 297 | } |
| 298 | return !strncmp(tgt->target_type, "multipath", DM_MAX_TYPE_NAME); |
| 299 | } |
| 300 | |
| 301 | static SCSISense mpath_generic_sense(int r) |
| 302 | { |
| 303 | switch (r) { |
| 304 | case MPATH_PR_SENSE_NOT_READY: |
| 305 | return SENSE_CODE(NOT_READY); |
| 306 | case MPATH_PR_SENSE_MEDIUM_ERROR: |
| 307 | return SENSE_CODE(READ_ERROR); |
| 308 | case MPATH_PR_SENSE_HARDWARE_ERROR: |
| 309 | return SENSE_CODE(TARGET_FAILURE); |
| 310 | case MPATH_PR_SENSE_ABORTED_COMMAND: |
| 311 | return SENSE_CODE(IO_ERROR); |
| 312 | default: |
| 313 | abort(); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | static int coroutine_fn mpath_reconstruct_sense(int fd, int r, uint8_t *sense) |
| 318 | { |
| 319 | switch (r) { |
| 320 | case MPATH_PR_SUCCESS: |
| 321 | return GOOD; |
| 322 | case MPATH_PR_SENSE_NOT_READY: |
| 323 | case MPATH_PR_SENSE_MEDIUM_ERROR: |
| 324 | case MPATH_PR_SENSE_HARDWARE_ERROR: |
| 325 | case MPATH_PR_SENSE_ABORTED_COMMAND: |
| 326 | { |
| 327 | /* libmpathpersist ate the exact sense. Try to find it by |
| 328 | * issuing TEST UNIT READY. |
| 329 | */ |
| 330 | uint8_t cdb[6] = { TEST_UNIT_READY }; |
| 331 | int sz = 0; |
| 332 | int ret = do_sgio(fd, cdb, sense, NULL, &sz, SG_DXFER_NONE); |
| 333 | |
| 334 | if (ret != GOOD) { |
| 335 | return ret; |
| 336 | } |
| 337 | scsi_build_sense(sense, mpath_generic_sense(r)); |
| 338 | return CHECK_CONDITION; |
| 339 | } |
| 340 | |
| 341 | case MPATH_PR_SENSE_UNIT_ATTENTION: |
| 342 | /* Congratulations libmpathpersist, you ruined the Unit Attention... |
| 343 | * Return a heavyweight one. |
| 344 | */ |
| 345 | scsi_build_sense(sense, SENSE_CODE(SCSI_BUS_RESET)); |
| 346 | return CHECK_CONDITION; |
| 347 | case MPATH_PR_SENSE_INVALID_OP: |
| 348 | /* Only one valid sense. */ |
| 349 | scsi_build_sense(sense, SENSE_CODE(INVALID_OPCODE)); |
| 350 | return CHECK_CONDITION; |
| 351 | case MPATH_PR_ILLEGAL_REQ: |
| 352 | /* Guess. */ |
| 353 | scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM)); |
| 354 | return CHECK_CONDITION; |
| 355 | case MPATH_PR_NO_SENSE: |
| 356 | scsi_build_sense(sense, SENSE_CODE(NO_SENSE)); |
| 357 | return CHECK_CONDITION; |
| 358 | |
| 359 | case MPATH_PR_RESERV_CONFLICT: |
| 360 | return RESERVATION_CONFLICT; |
| 361 | |
| 362 | case MPATH_PR_OTHER: |
| 363 | default: |
| 364 | scsi_build_sense(sense, SENSE_CODE(LUN_COMM_FAILURE)); |
| 365 | return CHECK_CONDITION; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | static int coroutine_fn multipath_pr_in(int fd, const uint8_t *cdb, uint8_t *sense, |
| 370 | uint8_t *data, int sz) |
| 371 | { |
| 372 | int rq_servact = cdb[1]; |
| 373 | struct prin_resp resp; |
| 374 | size_t written; |
| 375 | int r; |
| 376 | |
| 377 | switch (rq_servact) { |
| 378 | case MPATH_PRIN_RKEY_SA: |
| 379 | case MPATH_PRIN_RRES_SA: |
| 380 | case MPATH_PRIN_RCAP_SA: |
| 381 | break; |
| 382 | case MPATH_PRIN_RFSTAT_SA: |
| 383 | /* Nobody implements it anyway, so bail out. */ |
| 384 | default: |
| 385 | /* Cannot parse any other output. */ |
| 386 | scsi_build_sense(sense, SENSE_CODE(INVALID_FIELD)); |
| 387 | return CHECK_CONDITION; |
| 388 | } |
| 389 | |
| 390 | r = mpath_persistent_reserve_in(fd, rq_servact, &resp, noisy, verbose); |
| 391 | if (r == MPATH_PR_SUCCESS) { |
| 392 | switch (rq_servact) { |
| 393 | case MPATH_PRIN_RKEY_SA: |
| 394 | case MPATH_PRIN_RRES_SA: { |
| 395 | struct prin_readdescr *out = &resp.prin_descriptor.prin_readkeys; |
| 396 | assert(sz >= 8); |
| 397 | written = MIN(out->additional_length + 8, sz); |
| 398 | stl_be_p(&data[0], out->prgeneration); |
| 399 | stl_be_p(&data[4], out->additional_length); |
| 400 | memcpy(&data[8], out->key_list, written - 8); |
| 401 | break; |
| 402 | } |
| 403 | case MPATH_PRIN_RCAP_SA: { |
| 404 | struct prin_capdescr *out = &resp.prin_descriptor.prin_readcap; |
| 405 | assert(sz >= 6); |
| 406 | written = 6; |
| 407 | stw_be_p(&data[0], out->length); |
| 408 | data[2] = out->flags[0]; |
| 409 | data[3] = out->flags[1]; |
| 410 | stw_be_p(&data[4], out->pr_type_mask); |
| 411 | break; |
| 412 | } |
| 413 | default: |
| 414 | scsi_build_sense(sense, SENSE_CODE(INVALID_OPCODE)); |
| 415 | return CHECK_CONDITION; |
| 416 | } |
| 417 | assert(written <= sz); |
| 418 | memset(data + written, 0, sz - written); |
| 419 | } |
| 420 | |
| 421 | return mpath_reconstruct_sense(fd, r, sense); |
| 422 | } |
| 423 | |
| 424 | static int coroutine_fn multipath_pr_out(int fd, const uint8_t *cdb, uint8_t *sense, |
| 425 | const uint8_t *param, int sz) |
| 426 | { |
| 427 | int rq_servact = cdb[1]; |
| 428 | int rq_scope = cdb[2] >> 4; |
| 429 | int rq_type = cdb[2] & 0xf; |
| 430 | g_autofree struct prout_param_descriptor *paramp = NULL; |
| 431 | char transportids[PR_HELPER_DATA_SIZE]; |
| 432 | int r; |
| 433 | |
| 434 | paramp = g_malloc0(sizeof(struct prout_param_descriptor) |
| 435 | + sizeof(struct transportid *) * MPATH_MX_TIDS); |
| 436 | |
| 437 | if (sz < PR_OUT_FIXED_PARAM_SIZE) { |
| 438 | /* Illegal request, Parameter list length error. This isn't fatal; |
| 439 | * we have read the data, send an error without closing the socket. |
| 440 | */ |
| 441 | scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM_LEN)); |
| 442 | return CHECK_CONDITION; |
| 443 | } |
| 444 | |
| 445 | switch (rq_servact) { |
| 446 | case MPATH_PROUT_REG_SA: |
| 447 | case MPATH_PROUT_RES_SA: |
| 448 | case MPATH_PROUT_REL_SA: |
| 449 | case MPATH_PROUT_CLEAR_SA: |
| 450 | case MPATH_PROUT_PREE_SA: |
| 451 | case MPATH_PROUT_PREE_AB_SA: |
| 452 | case MPATH_PROUT_REG_IGN_SA: |
| 453 | break; |
| 454 | case MPATH_PROUT_REG_MOV_SA: |
| 455 | /* Not supported by struct prout_param_descriptor. */ |
| 456 | default: |
| 457 | /* Cannot parse any other input. */ |
| 458 | scsi_build_sense(sense, SENSE_CODE(INVALID_FIELD)); |
| 459 | return CHECK_CONDITION; |
| 460 | } |
| 461 | |
| 462 | /* Convert input data, especially transport IDs, to the structs |
| 463 | * used by libmpathpersist (which, of course, will immediately |
| 464 | * do the opposite). |
| 465 | */ |
| 466 | memcpy(¶mp->key, ¶m[0], 8); |
| 467 | memcpy(¶mp->sa_key, ¶m[8], 8); |
| 468 | paramp->sa_flags = param[20]; |
| 469 | if (sz > PR_OUT_FIXED_PARAM_SIZE) { |
| 470 | size_t transportid_len; |
| 471 | int i, j; |
| 472 | if (sz < PR_OUT_FIXED_PARAM_SIZE + 4) { |
| 473 | scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM_LEN)); |
| 474 | return CHECK_CONDITION; |
| 475 | } |
| 476 | transportid_len = ldl_be_p(¶m[24]) + PR_OUT_FIXED_PARAM_SIZE + 4; |
| 477 | if (transportid_len > sz) { |
| 478 | scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM)); |
| 479 | return CHECK_CONDITION; |
| 480 | } |
| 481 | for (i = PR_OUT_FIXED_PARAM_SIZE + 4, j = 0; i < transportid_len; ) { |
| 482 | struct transportid *id = (struct transportid *) &transportids[j]; |
| 483 | int len; |
| 484 | |
| 485 | id->format_code = param[i] & 0xc0; |
| 486 | id->protocol_id = param[i] & 0x0f; |
| 487 | switch (param[i] & 0xcf) { |
| 488 | case 0: |
| 489 | /* FC transport. */ |
| 490 | if (i + 24 > transportid_len) { |
| 491 | goto illegal_req; |
| 492 | } |
| 493 | memcpy(id->n_port_name, ¶m[i + 8], 8); |
| 494 | j += offsetof(struct transportid, n_port_name[8]); |
| 495 | i += 24; |
| 496 | break; |
| 497 | case 5: |
| 498 | case 0x45: |
| 499 | /* iSCSI transport. */ |
| 500 | len = lduw_be_p(¶m[i + 2]); |
| 501 | if (len > 252 || (len & 3) || i + len + 4 > transportid_len) { |
| 502 | /* For format code 00, the standard says the maximum is 223 |
| 503 | * plus the NUL terminator. For format code 01 there is no |
| 504 | * maximum length, but libmpathpersist ignores the first |
| 505 | * byte of id->iscsi_name so our maximum is 252. |
| 506 | */ |
| 507 | goto illegal_req; |
| 508 | } |
| 509 | if (memchr(¶m[i + 4], 0, len) == NULL) { |
| 510 | goto illegal_req; |
| 511 | } |
| 512 | memcpy(id->iscsi_name, ¶m[i + 2], len + 2); |
| 513 | j += offsetof(struct transportid, iscsi_name[len + 2]); |
| 514 | i += len + 4; |
| 515 | break; |
| 516 | case 6: |
| 517 | /* SAS transport. */ |
| 518 | if (i + 24 > transportid_len) { |
| 519 | goto illegal_req; |
| 520 | } |
| 521 | memcpy(id->sas_address, ¶m[i + 4], 8); |
| 522 | j += offsetof(struct transportid, sas_address[8]); |
| 523 | i += 24; |
| 524 | break; |
| 525 | default: |
| 526 | illegal_req: |
| 527 | scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM)); |
| 528 | return CHECK_CONDITION; |
| 529 | } |
| 530 | |
| 531 | assert(paramp->num_transportid < MPATH_MX_TIDS); |
| 532 | paramp->trnptid_list[paramp->num_transportid++] = id; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | r = mpath_persistent_reserve_out(fd, rq_servact, rq_scope, rq_type, |
| 537 | paramp, noisy, verbose); |
| 538 | return mpath_reconstruct_sense(fd, r, sense); |
| 539 | } |
| 540 | #endif |
| 541 | |
| 542 | static int coroutine_fn do_pr_in(int fd, const uint8_t *cdb, uint8_t *sense, |
| 543 | uint8_t *data, int *resp_sz) |
| 544 | { |
| 545 | #ifdef CONFIG_MPATH |
| 546 | if (is_mpath(fd)) { |
| 547 | /* multipath_pr_in fills the whole input buffer. */ |
| 548 | int r = multipath_pr_in(fd, cdb, sense, data, *resp_sz); |
| 549 | if (r != GOOD) { |
| 550 | *resp_sz = 0; |
| 551 | } |
| 552 | return r; |
| 553 | } |
| 554 | #endif |
| 555 | |
| 556 | return do_sgio(fd, cdb, sense, data, resp_sz, |
| 557 | SG_DXFER_FROM_DEV); |
| 558 | } |
| 559 | |
| 560 | static int coroutine_fn do_pr_out(int fd, const uint8_t *cdb, uint8_t *sense, |
| 561 | const uint8_t *param, int sz) |
| 562 | { |
| 563 | int resp_sz; |
| 564 | |
| 565 | if ((fcntl(fd, F_GETFL) & O_ACCMODE) == O_RDONLY) { |
| 566 | scsi_build_sense(sense, SENSE_CODE(INVALID_OPCODE)); |
| 567 | return CHECK_CONDITION; |
| 568 | } |
| 569 | |
| 570 | #ifdef CONFIG_MPATH |
| 571 | if (is_mpath(fd)) { |
| 572 | return multipath_pr_out(fd, cdb, sense, param, sz); |
| 573 | } |
| 574 | #endif |
| 575 | |
| 576 | resp_sz = sz; |
| 577 | return do_sgio(fd, cdb, sense, (uint8_t *)param, &resp_sz, |
| 578 | SG_DXFER_TO_DEV); |
| 579 | } |
| 580 | |
| 581 | /* Client */ |
| 582 | |
| 583 | typedef struct PRHelperClient { |
| 584 | QIOChannelSocket *ioc; |
| 585 | Coroutine *co; |
| 586 | int fd; |
| 587 | uint8_t data[PR_HELPER_DATA_SIZE]; |
| 588 | } PRHelperClient; |
| 589 | |
| 590 | typedef struct PRHelperRequest { |
| 591 | int fd; |
| 592 | size_t sz; |
| 593 | uint8_t cdb[PR_HELPER_CDB_SIZE]; |
| 594 | } PRHelperRequest; |
| 595 | |
| 596 | static int coroutine_fn prh_read(PRHelperClient *client, void *buf, int sz, |
| 597 | Error **errp) |
| 598 | { |
| 599 | int ret = 0; |
| 600 | |
| 601 | while (sz > 0) { |
| 602 | int *fds = NULL; |
| 603 | size_t nfds = 0; |
| 604 | int i; |
| 605 | struct iovec iov; |
| 606 | ssize_t n_read; |
| 607 | |
| 608 | iov.iov_base = buf; |
| 609 | iov.iov_len = sz; |
| 610 | n_read = qio_channel_readv_full(QIO_CHANNEL(client->ioc), &iov, 1, |
| 611 | &fds, &nfds, 0, errp); |
| 612 | |
| 613 | if (n_read == QIO_CHANNEL_ERR_BLOCK) { |
| 614 | qio_channel_yield(QIO_CHANNEL(client->ioc), G_IO_IN); |
| 615 | continue; |
| 616 | } |
| 617 | if (n_read <= 0) { |
| 618 | ret = n_read ? n_read : -1; |
| 619 | goto err; |
| 620 | } |
| 621 | |
| 622 | /* Stash one file descriptor per request. */ |
| 623 | if (nfds) { |
| 624 | bool too_many = false; |
| 625 | for (i = 0; i < nfds; i++) { |
| 626 | if (client->fd == -1) { |
| 627 | client->fd = fds[i]; |
| 628 | } else { |
| 629 | close(fds[i]); |
| 630 | too_many = true; |
| 631 | } |
| 632 | } |
| 633 | g_free(fds); |
| 634 | if (too_many) { |
| 635 | ret = -1; |
| 636 | goto err; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | buf += n_read; |
| 641 | sz -= n_read; |
| 642 | } |
| 643 | |
| 644 | return 0; |
| 645 | |
| 646 | err: |
| 647 | if (client->fd != -1) { |
| 648 | close(client->fd); |
| 649 | client->fd = -1; |
| 650 | } |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | static int coroutine_fn prh_read_request(PRHelperClient *client, |
| 655 | PRHelperRequest *req, |
| 656 | PRHelperResponse *resp, Error **errp) |
| 657 | { |
| 658 | uint32_t sz; |
| 659 | |
| 660 | if (prh_read(client, req->cdb, sizeof(req->cdb), NULL) < 0) { |
| 661 | return -1; |
| 662 | } |
| 663 | |
| 664 | if (client->fd == -1) { |
| 665 | error_setg(errp, "No file descriptor in request."); |
| 666 | return -1; |
| 667 | } |
| 668 | |
| 669 | if (req->cdb[0] != PERSISTENT_RESERVE_OUT && |
| 670 | req->cdb[0] != PERSISTENT_RESERVE_IN) { |
| 671 | error_setg(errp, "Invalid CDB, closing socket."); |
| 672 | goto out_close; |
| 673 | } |
| 674 | |
| 675 | sz = scsi_cdb_xfer(req->cdb); |
| 676 | if (sz > sizeof(client->data)) { |
| 677 | goto out_close; |
| 678 | } |
| 679 | |
| 680 | if (req->cdb[0] == PERSISTENT_RESERVE_OUT) { |
| 681 | if (qio_channel_read_all(QIO_CHANNEL(client->ioc), |
| 682 | (char *)client->data, sz, |
| 683 | errp) < 0) { |
| 684 | goto out_close; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | req->fd = client->fd; |
| 689 | req->sz = sz; |
| 690 | client->fd = -1; |
| 691 | return sz; |
| 692 | |
| 693 | out_close: |
| 694 | close(client->fd); |
| 695 | client->fd = -1; |
| 696 | return -1; |
| 697 | } |
| 698 | |
| 699 | static int coroutine_fn prh_write_response(PRHelperClient *client, |
| 700 | PRHelperRequest *req, |
| 701 | PRHelperResponse *resp, Error **errp) |
| 702 | { |
| 703 | ssize_t r; |
| 704 | size_t sz; |
| 705 | |
| 706 | if (req->cdb[0] == PERSISTENT_RESERVE_IN && resp->result == GOOD) { |
| 707 | assert(resp->sz <= req->sz && resp->sz <= sizeof(client->data)); |
| 708 | } else { |
| 709 | assert(resp->sz == 0); |
| 710 | } |
| 711 | |
| 712 | sz = resp->sz; |
| 713 | |
| 714 | resp->result = cpu_to_be32(resp->result); |
| 715 | resp->sz = cpu_to_be32(resp->sz); |
| 716 | r = qio_channel_write_all(QIO_CHANNEL(client->ioc), |
| 717 | (char *) resp, sizeof(*resp), errp); |
| 718 | if (r < 0) { |
| 719 | return r; |
| 720 | } |
| 721 | |
| 722 | r = qio_channel_write_all(QIO_CHANNEL(client->ioc), |
| 723 | (char *) client->data, |
| 724 | sz, errp); |
| 725 | return r < 0 ? r : 0; |
| 726 | } |
| 727 | |
| 728 | static void coroutine_fn prh_co_entry(void *opaque) |
| 729 | { |
| 730 | PRHelperClient *client = opaque; |
| 731 | Error *local_err = NULL; |
| 732 | uint32_t flags; |
| 733 | int r; |
| 734 | |
| 735 | if (!qio_channel_set_blocking(QIO_CHANNEL(client->ioc), |
| 736 | false, &local_err)) { |
| 737 | goto out; |
| 738 | } |
| 739 | |
| 740 | qio_channel_set_follow_coroutine_ctx(QIO_CHANNEL(client->ioc), true); |
| 741 | |
| 742 | /* A very simple negotiation for future extensibility. No features |
| 743 | * are defined so write 0. |
| 744 | */ |
| 745 | flags = cpu_to_be32(0); |
| 746 | r = qio_channel_write_all(QIO_CHANNEL(client->ioc), |
| 747 | (char *) &flags, sizeof(flags), NULL); |
| 748 | if (r < 0) { |
| 749 | goto out; |
| 750 | } |
| 751 | |
| 752 | r = qio_channel_read_all(QIO_CHANNEL(client->ioc), |
| 753 | (char *) &flags, sizeof(flags), NULL); |
| 754 | if (be32_to_cpu(flags) != 0 || r < 0) { |
| 755 | goto out; |
| 756 | } |
| 757 | |
| 758 | while (qatomic_read(&state) == RUNNING) { |
| 759 | PRHelperRequest req; |
| 760 | PRHelperResponse resp; |
| 761 | int sz; |
| 762 | |
| 763 | sz = prh_read_request(client, &req, &resp, &local_err); |
| 764 | if (sz < 0) { |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | num_active_sockets++; |
| 769 | if (req.cdb[0] == PERSISTENT_RESERVE_OUT) { |
| 770 | r = do_pr_out(req.fd, req.cdb, resp.sense, |
| 771 | client->data, sz); |
| 772 | resp.sz = 0; |
| 773 | } else { |
| 774 | resp.sz = sizeof(client->data); |
| 775 | r = do_pr_in(req.fd, req.cdb, resp.sense, |
| 776 | client->data, &resp.sz); |
| 777 | resp.sz = MIN(resp.sz, sz); |
| 778 | } |
| 779 | num_active_sockets--; |
| 780 | close(req.fd); |
| 781 | if (r == -1) { |
| 782 | break; |
| 783 | } |
| 784 | resp.result = r; |
| 785 | |
| 786 | if (prh_write_response(client, &req, &resp, &local_err) < 0) { |
| 787 | break; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | out: |
| 792 | if (local_err) { |
| 793 | if (verbose == 0) { |
| 794 | error_free(local_err); |
| 795 | } else { |
| 796 | error_report_err(local_err); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | object_unref(OBJECT(client->ioc)); |
| 801 | g_free(client); |
| 802 | } |
| 803 | |
| 804 | static gboolean accept_client(QIOChannel *ioc, GIOCondition cond, gpointer opaque) |
| 805 | { |
| 806 | QIOChannelSocket *cioc; |
| 807 | PRHelperClient *prh; |
| 808 | |
| 809 | cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), |
| 810 | NULL); |
| 811 | if (!cioc) { |
| 812 | return TRUE; |
| 813 | } |
| 814 | |
| 815 | prh = g_new(PRHelperClient, 1); |
| 816 | prh->ioc = cioc; |
| 817 | prh->fd = -1; |
| 818 | prh->co = qemu_coroutine_create(prh_co_entry, prh); |
| 819 | qemu_coroutine_enter(prh->co); |
| 820 | |
| 821 | return TRUE; |
| 822 | } |
| 823 | |
| 824 | static void termsig_handler(int signum) |
| 825 | { |
| 826 | qatomic_cmpxchg(&state, RUNNING, TERMINATE); |
| 827 | qemu_notify_event(); |
| 828 | } |
| 829 | |
| 830 | static void close_server_socket(void) |
| 831 | { |
| 832 | assert(server_ioc); |
| 833 | |
| 834 | g_source_remove(server_watch); |
| 835 | server_watch = -1; |
| 836 | object_unref(OBJECT(server_ioc)); |
| 837 | num_active_sockets--; |
| 838 | } |
| 839 | |
| 840 | #ifdef CONFIG_LIBCAP_NG |
| 841 | static int drop_privileges(void) |
| 842 | { |
| 843 | /* clear all capabilities */ |
| 844 | capng_clear(CAPNG_SELECT_BOTH); |
| 845 | |
| 846 | if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED, |
| 847 | CAP_SYS_RAWIO) < 0) { |
| 848 | return -1; |
| 849 | } |
| 850 | |
| 851 | #ifdef CONFIG_MPATH |
| 852 | /* For /dev/mapper/control ioctls */ |
| 853 | if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED, |
| 854 | CAP_SYS_ADMIN) < 0) { |
| 855 | return -1; |
| 856 | } |
| 857 | #endif |
| 858 | |
| 859 | /* Change user/group id, retaining the capabilities. Because file descriptors |
| 860 | * are passed via SCM_RIGHTS, we don't need supplementary groups (and in |
| 861 | * fact the helper can run as "nobody"). |
| 862 | */ |
| 863 | if (capng_change_id(uid != -1 ? uid : getuid(), |
| 864 | gid != -1 ? gid : getgid(), |
| 865 | CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING)) { |
| 866 | return -1; |
| 867 | } |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | #endif |
| 872 | |
| 873 | int main(int argc, char **argv) |
| 874 | { |
| 875 | const char *sopt = "hVk:f:dT:u:g:vq"; |
| 876 | struct option lopt[] = { |
| 877 | { "help", no_argument, NULL, 'h' }, |
| 878 | { "version", no_argument, NULL, 'V' }, |
| 879 | { "socket", required_argument, NULL, 'k' }, |
| 880 | { "pidfile", required_argument, NULL, 'f' }, |
| 881 | { "daemon", no_argument, NULL, 'd' }, |
| 882 | { "trace", required_argument, NULL, 'T' }, |
| 883 | { "user", required_argument, NULL, 'u' }, |
| 884 | { "group", required_argument, NULL, 'g' }, |
| 885 | { "verbose", no_argument, NULL, 'v' }, |
| 886 | { "quiet", no_argument, NULL, 'q' }, |
| 887 | { NULL, 0, NULL, 0 } |
| 888 | }; |
| 889 | int opt_ind = 0; |
| 890 | int loglevel = 1; |
| 891 | int quiet = 0; |
| 892 | int ch; |
| 893 | Error *local_err = NULL; |
| 894 | bool daemonize = false; |
| 895 | bool pidfile_specified = false; |
| 896 | bool socket_path_specified = false; |
| 897 | unsigned socket_activation; |
| 898 | |
| 899 | struct sigaction sa_sigterm; |
| 900 | memset(&sa_sigterm, 0, sizeof(sa_sigterm)); |
| 901 | sa_sigterm.sa_handler = termsig_handler; |
| 902 | sigaction(SIGTERM, &sa_sigterm, NULL); |
| 903 | sigaction(SIGINT, &sa_sigterm, NULL); |
| 904 | sigaction(SIGHUP, &sa_sigterm, NULL); |
| 905 | |
| 906 | signal(SIGPIPE, SIG_IGN); |
| 907 | |
| 908 | error_init(argv[0]); |
| 909 | module_call_init(MODULE_INIT_TRACE); |
| 910 | module_call_init(MODULE_INIT_QOM); |
| 911 | qemu_add_opts(&qemu_trace_opts); |
| 912 | qemu_init_exec_dir(argv[0]); |
| 913 | |
| 914 | compute_default_paths(); |
| 915 | |
| 916 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
| 917 | switch (ch) { |
| 918 | case 'k': |
| 919 | g_free(socket_path); |
| 920 | socket_path = g_strdup(optarg); |
| 921 | socket_path_specified = true; |
| 922 | if (socket_path[0] != '/') { |
| 923 | error_report("socket path must be absolute"); |
| 924 | exit(EXIT_FAILURE); |
| 925 | } |
| 926 | break; |
| 927 | case 'f': |
| 928 | g_free(pidfile); |
| 929 | pidfile = g_strdup(optarg); |
| 930 | pidfile_specified = true; |
| 931 | break; |
| 932 | #ifdef CONFIG_LIBCAP_NG |
| 933 | case 'u': { |
| 934 | unsigned long res; |
| 935 | struct passwd *userinfo = getpwnam(optarg); |
| 936 | if (userinfo) { |
| 937 | uid = userinfo->pw_uid; |
| 938 | } else if (qemu_strtoul(optarg, NULL, 10, &res) == 0 && |
| 939 | (uid_t)res == res) { |
| 940 | uid = res; |
| 941 | } else { |
| 942 | error_report("invalid user '%s'", optarg); |
| 943 | exit(EXIT_FAILURE); |
| 944 | } |
| 945 | break; |
| 946 | } |
| 947 | case 'g': { |
| 948 | unsigned long res; |
| 949 | struct group *groupinfo = getgrnam(optarg); |
| 950 | if (groupinfo) { |
| 951 | gid = groupinfo->gr_gid; |
| 952 | } else if (qemu_strtoul(optarg, NULL, 10, &res) == 0 && |
| 953 | (gid_t)res == res) { |
| 954 | gid = res; |
| 955 | } else { |
| 956 | error_report("invalid group '%s'", optarg); |
| 957 | exit(EXIT_FAILURE); |
| 958 | } |
| 959 | break; |
| 960 | } |
| 961 | #else |
| 962 | case 'u': |
| 963 | case 'g': |
| 964 | error_report("-%c not supported by this %s", ch, argv[0]); |
| 965 | exit(1); |
| 966 | #endif |
| 967 | case 'd': |
| 968 | daemonize = true; |
| 969 | break; |
| 970 | case 'q': |
| 971 | quiet = 1; |
| 972 | break; |
| 973 | case 'v': |
| 974 | ++loglevel; |
| 975 | break; |
| 976 | case 'T': |
| 977 | trace_opt_parse(optarg); |
| 978 | break; |
| 979 | case 'V': |
| 980 | version(argv[0]); |
| 981 | exit(EXIT_SUCCESS); |
| 982 | break; |
| 983 | case 'h': |
| 984 | usage(argv[0]); |
| 985 | exit(EXIT_SUCCESS); |
| 986 | break; |
| 987 | case '?': |
| 988 | error_report("Try `%s --help' for more information.", argv[0]); |
| 989 | exit(EXIT_FAILURE); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | /* set verbosity */ |
| 994 | noisy = !quiet && (loglevel >= 3); |
| 995 | verbose = quiet ? 0 : MIN(loglevel, 3); |
| 996 | |
| 997 | if (!trace_init_backends()) { |
| 998 | exit(EXIT_FAILURE); |
| 999 | } |
| 1000 | trace_init_file(); |
| 1001 | qemu_set_log(LOG_TRACE, &error_fatal); |
| 1002 | |
| 1003 | #ifdef CONFIG_MPATH |
| 1004 | dm_init(); |
| 1005 | multipath_pr_init(); |
| 1006 | #endif |
| 1007 | |
| 1008 | socket_activation = check_socket_activation(); |
| 1009 | if (socket_activation == 0) { |
| 1010 | SocketAddress saddr; |
| 1011 | saddr = (SocketAddress){ |
| 1012 | .type = SOCKET_ADDRESS_TYPE_UNIX, |
| 1013 | .u.q_unix.path = socket_path, |
| 1014 | }; |
| 1015 | server_ioc = qio_channel_socket_new(); |
| 1016 | if (qio_channel_socket_listen_sync(server_ioc, &saddr, |
| 1017 | 1, &local_err) < 0) { |
| 1018 | object_unref(OBJECT(server_ioc)); |
| 1019 | error_report_err(local_err); |
| 1020 | return 1; |
| 1021 | } |
| 1022 | } else { |
| 1023 | /* Using socket activation - check user didn't use -p etc. */ |
| 1024 | if (socket_path_specified) { |
| 1025 | error_report("Unix socket can't be set when using socket activation"); |
| 1026 | exit(EXIT_FAILURE); |
| 1027 | } |
| 1028 | |
| 1029 | /* Can only listen on a single socket. */ |
| 1030 | if (socket_activation > 1) { |
| 1031 | error_report("%s does not support socket activation with LISTEN_FDS > 1", |
| 1032 | argv[0]); |
| 1033 | exit(EXIT_FAILURE); |
| 1034 | } |
| 1035 | server_ioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD, |
| 1036 | &local_err); |
| 1037 | if (server_ioc == NULL) { |
| 1038 | error_reportf_err(local_err, |
| 1039 | "Failed to use socket activation: "); |
| 1040 | exit(EXIT_FAILURE); |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | qemu_init_main_loop(&error_fatal); |
| 1045 | |
| 1046 | server_watch = qio_channel_add_watch(QIO_CHANNEL(server_ioc), |
| 1047 | G_IO_IN, |
| 1048 | accept_client, |
| 1049 | NULL, NULL); |
| 1050 | |
| 1051 | if (daemonize) { |
| 1052 | if (daemon(0, 0) < 0) { |
| 1053 | error_report("Failed to daemonize: %s", strerror(errno)); |
| 1054 | exit(EXIT_FAILURE); |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | if (daemonize || pidfile_specified) { |
| 1059 | qemu_write_pidfile(pidfile, &error_fatal); |
| 1060 | } |
| 1061 | |
| 1062 | #ifdef CONFIG_LIBCAP_NG |
| 1063 | if (drop_privileges() < 0) { |
| 1064 | error_report("Failed to drop privileges: %s", strerror(errno)); |
| 1065 | exit(EXIT_FAILURE); |
| 1066 | } |
| 1067 | #endif |
| 1068 | |
| 1069 | state = RUNNING; |
| 1070 | do { |
| 1071 | main_loop_wait(false); |
| 1072 | if (state == TERMINATE) { |
| 1073 | state = TERMINATING; |
| 1074 | close_server_socket(); |
| 1075 | } |
| 1076 | } while (num_active_sockets > 0); |
| 1077 | |
| 1078 | exit(EXIT_SUCCESS); |
| 1079 | } |