Skip to contentSkip to conversation

libxl: prevent xl from running if xend is running.

PATCH In review Roger Pau Monne 2 participants 2 messages May 14, 2012

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 libxl: prevent xl from running if xend is running.

Prevent xl from doing any operation if xend daemon is running. That prevents bugs that happened when xl and xend raced to close a domain.

Changes since v3:

* Refresh to apply to current tree.

Changes since v2:

* Allow to dry run even if xend is running.

* Refresh to apply to current tree.

Changes since v1:

* Add documentation to xl man page.

* Permit the execution of commands that don't modify anything.

* Indent error message.

---
 docs/man/xl.pod.1         |    6 ++
 tools/libxl/xl.c          |   22 +++++++-
 tools/libxl/xl.h          |    1 +
 tools/libxl/xl_cmdimpl.c  |    5 +-
 tools/libxl/xl_cmdtable.c |  130 ++++++++++++++++++++++----------------------
 5 files changed, 96 insertions(+), 68 deletions(-)
docs/man/xl.pod.1 +6 -0
index 283c9ed..b192efa 100644
@@ -75,6 +75,12 @@ Verbose.
 
 Dry run: do not actually execute the command.
 
+=item B<-f>
+
+Force execution: xl will refuse to run some commands if it detects that xend is
+also running, this option will force the execution of those commands, even
+though it is unsafe.
+
 =back
 
 =head1 DOMAIN SUBCOMMANDS
tools/libxl/xl.c +21 -1
index d4db1f8..750a61c 100644
@@ -32,8 +32,11 @@
 #include "libxlutil.h"
 #include "xl.h"
 
+#define XEND_LOCK { "/var/lock/subsys/xend", "/var/lock/xend" }
+
 xentoollog_logger_stdiostream *logger;
 int dryrun_only;
+int force_execution;
 int autoballoon = 1;
 char *lockfile;
 char *default_vifscript = NULL;
@@ -126,8 +129,9 @@ int main(int argc, char **argv)
     char *config_file;
     void *config_data = 0;
     int config_len = 0;
+    const char *locks[] = XEND_LOCK;
 
-    while ((opt = getopt(argc, argv, "+vN")) >= 0) {
+    while ((opt = getopt(argc, argv, "+vfN")) >= 0) {
         switch (opt) {
         case 'v':
             if (minmsglevel > 0) minmsglevel--;
@@ -135,6 +139,9 @@ int main(int argc, char **argv)
         case 'N':
             dryrun_only = 1;
             break;
+        case 'f':
+            force_execution = 1;
+            break;
         default:
             fprintf(stderr, "unknown global option\n");
             exit(2);
@@ -185,6 +192,19 @@ int main(int argc, char **argv)
             ret = 1;
             goto xit;
         }
+        if (cspec->modifies && !dryrun_only) {
+            for (int i = 0; i < sizeof(locks)/sizeof(locks[0]); i++) {
+                if (!access(locks[i], F_OK) && !force_execution) {
+                    fprintf(stderr,
+"xend is running, which prevents xl from working correctly.\n"
+"If you still want to force the execution of xl please use the -f\n"
+"option.\n"
+                            );
+                    ret = 1;
+                    goto xit;
+                }
+            }
+        }
         ret = cspec->cmd_impl(argc, argv);
     } else if (!strcmp(cmd, "help")) {
         help(argv[1]);
tools/libxl/xl.h +1 -0
index 2b6714a..5d0d504 100644
@@ -21,6 +21,7 @@ struct cmd_spec {
     char *cmd_name;
     int (*cmd_impl)(int argc, char **argv);
     int can_dryrun;
+    int modifies;
     char *cmd_desc;
     char *cmd_usage;
     char *cmd_option;
tools/libxl/xl_cmdimpl.c +3 -2
index efaf3de..5fc5cde 100644
@@ -1937,7 +1937,7 @@ void help(const char *command)
     struct cmd_spec *cmd;
 
     if (!command || !strcmp(command, "help")) {
-        printf("Usage xl [-vN] <subcommand> [args]\n\n");
+        printf("Usage xl [-vfN] <subcommand> [args]\n\n");
         printf("xl full list of subcommands:\n\n");
         for (i = 0; i < cmdtable_len; i++) {
             printf(" %-19s ", cmd_table[i].cmd_name);
@@ -1948,7 +1948,8 @@ void help(const char *command)
     } else {
         cmd = cmdtable_lookup(command);
         if (cmd) {
-            printf("Usage: xl [-v%s] %s %s\n\n%s.\n\n",
+            printf("Usage: xl [-v%s%s] %s %s\n\n%s.\n\n",
+                   cmd->modifies ? "f" : "",
                    cmd->can_dryrun ? "N" : "",
                    cmd->cmd_name,
                    cmd->cmd_usage,
tools/libxl/xl_cmdtable.c +65 -65

432 lines not on this page · raw message

Signature · 1 line
1.7.7.5 (Apple Git-26)