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>
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 compute_default_paths(void)
79 socket_path
= qemu_get_local_state_pathname("run/qemu-pr-helper.sock");
80 pidfile
= qemu_get_local_state_pathname("run/qemu-pr-helper.pid");
83 static void usage(const char *name
)
86 "Usage: %s [OPTIONS] FILE\n"
87 "Persistent Reservation helper program for QEMU\n"
89 " -h, --help display this help and exit\n"
90 " -V, --version output version information and exit\n"
92 " -d, --daemon run in the background\n"
93 " -f, --pidfile=PATH PID file when running as a daemon\n"
95 " -k, --socket=PATH path to the unix socket\n"
97 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
98 " specify tracing options\n"
100 " -u, --user=USER user to drop privileges to\n"
101 " -g, --group=GROUP group to drop privileges to\n"
104 QEMU_HELP_BOTTOM
"\n"
105 , name
, pidfile
, socket_path
);
108 static void version(const char *name
)
111 "%s " QEMU_FULL_VERSION
"\n"
112 "Written by Paolo Bonzini.\n"
115 "This is free software; see the source for copying conditions. There is NO\n"
116 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
120 static void write_pidfile(void)
125 pidfd
= qemu_open(pidfile
, O_CREAT
|O_WRONLY
, S_IRUSR
|S_IWUSR
);
127 error_report("Cannot open pid file, %s", strerror(errno
));
131 if (lockf(pidfd
, F_TLOCK
, 0)) {
132 error_report("Cannot lock pid file, %s", strerror(errno
));
135 if (ftruncate(pidfd
, 0)) {
136 error_report("Failed to truncate pid file");
140 snprintf(pidstr
, sizeof(pidstr
), "%d\n", getpid());
141 if (write(pidfd
, pidstr
, strlen(pidstr
)) != strlen(pidstr
)) {
142 error_report("Failed to write pid file");
155 typedef struct PRHelperSGIOData
{
160 int sz
; /* input/output */
164 static int do_sgio_worker(void *opaque
)
166 PRHelperSGIOData
*data
= opaque
;
167 struct sg_io_hdr io_hdr
;
170 SCSISense sense_code
;
172 memset(data
->sense
, 0, PR_HELPER_SENSE_SIZE
);
173 memset(&io_hdr
, 0, sizeof(io_hdr
));
174 io_hdr
.interface_id
= 'S';
175 io_hdr
.cmd_len
= PR_HELPER_CDB_SIZE
;
176 io_hdr
.cmdp
= (uint8_t *)data
->cdb
;
177 io_hdr
.sbp
= data
->sense
;
178 io_hdr
.mx_sb_len
= PR_HELPER_SENSE_SIZE
;
180 io_hdr
.dxfer_direction
= data
->dir
;
181 io_hdr
.dxferp
= (char *)data
->buf
;
182 io_hdr
.dxfer_len
= data
->sz
;
183 ret
= ioctl(data
->fd
, SG_IO
, &io_hdr
);
184 status
= sg_io_sense_from_errno(ret
< 0 ? errno
: 0, &io_hdr
,
186 if (status
== GOOD
) {
187 data
->sz
-= io_hdr
.resid
;
192 if (status
== CHECK_CONDITION
&&
193 !(io_hdr
.driver_status
& SG_ERR_DRIVER_SENSE
)) {
194 scsi_build_sense(data
->sense
, sense_code
);
200 static int do_sgio(int fd
, const uint8_t *cdb
, uint8_t *sense
,
201 uint8_t *buf
, int *sz
, int dir
)
203 ThreadPool
*pool
= aio_get_thread_pool(qemu_get_aio_context());
206 PRHelperSGIOData data
= {
215 r
= thread_pool_submit_co(pool
, do_sgio_worker
, &data
);
220 /* Device mapper interface */
223 #define CONTROL_PATH "/dev/mapper/control"
225 typedef struct DMData
{
230 static int control_fd
;
232 static void *dm_ioctl(int ioc
, struct dm_ioctl
*dm
)
235 memcpy(&d
.dm
, dm
, sizeof(d
.dm
));
236 QEMU_BUILD_BUG_ON(sizeof(d
.data
) < sizeof(struct dm_target_spec
));
238 d
.dm
.version
[0] = DM_VERSION_MAJOR
;
241 d
.dm
.data_size
= 1024;
242 d
.dm
.data_start
= offsetof(DMData
, data
);
243 if (ioctl(control_fd
, ioc
, &d
) < 0) {
246 memcpy(dm
, &d
.dm
, sizeof(d
.dm
));
250 static void *dm_dev_ioctl(int fd
, int ioc
, struct dm_ioctl
*dm
)
261 dm
->dev
= st
.st_rdev
;
262 return dm_ioctl(ioc
, dm
);
265 static void dm_init(void)
267 control_fd
= open(CONTROL_PATH
, O_RDWR
);
268 if (control_fd
< 0) {
269 perror("Cannot open " CONTROL_PATH
);
272 struct dm_ioctl dm
= { 0 };
273 if (!dm_ioctl(DM_VERSION
, &dm
)) {
277 if (dm
.version
[0] != DM_VERSION_MAJOR
) {
278 fprintf(stderr
, "Unsupported device mapper interface");
283 /* Variables required by libmultipath and libmpathpersist. */
284 QEMU_BUILD_BUG_ON(PR_HELPER_DATA_SIZE
> MPATH_MAX_PARAM_LEN
);
285 static struct config
*multipath_conf
;
286 unsigned mpath_mx_alloc_len
= PR_HELPER_DATA_SIZE
;
290 extern struct config
*get_multipath_config(void);
291 struct config
*get_multipath_config(void)
293 return multipath_conf
;
296 extern void put_multipath_config(struct config
*conf
);
297 void put_multipath_config(struct config
*conf
)
301 static void multipath_pr_init(void)
304 multipath_conf
= mpath_lib_init();
307 static int is_mpath(int fd
)
309 struct dm_ioctl dm
= { .flags
= DM_NOFLUSH_FLAG
};
310 struct dm_target_spec
*tgt
;
312 tgt
= dm_dev_ioctl(fd
, DM_TABLE_STATUS
, &dm
);
314 if (errno
== ENXIO
) {
320 return !strncmp(tgt
->target_type
, "multipath", DM_MAX_TYPE_NAME
);
323 static SCSISense
mpath_generic_sense(int r
)
326 case MPATH_PR_SENSE_NOT_READY
:
327 return SENSE_CODE(NOT_READY
);
328 case MPATH_PR_SENSE_MEDIUM_ERROR
:
329 return SENSE_CODE(READ_ERROR
);
330 case MPATH_PR_SENSE_HARDWARE_ERROR
:
331 return SENSE_CODE(TARGET_FAILURE
);
332 case MPATH_PR_SENSE_ABORTED_COMMAND
:
333 return SENSE_CODE(IO_ERROR
);
339 static int mpath_reconstruct_sense(int fd
, int r
, uint8_t *sense
)
342 case MPATH_PR_SUCCESS
:
344 case MPATH_PR_SENSE_NOT_READY
:
345 case MPATH_PR_SENSE_MEDIUM_ERROR
:
346 case MPATH_PR_SENSE_HARDWARE_ERROR
:
347 case MPATH_PR_SENSE_ABORTED_COMMAND
:
349 /* libmpathpersist ate the exact sense. Try to find it by
350 * issuing TEST UNIT READY.
352 uint8_t cdb
[6] = { TEST_UNIT_READY
};
354 int r
= do_sgio(fd
, cdb
, sense
, NULL
, &sz
, SG_DXFER_NONE
);
359 scsi_build_sense(sense
, mpath_generic_sense(r
));
360 return CHECK_CONDITION
;
363 case MPATH_PR_SENSE_UNIT_ATTENTION
:
364 /* Congratulations libmpathpersist, you ruined the Unit Attention...
365 * Return a heavyweight one.
367 scsi_build_sense(sense
, SENSE_CODE(SCSI_BUS_RESET
));
368 return CHECK_CONDITION
;
369 case MPATH_PR_SENSE_INVALID_OP
:
370 /* Only one valid sense. */
371 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
372 return CHECK_CONDITION
;
373 case MPATH_PR_ILLEGAL_REQ
:
375 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM
));
376 return CHECK_CONDITION
;
377 case MPATH_PR_NO_SENSE
:
378 scsi_build_sense(sense
, SENSE_CODE(NO_SENSE
));
379 return CHECK_CONDITION
;
381 case MPATH_PR_RESERV_CONFLICT
:
382 return RESERVATION_CONFLICT
;
386 scsi_build_sense(sense
, SENSE_CODE(LUN_COMM_FAILURE
));
387 return CHECK_CONDITION
;
391 static int multipath_pr_in(int fd
, const uint8_t *cdb
, uint8_t *sense
,
392 uint8_t *data
, int sz
)
394 int rq_servact
= cdb
[1];
395 struct prin_resp resp
;
399 switch (rq_servact
) {
400 case MPATH_PRIN_RKEY_SA
:
401 case MPATH_PRIN_RRES_SA
:
402 case MPATH_PRIN_RCAP_SA
:
404 case MPATH_PRIN_RFSTAT_SA
:
405 /* Nobody implements it anyway, so bail out. */
407 /* Cannot parse any other output. */
408 scsi_build_sense(sense
, SENSE_CODE(INVALID_FIELD
));
409 return CHECK_CONDITION
;
412 r
= mpath_persistent_reserve_in(fd
, rq_servact
, &resp
, noisy
, verbose
);
413 if (r
== MPATH_PR_SUCCESS
) {
414 switch (rq_servact
) {
415 case MPATH_PRIN_RKEY_SA
:
416 case MPATH_PRIN_RRES_SA
: {
417 struct prin_readdescr
*out
= &resp
.prin_descriptor
.prin_readkeys
;
419 written
= MIN(out
->additional_length
+ 8, sz
);
420 stl_be_p(&data
[0], out
->prgeneration
);
421 stl_be_p(&data
[4], out
->additional_length
);
422 memcpy(&data
[8], out
->key_list
, written
- 8);
425 case MPATH_PRIN_RCAP_SA
: {
426 struct prin_capdescr
*out
= &resp
.prin_descriptor
.prin_readcap
;
429 stw_be_p(&data
[0], out
->length
);
430 data
[2] = out
->flags
[0];
431 data
[3] = out
->flags
[1];
432 stw_be_p(&data
[4], out
->pr_type_mask
);
436 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
437 return CHECK_CONDITION
;
439 assert(written
<= sz
);
440 memset(data
+ written
, 0, sz
- written
);
443 return mpath_reconstruct_sense(fd
, r
, sense
);
446 static int multipath_pr_out(int fd
, const uint8_t *cdb
, uint8_t *sense
,
447 const uint8_t *param
, int sz
)
449 int rq_servact
= cdb
[1];
450 int rq_scope
= cdb
[2] >> 4;
451 int rq_type
= cdb
[2] & 0xf;
452 struct prout_param_descriptor paramp
;
453 char transportids
[PR_HELPER_DATA_SIZE
];
456 if (sz
< PR_OUT_FIXED_PARAM_SIZE
) {
457 /* Illegal request, Parameter list length error. This isn't fatal;
458 * we have read the data, send an error without closing the socket.
460 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM_LEN
));
461 return CHECK_CONDITION
;
464 switch (rq_servact
) {
465 case MPATH_PROUT_REG_SA
:
466 case MPATH_PROUT_RES_SA
:
467 case MPATH_PROUT_REL_SA
:
468 case MPATH_PROUT_CLEAR_SA
:
469 case MPATH_PROUT_PREE_SA
:
470 case MPATH_PROUT_PREE_AB_SA
:
471 case MPATH_PROUT_REG_IGN_SA
:
473 case MPATH_PROUT_REG_MOV_SA
:
474 /* Not supported by struct prout_param_descriptor. */
476 /* Cannot parse any other input. */
477 scsi_build_sense(sense
, SENSE_CODE(INVALID_FIELD
));
478 return CHECK_CONDITION
;
481 /* Convert input data, especially transport IDs, to the structs
482 * used by libmpathpersist (which, of course, will immediately
485 memset(¶mp
, 0, sizeof(paramp
));
486 memcpy(¶mp
.key
, ¶m
[0], 8);
487 memcpy(¶mp
.sa_key
, ¶m
[8], 8);
488 paramp
.sa_flags
= param
[20];
489 if (sz
> PR_OUT_FIXED_PARAM_SIZE
) {
490 size_t transportid_len
;
492 if (sz
< PR_OUT_FIXED_PARAM_SIZE
+ 4) {
493 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM_LEN
));
494 return CHECK_CONDITION
;
496 transportid_len
= ldl_be_p(¶m
[24]) + PR_OUT_FIXED_PARAM_SIZE
+ 4;
497 if (transportid_len
> sz
) {
498 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM
));
499 return CHECK_CONDITION
;
501 for (i
= PR_OUT_FIXED_PARAM_SIZE
+ 4, j
= 0; i
< transportid_len
; ) {
502 struct transportid
*id
= (struct transportid
*) &transportids
[j
];
505 id
->format_code
= param
[i
] & 0xc0;
506 id
->protocol_id
= param
[i
] & 0x0f;
507 switch (param
[i
] & 0xcf) {
510 if (i
+ 24 > transportid_len
) {
513 memcpy(id
->n_port_name
, ¶m
[i
+ 8], 8);
514 j
+= offsetof(struct transportid
, n_port_name
[8]);
519 /* iSCSI transport. */
520 len
= lduw_be_p(¶m
[i
+ 2]);
521 if (len
> 252 || (len
& 3) || i
+ len
+ 4 > transportid_len
) {
522 /* For format code 00, the standard says the maximum is 223
523 * plus the NUL terminator. For format code 01 there is no
524 * maximum length, but libmpathpersist ignores the first
525 * byte of id->iscsi_name so our maximum is 252.
529 if (memchr(¶m
[i
+ 4], 0, len
) == NULL
) {
532 memcpy(id
->iscsi_name
, ¶m
[i
+ 2], len
+ 2);
533 j
+= offsetof(struct transportid
, iscsi_name
[len
+ 2]);
538 if (i
+ 24 > transportid_len
) {
541 memcpy(id
->sas_address
, ¶m
[i
+ 4], 8);
542 j
+= offsetof(struct transportid
, sas_address
[8]);
547 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM
));
548 return CHECK_CONDITION
;
551 paramp
.trnptid_list
[paramp
.num_transportid
++] = id
;
555 r
= mpath_persistent_reserve_out(fd
, rq_servact
, rq_scope
, rq_type
,
556 ¶mp
, noisy
, verbose
);
557 return mpath_reconstruct_sense(fd
, r
, sense
);
561 static int do_pr_in(int fd
, const uint8_t *cdb
, uint8_t *sense
,
562 uint8_t *data
, int *resp_sz
)
566 /* multipath_pr_in fills the whole input buffer. */
567 int r
= multipath_pr_in(fd
, cdb
, sense
, data
, *resp_sz
);
575 return do_sgio(fd
, cdb
, sense
, data
, resp_sz
,
579 static int do_pr_out(int fd
, const uint8_t *cdb
, uint8_t *sense
,
580 const uint8_t *param
, int sz
)
584 if ((fcntl(fd
, F_GETFL
) & O_ACCMODE
) == O_RDONLY
) {
585 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
586 return CHECK_CONDITION
;
591 return multipath_pr_out(fd
, cdb
, sense
, param
, sz
);
596 return do_sgio(fd
, cdb
, sense
, (uint8_t *)param
, &resp_sz
,
602 typedef struct PRHelperClient
{
603 QIOChannelSocket
*ioc
;
606 uint8_t data
[PR_HELPER_DATA_SIZE
];
609 typedef struct PRHelperRequest
{
612 uint8_t cdb
[PR_HELPER_CDB_SIZE
];
615 static int coroutine_fn
prh_read(PRHelperClient
*client
, void *buf
, int sz
,
629 n_read
= qio_channel_readv_full(QIO_CHANNEL(client
->ioc
), &iov
, 1,
632 if (n_read
== QIO_CHANNEL_ERR_BLOCK
) {
633 qio_channel_yield(QIO_CHANNEL(client
->ioc
), G_IO_IN
);
637 ret
= n_read
? n_read
: -1;
641 /* Stash one file descriptor per request. */
643 bool too_many
= false;
644 for (i
= 0; i
< nfds
; i
++) {
645 if (client
->fd
== -1) {
666 if (client
->fd
!= -1) {
673 static int coroutine_fn
prh_read_request(PRHelperClient
*client
,
674 PRHelperRequest
*req
,
675 PRHelperResponse
*resp
, Error
**errp
)
679 if (prh_read(client
, req
->cdb
, sizeof(req
->cdb
), NULL
) < 0) {
683 if (client
->fd
== -1) {
684 error_setg(errp
, "No file descriptor in request.");
688 if (req
->cdb
[0] != PERSISTENT_RESERVE_OUT
&&
689 req
->cdb
[0] != PERSISTENT_RESERVE_IN
) {
690 error_setg(errp
, "Invalid CDB, closing socket.");
694 sz
= scsi_cdb_xfer(req
->cdb
);
695 if (sz
> sizeof(client
->data
)) {
699 if (req
->cdb
[0] == PERSISTENT_RESERVE_OUT
) {
700 if (qio_channel_read_all(QIO_CHANNEL(client
->ioc
),
701 (char *)client
->data
, sz
,
707 req
->fd
= client
->fd
;
718 static int coroutine_fn
prh_write_response(PRHelperClient
*client
,
719 PRHelperRequest
*req
,
720 PRHelperResponse
*resp
, Error
**errp
)
725 if (req
->cdb
[0] == PERSISTENT_RESERVE_IN
&& resp
->result
== GOOD
) {
726 assert(resp
->sz
<= req
->sz
&& resp
->sz
<= sizeof(client
->data
));
728 assert(resp
->sz
== 0);
733 resp
->result
= cpu_to_be32(resp
->result
);
734 resp
->sz
= cpu_to_be32(resp
->sz
);
735 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
736 (char *) resp
, sizeof(*resp
), errp
);
741 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
742 (char *) client
->data
,
744 return r
< 0 ? r
: 0;
747 static void coroutine_fn
prh_co_entry(void *opaque
)
749 PRHelperClient
*client
= opaque
;
750 Error
*local_err
= NULL
;
754 qio_channel_set_blocking(QIO_CHANNEL(client
->ioc
),
756 qio_channel_attach_aio_context(QIO_CHANNEL(client
->ioc
),
757 qemu_get_aio_context());
759 /* A very simple negotiation for future extensibility. No features
760 * are defined so write 0.
762 flags
= cpu_to_be32(0);
763 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
764 (char *) &flags
, sizeof(flags
), NULL
);
769 r
= qio_channel_read_all(QIO_CHANNEL(client
->ioc
),
770 (char *) &flags
, sizeof(flags
), NULL
);
771 if (be32_to_cpu(flags
) != 0 || r
< 0) {
775 while (atomic_read(&state
) == RUNNING
) {
777 PRHelperResponse resp
;
780 sz
= prh_read_request(client
, &req
, &resp
, &local_err
);
785 num_active_sockets
++;
786 if (req
.cdb
[0] == PERSISTENT_RESERVE_OUT
) {
787 r
= do_pr_out(req
.fd
, req
.cdb
, resp
.sense
,
791 resp
.sz
= sizeof(client
->data
);
792 r
= do_pr_in(req
.fd
, req
.cdb
, resp
.sense
,
793 client
->data
, &resp
.sz
);
794 resp
.sz
= MIN(resp
.sz
, sz
);
796 num_active_sockets
--;
803 if (prh_write_response(client
, &req
, &resp
, &local_err
) < 0) {
810 error_free(local_err
);
812 error_report_err(local_err
);
817 qio_channel_detach_aio_context(QIO_CHANNEL(client
->ioc
));
818 object_unref(OBJECT(client
->ioc
));
822 static gboolean
accept_client(QIOChannel
*ioc
, GIOCondition cond
, gpointer opaque
)
824 QIOChannelSocket
*cioc
;
827 cioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc
),
833 prh
= g_new(PRHelperClient
, 1);
836 prh
->co
= qemu_coroutine_create(prh_co_entry
, prh
);
837 qemu_coroutine_enter(prh
->co
);
842 static void termsig_handler(int signum
)
844 atomic_cmpxchg(&state
, RUNNING
, TERMINATE
);
848 static void close_server_socket(void)
852 g_source_remove(server_watch
);
854 object_unref(OBJECT(server_ioc
));
855 num_active_sockets
--;
859 static int drop_privileges(void)
861 /* clear all capabilities */
862 capng_clear(CAPNG_SELECT_BOTH
);
864 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
865 CAP_SYS_RAWIO
) < 0) {
870 /* For /dev/mapper/control ioctls */
871 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
872 CAP_SYS_ADMIN
) < 0) {
877 /* Change user/group id, retaining the capabilities. Because file descriptors
878 * are passed via SCM_RIGHTS, we don't need supplementary groups (and in
879 * fact the helper can run as "nobody").
881 if (capng_change_id(uid
!= -1 ? uid
: getuid(),
882 gid
!= -1 ? gid
: getgid(),
883 CAPNG_DROP_SUPP_GRP
| CAPNG_CLEAR_BOUNDING
)) {
891 int main(int argc
, char **argv
)
893 const char *sopt
= "hVk:f:dT:u:g:vq";
894 struct option lopt
[] = {
895 { "help", no_argument
, NULL
, 'h' },
896 { "version", no_argument
, NULL
, 'V' },
897 { "socket", required_argument
, NULL
, 'k' },
898 { "pidfile", required_argument
, NULL
, 'f' },
899 { "daemon", no_argument
, NULL
, 'd' },
900 { "trace", required_argument
, NULL
, 'T' },
901 { "user", required_argument
, NULL
, 'u' },
902 { "group", required_argument
, NULL
, 'g' },
903 { "verbose", no_argument
, NULL
, 'v' },
904 { "quiet", no_argument
, NULL
, 'q' },
911 Error
*local_err
= NULL
;
912 char *trace_file
= NULL
;
913 bool daemonize
= false;
914 bool pidfile_specified
= false;
915 bool socket_path_specified
= false;
916 unsigned socket_activation
;
918 struct sigaction sa_sigterm
;
919 memset(&sa_sigterm
, 0, sizeof(sa_sigterm
));
920 sa_sigterm
.sa_handler
= termsig_handler
;
921 sigaction(SIGTERM
, &sa_sigterm
, NULL
);
922 sigaction(SIGINT
, &sa_sigterm
, NULL
);
923 sigaction(SIGHUP
, &sa_sigterm
, NULL
);
925 signal(SIGPIPE
, SIG_IGN
);
927 module_call_init(MODULE_INIT_TRACE
);
928 module_call_init(MODULE_INIT_QOM
);
929 qemu_add_opts(&qemu_trace_opts
);
930 qemu_init_exec_dir(argv
[0]);
932 compute_default_paths();
934 while ((ch
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_ind
)) != -1) {
938 socket_path
= g_strdup(optarg
);
939 socket_path_specified
= true;
940 if (socket_path
[0] != '/') {
941 error_report("socket path must be absolute");
947 pidfile
= g_strdup(optarg
);
948 pidfile_specified
= true;
953 struct passwd
*userinfo
= getpwnam(optarg
);
955 uid
= userinfo
->pw_uid
;
956 } else if (qemu_strtoul(optarg
, NULL
, 10, &res
) == 0 &&
960 error_report("invalid user '%s'", optarg
);
967 struct group
*groupinfo
= getgrnam(optarg
);
969 gid
= groupinfo
->gr_gid
;
970 } else if (qemu_strtoul(optarg
, NULL
, 10, &res
) == 0 &&
974 error_report("invalid group '%s'", optarg
);
982 error_report("-%c not supported by this %s", ch
, argv
[0]);
996 trace_file
= trace_opt_parse(optarg
);
1007 error_report("Try `%s --help' for more information.", argv
[0]);
1013 noisy
= !quiet
&& (loglevel
>= 3);
1014 verbose
= quiet
? 0 : MIN(loglevel
, 3);
1016 if (!trace_init_backends()) {
1019 trace_init_file(trace_file
);
1020 qemu_set_log(LOG_TRACE
);
1024 multipath_pr_init();
1027 socket_activation
= check_socket_activation();
1028 if (socket_activation
== 0) {
1029 SocketAddress saddr
;
1030 saddr
= (SocketAddress
){
1031 .type
= SOCKET_ADDRESS_TYPE_UNIX
,
1032 .u
.q_unix
.path
= socket_path
,
1034 server_ioc
= qio_channel_socket_new();
1035 if (qio_channel_socket_listen_sync(server_ioc
, &saddr
, &local_err
) < 0) {
1036 object_unref(OBJECT(server_ioc
));
1037 error_report_err(local_err
);
1041 /* Using socket activation - check user didn't use -p etc. */
1042 if (socket_path_specified
) {
1043 error_report("Unix socket can't be set when using socket activation");
1047 /* Can only listen on a single socket. */
1048 if (socket_activation
> 1) {
1049 error_report("%s does not support socket activation with LISTEN_FDS > 1",
1053 server_ioc
= qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD
,
1055 if (server_ioc
== NULL
) {
1056 error_report("Failed to use socket activation: %s",
1057 error_get_pretty(local_err
));
1062 if (qemu_init_main_loop(&local_err
)) {
1063 error_report_err(local_err
);
1067 server_watch
= qio_channel_add_watch(QIO_CHANNEL(server_ioc
),
1073 if (daemon(0, 0) < 0) {
1074 error_report("Failed to daemonize: %s", strerror(errno
));
1079 if (daemonize
|| pidfile_specified
)
1082 #ifdef CONFIG_LIBCAP
1083 if (drop_privileges() < 0) {
1084 error_report("Failed to drop privileges: %s", strerror(errno
));
1091 main_loop_wait(false);
1092 if (state
== TERMINATE
) {
1093 state
= TERMINATING
;
1094 close_server_socket();
1096 } while (num_active_sockets
> 0);