aboutsummaryrefslogtreecommitdiff
path: root/storage-daemon/qemu-storage-daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'storage-daemon/qemu-storage-daemon.c')
-rw-r--r--storage-daemon/qemu-storage-daemon.c58
1 files changed, 53 insertions, 5 deletions
diff --git a/storage-daemon/qemu-storage-daemon.c b/storage-daemon/qemu-storage-daemon.c
index 504d33aa91..dd18b2cde8 100644
--- a/storage-daemon/qemu-storage-daemon.c
+++ b/storage-daemon/qemu-storage-daemon.c
@@ -93,6 +93,9 @@ static void help(void)
" --chardev <options> configure a character device backend\n"
" (see the qemu(1) man page for possible options)\n"
"\n"
+" --daemonize daemonize the process, and have the parent exit\n"
+" once startup is complete\n"
+"\n"
" --export [type=]nbd,id=<id>,node-name=<node-name>[,name=<export-name>]\n"
" [,writable=on|off][,bitmap=<name>]\n"
" export the specified block node over NBD\n"
@@ -144,6 +147,7 @@ QEMU_HELP_BOTTOM "\n",
enum {
OPTION_BLOCKDEV = 256,
OPTION_CHARDEV,
+ OPTION_DAEMONIZE,
OPTION_EXPORT,
OPTION_MONITOR,
OPTION_NBD_SERVER,
@@ -177,13 +181,30 @@ static int getopt_set_loc(int argc, char **argv, const char *optstring,
return c;
}
-static void process_options(int argc, char *argv[])
+/**
+ * Process QSD command-line arguments.
+ *
+ * This is done in two passes:
+ *
+ * First (@pre_init_pass is true), we do a pass where all global
+ * arguments pertaining to the QSD process (like --help or --daemonize)
+ * are processed. This pass is done before most of the QEMU-specific
+ * initialization steps (e.g. initializing the block layer or QMP), and
+ * so must only process arguments that are not really QEMU-specific.
+ *
+ * Second (@pre_init_pass is false), we (sequentially) process all
+ * QEMU/QSD-specific arguments. Many of these arguments are effectively
+ * translated to QMP commands (like --blockdev for blockdev-add, or
+ * --export for block-export-add).
+ */
+static void process_options(int argc, char *argv[], bool pre_init_pass)
{
int c;
static const struct option long_options[] = {
{"blockdev", required_argument, NULL, OPTION_BLOCKDEV},
{"chardev", required_argument, NULL, OPTION_CHARDEV},
+ {"daemonize", no_argument, NULL, OPTION_DAEMONIZE},
{"export", required_argument, NULL, OPTION_EXPORT},
{"help", no_argument, NULL, 'h'},
{"monitor", required_argument, NULL, OPTION_MONITOR},
@@ -196,11 +217,27 @@ static void process_options(int argc, char *argv[])
};
/*
- * In contrast to the system emulator, options are processed in the order
- * they are given on the command lines. This means that things must be
- * defined first before they can be referenced in another option.
+ * In contrast to the system emulator, QEMU-specific options are processed
+ * in the order they are given on the command lines. This means that things
+ * must be defined first before they can be referenced in another option.
*/
+ optind = 1;
while ((c = getopt_set_loc(argc, argv, "-hT:V", long_options)) != -1) {
+ bool handle_option_pre_init;
+
+ /* Should this argument be processed in the pre-init pass? */
+ handle_option_pre_init =
+ c == '?' ||
+ c == 'h' ||
+ c == 'V' ||
+ c == OPTION_DAEMONIZE ||
+ c == OPTION_PIDFILE;
+
+ /* Process every option only in its respective pass */
+ if (pre_init_pass != handle_option_pre_init) {
+ continue;
+ }
+
switch (c) {
case '?':
exit(EXIT_FAILURE);
@@ -246,6 +283,12 @@ static void process_options(int argc, char *argv[])
qemu_opts_del(opts);
break;
}
+ case OPTION_DAEMONIZE:
+ if (os_set_daemonize(true) < 0) {
+ error_report("--daemonize not supported in this build");
+ exit(EXIT_FAILURE);
+ }
+ break;
case OPTION_EXPORT:
{
Visitor *v;
@@ -334,6 +377,10 @@ int main(int argc, char *argv[])
qemu_init_exec_dir(argv[0]);
os_setup_signal_handling();
+ process_options(argc, argv, true);
+
+ os_daemonize();
+
module_call_init(MODULE_INIT_QOM);
module_call_init(MODULE_INIT_TRACE);
qemu_add_opts(&qemu_trace_opts);
@@ -348,7 +395,7 @@ int main(int argc, char *argv[])
qemu_set_log(LOG_TRACE);
qemu_init_main_loop(&error_fatal);
- process_options(argc, argv);
+ process_options(argc, argv, false);
/*
* Write the pid file after creating chardevs, exports, and NBD servers but
@@ -356,6 +403,7 @@ int main(int argc, char *argv[])
* it.
*/
pid_file_init();
+ os_setup_post();
while (!exit_requested) {
main_loop_wait(false);