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"
122 typedef struct PRHelperSGIOData
{
127 int sz
; /* input/output */
131 static int do_sgio_worker(void *opaque
)
133 PRHelperSGIOData
*data
= opaque
;
134 struct sg_io_hdr io_hdr
;
137 SCSISense sense_code
;
139 memset(data
->sense
, 0, PR_HELPER_SENSE_SIZE
);
140 memset(&io_hdr
, 0, sizeof(io_hdr
));
141 io_hdr
.interface_id
= 'S';
142 io_hdr
.cmd_len
= PR_HELPER_CDB_SIZE
;
143 io_hdr
.cmdp
= (uint8_t *)data
->cdb
;
144 io_hdr
.sbp
= data
->sense
;
145 io_hdr
.mx_sb_len
= PR_HELPER_SENSE_SIZE
;
147 io_hdr
.dxfer_direction
= data
->dir
;
148 io_hdr
.dxferp
= (char *)data
->buf
;
149 io_hdr
.dxfer_len
= data
->sz
;
150 ret
= ioctl(data
->fd
, SG_IO
, &io_hdr
);
151 status
= sg_io_sense_from_errno(ret
< 0 ? errno
: 0, &io_hdr
,
153 if (status
== GOOD
) {
154 data
->sz
-= io_hdr
.resid
;
159 if (status
== CHECK_CONDITION
&&
160 !(io_hdr
.driver_status
& SG_ERR_DRIVER_SENSE
)) {
161 scsi_build_sense(data
->sense
, sense_code
);
167 static int do_sgio(int fd
, const uint8_t *cdb
, uint8_t *sense
,
168 uint8_t *buf
, int *sz
, int dir
)
170 ThreadPool
*pool
= aio_get_thread_pool(qemu_get_aio_context());
173 PRHelperSGIOData data
= {
182 r
= thread_pool_submit_co(pool
, do_sgio_worker
, &data
);
187 /* Device mapper interface */
190 #define CONTROL_PATH "/dev/mapper/control"
192 typedef struct DMData
{
197 static int control_fd
;
199 static void *dm_ioctl(int ioc
, struct dm_ioctl
*dm
)
202 memcpy(&d
.dm
, dm
, sizeof(d
.dm
));
203 QEMU_BUILD_BUG_ON(sizeof(d
.data
) < sizeof(struct dm_target_spec
));
205 d
.dm
.version
[0] = DM_VERSION_MAJOR
;
208 d
.dm
.data_size
= 1024;
209 d
.dm
.data_start
= offsetof(DMData
, data
);
210 if (ioctl(control_fd
, ioc
, &d
) < 0) {
213 memcpy(dm
, &d
.dm
, sizeof(d
.dm
));
217 static void *dm_dev_ioctl(int fd
, int ioc
, struct dm_ioctl
*dm
)
228 dm
->dev
= st
.st_rdev
;
229 return dm_ioctl(ioc
, dm
);
232 static void dm_init(void)
234 control_fd
= open(CONTROL_PATH
, O_RDWR
);
235 if (control_fd
< 0) {
236 perror("Cannot open " CONTROL_PATH
);
239 struct dm_ioctl dm
= { 0 };
240 if (!dm_ioctl(DM_VERSION
, &dm
)) {
244 if (dm
.version
[0] != DM_VERSION_MAJOR
) {
245 fprintf(stderr
, "Unsupported device mapper interface");
250 /* Variables required by libmultipath and libmpathpersist. */
251 QEMU_BUILD_BUG_ON(PR_HELPER_DATA_SIZE
> MPATH_MAX_PARAM_LEN
);
252 static struct config
*multipath_conf
;
253 unsigned mpath_mx_alloc_len
= PR_HELPER_DATA_SIZE
;
257 extern struct config
*get_multipath_config(void);
258 struct config
*get_multipath_config(void)
260 return multipath_conf
;
263 extern void put_multipath_config(struct config
*conf
);
264 void put_multipath_config(struct config
*conf
)
268 static void multipath_pr_init(void)
271 #ifdef CONFIG_MPATH_NEW_API
272 multipath_conf
= mpath_lib_init();
274 mpath_lib_init(udev
);
278 static int is_mpath(int fd
)
280 struct dm_ioctl dm
= { .flags
= DM_NOFLUSH_FLAG
};
281 struct dm_target_spec
*tgt
;
283 tgt
= dm_dev_ioctl(fd
, DM_TABLE_STATUS
, &dm
);
285 if (errno
== ENXIO
) {
291 return !strncmp(tgt
->target_type
, "multipath", DM_MAX_TYPE_NAME
);
294 static SCSISense
mpath_generic_sense(int r
)
297 case MPATH_PR_SENSE_NOT_READY
:
298 return SENSE_CODE(NOT_READY
);
299 case MPATH_PR_SENSE_MEDIUM_ERROR
:
300 return SENSE_CODE(READ_ERROR
);
301 case MPATH_PR_SENSE_HARDWARE_ERROR
:
302 return SENSE_CODE(TARGET_FAILURE
);
303 case MPATH_PR_SENSE_ABORTED_COMMAND
:
304 return SENSE_CODE(IO_ERROR
);
310 static int mpath_reconstruct_sense(int fd
, int r
, uint8_t *sense
)
313 case MPATH_PR_SUCCESS
:
315 case MPATH_PR_SENSE_NOT_READY
:
316 case MPATH_PR_SENSE_MEDIUM_ERROR
:
317 case MPATH_PR_SENSE_HARDWARE_ERROR
:
318 case MPATH_PR_SENSE_ABORTED_COMMAND
:
320 /* libmpathpersist ate the exact sense. Try to find it by
321 * issuing TEST UNIT READY.
323 uint8_t cdb
[6] = { TEST_UNIT_READY
};
325 int r
= do_sgio(fd
, cdb
, sense
, NULL
, &sz
, SG_DXFER_NONE
);
330 scsi_build_sense(sense
, mpath_generic_sense(r
));
331 return CHECK_CONDITION
;
334 case MPATH_PR_SENSE_UNIT_ATTENTION
:
335 /* Congratulations libmpathpersist, you ruined the Unit Attention...
336 * Return a heavyweight one.
338 scsi_build_sense(sense
, SENSE_CODE(SCSI_BUS_RESET
));
339 return CHECK_CONDITION
;
340 case MPATH_PR_SENSE_INVALID_OP
:
341 /* Only one valid sense. */
342 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
343 return CHECK_CONDITION
;
344 case MPATH_PR_ILLEGAL_REQ
:
346 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM
));
347 return CHECK_CONDITION
;
348 case MPATH_PR_NO_SENSE
:
349 scsi_build_sense(sense
, SENSE_CODE(NO_SENSE
));
350 return CHECK_CONDITION
;
352 case MPATH_PR_RESERV_CONFLICT
:
353 return RESERVATION_CONFLICT
;
357 scsi_build_sense(sense
, SENSE_CODE(LUN_COMM_FAILURE
));
358 return CHECK_CONDITION
;
362 static int multipath_pr_in(int fd
, const uint8_t *cdb
, uint8_t *sense
,
363 uint8_t *data
, int sz
)
365 int rq_servact
= cdb
[1];
366 struct prin_resp resp
;
370 switch (rq_servact
) {
371 case MPATH_PRIN_RKEY_SA
:
372 case MPATH_PRIN_RRES_SA
:
373 case MPATH_PRIN_RCAP_SA
:
375 case MPATH_PRIN_RFSTAT_SA
:
376 /* Nobody implements it anyway, so bail out. */
378 /* Cannot parse any other output. */
379 scsi_build_sense(sense
, SENSE_CODE(INVALID_FIELD
));
380 return CHECK_CONDITION
;
383 r
= mpath_persistent_reserve_in(fd
, rq_servact
, &resp
, noisy
, verbose
);
384 if (r
== MPATH_PR_SUCCESS
) {
385 switch (rq_servact
) {
386 case MPATH_PRIN_RKEY_SA
:
387 case MPATH_PRIN_RRES_SA
: {
388 struct prin_readdescr
*out
= &resp
.prin_descriptor
.prin_readkeys
;
390 written
= MIN(out
->additional_length
+ 8, sz
);
391 stl_be_p(&data
[0], out
->prgeneration
);
392 stl_be_p(&data
[4], out
->additional_length
);
393 memcpy(&data
[8], out
->key_list
, written
- 8);
396 case MPATH_PRIN_RCAP_SA
: {
397 struct prin_capdescr
*out
= &resp
.prin_descriptor
.prin_readcap
;
400 stw_be_p(&data
[0], out
->length
);
401 data
[2] = out
->flags
[0];
402 data
[3] = out
->flags
[1];
403 stw_be_p(&data
[4], out
->pr_type_mask
);
407 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
408 return CHECK_CONDITION
;
410 assert(written
<= sz
);
411 memset(data
+ written
, 0, sz
- written
);
414 return mpath_reconstruct_sense(fd
, r
, sense
);
417 static int multipath_pr_out(int fd
, const uint8_t *cdb
, uint8_t *sense
,
418 const uint8_t *param
, int sz
)
420 int rq_servact
= cdb
[1];
421 int rq_scope
= cdb
[2] >> 4;
422 int rq_type
= cdb
[2] & 0xf;
423 struct prout_param_descriptor paramp
;
424 char transportids
[PR_HELPER_DATA_SIZE
];
427 if (sz
< PR_OUT_FIXED_PARAM_SIZE
) {
428 /* Illegal request, Parameter list length error. This isn't fatal;
429 * we have read the data, send an error without closing the socket.
431 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM_LEN
));
432 return CHECK_CONDITION
;
435 switch (rq_servact
) {
436 case MPATH_PROUT_REG_SA
:
437 case MPATH_PROUT_RES_SA
:
438 case MPATH_PROUT_REL_SA
:
439 case MPATH_PROUT_CLEAR_SA
:
440 case MPATH_PROUT_PREE_SA
:
441 case MPATH_PROUT_PREE_AB_SA
:
442 case MPATH_PROUT_REG_IGN_SA
:
444 case MPATH_PROUT_REG_MOV_SA
:
445 /* Not supported by struct prout_param_descriptor. */
447 /* Cannot parse any other input. */
448 scsi_build_sense(sense
, SENSE_CODE(INVALID_FIELD
));
449 return CHECK_CONDITION
;
452 /* Convert input data, especially transport IDs, to the structs
453 * used by libmpathpersist (which, of course, will immediately
456 memset(¶mp
, 0, sizeof(paramp
));
457 memcpy(¶mp
.key
, ¶m
[0], 8);
458 memcpy(¶mp
.sa_key
, ¶m
[8], 8);
459 paramp
.sa_flags
= param
[20];
460 if (sz
> PR_OUT_FIXED_PARAM_SIZE
) {
461 size_t transportid_len
;
463 if (sz
< PR_OUT_FIXED_PARAM_SIZE
+ 4) {
464 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM_LEN
));
465 return CHECK_CONDITION
;
467 transportid_len
= ldl_be_p(¶m
[24]) + PR_OUT_FIXED_PARAM_SIZE
+ 4;
468 if (transportid_len
> sz
) {
469 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM
));
470 return CHECK_CONDITION
;
472 for (i
= PR_OUT_FIXED_PARAM_SIZE
+ 4, j
= 0; i
< transportid_len
; ) {
473 struct transportid
*id
= (struct transportid
*) &transportids
[j
];
476 id
->format_code
= param
[i
] & 0xc0;
477 id
->protocol_id
= param
[i
] & 0x0f;
478 switch (param
[i
] & 0xcf) {
481 if (i
+ 24 > transportid_len
) {
484 memcpy(id
->n_port_name
, ¶m
[i
+ 8], 8);
485 j
+= offsetof(struct transportid
, n_port_name
[8]);
490 /* iSCSI transport. */
491 len
= lduw_be_p(¶m
[i
+ 2]);
492 if (len
> 252 || (len
& 3) || i
+ len
+ 4 > transportid_len
) {
493 /* For format code 00, the standard says the maximum is 223
494 * plus the NUL terminator. For format code 01 there is no
495 * maximum length, but libmpathpersist ignores the first
496 * byte of id->iscsi_name so our maximum is 252.
500 if (memchr(¶m
[i
+ 4], 0, len
) == NULL
) {
503 memcpy(id
->iscsi_name
, ¶m
[i
+ 2], len
+ 2);
504 j
+= offsetof(struct transportid
, iscsi_name
[len
+ 2]);
509 if (i
+ 24 > transportid_len
) {
512 memcpy(id
->sas_address
, ¶m
[i
+ 4], 8);
513 j
+= offsetof(struct transportid
, sas_address
[8]);
518 scsi_build_sense(sense
, SENSE_CODE(INVALID_PARAM
));
519 return CHECK_CONDITION
;
522 paramp
.trnptid_list
[paramp
.num_transportid
++] = id
;
526 r
= mpath_persistent_reserve_out(fd
, rq_servact
, rq_scope
, rq_type
,
527 ¶mp
, noisy
, verbose
);
528 return mpath_reconstruct_sense(fd
, r
, sense
);
532 static int do_pr_in(int fd
, const uint8_t *cdb
, uint8_t *sense
,
533 uint8_t *data
, int *resp_sz
)
537 /* multipath_pr_in fills the whole input buffer. */
538 int r
= multipath_pr_in(fd
, cdb
, sense
, data
, *resp_sz
);
546 return do_sgio(fd
, cdb
, sense
, data
, resp_sz
,
550 static int do_pr_out(int fd
, const uint8_t *cdb
, uint8_t *sense
,
551 const uint8_t *param
, int sz
)
555 if ((fcntl(fd
, F_GETFL
) & O_ACCMODE
) == O_RDONLY
) {
556 scsi_build_sense(sense
, SENSE_CODE(INVALID_OPCODE
));
557 return CHECK_CONDITION
;
562 return multipath_pr_out(fd
, cdb
, sense
, param
, sz
);
567 return do_sgio(fd
, cdb
, sense
, (uint8_t *)param
, &resp_sz
,
573 typedef struct PRHelperClient
{
574 QIOChannelSocket
*ioc
;
577 uint8_t data
[PR_HELPER_DATA_SIZE
];
580 typedef struct PRHelperRequest
{
583 uint8_t cdb
[PR_HELPER_CDB_SIZE
];
586 static int coroutine_fn
prh_read(PRHelperClient
*client
, void *buf
, int sz
,
600 n_read
= qio_channel_readv_full(QIO_CHANNEL(client
->ioc
), &iov
, 1,
603 if (n_read
== QIO_CHANNEL_ERR_BLOCK
) {
604 qio_channel_yield(QIO_CHANNEL(client
->ioc
), G_IO_IN
);
608 ret
= n_read
? n_read
: -1;
612 /* Stash one file descriptor per request. */
614 bool too_many
= false;
615 for (i
= 0; i
< nfds
; i
++) {
616 if (client
->fd
== -1) {
637 if (client
->fd
!= -1) {
644 static int coroutine_fn
prh_read_request(PRHelperClient
*client
,
645 PRHelperRequest
*req
,
646 PRHelperResponse
*resp
, Error
**errp
)
650 if (prh_read(client
, req
->cdb
, sizeof(req
->cdb
), NULL
) < 0) {
654 if (client
->fd
== -1) {
655 error_setg(errp
, "No file descriptor in request.");
659 if (req
->cdb
[0] != PERSISTENT_RESERVE_OUT
&&
660 req
->cdb
[0] != PERSISTENT_RESERVE_IN
) {
661 error_setg(errp
, "Invalid CDB, closing socket.");
665 sz
= scsi_cdb_xfer(req
->cdb
);
666 if (sz
> sizeof(client
->data
)) {
670 if (req
->cdb
[0] == PERSISTENT_RESERVE_OUT
) {
671 if (qio_channel_read_all(QIO_CHANNEL(client
->ioc
),
672 (char *)client
->data
, sz
,
678 req
->fd
= client
->fd
;
689 static int coroutine_fn
prh_write_response(PRHelperClient
*client
,
690 PRHelperRequest
*req
,
691 PRHelperResponse
*resp
, Error
**errp
)
696 if (req
->cdb
[0] == PERSISTENT_RESERVE_IN
&& resp
->result
== GOOD
) {
697 assert(resp
->sz
<= req
->sz
&& resp
->sz
<= sizeof(client
->data
));
699 assert(resp
->sz
== 0);
704 resp
->result
= cpu_to_be32(resp
->result
);
705 resp
->sz
= cpu_to_be32(resp
->sz
);
706 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
707 (char *) resp
, sizeof(*resp
), errp
);
712 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
713 (char *) client
->data
,
715 return r
< 0 ? r
: 0;
718 static void coroutine_fn
prh_co_entry(void *opaque
)
720 PRHelperClient
*client
= opaque
;
721 Error
*local_err
= NULL
;
725 qio_channel_set_blocking(QIO_CHANNEL(client
->ioc
),
727 qio_channel_attach_aio_context(QIO_CHANNEL(client
->ioc
),
728 qemu_get_aio_context());
730 /* A very simple negotiation for future extensibility. No features
731 * are defined so write 0.
733 flags
= cpu_to_be32(0);
734 r
= qio_channel_write_all(QIO_CHANNEL(client
->ioc
),
735 (char *) &flags
, sizeof(flags
), NULL
);
740 r
= qio_channel_read_all(QIO_CHANNEL(client
->ioc
),
741 (char *) &flags
, sizeof(flags
), NULL
);
742 if (be32_to_cpu(flags
) != 0 || r
< 0) {
746 while (atomic_read(&state
) == RUNNING
) {
748 PRHelperResponse resp
;
751 sz
= prh_read_request(client
, &req
, &resp
, &local_err
);
756 num_active_sockets
++;
757 if (req
.cdb
[0] == PERSISTENT_RESERVE_OUT
) {
758 r
= do_pr_out(req
.fd
, req
.cdb
, resp
.sense
,
762 resp
.sz
= sizeof(client
->data
);
763 r
= do_pr_in(req
.fd
, req
.cdb
, resp
.sense
,
764 client
->data
, &resp
.sz
);
765 resp
.sz
= MIN(resp
.sz
, sz
);
767 num_active_sockets
--;
774 if (prh_write_response(client
, &req
, &resp
, &local_err
) < 0) {
781 error_free(local_err
);
783 error_report_err(local_err
);
788 qio_channel_detach_aio_context(QIO_CHANNEL(client
->ioc
));
789 object_unref(OBJECT(client
->ioc
));
793 static gboolean
accept_client(QIOChannel
*ioc
, GIOCondition cond
, gpointer opaque
)
795 QIOChannelSocket
*cioc
;
798 cioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc
),
804 prh
= g_new(PRHelperClient
, 1);
807 prh
->co
= qemu_coroutine_create(prh_co_entry
, prh
);
808 qemu_coroutine_enter(prh
->co
);
813 static void termsig_handler(int signum
)
815 atomic_cmpxchg(&state
, RUNNING
, TERMINATE
);
819 static void close_server_socket(void)
823 g_source_remove(server_watch
);
825 object_unref(OBJECT(server_ioc
));
826 num_active_sockets
--;
830 static int drop_privileges(void)
832 /* clear all capabilities */
833 capng_clear(CAPNG_SELECT_BOTH
);
835 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
836 CAP_SYS_RAWIO
) < 0) {
841 /* For /dev/mapper/control ioctls */
842 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
843 CAP_SYS_ADMIN
) < 0) {
848 /* Change user/group id, retaining the capabilities. Because file descriptors
849 * are passed via SCM_RIGHTS, we don't need supplementary groups (and in
850 * fact the helper can run as "nobody").
852 if (capng_change_id(uid
!= -1 ? uid
: getuid(),
853 gid
!= -1 ? gid
: getgid(),
854 CAPNG_DROP_SUPP_GRP
| CAPNG_CLEAR_BOUNDING
)) {
862 int main(int argc
, char **argv
)
864 const char *sopt
= "hVk:f:dT:u:g:vq";
865 struct option lopt
[] = {
866 { "help", no_argument
, NULL
, 'h' },
867 { "version", no_argument
, NULL
, 'V' },
868 { "socket", required_argument
, NULL
, 'k' },
869 { "pidfile", required_argument
, NULL
, 'f' },
870 { "daemon", no_argument
, NULL
, 'd' },
871 { "trace", required_argument
, NULL
, 'T' },
872 { "user", required_argument
, NULL
, 'u' },
873 { "group", required_argument
, NULL
, 'g' },
874 { "verbose", no_argument
, NULL
, 'v' },
875 { "quiet", no_argument
, NULL
, 'q' },
882 Error
*local_err
= NULL
;
883 char *trace_file
= NULL
;
884 bool daemonize
= false;
885 bool pidfile_specified
= false;
886 bool socket_path_specified
= false;
887 unsigned socket_activation
;
889 struct sigaction sa_sigterm
;
890 memset(&sa_sigterm
, 0, sizeof(sa_sigterm
));
891 sa_sigterm
.sa_handler
= termsig_handler
;
892 sigaction(SIGTERM
, &sa_sigterm
, NULL
);
893 sigaction(SIGINT
, &sa_sigterm
, NULL
);
894 sigaction(SIGHUP
, &sa_sigterm
, NULL
);
896 signal(SIGPIPE
, SIG_IGN
);
898 module_call_init(MODULE_INIT_TRACE
);
899 module_call_init(MODULE_INIT_QOM
);
900 qemu_add_opts(&qemu_trace_opts
);
901 qemu_init_exec_dir(argv
[0]);
903 compute_default_paths();
905 while ((ch
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_ind
)) != -1) {
909 socket_path
= g_strdup(optarg
);
910 socket_path_specified
= true;
911 if (socket_path
[0] != '/') {
912 error_report("socket path must be absolute");
918 pidfile
= g_strdup(optarg
);
919 pidfile_specified
= true;
924 struct passwd
*userinfo
= getpwnam(optarg
);
926 uid
= userinfo
->pw_uid
;
927 } else if (qemu_strtoul(optarg
, NULL
, 10, &res
) == 0 &&
931 error_report("invalid user '%s'", optarg
);
938 struct group
*groupinfo
= getgrnam(optarg
);
940 gid
= groupinfo
->gr_gid
;
941 } else if (qemu_strtoul(optarg
, NULL
, 10, &res
) == 0 &&
945 error_report("invalid group '%s'", optarg
);
953 error_report("-%c not supported by this %s", ch
, argv
[0]);
967 trace_file
= trace_opt_parse(optarg
);
978 error_report("Try `%s --help' for more information.", argv
[0]);
984 noisy
= !quiet
&& (loglevel
>= 3);
985 verbose
= quiet
? 0 : MIN(loglevel
, 3);
987 if (!trace_init_backends()) {
990 trace_init_file(trace_file
);
991 qemu_set_log(LOG_TRACE
);
998 socket_activation
= check_socket_activation();
999 if (socket_activation
== 0) {
1000 SocketAddress saddr
;
1001 saddr
= (SocketAddress
){
1002 .type
= SOCKET_ADDRESS_TYPE_UNIX
,
1003 .u
.q_unix
.path
= socket_path
,
1005 server_ioc
= qio_channel_socket_new();
1006 if (qio_channel_socket_listen_sync(server_ioc
, &saddr
, &local_err
) < 0) {
1007 object_unref(OBJECT(server_ioc
));
1008 error_report_err(local_err
);
1012 /* Using socket activation - check user didn't use -p etc. */
1013 if (socket_path_specified
) {
1014 error_report("Unix socket can't be set when using socket activation");
1018 /* Can only listen on a single socket. */
1019 if (socket_activation
> 1) {
1020 error_report("%s does not support socket activation with LISTEN_FDS > 1",
1024 server_ioc
= qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD
,
1026 if (server_ioc
== NULL
) {
1027 error_report("Failed to use socket activation: %s",
1028 error_get_pretty(local_err
));
1033 if (qemu_init_main_loop(&local_err
)) {
1034 error_report_err(local_err
);
1038 server_watch
= qio_channel_add_watch(QIO_CHANNEL(server_ioc
),
1044 if (daemon(0, 0) < 0) {
1045 error_report("Failed to daemonize: %s", strerror(errno
));
1050 if ((daemonize
|| pidfile_specified
) &&
1051 !qemu_write_pidfile(pidfile
, &local_err
)) {
1052 error_report_err(local_err
);
1056 #ifdef CONFIG_LIBCAP
1057 if (drop_privileges() < 0) {
1058 error_report("Failed to drop privileges: %s", strerror(errno
));
1065 main_loop_wait(false);
1066 if (state
== TERMINATE
) {
1067 state
= TERMINATING
;
1068 close_server_socket();
1070 } while (num_active_sockets
> 0);