2 * Privileged helper to handle persistent reservation commands for QEMU
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.
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.
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/>.
21 #include "qemu/osdep.h"
23 #include <sys/ioctl.h>
24 #include <linux/dm-ioctl.h>
35 #include <mpath_cmd.h>
36 #include <mpath_persist.h>
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"
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"
54 #include "block/aio.h"
55 #include "block/thread-pool.h"
57 #include "scsi/constants.h"
58 #include "scsi/utils.h"
59 #include "pr-helper.h"
61 #define PR_OUT_FIXED_PARAM_SIZE 24
63 static char *socket_path;
65 static enum { RUNNING, TERMINATE, TERMINATING } state;
66 static QIOChannelSocket *server_ioc;
67 static int server_watch;
68 static int num_active_sockets = 1;
77 static void usage(const char *name)
80 "Usage: %s [OPTIONS] FILE\n"
81 "Persistent Reservation helper program for QEMU\n"
83 " -h, --help display this help and exit\n"
84 " -V, --version output version information and exit\n"
86 " -d, --daemon run in the background\n"
87 " -f, --pidfile=PATH PID file when running as a daemon\n"
89 " -k, --socket=PATH path to the unix socket\n"
91 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
92 " specify tracing options\n"
94 " -u, --user=USER user to drop privileges to\n"
95 " -g, --group=GROUP group to drop privileges to\n"
99 , name, pidfile, socket_path);
102 static void version(const char *name)
105 "%s " QEMU_VERSION QEMU_PKGVERSION "\n"
106 "Written by Paolo Bonzini.\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"
114 static void write_pidfile(void)
119 pidfd = qemu_open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
121 error_report("Cannot open pid file, %s", strerror(errno));
125 if (lockf(pidfd, F_TLOCK, 0)) {
126 error_report("Cannot lock pid file, %s", strerror(errno));
129 if (ftruncate(pidfd, 0)) {
130 error_report("Failed to truncate pid file");
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");
149 typedef struct PRHelperSGIOData {
154 int sz; /* input/output */
158 static int do_sgio_worker(void *opaque)
160 PRHelperSGIOData *data = opaque;
161 struct sg_io_hdr io_hdr;
164 SCSISense sense_code;
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;
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,
180 if (status == GOOD) {
181 data->sz -= io_hdr.resid;
186 if (status == CHECK_CONDITION &&
187 !(io_hdr.driver_status & SG_ERR_DRIVER_SENSE)) {
188 scsi_build_sense(data->sense, sense_code);
194 static int do_sgio(int fd, const uint8_t *cdb, uint8_t *sense,
195 uint8_t *buf, int *sz, int dir)
197 ThreadPool *pool = aio_get_thread_pool(qemu_get_aio_context());
200 PRHelperSGIOData data = {
209 r = thread_pool_submit_co(pool, do_sgio_worker, &data);
214 /* Device mapper interface */
217 #define CONTROL_PATH "/dev/mapper/control"
219 typedef struct DMData {
224 static int control_fd;
226 static void *dm_ioctl(int ioc, struct dm_ioctl *dm)
229 memcpy(&d.dm, dm, sizeof(d.dm));
230 QEMU_BUILD_BUG_ON(sizeof(d.data) < sizeof(struct dm_target_spec));
232 d.dm.version[0] = DM_VERSION_MAJOR;
235 d.dm.data_size = 1024;
236 d.dm.data_start = offsetof(DMData, data);
237 if (ioctl(control_fd, ioc, &d) < 0) {
240 memcpy(dm, &d.dm, sizeof(d.dm));
244 static void *dm_dev_ioctl(int fd, int ioc, struct dm_ioctl *dm)
255 dm->dev = st.st_rdev;
256 return dm_ioctl(ioc, dm);
259 static void dm_init(void)
261 control_fd = open(CONTROL_PATH, O_RDWR);
262 if (control_fd < 0) {
263 perror("Cannot open " CONTROL_PATH);
266 struct dm_ioctl dm = { 0 };
267 if (!dm_ioctl(DM_VERSION, &dm)) {
271 if (dm.version[0] != DM_VERSION_MAJOR) {
272 fprintf(stderr, "Unsupported device mapper interface");
277 /* Variables required by libmultipath and libmpathpersist. */
278 QEMU_BUILD_BUG_ON(PR_HELPER_DATA_SIZE > MPATH_MAX_PARAM_LEN);
279 static struct config *multipath_conf;
280 unsigned mpath_mx_alloc_len = PR_HELPER_DATA_SIZE;
284 extern struct config *get_multipath_config(void);
285 struct config *get_multipath_config(void)
287 return multipath_conf;
290 extern void put_multipath_config(struct config *conf);
291 void put_multipath_config(struct config *conf)
295 static void multipath_pr_init(void)
298 multipath_conf = mpath_lib_init();
301 static int is_mpath(int fd)
303 struct dm_ioctl dm = { .flags = DM_NOFLUSH_FLAG };
304 struct dm_target_spec *tgt;
306 tgt = dm_dev_ioctl(fd, DM_TABLE_STATUS, &dm);
308 if (errno == ENXIO) {
314 return !strncmp(tgt->target_type, "multipath", DM_MAX_TYPE_NAME);
317 static SCSISense mpath_generic_sense(int r)
320 case MPATH_PR_SENSE_NOT_READY:
321 return SENSE_CODE(NOT_READY);
322 case MPATH_PR_SENSE_MEDIUM_ERROR:
323 return SENSE_CODE(READ_ERROR);
324 case MPATH_PR_SENSE_HARDWARE_ERROR:
325 return SENSE_CODE(TARGET_FAILURE);
326 case MPATH_PR_SENSE_ABORTED_COMMAND:
327 return SENSE_CODE(IO_ERROR);
333 static int mpath_reconstruct_sense(int fd, int r, uint8_t *sense)
336 case MPATH_PR_SUCCESS:
338 case MPATH_PR_SENSE_NOT_READY:
339 case MPATH_PR_SENSE_MEDIUM_ERROR:
340 case MPATH_PR_SENSE_HARDWARE_ERROR:
341 case MPATH_PR_SENSE_ABORTED_COMMAND:
343 /* libmpathpersist ate the exact sense. Try to find it by
344 * issuing TEST UNIT READY.
346 uint8_t cdb[6] = { TEST_UNIT_READY };
348 int r = do_sgio(fd, cdb, sense, NULL, &sz, SG_DXFER_NONE);
353 scsi_build_sense(sense, mpath_generic_sense(r));
354 return CHECK_CONDITION;
357 case MPATH_PR_SENSE_UNIT_ATTENTION:
358 /* Congratulations libmpathpersist, you ruined the Unit Attention...
359 * Return a heavyweight one.
361 scsi_build_sense(sense, SENSE_CODE(SCSI_BUS_RESET));
362 return CHECK_CONDITION;
363 case MPATH_PR_SENSE_INVALID_OP:
364 /* Only one valid sense. */
365 scsi_build_sense(sense, SENSE_CODE(INVALID_OPCODE));
366 return CHECK_CONDITION;
367 case MPATH_PR_ILLEGAL_REQ:
369 scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM));
370 return CHECK_CONDITION;
371 case MPATH_PR_NO_SENSE:
372 scsi_build_sense(sense, SENSE_CODE(NO_SENSE));
373 return CHECK_CONDITION;
375 case MPATH_PR_RESERV_CONFLICT:
376 return RESERVATION_CONFLICT;
380 scsi_build_sense(sense, SENSE_CODE(LUN_COMM_FAILURE));
381 return CHECK_CONDITION;
385 static int multipath_pr_in(int fd, const uint8_t *cdb, uint8_t *sense,
386 uint8_t *data, int sz)
388 int rq_servact = cdb[1];
389 struct prin_resp resp;
393 switch (rq_servact) {
394 case MPATH_PRIN_RKEY_SA:
395 case MPATH_PRIN_RRES_SA:
396 case MPATH_PRIN_RCAP_SA:
398 case MPATH_PRIN_RFSTAT_SA:
399 /* Nobody implements it anyway, so bail out. */
401 /* Cannot parse any other output. */
402 scsi_build_sense(sense, SENSE_CODE(INVALID_FIELD));
403 return CHECK_CONDITION;
406 r = mpath_persistent_reserve_in(fd, rq_servact, &resp, noisy, verbose);
407 if (r == MPATH_PR_SUCCESS) {
408 switch (rq_servact) {
409 case MPATH_PRIN_RKEY_SA:
410 case MPATH_PRIN_RRES_SA: {
411 struct prin_readdescr *out = &resp.prin_descriptor.prin_readkeys;
413 written = MIN(out->additional_length + 8, sz);
414 stl_be_p(&data[0], out->prgeneration);
415 stl_be_p(&data[4], out->additional_length);
416 memcpy(&data[8], out->key_list, written - 8);
419 case MPATH_PRIN_RCAP_SA: {
420 struct prin_capdescr *out = &resp.prin_descriptor.prin_readcap;
423 stw_be_p(&data[0], out->length);
424 data[2] = out->flags[0];
425 data[3] = out->flags[1];
426 stw_be_p(&data[4], out->pr_type_mask);
430 scsi_build_sense(sense, SENSE_CODE(INVALID_OPCODE));
431 return CHECK_CONDITION;
433 assert(written <= sz);
434 memset(data + written, 0, sz - written);
437 return mpath_reconstruct_sense(fd, r, sense);
440 static int multipath_pr_out(int fd, const uint8_t *cdb, uint8_t *sense,
441 const uint8_t *param, int sz)
443 int rq_servact = cdb[1];
444 int rq_scope = cdb[2] >> 4;
445 int rq_type = cdb[2] & 0xf;
446 struct prout_param_descriptor paramp;
447 char transportids[PR_HELPER_DATA_SIZE];
450 switch (rq_servact) {
451 case MPATH_PROUT_REG_SA:
452 case MPATH_PROUT_RES_SA:
453 case MPATH_PROUT_REL_SA:
454 case MPATH_PROUT_CLEAR_SA:
455 case MPATH_PROUT_PREE_SA:
456 case MPATH_PROUT_PREE_AB_SA:
457 case MPATH_PROUT_REG_IGN_SA:
459 case MPATH_PROUT_REG_MOV_SA:
460 /* Not supported by struct prout_param_descriptor. */
462 /* Cannot parse any other input. */
463 scsi_build_sense(sense, SENSE_CODE(INVALID_FIELD));
464 return CHECK_CONDITION;
467 /* Convert input data, especially transport IDs, to the structs
468 * used by libmpathpersist (which, of course, will immediately
471 memset(¶mp, 0, sizeof(paramp));
472 memcpy(¶mp.key, ¶m[0], 8);
473 memcpy(¶mp.sa_key, ¶m[8], 8);
474 paramp.sa_flags = param[20];
475 if (sz > PR_OUT_FIXED_PARAM_SIZE) {
476 size_t transportid_len;
478 if (sz < PR_OUT_FIXED_PARAM_SIZE + 4) {
479 scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM_LEN));
480 return CHECK_CONDITION;
482 transportid_len = ldl_be_p(¶m[24]) + PR_OUT_FIXED_PARAM_SIZE + 4;
483 if (transportid_len > sz) {
484 scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM));
485 return CHECK_CONDITION;
487 for (i = PR_OUT_FIXED_PARAM_SIZE + 4, j = 0; i < transportid_len; ) {
488 struct transportid *id = (struct transportid *) &transportids[j];
491 id->format_code = param[i] & 0xc0;
492 id->protocol_id = param[i] & 0x0f;
493 switch (param[i] & 0xcf) {
496 if (i + 24 > transportid_len) {
499 memcpy(id->n_port_name, ¶m[i + 8], 8);
500 j += offsetof(struct transportid, n_port_name[8]);
505 /* iSCSI transport. */
506 len = lduw_be_p(¶m[i + 2]);
507 if (len > 252 || (len & 3) || i + len + 4 > transportid_len) {
508 /* For format code 00, the standard says the maximum is 223
509 * plus the NUL terminator. For format code 01 there is no
510 * maximum length, but libmpathpersist ignores the first
511 * byte of id->iscsi_name so our maximum is 252.
515 if (memchr(¶m[i + 4], 0, len) == NULL) {
518 memcpy(id->iscsi_name, ¶m[i + 2], len + 2);
519 j += offsetof(struct transportid, iscsi_name[len + 2]);
524 if (i + 24 > transportid_len) {
527 memcpy(id->sas_address, ¶m[i + 4], 8);
528 j += offsetof(struct transportid, sas_address[8]);
533 scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM));
534 return CHECK_CONDITION;
537 paramp.trnptid_list[paramp.num_transportid++] = id;
541 r = mpath_persistent_reserve_out(fd, rq_servact, rq_scope, rq_type,
542 ¶mp, noisy, verbose);
543 return mpath_reconstruct_sense(fd, r, sense);
547 static int do_pr_in(int fd, const uint8_t *cdb, uint8_t *sense,
548 uint8_t *data, int *resp_sz)
552 /* multipath_pr_in fills the whole input buffer. */
553 return multipath_pr_in(fd, cdb, sense, data, *resp_sz);
557 return do_sgio(fd, cdb, sense, data, resp_sz,
561 static int do_pr_out(int fd, const uint8_t *cdb, uint8_t *sense,
562 const uint8_t *param, int sz)
567 return multipath_pr_out(fd, cdb, sense, param, sz);
572 return do_sgio(fd, cdb, sense, (uint8_t *)param, &resp_sz,
578 typedef struct PRHelperClient {
579 QIOChannelSocket *ioc;
582 uint8_t data[PR_HELPER_DATA_SIZE];
585 typedef struct PRHelperRequest {
588 uint8_t cdb[PR_HELPER_CDB_SIZE];
591 static int coroutine_fn prh_read(PRHelperClient *client, void *buf, int sz,
605 n_read = qio_channel_readv_full(QIO_CHANNEL(client->ioc), &iov, 1,
608 if (n_read == QIO_CHANNEL_ERR_BLOCK) {
609 qio_channel_yield(QIO_CHANNEL(client->ioc), G_IO_IN);
613 ret = n_read ? n_read : -1;
617 /* Stash one file descriptor per request. */
619 bool too_many = false;
620 for (i = 0; i < nfds; i++) {
621 if (client->fd == -1) {
642 if (client->fd != -1) {
649 static int coroutine_fn prh_read_request(PRHelperClient *client,
650 PRHelperRequest *req,
651 PRHelperResponse *resp, Error **errp)
655 if (prh_read(client, req->cdb, sizeof(req->cdb), NULL) < 0) {
659 if (client->fd == -1) {
660 error_setg(errp, "No file descriptor in request.");
664 if (req->cdb[0] != PERSISTENT_RESERVE_OUT &&
665 req->cdb[0] != PERSISTENT_RESERVE_IN) {
666 error_setg(errp, "Invalid CDB, closing socket.");
670 sz = scsi_cdb_xfer(req->cdb);
671 if (sz > sizeof(client->data)) {
675 if (req->cdb[0] == PERSISTENT_RESERVE_OUT) {
676 if (qio_channel_read_all(QIO_CHANNEL(client->ioc),
677 (char *)client->data, sz,
681 if ((fcntl(client->fd, F_GETFL) & O_ACCMODE) == O_RDONLY) {
682 scsi_build_sense(resp->sense, SENSE_CODE(INVALID_OPCODE));
684 } else if (sz < PR_OUT_FIXED_PARAM_SIZE) {
685 /* Illegal request, Parameter list length error. This isn't fatal;
686 * we have read the data, send an error without closing the socket.
688 scsi_build_sense(resp->sense, SENSE_CODE(INVALID_PARAM_LEN));
692 resp->result = CHECK_CONDITION;
698 req->fd = client->fd;
709 static int coroutine_fn prh_write_response(PRHelperClient *client,
710 PRHelperRequest *req,
711 PRHelperResponse *resp, Error **errp)
716 if (req->cdb[0] == PERSISTENT_RESERVE_IN && resp->result == GOOD) {
717 assert(resp->sz <= req->sz && resp->sz <= sizeof(client->data));
719 assert(resp->sz == 0);
724 resp->result = cpu_to_be32(resp->result);
725 resp->sz = cpu_to_be32(resp->sz);
726 r = qio_channel_write_all(QIO_CHANNEL(client->ioc),
727 (char *) resp, sizeof(*resp), errp);
732 r = qio_channel_write_all(QIO_CHANNEL(client->ioc),
733 (char *) client->data,
735 return r < 0 ? r : 0;
738 static void coroutine_fn prh_co_entry(void *opaque)
740 PRHelperClient *client = opaque;
741 Error *local_err = NULL;
745 qio_channel_set_blocking(QIO_CHANNEL(client->ioc),
747 qio_channel_attach_aio_context(QIO_CHANNEL(client->ioc),
748 qemu_get_aio_context());
750 /* A very simple negotiation for future extensibility. No features
751 * are defined so write 0.
753 flags = cpu_to_be32(0);
754 r = qio_channel_write_all(QIO_CHANNEL(client->ioc),
755 (char *) &flags, sizeof(flags), NULL);
760 r = qio_channel_read_all(QIO_CHANNEL(client->ioc),
761 (char *) &flags, sizeof(flags), NULL);
762 if (be32_to_cpu(flags) != 0 || r < 0) {
766 while (atomic_read(&state) == RUNNING) {
768 PRHelperResponse resp;
771 sz = prh_read_request(client, &req, &resp, &local_err);
777 num_active_sockets++;
778 if (req.cdb[0] == PERSISTENT_RESERVE_OUT) {
779 r = do_pr_out(req.fd, req.cdb, resp.sense,
783 resp.sz = sizeof(client->data);
784 r = do_pr_in(req.fd, req.cdb, resp.sense,
785 client->data, &resp.sz);
786 resp.sz = MIN(resp.sz, sz);
788 num_active_sockets--;
796 if (prh_write_response(client, &req, &resp, &local_err) < 0) {
803 error_free(local_err);
805 error_report_err(local_err);
810 qio_channel_detach_aio_context(QIO_CHANNEL(client->ioc));
811 object_unref(OBJECT(client->ioc));
815 static gboolean accept_client(QIOChannel *ioc, GIOCondition cond, gpointer opaque)
817 QIOChannelSocket *cioc;
820 cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
826 prh = g_new(PRHelperClient, 1);
829 prh->co = qemu_coroutine_create(prh_co_entry, prh);
830 qemu_coroutine_enter(prh->co);
837 * Check socket parameters compatibility when socket activation is used.
839 static const char *socket_activation_validate_opts(void)
841 if (socket_path != NULL) {
842 return "Unix socket can't be set when using socket activation";
848 static void compute_default_paths(void)
851 socket_path = qemu_get_local_state_pathname("run/qemu-pr-helper.sock");
855 static void termsig_handler(int signum)
857 atomic_cmpxchg(&state, RUNNING, TERMINATE);
861 static void close_server_socket(void)
865 g_source_remove(server_watch);
867 object_unref(OBJECT(server_ioc));
868 num_active_sockets--;
872 static int drop_privileges(void)
874 /* clear all capabilities */
875 capng_clear(CAPNG_SELECT_BOTH);
877 if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
878 CAP_SYS_RAWIO) < 0) {
883 /* For /dev/mapper/control ioctls */
884 if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
885 CAP_SYS_ADMIN) < 0) {
890 /* Change user/group id, retaining the capabilities. Because file descriptors
891 * are passed via SCM_RIGHTS, we don't need supplementary groups (and in
892 * fact the helper can run as "nobody").
894 if (capng_change_id(uid != -1 ? uid : getuid(),
895 gid != -1 ? gid : getgid(),
896 CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING)) {
904 int main(int argc, char **argv)
906 const char *sopt = "hVk:fdT:u:g:vq";
907 struct option lopt[] = {
908 { "help", no_argument, NULL, 'h' },
909 { "version", no_argument, NULL, 'V' },
910 { "socket", required_argument, NULL, 'k' },
911 { "pidfile", no_argument, NULL, 'f' },
912 { "daemon", no_argument, NULL, 'd' },
913 { "trace", required_argument, NULL, 'T' },
914 { "user", required_argument, NULL, 'u' },
915 { "group", required_argument, NULL, 'g' },
916 { "verbose", no_argument, NULL, 'v' },
917 { "quiet", no_argument, NULL, 'q' },
924 Error *local_err = NULL;
925 char *trace_file = NULL;
926 bool daemonize = false;
927 unsigned socket_activation;
929 struct sigaction sa_sigterm;
930 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
931 sa_sigterm.sa_handler = termsig_handler;
932 sigaction(SIGTERM, &sa_sigterm, NULL);
933 sigaction(SIGINT, &sa_sigterm, NULL);
934 sigaction(SIGHUP, &sa_sigterm, NULL);
936 signal(SIGPIPE, SIG_IGN);
938 module_call_init(MODULE_INIT_TRACE);
939 module_call_init(MODULE_INIT_QOM);
940 qemu_add_opts(&qemu_trace_opts);
941 qemu_init_exec_dir(argv[0]);
943 pidfile = qemu_get_local_state_pathname("run/qemu-pr-helper.pid");
945 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
948 socket_path = optarg;
949 if (socket_path[0] != '/') {
950 error_report("socket path must be absolute");
960 struct passwd *userinfo = getpwnam(optarg);
962 uid = userinfo->pw_uid;
963 } else if (qemu_strtoul(optarg, NULL, 10, &res) == 0 &&
967 error_report("invalid user '%s'", optarg);
974 struct group *groupinfo = getgrnam(optarg);
976 gid = groupinfo->gr_gid;
977 } else if (qemu_strtoul(optarg, NULL, 10, &res) == 0 &&
981 error_report("invalid group '%s'", optarg);
989 error_report("-%c not supported by this %s", ch, argv[0]);
1003 trace_file = trace_opt_parse(optarg);
1014 error_report("Try `%s --help' for more information.", argv[0]);
1020 noisy = !quiet && (loglevel >= 3);
1021 verbose = quiet ? 0 : MIN(loglevel, 3);
1023 if (!trace_init_backends()) {
1026 trace_init_file(trace_file);
1027 qemu_set_log(LOG_TRACE);
1031 multipath_pr_init();
1034 socket_activation = check_socket_activation();
1035 if (socket_activation == 0) {
1036 SocketAddress saddr;
1037 compute_default_paths();
1038 saddr = (SocketAddress){
1039 .type = SOCKET_ADDRESS_TYPE_UNIX,
1040 .u.q_unix.path = g_strdup(socket_path)
1042 server_ioc = qio_channel_socket_new();
1043 if (qio_channel_socket_listen_sync(server_ioc, &saddr, &local_err) < 0) {
1044 object_unref(OBJECT(server_ioc));
1045 error_report_err(local_err);
1048 g_free(saddr.u.q_unix.path);
1050 /* Using socket activation - check user didn't use -p etc. */
1051 const char *err_msg = socket_activation_validate_opts();
1052 if (err_msg != NULL) {
1053 error_report("%s", err_msg);
1057 /* Can only listen on a single socket. */
1058 if (socket_activation > 1) {
1059 error_report("%s does not support socket activation with LISTEN_FDS > 1",
1063 server_ioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD,
1065 if (server_ioc == NULL) {
1066 error_report("Failed to use socket activation: %s",
1067 error_get_pretty(local_err));
1073 if (qemu_init_main_loop(&local_err)) {
1074 error_report_err(local_err);
1078 server_watch = qio_channel_add_watch(QIO_CHANNEL(server_ioc),
1083 #ifdef CONFIG_LIBCAP
1084 if (drop_privileges() < 0) {
1085 error_report("Failed to drop privileges: %s", strerror(errno));
1091 if (daemon(0, 0) < 0) {
1092 error_report("Failed to daemonize: %s", strerror(errno));
1100 main_loop_wait(false);
1101 if (state == TERMINATE) {
1102 state = TERMINATING;
1103 close_server_socket();
1105 } while (num_active_sockets > 0);