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/help-texts.h"
40 #include "qapi/error.h"
41 #include "qemu/cutils.h"
42 #include "qemu/main-loop.h"
43 #include "qemu/module.h"
44 #include "qemu/error-report.h"
45 #include "qemu/config-file.h"
46 #include "qemu/bswap.h"
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 g_autofree
char *state
= qemu_get_local_state_dir();
82 socket_path
= g_build_filename(state
, "run", "qemu-pr-helper.sock", NULL
);
83 pidfile
= g_build_filename(state
, "run", "qemu-pr-helper.pid", NULL
);
86 static void usage(const char *name
)
89 "Usage: %s [OPTIONS] FILE\n"
90 "Persistent Reservation helper program for QEMU\n"
92 " -h, --help display this help and exit\n"
93 " -V, --version output version information and exit\n"
95 " -d, --daemon run in the background\n"
96 " -f, --pidfile=PATH PID file when running as a daemon\n"
98 " -k, --socket=PATH path to the unix socket\n"
100 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
101 " specify tracing options\n"
102 #ifdef CONFIG_LIBCAP_NG
103 " -u, --user=USER user to drop privileges to\n"
104 " -g, --group=GROUP group to drop privileges to\n"
107 QEMU_HELP_BOTTOM
"\n"
108 , name
, pidfile
, socket_path
);
111 static void version(const char *name
)
114 "%s " QEMU_FULL_VERSION
"\n"
115 "Written by Paolo Bonzini.\n"
118 "This is free software; see the source for copying conditions. There is NO\n"
119 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
125 typedef struct PRHelperSGIOData
{
130 int sz
; /* input/output */
134 static int do_sgio_worker(void *opaque
)
136 PRHelperSGIOData
*data
= opaque
;
137 struct sg_io_hdr io_hdr
;
140 SCSISense sense_code
;
142 memset(data
->sense
, 0, PR_HELPER_SENSE_SIZE
);
143 memset(&io_hdr
, 0, sizeof(io_hdr
));
144 io_hdr
.interface_id
= 'S';
145 io_hdr
.cmd_len
= PR_HELPER_CDB_SIZE
;
146 io_hdr
.cmdp
= (uint8_t *)data
->cdb
;
147 io_hdr
.sbp
= data
->sense
;
148 io_hdr
.mx_sb_len
= PR_HELPER_SENSE_SIZE
;
150 io_hdr
.dxfer_direction
= data
->dir
;
151 io_hdr
.dxferp
= (char *)data
->buf
;
152 io_hdr
.dxfer_len
= data
->sz
;
153 ret
= ioctl(data
->fd
, SG_IO
, &io_hdr
);
156 status
= scsi_sense_from_errno(errno
, &sense_code
);
157 if (status
== CHECK_CONDITION
) {
158 scsi_build_sense(data
->sense
, sense_code
);
160 } else if (io_hdr
.host_status
!= SCSI_HOST_OK
) {
161 status
= scsi_sense_from_host_status(io_hdr
.host_status
, &sense_code
);
162 if (status
== CHECK_CONDITION
) {
163 scsi_build_sense(data
->sense
, sense_code
);
165 } else if (io_hdr
.driver_status
& SG_ERR_DRIVER_TIMEOUT
) {
168 status
= io_hdr
.status
;
171 if (status
== GOOD
) {
172 data
->sz
-= io_hdr
.resid
;
180 static int do_sgio(int fd
, const uint8_t *cdb
, uint8_t *sense
,
181 uint8_t *buf
, int *sz
, int dir
)
183 ThreadPool
*pool
= aio_get_thread_pool(qemu_get_aio_context());
186 PRHelperSGIOData data
= {
195 r
= thread_pool_submit_co(pool
, do_sgio_worker
, &data
);
200 /* Device mapper interface */
203 #define CONTROL_PATH "/dev/mapper/control"
205 typedef struct DMData
{
210 static int control_fd
;
212 static void *dm_ioctl(int ioc
, struct dm_ioctl
*dm
)
215 memcpy(&d
.dm
, dm
, sizeof(d
.dm
));
216 QEMU_BUILD_BUG_ON(sizeof(d
.data
) < sizeof(struct dm_target_spec
));
218 d
.dm
.version
[0] = DM_VERSION_MAJOR
;
221 d
.dm
.data_size
= 1024;
222 d
.dm
.data_start
= offsetof(DMData
, data
);
223 if (ioctl(control_fd
, ioc
, &d
) < 0) {
226 memcpy(dm
, &d
.dm
, sizeof(d
.dm
));
230 static void *dm_dev_ioctl(int fd
, int ioc
, struct dm_ioctl
*dm
)
241 dm
->dev
= st
.st_rdev
;
242 return dm_ioctl(ioc
, dm
);
245 static void dm_init(void)
247 control_fd
= open(CONTROL_PATH
, O_RDWR
);
248 if (control_fd
< 0) {
249 perror("Cannot open " CONTROL_PATH
);
252 struct dm_ioctl dm
= { };
253 if (!dm_ioctl(DM_VERSION
, &dm
)) {
257 if (dm
.version
[0] != DM_VERSION_MAJOR
) {
258 fprintf(stderr
, "Unsupported device mapper interface");
263 /* Variables required by libmultipath and libmpathpersist. */
264 QEMU_BUILD_BUG_ON(PR_HELPER_DATA_SIZE
> MPATH_MAX_PARAM_LEN
);
265 static struct config
*multipath_conf
;
266 unsigned mpath_mx_alloc_len
= PR_HELPER_DATA_SIZE
;
270 extern struct config
*get_multipath_config(void);
271 struct config
*get_multipath_config(void)
273 return multipath_conf
;
276 extern void put_multipath_config(struct config
*conf
);
277 void put_multipath_config(struct config
*conf
)
281 static void multipath_pr_init(void)
284 #ifdef CONFIG_MPATH_NEW_API
285 multipath_conf
= mpath_lib_init();
287 mpath_lib_init(udev
);
291 static int is_mpath(int fd
)
293 struct dm_ioctl dm
= { .flags
= DM_NOFLUSH_FLAG
};
294 struct dm_target_spec
*tgt
;
296 tgt
= dm_dev_ioctl(fd
, DM_TABLE_STATUS
, &dm
);
298 if (errno
== ENXIO
) {
304 return !strncmp(tgt
->target_type
, "multipath", DM_MAX_TYPE_NAME
);
307 static SCSISense
mpath_generic_sense(int r
)
310 case MPATH_PR_SENSE_NOT_READY
:
311 return SENSE_CODE(NOT_READY
);
312 case MPATH_PR_SENSE_MEDIUM_ERROR
:
313 return SENSE_CODE(READ_ERROR
);
314 case MPATH_PR_SENSE_HARDWARE_ERROR
:
315 return SENSE_CODE(TARGET_FAILURE
);
316 case MPATH_PR_SENSE_ABORTED_COMMAND
:
317 return SENSE_CODE(IO_ERROR
);
323 static int mpath_reconstruct_sense(int fd
, int r
, uint8_t *sense
)
326 case MPATH_PR_SUCCESS
:
328 case MPATH_PR_SENSE_NOT_READY
:
329 case MPATH_PR_SENSE_MEDIUM_ERROR
:
330 case MPATH_PR_SENSE_HARDWARE_ERROR
:
331 case MPATH_PR_SENSE_ABORTED_COMMAND
:
333 /* libmpathpersist ate the exact sense. Try to find it by
334 * issuing TEST UNIT READY.
336 uint8_t cdb
[6] = { TEST_UNIT_READY
};
338 int ret
= do_sgio(fd
, cdb
, sense
, NULL
, &sz
, SG_DXFER_NONE
);
343 scsi_build_sense(sense
, mpath_generic_sense(r
));
344 return CHECK_CONDITION
;
347 case MPATH_PR_SENSE_UNIT_ATTENTION
:
348 /* Congratulations libmpathpersist, you ruined the Unit Attention...
349 * Return a heavyweight one.
351 scsi_build_sense(sense
, SENSE_CODE(SCSI_BUS_RESET
));
352 return CHECK_CONDITION
;
353 case MPATH_PR_SENSE_INVALID_OP
:
354 /* Only one valid sense. */
355 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
356 return CHECK_CONDITION
;
357 case MPATH_PR_ILLEGAL_REQ
:
359 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM
));
360 return CHECK_CONDITION
;
361 case MPATH_PR_NO_SENSE
:
362 scsi_build_sense(sense
, SENSE_CODE(NO_SENSE
));
363 return CHECK_CONDITION
;
365 case MPATH_PR_RESERV_CONFLICT
:
366 return RESERVATION_CONFLICT
;
370 scsi_build_sense(sense
, SENSE_CODE(LUN_COMM_FAILURE
));
371 return CHECK_CONDITION
;
375 static int multipath_pr_in(int fd
, const uint8_t *cdb
, uint8_t *sense
,
376 uint8_t *data
, int sz
)
378 int rq_servact
= cdb
[1];
379 struct prin_resp resp
;
383 switch (rq_servact
) {
384 case MPATH_PRIN_RKEY_SA
:
385 case MPATH_PRIN_RRES_SA
:
386 case MPATH_PRIN_RCAP_SA
:
388 case MPATH_PRIN_RFSTAT_SA
:
389 /* Nobody implements it anyway, so bail out. */
391 /* Cannot parse any other output. */
392 scsi_build_sense(sense
, SENSE_CODE(INVALID_FIELD
));
393 return CHECK_CONDITION
;
396 r
= mpath_persistent_reserve_in(fd
, rq_servact
, &resp
, noisy
, verbose
);
397 if (r
== MPATH_PR_SUCCESS
) {
398 switch (rq_servact
) {
399 case MPATH_PRIN_RKEY_SA
:
400 case MPATH_PRIN_RRES_SA
: {
401 struct prin_readdescr
*out
= &resp
.prin_descriptor
.prin_readkeys
;
403 written
= MIN(out
->additional_length
+ 8, sz
);
404 stl_be_p(&data
[0], out
->prgeneration
);
405 stl_be_p(&data
[4], out
->additional_length
);
406 memcpy(&data
[8], out
->key_list
, written
- 8);
409 case MPATH_PRIN_RCAP_SA
: {
410 struct prin_capdescr
*out
= &resp
.prin_descriptor
.prin_readcap
;
413 stw_be_p(&data
[0], out
->length
);
414 data
[2] = out
->flags
[0];
415 data
[3] = out
->flags
[1];
416 stw_be_p(&data
[4], out
->pr_type_mask
);
420 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
421 return CHECK_CONDITION
;
423 assert(written
<= sz
);
424 memset(data
+ written
, 0, sz
- written
);
427 return mpath_reconstruct_sense(fd
, r
, sense
);
430 static int multipath_pr_out(int fd
, const uint8_t *cdb
, uint8_t *sense
,
431 const uint8_t *param
, int sz
)
433 int rq_servact
= cdb
[1];
434 int rq_scope
= cdb
[2] >> 4;
435 int rq_type
= cdb
[2] & 0xf;
436 g_autofree
struct prout_param_descriptor
*paramp
= NULL
;
437 char transportids
[PR_HELPER_DATA_SIZE
];
440 paramp
= g_malloc0(sizeof(struct prout_param_descriptor
)
441 + sizeof(struct transportid
*) * MPATH_MX_TIDS
);
443 if (sz
< PR_OUT_FIXED_PARAM_SIZE
) {
444 /* Illegal request, Parameter list length error. This isn't fatal;
445 * we have read the data, send an error without closing the socket.
447 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM_LEN
));
448 return CHECK_CONDITION
;
451 switch (rq_servact
) {
452 case MPATH_PROUT_REG_SA
:
453 case MPATH_PROUT_RES_SA
:
454 case MPATH_PROUT_REL_SA
:
455 case MPATH_PROUT_CLEAR_SA
:
456 case MPATH_PROUT_PREE_SA
:
457 case MPATH_PROUT_PREE_AB_SA
:
458 case MPATH_PROUT_REG_IGN_SA
:
460 case MPATH_PROUT_REG_MOV_SA
:
461 /* Not supported by struct prout_param_descriptor. */
463 /* Cannot parse any other input. */
464 scsi_build_sense(sense
, SENSE_CODE(INVALID_FIELD
));
465 return CHECK_CONDITION
;
468 /* Convert input data, especially transport IDs, to the structs
469 * used by libmpathpersist (which, of course, will immediately
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 assert(paramp
->num_transportid
< MPATH_MX_TIDS
);
538 paramp
->trnptid_list
[paramp
->num_transportid
++] = id
;
542 r
= mpath_persistent_reserve_out(fd
, rq_servact
, rq_scope
, rq_type
,
543 paramp
, noisy
, verbose
);
544 return mpath_reconstruct_sense(fd
, r
, sense
);
548 static int do_pr_in(int fd
, const uint8_t *cdb
, uint8_t *sense
,
549 uint8_t *data
, int *resp_sz
)
553 /* multipath_pr_in fills the whole input buffer. */
554 int r
= multipath_pr_in(fd
, cdb
, sense
, data
, *resp_sz
);
562 return do_sgio(fd
, cdb
, sense
, data
, resp_sz
,
566 static int do_pr_out(int fd
, const uint8_t *cdb
, uint8_t *sense
,
567 const uint8_t *param
, int sz
)
571 if ((fcntl(fd
, F_GETFL
) & O_ACCMODE
) == O_RDONLY
) {
572 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
573 return CHECK_CONDITION
;
578 return multipath_pr_out(fd
, cdb
, sense
, param
, sz
);
583 return do_sgio(fd
, cdb
, sense
, (uint8_t *)param
, &resp_sz
,
589 typedef struct PRHelperClient
{
590 QIOChannelSocket
*ioc
;
593 uint8_t data
[PR_HELPER_DATA_SIZE
];
596 typedef struct PRHelperRequest
{
599 uint8_t cdb
[PR_HELPER_CDB_SIZE
];
602 static int coroutine_fn
prh_read(PRHelperClient
*client
, void *buf
, int sz
,
616 n_read
= qio_channel_readv_full(QIO_CHANNEL(client
->ioc
), &iov
, 1,
619 if (n_read
== QIO_CHANNEL_ERR_BLOCK
) {
620 qio_channel_yield(QIO_CHANNEL(client
->ioc
), G_IO_IN
);
624 ret
= n_read
? n_read
: -1;
628 /* Stash one file descriptor per request. */
630 bool too_many
= false;
631 for (i
= 0; i
< nfds
; i
++) {
632 if (client
->fd
== -1) {
653 if (client
->fd
!= -1) {
660 static int coroutine_fn
prh_read_request(PRHelperClient
*client
,
661 PRHelperRequest
*req
,
662 PRHelperResponse
*resp
, Error
**errp
)
666 if (prh_read(client
, req
->cdb
, sizeof(req
->cdb
), NULL
) < 0) {
670 if (client
->fd
== -1) {
671 error_setg(errp
, "No file descriptor in request.");
675 if (req
->cdb
[0] != PERSISTENT_RESERVE_OUT
&&
676 req
->cdb
[0] != PERSISTENT_RESERVE_IN
) {
677 error_setg(errp
, "Invalid CDB, closing socket.");
681 sz
= scsi_cdb_xfer(req
->cdb
);
682 if (sz
> sizeof(client
->data
)) {
686 if (req
->cdb
[0] == PERSISTENT_RESERVE_OUT
) {
687 if (qio_channel_read_all(QIO_CHANNEL(client
->ioc
),
688 (char *)client
->data
, sz
,
694 req
->fd
= client
->fd
;
705 static int coroutine_fn
prh_write_response(PRHelperClient
*client
,
706 PRHelperRequest
*req
,
707 PRHelperResponse
*resp
, Error
**errp
)
712 if (req
->cdb
[0] == PERSISTENT_RESERVE_IN
&& resp
->result
== GOOD
) {
713 assert(resp
->sz
<= req
->sz
&& resp
->sz
<= sizeof(client
->data
));
715 assert(resp
->sz
== 0);
720 resp
->result
= cpu_to_be32(resp
->result
);
721 resp
->sz
= cpu_to_be32(resp
->sz
);
722 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
723 (char *) resp
, sizeof(*resp
), errp
);
728 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
729 (char *) client
->data
,
731 return r
< 0 ? r
: 0;
734 static void coroutine_fn
prh_co_entry(void *opaque
)
736 PRHelperClient
*client
= opaque
;
737 Error
*local_err
= NULL
;
741 qio_channel_set_blocking(QIO_CHANNEL(client
->ioc
),
743 qio_channel_attach_aio_context(QIO_CHANNEL(client
->ioc
),
744 qemu_get_aio_context());
746 /* A very simple negotiation for future extensibility. No features
747 * are defined so write 0.
749 flags
= cpu_to_be32(0);
750 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
751 (char *) &flags
, sizeof(flags
), NULL
);
756 r
= qio_channel_read_all(QIO_CHANNEL(client
->ioc
),
757 (char *) &flags
, sizeof(flags
), NULL
);
758 if (be32_to_cpu(flags
) != 0 || r
< 0) {
762 while (qatomic_read(&state
) == RUNNING
) {
764 PRHelperResponse resp
;
767 sz
= prh_read_request(client
, &req
, &resp
, &local_err
);
772 num_active_sockets
++;
773 if (req
.cdb
[0] == PERSISTENT_RESERVE_OUT
) {
774 r
= do_pr_out(req
.fd
, req
.cdb
, resp
.sense
,
778 resp
.sz
= sizeof(client
->data
);
779 r
= do_pr_in(req
.fd
, req
.cdb
, resp
.sense
,
780 client
->data
, &resp
.sz
);
781 resp
.sz
= MIN(resp
.sz
, sz
);
783 num_active_sockets
--;
790 if (prh_write_response(client
, &req
, &resp
, &local_err
) < 0) {
797 error_free(local_err
);
799 error_report_err(local_err
);
804 qio_channel_detach_aio_context(QIO_CHANNEL(client
->ioc
));
805 object_unref(OBJECT(client
->ioc
));
809 static gboolean
accept_client(QIOChannel
*ioc
, GIOCondition cond
, gpointer opaque
)
811 QIOChannelSocket
*cioc
;
814 cioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc
),
820 prh
= g_new(PRHelperClient
, 1);
823 prh
->co
= qemu_coroutine_create(prh_co_entry
, prh
);
824 qemu_coroutine_enter(prh
->co
);
829 static void termsig_handler(int signum
)
831 qatomic_cmpxchg(&state
, RUNNING
, TERMINATE
);
835 static void close_server_socket(void)
839 g_source_remove(server_watch
);
841 object_unref(OBJECT(server_ioc
));
842 num_active_sockets
--;
845 #ifdef CONFIG_LIBCAP_NG
846 static int drop_privileges(void)
848 /* clear all capabilities */
849 capng_clear(CAPNG_SELECT_BOTH
);
851 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
852 CAP_SYS_RAWIO
) < 0) {
857 /* For /dev/mapper/control ioctls */
858 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
859 CAP_SYS_ADMIN
) < 0) {
864 /* Change user/group id, retaining the capabilities. Because file descriptors
865 * are passed via SCM_RIGHTS, we don't need supplementary groups (and in
866 * fact the helper can run as "nobody").
868 if (capng_change_id(uid
!= -1 ? uid
: getuid(),
869 gid
!= -1 ? gid
: getgid(),
870 CAPNG_DROP_SUPP_GRP
| CAPNG_CLEAR_BOUNDING
)) {
878 int main(int argc
, char **argv
)
880 const char *sopt
= "hVk:f:dT:u:g:vq";
881 struct option lopt
[] = {
882 { "help", no_argument
, NULL
, 'h' },
883 { "version", no_argument
, NULL
, 'V' },
884 { "socket", required_argument
, NULL
, 'k' },
885 { "pidfile", required_argument
, NULL
, 'f' },
886 { "daemon", no_argument
, NULL
, 'd' },
887 { "trace", required_argument
, NULL
, 'T' },
888 { "user", required_argument
, NULL
, 'u' },
889 { "group", required_argument
, NULL
, 'g' },
890 { "verbose", no_argument
, NULL
, 'v' },
891 { "quiet", no_argument
, NULL
, 'q' },
898 Error
*local_err
= NULL
;
899 bool daemonize
= false;
900 bool pidfile_specified
= false;
901 bool socket_path_specified
= false;
902 unsigned socket_activation
;
904 struct sigaction sa_sigterm
;
905 memset(&sa_sigterm
, 0, sizeof(sa_sigterm
));
906 sa_sigterm
.sa_handler
= termsig_handler
;
907 sigaction(SIGTERM
, &sa_sigterm
, NULL
);
908 sigaction(SIGINT
, &sa_sigterm
, NULL
);
909 sigaction(SIGHUP
, &sa_sigterm
, NULL
);
911 signal(SIGPIPE
, SIG_IGN
);
914 module_call_init(MODULE_INIT_TRACE
);
915 module_call_init(MODULE_INIT_QOM
);
916 qemu_add_opts(&qemu_trace_opts
);
917 qemu_init_exec_dir(argv
[0]);
919 compute_default_paths();
921 while ((ch
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_ind
)) != -1) {
925 socket_path
= g_strdup(optarg
);
926 socket_path_specified
= true;
927 if (socket_path
[0] != '/') {
928 error_report("socket path must be absolute");
934 pidfile
= g_strdup(optarg
);
935 pidfile_specified
= true;
937 #ifdef CONFIG_LIBCAP_NG
940 struct passwd
*userinfo
= getpwnam(optarg
);
942 uid
= userinfo
->pw_uid
;
943 } else if (qemu_strtoul(optarg
, NULL
, 10, &res
) == 0 &&
947 error_report("invalid user '%s'", optarg
);
954 struct group
*groupinfo
= getgrnam(optarg
);
956 gid
= groupinfo
->gr_gid
;
957 } else if (qemu_strtoul(optarg
, NULL
, 10, &res
) == 0 &&
961 error_report("invalid group '%s'", optarg
);
969 error_report("-%c not supported by this %s", ch
, argv
[0]);
982 trace_opt_parse(optarg
);
993 error_report("Try `%s --help' for more information.", argv
[0]);
999 noisy
= !quiet
&& (loglevel
>= 3);
1000 verbose
= quiet
? 0 : MIN(loglevel
, 3);
1002 if (!trace_init_backends()) {
1006 qemu_set_log(LOG_TRACE
, &error_fatal
);
1010 multipath_pr_init();
1013 socket_activation
= check_socket_activation();
1014 if (socket_activation
== 0) {
1015 SocketAddress saddr
;
1016 saddr
= (SocketAddress
){
1017 .type
= SOCKET_ADDRESS_TYPE_UNIX
,
1018 .u
.q_unix
.path
= socket_path
,
1020 server_ioc
= qio_channel_socket_new();
1021 if (qio_channel_socket_listen_sync(server_ioc
, &saddr
,
1022 1, &local_err
) < 0) {
1023 object_unref(OBJECT(server_ioc
));
1024 error_report_err(local_err
);
1028 /* Using socket activation - check user didn't use -p etc. */
1029 if (socket_path_specified
) {
1030 error_report("Unix socket can't be set when using socket activation");
1034 /* Can only listen on a single socket. */
1035 if (socket_activation
> 1) {
1036 error_report("%s does not support socket activation with LISTEN_FDS > 1",
1040 server_ioc
= qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD
,
1042 if (server_ioc
== NULL
) {
1043 error_reportf_err(local_err
,
1044 "Failed to use socket activation: ");
1049 qemu_init_main_loop(&error_fatal
);
1051 server_watch
= qio_channel_add_watch(QIO_CHANNEL(server_ioc
),
1057 if (daemon(0, 0) < 0) {
1058 error_report("Failed to daemonize: %s", strerror(errno
));
1063 if (daemonize
|| pidfile_specified
) {
1064 qemu_write_pidfile(pidfile
, &error_fatal
);
1067 #ifdef CONFIG_LIBCAP_NG
1068 if (drop_privileges() < 0) {
1069 error_report("Failed to drop privileges: %s", strerror(errno
));
1076 main_loop_wait(false);
1077 if (state
== TERMINATE
) {
1078 state
= TERMINATING
;
1079 close_server_socket();
1081 } while (num_active_sockets
> 0);