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