[libvirt] [PATCH 3/4] virsh: split out virsh-domain.c

Eric Blake eblake at redhat.com
Sat Aug 18 04:38:23 UTC 2012


The virsh-domain.c file was pretty self-contained; the only
entry point was the table of command definitions.  The bulk
of this patch is making more functions in virsh.c reusable.
A later patch will clean up poor naming choices.

* tools/Makefile.am (virsh_SOURCES): Build virsh-domain.c.
* tools/virsh-domain.h: New file.
* tools/virsh.h (virshReportError, vshResetLibvirtError)
(vshAskReedit, vshStreamSink): Declare.
* tools/virsh.c: Switch from using .c to .h.
(virshReportError, vshResetLibvirtError, vshAskReedit)
(vshStreamSink, prettyCapacity): Export.
(vshCatchInt): Move...
* tools/virsh-domain.c: ...into sole user.  Use header.
---
 tools/Makefile.am    |  2 +-
 tools/virsh-domain.c | 40 +++++++++++++++++++++++++++++++++++++++-
 tools/virsh-domain.h | 33 +++++++++++++++++++++++++++++++++
 tools/virsh.c        | 28 ++++++++++------------------
 tools/virsh.h        | 12 ++++++++++--
 5 files changed, 93 insertions(+), 22 deletions(-)
 create mode 100644 tools/virsh-domain.h

diff --git a/tools/Makefile.am b/tools/Makefile.am
index 52a8699..b885892 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -106,8 +106,8 @@ virt_host_validate_CFLAGS = \
 virsh_SOURCES =							\
 		console.c console.h				\
 		virsh.c virsh.h					\
+		virsh-domain.c virsh-domain.h			\
 		$(NULL)
-#		virsh-domain.c virsh-domain.h			\
 #		virsh-domain-monitor.c virsh-domain-monitor.h	\
 #		virsh-host.c virsh-host.h			\
 #		virsh-interface.c virsh-interface.h		\
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index dc8620e..edbda91 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -23,6 +23,35 @@
  *
  */

+#include <config.h>
+#include "virsh-domain.h"
+
+#include <fcntl.h>
+#include <poll.h>
+#include <signal.h>
+#include <sys/time.h>
+#include <termios.h>
+
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <libxml/xpath.h>
+#include <libxml/xmlsave.h>
+
+#include "internal.h"
+#include "bitmap.h"
+#include "buf.h"
+#include "c-ctype.h"
+#include "conf/domain_conf.h"
+#include "console.h"
+#include "memory.h"
+#include "util.h"
+#include "virfile.h"
+#include "virkeycode.h"
+#include "virmacaddr.h"
+#include "virterror_internal.h"
+#include "virtypedparam.h"
+#include "xml.h"
+
 static const char *
 vshDomainVcpuStateToString(int state)
 {
@@ -1185,6 +1214,15 @@ print_job_progress(const char *label, unsigned long long remaining,
     fflush(stderr);
 }

+static volatile sig_atomic_t intCaught = 0;
+
+static void vshCatchInt(int sig ATTRIBUTE_UNUSED,
+                        siginfo_t *siginfo ATTRIBUTE_UNUSED,
+                        void *context ATTRIBUTE_UNUSED)
+{
+    intCaught = 1;
+}
+
 /*
  * "blockcopy" command
  */
@@ -8044,7 +8082,7 @@ cleanup:
     return ret;
 }

