2 * Privileged helper to handle persistent reservation commands for QEMU
4 * Copyright (C) 2017 Red Hat, Inc. <pbonzini@redhat.com>
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
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>
27 #ifdef CONFIG_LIBCAP_NG
35 #include <mpath_cmd.h>
36 #include <mpath_persist.h>
39 #include "qemu-common.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"
48 #include "qemu/systemd.h"
49 #include "qapi/util.h"
50 #include "qapi/qmp/qstring.h"
51 #include "io/channel-socket.h"
52 #include "trace/control.h"
53 #include "qemu-version.h"
55 #include "block/aio.h"
56 #include "block/thread-pool.h"
58 #include "scsi/constants.h"
59 #include "scsi/utils.h"
60 #include "pr-helper.h"
62 #define PR_OUT_FIXED_PARAM_SIZE 24
64 static char *socket_path
;
66 static enum { RUNNING
, TERMINATE
, TERMINATING
} state
;
67 static QIOChannelSocket
*server_ioc
;
68 static int server_watch
;
69 static int num_active_sockets
= 1;
73 #ifdef CONFIG_LIBCAP_NG
78 static void compute_default_paths(void)
80 socket_path
= qemu_get_local_state_pathname("run/qemu-pr-helper.sock");
81 pidfile
= qemu_get_local_state_pathname("run/qemu-pr-helper.pid");
84 static void usage(const char *name
)
87 "Usage: %s [OPTIONS] FILE\n"
88 "Persistent Reservation helper program for QEMU\n"
90 " -h, --help display this help and exit\n"
91 " -V, --version output version information and exit\n"
93 " -d, --daemon run in the background\n"
94 " -f, --pidfile=PATH PID file when running as a daemon\n"
96 " -k, --socket=PATH path to the unix socket\n"
98 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
99 " specify tracing options\n"
100 #ifdef CONFIG_LIBCAP_NG
101 " -u, --user=USER user to drop privileges to\n"
102 " -g, --group=GROUP group to drop privileges to\n"
105 QEMU_HELP_BOTTOM
"\n"
106 , name
, pidfile
, socket_path
);
109 static void version(const char *name
)
112 "%s " QEMU_FULL_VERSION
"\n"
113 "Written by Paolo Bonzini.\n"
116 "This is free software; see the source for copying conditions. There is NO\n"
117 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
123 typedef struct PRHelperSGIOData
{
128 int sz
; /* input/output */
132 static int do_sgio_worker(void *opaque
)
134 PRHelperSGIOData
*data
= opaque
;
135 struct sg_io_hdr io_hdr
;
138 SCSISense sense_code
;
140 memset(data
->sense
, 0, PR_HELPER_SENSE_SIZE
);
141 memset(&io_hdr
, 0, sizeof(io_hdr
));
142 io_hdr
.interface_id
= 'S';
143 io_hdr
.cmd_len
= PR_HELPER_CDB_SIZE
;
144 io_hdr
.cmdp
= (uint8_t *)data
->cdb
;
145 io_hdr
.sbp
= data
->sense
;
146 io_hdr
.mx_sb_len
= PR_HELPER_SENSE_SIZE
;
148 io_hdr
.dxfer_direction
= data
->dir
;
149 io_hdr
.dxferp
= (char *)data
->buf
;
150 io_hdr
.dxfer_len
= data
->sz
;
151 ret
= ioctl(data
->fd
, SG_IO
, &io_hdr
);
154 status
= scsi_sense_from_errno(errno
, &sense_code
);
155 if (status
== CHECK_CONDITION
) {
156 scsi_build_sense(data
->sense
, sense_code
);
158 } else if (io_hdr
.host_status
!= SCSI_HOST_OK
) {
159 status
= scsi_sense_from_host_status(io_hdr
.host_status
, &sense_code
);
160 if (status
== CHECK_CONDITION
) {
161 scsi_build_sense(data
->sense
, sense_code
);
163 } else if (io_hdr
.driver_status
& SG_ERR_DRIVER_TIMEOUT
) {
166 status
= io_hdr
.status
;
169 if (status
== GOOD
) {
170 data
->sz
-= io_hdr
.resid
;
178 static int do_sgio(int fd
, const uint8_t *cdb
, uint8_t *sense
,
179 uint8_t *buf
, int *sz
, int dir
)
181 ThreadPool
*pool
= aio_get_thread_pool(qemu_get_aio_context());
184 PRHelperSGIOData data
= {
193 r
= thread_pool_submit_co(pool
, do_sgio_worker
, &data
);
198 /* Device mapper interface */
201 #define CONTROL_PATH "/dev/mapper/control"
203 typedef struct DMData
{
208 static int control_fd
;
210 static void *dm_ioctl(int ioc
, struct dm_ioctl
*dm
)
213 memcpy(&d
.dm
, dm
, sizeof(d
.dm
));
214 QEMU_BUILD_BUG_ON(sizeof(d
.data
) < sizeof(struct dm_target_spec
));
216 d
.dm
.version
[0] = DM_VERSION_MAJOR
;
219 d
.dm
.data_size
= 1024;
220 d
.dm
.data_start
= offsetof(DMData
, data
);
221 if (ioctl(control_fd
, ioc
, &d
) < 0) {
224 memcpy(dm
, &d
.dm
, sizeof(d
.dm
));
228 static void *dm_dev_ioctl(int fd
, int ioc
, struct dm_ioctl
*dm
)
239 dm
->dev
= st
.st_rdev
;
240 return dm_ioctl(ioc
, dm
);
243 static void dm_init(void)
245 control_fd
= open(CONTROL_PATH
, O_RDWR
);
246 if (control_fd
< 0) {
247 perror("Cannot open " CONTROL_PATH
);
250 struct dm_ioctl dm
= { };
251 if (!dm_ioctl(DM_VERSION
, &dm
)) {
255 if (dm
.version
[0] != DM_VERSION_MAJOR
) {
256 fprintf(stderr
, "Unsupported device mapper interface");
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
;
268 extern struct config
*get_multipath_config(void);
269 struct config
*get_multipath_config(void)
271 return multipath_conf
;
274 extern void put_multipath_config(struct config
*conf
);
275 void put_multipath_config(struct config
*conf
)
279 static void multipath_pr_init(void)
282 #ifdef CONFIG_MPATH_NEW_API
283 multipath_conf
= mpath_lib_init();
285 mpath_lib_init(udev
);
289 static int is_mpath(int fd
)
291 struct dm_ioctl dm
= { .flags
= DM_NOFLUSH_FLAG
};
292 struct dm_target_spec
*tgt
;
294 tgt
= dm_dev_ioctl(fd
, DM_TABLE_STATUS
, &dm
);
296 if (errno
== ENXIO
) {
302 return !strncmp(tgt
->target_type
, "multipath", DM_MAX_TYPE_NAME
);
305 static SCSISense
mpath_generic_sense(int r
)
308 case MPATH_PR_SENSE_NOT_READY
:
309 return SENSE_CODE(NOT_READY
);
310 case MPATH_PR_SENSE_MEDIUM_ERROR
:
311 return SENSE_CODE(READ_ERROR
);
312 case MPATH_PR_SENSE_HARDWARE_ERROR
:
313 return SENSE_CODE(TARGET_FAILURE
);
314 case MPATH_PR_SENSE_ABORTED_COMMAND
:
315 return SENSE_CODE(IO_ERROR
);
321 static int mpath_reconstruct_sense(int fd
, int r
, uint8_t *sense
)
324 case MPATH_PR_SUCCESS
:
326 case MPATH_PR_SENSE_NOT_READY
:
327 case MPATH_PR_SENSE_MEDIUM_ERROR
:
328 case MPATH_PR_SENSE_HARDWARE_ERROR
:
329 case MPATH_PR_SENSE_ABORTED_COMMAND
:
331 /* libmpathpersist ate the exact sense. Try to find it by
332 * issuing TEST UNIT READY.
334 uint8_t cdb
[6] = { TEST_UNIT_READY
};
336 int ret
= do_sgio(fd
, cdb
, sense
, NULL
, &sz
, SG_DXFER_NONE
);
341 scsi_build_sense(sense
, mpath_generic_sense(r
));
342 return CHECK_CONDITION
;
345 case MPATH_PR_SENSE_UNIT_ATTENTION
:
346 /* Congratulations libmpathpersist, you ruined the Unit Attention...
347 * Return a heavyweight one.
349 scsi_build_sense(sense
, SENSE_CODE(SCSI_BUS_RESET
));
350 return CHECK_CONDITION
;
351 case MPATH_PR_SENSE_INVALID_OP
:
352 /* Only one valid sense. */
353 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
354 return CHECK_CONDITION
;
355 case MPATH_PR_ILLEGAL_REQ
:
357 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM
));
358 return CHECK_CONDITION
;
359 case MPATH_PR_NO_SENSE
:
360 scsi_build_sense(sense
, SENSE_CODE(NO_SENSE
));
361 return CHECK_CONDITION
;
363 case MPATH_PR_RESERV_CONFLICT
:
364 return RESERVATION_CONFLICT
;
368 scsi_build_sense(sense
, SENSE_CODE(LUN_COMM_FAILURE
));
369 return CHECK_CONDITION
;
373 static int multipath_pr_in(int fd
, const uint8_t *cdb
, uint8_t *sense
,
374 uint8_t *data
, int sz
)
376 int rq_servact
= cdb
[1];
377 struct prin_resp resp
;
381 switch (rq_servact
) {
382 case MPATH_PRIN_RKEY_SA
:
383 case MPATH_PRIN_RRES_SA
:
384 case MPATH_PRIN_RCAP_SA
:
386 case MPATH_PRIN_RFSTAT_SA
:
387 /* Nobody implements it anyway, so bail out. */
389 /* Cannot parse any other output. */
390 scsi_build_sense(sense
, SENSE_CODE(INVALID_FIELD
));
391 return CHECK_CONDITION
;
394 r
= mpath_persistent_reserve_in(fd
, rq_servact
, &resp
, noisy
, verbose
);
395 if (r
== MPATH_PR_SUCCESS
) {
396 switch (rq_servact
) {
397 case MPATH_PRIN_RKEY_SA
:
398 case MPATH_PRIN_RRES_SA
: {
399 struct prin_readdescr
*out
= &resp
.prin_descriptor
.prin_readkeys
;
401 written
= MIN(out
->additional_length
+ 8, sz
);
402 stl_be_p(&data
[0], out
->prgeneration
);
403 stl_be_p(&data
[4], out
->additional_length
);
404 memcpy(&data
[8], out
->key_list
, written
- 8);
407 case MPATH_PRIN_RCAP_SA
: {
408 struct prin_capdescr
*out
= &resp
.prin_descriptor
.prin_readcap
;
411 stw_be_p(&data
[0], out
->length
);
412 data
[2] = out
->flags
[0];
413 data
[3] = out
->flags
[1];
414 stw_be_p(&data
[4], out
->pr_type_mask
);
418 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
419 return CHECK_CONDITION
;
421 assert(written
<= sz
);
422 memset(data
+ written
, 0, sz
- written
);
425 return mpath_reconstruct_sense(fd
, r
, sense
);
428 static int multipath_pr_out(int fd
, const uint8_t *cdb
, uint8_t *sense
,
429 const uint8_t *param
, int sz
)
431 int rq_servact
= cdb
[1];
432 int rq_scope
= cdb
[2] >> 4;
433 int rq_type
= cdb
[2] & 0xf;
434 g_autofree
struct prout_param_descriptor
*paramp
= NULL
;
435 char transportids
[PR_HELPER_DATA_SIZE
];
438 paramp
= g_malloc0(sizeof(struct prout_param_descriptor
)
439 + sizeof(struct transportid
*) * MPATH_MX_TIDS
);
441 if (sz
< PR_OUT_FIXED_PARAM_SIZE
) {
442 /* Illegal request, Parameter list length error. This isn't fatal;
443 * we have read the data, send an error without closing the socket.
445 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM_LEN
));
446 return CHECK_CONDITION
;
449 switch (rq_servact
) {
450 case MPATH_PROUT_REG_SA
:
451 case MPATH_PROUT_RES_SA
:
452 case MPATH_PROUT_REL_SA
:
453 case MPATH_PROUT_CLEAR_SA
:
454 case MPATH_PROUT_PREE_SA
:
455 case MPATH_PROUT_PREE_AB_SA
:
456 case MPATH_PROUT_REG_IGN_SA
:
458 case MPATH_PROUT_REG_MOV_SA
:
459 /* Not supported by struct prout_param_descriptor. */
461 /* Cannot parse any other input. */
462 scsi_build_sense(sense
, SENSE_CODE(INVALID_FIELD
));
463 return CHECK_CONDITION
;
466 /* Convert input data, especially transport IDs, to the structs
467 * used by libmpathpersist (which, of course, will immediately
470 memcpy(¶mp
->key
, ¶m
[0], 8);
471 memcpy(¶mp
->sa_key
, ¶m
[8], 8);
472 paramp
->sa_flags
= param
[20];
473 if (sz
> PR_OUT_FIXED_PARAM_SIZE
) {
474 size_t transportid_len
;
476 if (sz
< PR_OUT_FIXED_PARAM_SIZE
+ 4) {
477 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM_LEN
));
478 return CHECK_CONDITION
;
480 transportid_len
= ldl_be_p(¶m
[24]) + PR_OUT_FIXED_PARAM_SIZE
+ 4;
481 if (transportid_len
> sz
) {
482 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM
));
483 return CHECK_CONDITION
;
485 for (i
= PR_OUT_FIXED_PARAM_SIZE
+ 4, j
= 0; i
< transportid_len
; ) {
486 struct transportid
*id
= (struct transportid
*) &transportids
[j
];
489 id
->format_code
= param
[i
] & 0xc0;
490 id
->protocol_id
= param
[i
] & 0x0f;
491 switch (param
[i
] & 0xcf) {
494 if (i
+ 24 > transportid_len
) {
497 memcpy(id
->n_port_name
, ¶m
[i
+ 8], 8);
498 j
+= offsetof(struct transportid
, n_port_name
[8]);
503 /* iSCSI transport. */
504 len
= lduw_be_p(¶m
[i
+ 2]);
505 if (len
> 252 || (len
& 3) || i
+ len
+ 4 > transportid_len
) {
506 /* For format code 00, the standard says the maximum is 223
507 * plus the NUL terminator. For format code 01 there is no
508 * maximum length, but libmpathpersist ignores the first
509 * byte of id->iscsi_name so our maximum is 252.
513 if (memchr(¶m
[i
+ 4], 0, len
) == NULL
) {
516 memcpy(id
->iscsi_name
, ¶m
[i
+ 2], len
+ 2);
517 j
+= offsetof(struct transportid
, iscsi_name
[len
+ 2]);
522 if (i
+ 24 > transportid_len
) {
525 memcpy(id
->sas_address
, ¶m
[i
+ 4], 8);
526 j
+= offsetof(struct transportid
, sas_address
[8]);
531 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM
));
532 return CHECK_CONDITION
;
535 assert(paramp
->num_transportid
< MPATH_MX_TIDS
);
536 paramp
->trnptid_list
[paramp
->num_transportid
++] = id
;
540 r
= mpath_persistent_reserve_out(fd
, rq_servact
, rq_scope
, rq_type
,
541 paramp
, noisy
, verbose
);
542 return mpath_reconstruct_sense(fd
, r
, sense
);
546 static int do_pr_in(int fd
, const uint8_t *cdb
, uint8_t *sense
,
547 uint8_t *data
, int *resp_sz
)
551 /* multipath_pr_in fills the whole input buffer. */
552 int r
= multipath_pr_in(fd
, cdb
, sense
, data
, *resp_sz
);
560 return do_sgio(fd
, cdb
, sense
, data
, resp_sz
,
564 static int do_pr_out(int fd
, const uint8_t *cdb
, uint8_t *sense
,
565 const uint8_t *param
, int sz
)
569 if ((fcntl(fd
, F_GETFL
) & O_ACCMODE
) == O_RDONLY
) {
570 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
571 return CHECK_CONDITION
;
576 return multipath_pr_out(fd
, cdb
, sense
, param
, sz
);
581 return do_sgio(fd
, cdb
, sense
, (uint8_t *)param
, &resp_sz
,
587 typedef struct PRHelperClient
{
588 QIOChannelSocket
*ioc
;
591 uint8_t data
[PR_HELPER_DATA_SIZE
];
594 typedef struct PRHelperRequest
{
597 uint8_t cdb
[PR_HELPER_CDB_SIZE
];
600 static int coroutine_fn
prh_read(PRHelperClient
*client
, void *buf
, int sz
,
614 n_read
= qio_channel_readv_full(QIO_CHANNEL(client
->ioc
), &iov
, 1,
617 if (n_read
== QIO_CHANNEL_ERR_BLOCK
) {
618 qio_channel_yield(QIO_CHANNEL(client
->ioc
), G_IO_IN
);
622 ret
= n_read
? n_read
: -1;
626 /* Stash one file descriptor per request. */
628 bool too_many
= false;
629 for (i
= 0; i
< nfds
; i
++) {
630 if (client
->fd
== -1) {
651 if (client
->fd
!= -1) {
658 static int coroutine_fn
prh_read_request(PRHelperClient
*client
,
659 PRHelperRequest
*req
,
660 PRHelperResponse
*resp
, Error
**errp
)
664 if (prh_read(client
, req
->cdb
, sizeof(req
->cdb
), NULL
) < 0) {
668 if (client
->fd
== -1) {
669 error_setg(errp
, "No file descriptor in request.");
673 if (req
->cdb
[0] != PERSISTENT_RESERVE_OUT
&&
674 req
->cdb
[0] != PERSISTENT_RESERVE_IN
) {
675 error_setg(errp
, "Invalid CDB, closing socket.");
679 sz
= scsi_cdb_xfer(req
->cdb
);
680 if (sz
> sizeof(client
->data
)) {
684 if (req
->cdb
[0] == PERSISTENT_RESERVE_OUT
) {
685 if (qio_channel_read_all(QIO_CHANNEL(client
->ioc
),
686 (char *)client
->data
, sz
,
692 req
->fd
= client
->fd
;
703 static int coroutine_fn
prh_write_response(PRHelperClient
*client
,
704 PRHelperRequest
*req
,
705 PRHelperResponse
*resp
, Error
**errp
)
710 if (req
->cdb
[0] == PERSISTENT_RESERVE_IN
&& resp
->result
== GOOD
) {
711 assert(resp
->sz
<= req
->sz
&& resp
->sz
<= sizeof(client
->data
));
713 assert(resp
->sz
== 0);
718 resp
->result
= cpu_to_be32(resp
->result
);
719 resp
->sz
= cpu_to_be32(resp
->sz
);
720 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
721 (char *) resp
, sizeof(*resp
), errp
);
726 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
727 (char *) client
->data
,
729 return r
< 0 ? r
: 0;
732 static void coroutine_fn
prh_co_entry(void *opaque
)
734 PRHelperClient
*client
= opaque
;
735 Error
*local_err
= NULL
;
739 qio_channel_set_blocking(QIO_CHANNEL(client
->ioc
),
741 qio_channel_attach_aio_context(QIO_CHANNEL(client
->ioc
),
742 qemu_get_aio_context());
744 /* A very simple negotiation for future extensibility. No features
745 * are defined so write 0.
747 flags
= cpu_to_be32(0);
748 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
749 (char *) &flags
, sizeof(flags
), NULL
);
754 r
= qio_channel_read_all(QIO_CHANNEL(client
->ioc
),
755 (char *) &flags
, sizeof(flags
), NULL
);
756 if (be32_to_cpu(flags
) != 0 || r
< 0) {
760 while (qatomic_read(&state
) == RUNNING
) {
762 PRHelperResponse resp
;
765 sz
= prh_read_request(client
, &req
, &resp
, &local_err
);
770 num_active_sockets
++;
771 if (req
.cdb
[0] == PERSISTENT_RESERVE_OUT
) {
772 r
= do_pr_out(req
.fd
, req
.cdb
, resp
.sense
,
776 resp
.sz
= sizeof(client
->data
);
777 r
= do_pr_in(req
.fd
, req
.cdb
, resp
.sense
,
778 client
->data
, &resp
.sz
);
779 resp
.sz
= MIN(resp
.sz
, sz
);
781 num_active_sockets
--;
788 if (prh_write_response(client
, &req
, &resp
, &local_err
) < 0) {
795 error_free(local_err
);
797 error_report_err(local_err
);
802 qio_channel_detach_aio_context(QIO_CHANNEL(client
->ioc
));
803 object_unref(OBJECT(client
->ioc
));
807 static gboolean
accept_client(QIOChannel
*ioc
, GIOCondition cond
, gpointer opaque
)
809 QIOChannelSocket
*cioc
;
812 cioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc
),
818 prh
= g_new(PRHelperClient
, 1);
821 prh
->co
= qemu_coroutine_create(prh_co_entry
, prh
);
822 qemu_coroutine_enter(prh
->co
);
827 static void termsig_handler(int signum
)
829 qatomic_cmpxchg(&state
, RUNNING
, TERMINATE
);
833 static void close_server_socket(void)
837 g_source_remove(server_watch
);
839 object_unref(OBJECT(server_ioc
));
840 num_active_sockets
--;
843 #ifdef CONFIG_LIBCAP_NG
844 static int drop_privileges(void)
846 /* clear all capabilities */
847 capng_clear(CAPNG_SELECT_BOTH
);
849 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
850 CAP_SYS_RAWIO
) < 0) {
855 /* For /dev/mapper/control ioctls */
856 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
857 CAP_SYS_ADMIN
) < 0) {
862 /* Change user/group id, retaining the capabilities. Because file descriptors
863 * are passed via SCM_RIGHTS, we don't need supplementary groups (and in
864 * fact the helper can run as "nobody").
866 if (capng_change_id(uid
!= -1 ? uid
: getuid(),
867 gid
!= -1 ? gid
: getgid(),
868 CAPNG_DROP_SUPP_GRP
| CAPNG_CLEAR_BOUNDING
)) {
876 int main(int argc
, char **argv
)
878 const char *sopt
= "hVk:f:dT:u:g:vq";
879 struct option lopt
[] = {
880 { "help", no_argument
, NULL
, 'h' },
881 { "version", no_argument
, NULL
, 'V' },
882 { "socket", required_argument
, NULL
, 'k' },
883 { "pidfile", required_argument
, NULL
, 'f' },
884 { "daemon", no_argument
, NULL
, 'd' },
885 { "trace", required_argument
, NULL
, 'T' },
886 { "user", required_argument
, NULL
, 'u' },
887 { "group", required_argument
, NULL
, 'g' },
888 { "verbose", no_argument
, NULL
, 'v' },
889 { "quiet", no_argument
, NULL
, 'q' },
896 Error
*local_err
= NULL
;
897 bool daemonize
= false;
898 bool pidfile_specified
= false;
899 bool socket_path_specified
= false;
900 unsigned socket_activation
;
902 struct sigaction sa_sigterm
;
903 memset(&sa_sigterm
, 0, sizeof(sa_sigterm
));
904 sa_sigterm
.sa_handler
= termsig_handler
;
905 sigaction(SIGTERM
, &sa_sigterm
, NULL
);
906 sigaction(SIGINT
, &sa_sigterm
, NULL
);
907 sigaction(SIGHUP
, &sa_sigterm
, NULL
);
909 signal(SIGPIPE
, SIG_IGN
);
912 module_call_init(MODULE_INIT_TRACE
);
913 module_call_init(MODULE_INIT_QOM
);
914 qemu_add_opts(&qemu_trace_opts
);
915 qemu_init_exec_dir(argv
[0]);
917 compute_default_paths();
919 while ((ch
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_ind
)) != -1) {
923 socket_path
= g_strdup(optarg
);
924 socket_path_specified
= true;
925 if (socket_path
[0] != '/') {
926 error_report("socket path must be absolute");
932 pidfile
= g_strdup(optarg
);
933 pidfile_specified
= true;
935 #ifdef CONFIG_LIBCAP_NG
938 struct passwd
*userinfo
= getpwnam(optarg
);
940 uid
= userinfo
->pw_uid
;
941 } else if (qemu_strtoul(optarg
, NULL
, 10, &res
) == 0 &&
945 error_report("invalid user '%s'", optarg
);
952 struct group
*groupinfo
= getgrnam(optarg
);
954 gid
= groupinfo
->gr_gid
;
955 } else if (qemu_strtoul(optarg
, NULL
, 10, &res
) == 0 &&
959 error_report("invalid group '%s'", optarg
);
967 error_report("-%c not supported by this %s", ch
, argv
[0]);
980 trace_opt_parse(optarg
);
991 error_report("Try `%s --help' for more information.", argv
[0]);
997 noisy
= !quiet
&& (loglevel
>= 3);
998 verbose
= quiet
? 0 : MIN(loglevel
, 3);
1000 if (!trace_init_backends()) {
1004 qemu_set_log(LOG_TRACE
);
1008 multipath_pr_init();
1011 socket_activation
= check_socket_activation();
1012 if (socket_activation
== 0) {
1013 SocketAddress saddr
;
1014 saddr
= (SocketAddress
){
1015 .type
= SOCKET_ADDRESS_TYPE_UNIX
,
1016 .u
.q_unix
.path
= socket_path
,
1018 server_ioc
= qio_channel_socket_new();
1019 if (qio_channel_socket_listen_sync(server_ioc
, &saddr
,
1020 1, &local_err
) < 0) {
1021 object_unref(OBJECT(server_ioc
));
1022 error_report_err(local_err
);
1026 /* Using socket activation - check user didn't use -p etc. */
1027 if (socket_path_specified
) {
1028 error_report("Unix socket can't be set when using socket activation");
1032 /* Can only listen on a single socket. */
1033 if (socket_activation
> 1) {
1034 error_report("%s does not support socket activation with LISTEN_FDS > 1",
1038 server_ioc
= qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD
,
1040 if (server_ioc
== NULL
) {
1041 error_reportf_err(local_err
,
1042 "Failed to use socket activation: ");
1047 if (qemu_init_main_loop(&local_err
)) {
1048 error_report_err(local_err
);
1052 server_watch
= qio_channel_add_watch(QIO_CHANNEL(server_ioc
),
1058 if (daemon(0, 0) < 0) {
1059 error_report("Failed to daemonize: %s", strerror(errno
));
1064 if ((daemonize
|| pidfile_specified
) &&
1065 !qemu_write_pidfile(pidfile
, &local_err
)) {
1066 error_report_err(local_err
);
1070 #ifdef CONFIG_LIBCAP_NG
1071 if (drop_privileges() < 0) {
1072 error_report("Failed to drop privileges: %s", strerror(errno
));
1079 main_loop_wait(false);
1080 if (state
== TERMINATE
) {
1081 state
= TERMINATING
;
1082 close_server_socket();
1084 } while (num_active_sockets
> 0);