-static const vshCmdDef domManagementCmds[] = {
+const vshCmdDef domManagementCmds[] = {
     {"attach-device", cmdAttachDevice, opts_attach_device,
      info_attach_device, 0},
     {"attach-disk", cmdAttachDisk, opts_attach_disk,
diff --git a/tools/virsh-domain.h b/tools/virsh-domain.h
new file mode 100644
index 0000000..797462f
--- /dev/null
+++ b/tools/virsh-domain.h
@@ -0,0 +1,33 @@
+/*
+ * virsh-domain.h: Commands to manage domain
+ *
+ * Copyright (C) 2005, 2007-2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library;  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ *  Daniel Veillard <veillard at redhat.com>
+ *  Karel Zak <kzak at redhat.com>
+ *  Daniel P. Berrange <berrange at redhat.com>
+ *
+ */
+
+#ifndef VIRSH_DOMAIN_H
+#define VIRSH_DOMAIN_H
+
+# include "virsh.h"
+
+extern const vshCmdDef domManagementCmds[];
+
+#endif /* VIRSH_DOMAIN_H */
diff --git a/tools/virsh.c b/tools/virsh.c
index 610236b..054047f 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -78,6 +78,8 @@
 #include "conf/domain_conf.h"
 #include "virtypedparam.h"

+#include "virsh-domain.h"
+
 static char *progname;

 static const vshCmdGrp cmdGroups[];
@@ -137,9 +139,9 @@ vshNameSorter(const void *a, const void *b)
     return vshStrcasecmp(*sa, *sb);
 }

-static double
-prettyCapacity(unsigned long long val,
-               const char **unit) {
+double
+prettyCapacity(unsigned long long val, const char **unit)
+{
     if (val < 1024) {
         *unit = "";
         return (double)val;
@@ -176,7 +178,7 @@ virshErrorHandler(void *unused ATTRIBUTE_UNUSED, virErrorPtr error)
 /*
  * Reset libvirt error on graceful fallback paths
  */
-static void
+void
 vshResetLibvirtError(void)
 {
     virFreeError(last_error);
@@ -191,7 +193,7 @@ vshResetLibvirtError(void)
  * twice during one command.  This case shouldn't really happen anyway,
  * and it's IMHO a bug that libvirt does that sometimes.
  */
-static void
+void
 virshReportError(vshControl *ctl)
 {
     if (last_error == NULL) {
@@ -216,15 +218,6 @@ out:
     vshResetLibvirtError();
 }

-static volatile sig_atomic_t intCaught = 0;
-
-static void vshCatchInt(int sig ATTRIBUTE_UNUSED,
-                        siginfo_t *siginfo ATTRIBUTE_UNUSED,
-                        void *context ATTRIBUTE_UNUSED)
-{
-    intCaught = 1;
-}
-
 /*
  * Detection of disconnections and automatic reconnection support
  */
@@ -310,7 +303,7 @@ vshPrintRaw(vshControl *ctl, ...)
  *         -1  on error
  *          0  otherwise
  */
-static int
+int
 vshAskReedit(vshControl *ctl, const char *msg)
 {
     int c = -1;
@@ -359,8 +352,8 @@ vshAskReedit(vshControl *ctl, const char *msg ATTRIBUTE_UNUSED)
 }
 #endif /* WIN32 */

-static int vshStreamSink(virStreamPtr st ATTRIBUTE_UNUSED,
-                         const char *bytes, size_t nbytes, void *opaque)
+int vshStreamSink(virStreamPtr st ATTRIBUTE_UNUSED,
+                  const char *bytes, size_t nbytes, void *opaque)
 {
     int *fd = opaque;

@@ -2873,7 +2866,6 @@ vshParseArgv(vshControl *ctl, int argc, char **argv)
     return true;
 }

-#include "virsh-domain.c"
 #include "virsh-domain-monitor.c"
 #include "virsh-host.c"
 #include "virsh-interface.c"
diff --git a/tools/virsh.h b/tools/virsh.h
index 8680a0a..7e505aa 100644
--- a/tools/virsh.h
+++ b/tools/virsh.h
@@ -329,6 +329,10 @@ char *vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
 char *editWriteToTempFile(vshControl *ctl, const char *doc);
 int   editFile(vshControl *ctl, const char *filename);
 char *editReadBackFile(vshControl *ctl, const char *filename);
+int vshAskReedit(vshControl *ctl, const char *msg);
+int vshStreamSink(virStreamPtr st, const char *bytes, size_t nbytes,
+                  void *opaque);
+double prettyCapacity(unsigned long long val, const char **unit);

 /* Typedefs, function prototypes for job progress reporting.
  * There are used by some long lingering commands like
@@ -343,6 +347,12 @@ typedef struct __vshCtrlData {
 typedef void (*jobWatchTimeoutFunc) (vshControl *ctl, virDomainPtr dom,
                                      void *opaque);

+/* error handling */
+extern virErrorPtr last_error;
+void virshReportError(vshControl *ctl);
+void vshResetLibvirtError(void);
+
+/* allocation wrappers */
 void *_vshMalloc(vshControl *ctl, size_t sz, const char *filename, int line);
 # define vshMalloc(_ctl, _sz)    _vshMalloc(_ctl, _sz, __FILE__, __LINE__)

@@ -365,6 +375,4 @@ char *_vshStrdup(vshControl *ctl, const char *s, const char *filename,
 # define realloc use_vshRealloc_instead_of_realloc
 # define strdup use_vshStrdup_instead_of_strdup

-extern virErrorPtr last_error;
-
 #endif /* VIRSH_H */
-- 
1.7.11.2




More information about the libvir-list mailing list