[Libguestfs] [PATCH v6 14/41] daemon: Link guestfsd with libutils.

Richard W.M. Jones
Thu, 15 Jun 2017 17:06:04 -0000

After the previous refactoring, we are able to link the daemon to
common/utils, and also remove some of the "duplicate" functions that
the daemon carried ("duplicate" in quotes because they were often not
exact duplicates).

It also allows us in future (but not in this commit) to move utility
functions from the daemon into libutils.
---
 daemon/Makefile.am   |  6 ++++-
 daemon/augeas.c      |  2 +-
 daemon/btrfs.c       | 18 +++++++--------
 daemon/cleanups.c    |  6 -----
 daemon/daemon.h      | 14 +-----------
 daemon/debug.c       |  4 ++--
 daemon/echo-daemon.c |  2 +-
 daemon/guestfsd.c    | 64 ----------------------------------------------------
 daemon/ldm.c         |  2 +-
 daemon/lvm.c         |  4 ++--
 daemon/md.c          |  8 ++++---
 daemon/stat.c        |  2 +-
 generator/daemon.ml  |  8 +++----
 13 files changed, 32 insertions(+), 108 deletions(-)

diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 9695500bf..84d9d279e 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -50,6 +50,7 @@ guestfsd_SOURCES = \
 	../common/errnostring/errnostring.h \
 	../common/protocol/guestfs_protocol.h \
 	../common/cleanups/cleanups.h \
+	../common/utils/utils.h \
 	9p.c \
 	acl.c \
 	actions.h \
@@ -179,6 +180,7 @@ guestfsd_LDADD = \
 	../common/errnostring/liberrnostring.la \
 	../common/protocol/libprotocol.la \
 	../common/cleanups/libcleanups.la \
+	../common/utils/libutils.la \
 	$(ACL_LIBS) \
 	$(CAP_LIBS) \
 	$(YAJL_LIBS) \
@@ -209,7 +211,9 @@ guestfsd_CPPFLAGS = \
 	-I$(top_srcdir)/common/protocol \
 	-I$(top_builddir)/common/protocol \
 	-I$(top_srcdir)/common/cleanups \
-	-I$(top_builddir)/common/cleanups
+	-I$(top_builddir)/common/cleanups \
+	-I$(top_srcdir)/common/utils \
+	-I$(top_builddir)/common/utils
 guestfsd_CFLAGS = \
 	$(WARN_CFLAGS) $(WERROR_CFLAGS) \
 	$(RPC_CFLAGS) \
diff --git a/daemon/augeas.c b/daemon/augeas.c
index 5adc959a5..bd54c4849 100644
--- a/daemon/augeas.c
+++ b/daemon/augeas.c
@@ -436,7 +436,7 @@ do_aug_ls (const char *path)
   if (matches == NULL)
     return NULL;		/* do_aug_match has already sent the error */

-  sort_strings (matches, count_strings ((void *) matches));
+  sort_strings (matches, guestfs_int_count_strings ((void *) matches));
   return matches;		/* Caller frees. */
 }

diff --git a/daemon/btrfs.c b/daemon/btrfs.c
index ae2310b53..5f1e5d1d0 100644
--- a/daemon/btrfs.c
+++ b/daemon/btrfs.c
@@ -152,7 +152,7 @@ do_mkfs_btrfs (char *const *devices,
                int leafsize, const char *label, const char *metadata,
                int nodesize, int sectorsize)
 {
-  const size_t nr_devices = count_strings (devices);
+  const size_t nr_devices = guestfs_int_count_strings (devices);
   const size_t MAX_ARGS = nr_devices + 64;
   const char *argv[MAX_ARGS];
   size_t i = 0, j;
@@ -500,7 +500,7 @@ do_btrfs_subvolume_list (const mountable_t *fs)

   guestfs_int_btrfssubvolume_list *ret = NULL;

-  const size_t nr_subvolumes = count_strings (lines);
+  const size_t nr_subvolumes = guestfs_int_count_strings (lines);

   ret = malloc (sizeof *ret);
   if (!ret) {
@@ -733,7 +733,7 @@ int
 do_btrfs_device_add (char *const *devices, const char *fs)
 {
   static int btrfs_device_add_needs_force = -1;
-  const size_t nr_devices = count_strings (devices);
+  const size_t nr_devices = guestfs_int_count_strings (devices);
   const size_t MAX_ARGS = nr_devices + 8;
   const char *argv[MAX_ARGS];
   size_t i = 0, j;
@@ -781,7 +781,7 @@ do_btrfs_device_add (char *const *devices, const char *fs)
 int
 do_btrfs_device_delete (char *const *devices, const char *fs)
 {
-  const size_t nr_devices = count_strings (devices);
+  const size_t nr_devices = guestfs_int_count_strings (devices);

   if (nr_devices == 0)
     return 0;
@@ -1391,7 +1391,7 @@ do_btrfs_qgroup_show (const char *path)
    *  0/5        9249849344   9249849344
    *
    */
-  const size_t nr_qgroups = count_strings (lines) - 2;
+  const size_t nr_qgroups = guestfs_int_count_strings (lines) - 2;
   guestfs_int_btrfsqgroup_list *ret = NULL;
   ret = malloc (sizeof *ret);
   if (!ret) {
@@ -1821,7 +1821,7 @@ do_btrfs_balance_status (const char *path)
   if (!lines)
     return NULL;

-  nlines = count_strings (lines);
+  nlines = guestfs_int_count_strings (lines);

   ret = calloc (1, sizeof *ret);
   if (ret == NULL) {
@@ -1938,7 +1938,7 @@ do_btrfs_scrub_status (const char *path)
   if (!lines)
     return NULL;

-  if (count_strings (lines) < 2) {
+  if (guestfs_int_count_strings (lines) < 2) {
     reply_with_error ("truncated output from 'btrfs scrub status
-R' command");
     return NULL;
   }
@@ -2124,7 +2124,7 @@ int
 do_btrfs_image (char *const *sources, const char *image,
 		int compresslevel)
 {
-  const size_t nr_sources =  count_strings (sources);
+  const size_t nr_sources =  guestfs_int_count_strings (sources);
   const size_t MAX_ARGS = 64 + nr_sources;
   const char *argv[MAX_ARGS];
   size_t i = 0, j;
@@ -2229,7 +2229,7 @@ do_btrfs_filesystem_show (const char *device)
   if (!lines)
     return NULL;

-  if (count_strings (lines) < 3) {
+  if (guestfs_int_count_strings (lines) < 3) {
     reply_with_error ("truncated output from 'btrfs filesystem
show' command");
     return NULL;
   }
diff --git a/daemon/cleanups.c b/daemon/cleanups.c
index c73b9e492..b4767178a 100644
--- a/daemon/cleanups.c
+++ b/daemon/cleanups.c
@@ -36,12 +36,6 @@ cleanup_aug_close (void *ptr)
 }

 void
-cleanup_free_string_list (void *ptr)
-{
-  free_strings (* (char ***) ptr);
-}
-
-void
 cleanup_free_stringsbuf (void *ptr)
 {
   free_stringsbuf ((struct stringsbuf *) ptr);
diff --git a/daemon/daemon.h b/daemon/daemon.h
index 746af22b9..2d296a6c2 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -31,6 +31,7 @@
 #include "guestfs_protocol.h"

 #include "cleanups.h"
+#include "utils.h"

 #include "guestfs-internal-all.h"

@@ -83,17 +84,13 @@ extern char *mountable_to_string (const mountable_t
*mountable);
  * Don't call them directly.
  */
 extern void cleanup_aug_close (void *ptr);
-extern void cleanup_free_string_list (void *ptr);
 extern void cleanup_free_stringsbuf (void *ptr);

 #ifdef HAVE_ATTRIBUTE_CLEANUP
 #define CLEANUP_AUG_CLOSE __attribute__((cleanup(cleanup_aug_close)))
-#define CLEANUP_FREE_STRING_LIST                        \
-  __attribute__((cleanup(cleanup_free_string_list)))
 #define CLEANUP_FREE_STRINGSBUF
__attribute__((cleanup(cleanup_free_stringsbuf)))
 #else
 #define CLEANUP_AUG_CLOSE
-#define CLEANUP_FREE_STRING_LIST
 #define CLEANUP_FREE_STRINGSBUF
 #endif

@@ -130,21 +127,12 @@ extern int end_stringsbuf (struct stringsbuf *sb);
 extern char **take_stringsbuf (struct stringsbuf *sb);
 extern void free_stringsbuf (struct stringsbuf *sb);

-extern size_t count_strings (char *const *argv);
 extern void sort_strings (char **argv, size_t len);
-extern void free_strings (char **argv);
 extern void free_stringslen (char **argv, size_t len);

 extern void sort_device_names (char **argv, size_t len);
 extern int compare_device_names (const char *a, const char *b);

-/* Concatenate strings, optionally with a separator string between
- * each.  On error, these return NULL but do NOT call reply_with_* nor
- * free anything.
- */
-extern char *concat_strings (char *const *argv);
-extern char *join_strings (const char *separator, char *const *argv);
-
 extern struct stringsbuf split_lines_sb (char *str);
 extern char **split_lines (char *str);

diff --git a/daemon/debug.c b/daemon/debug.c
index b18d87c26..e2d43a7ca 100644
--- a/daemon/debug.c
+++ b/daemon/debug.c
@@ -444,7 +444,7 @@ debug_ldd (const char *subcmd, size_t argc, char *const
*const argv)
 static char *
 debug_ls (const char *subcmd, size_t argc, char *const *const argv)
 {
-  const size_t len = count_strings (argv);
+  const size_t len = guestfs_int_count_strings (argv);
   CLEANUP_FREE const char **cargv = NULL;
   size_t i;
   int r;
@@ -477,7 +477,7 @@ debug_ls (const char *subcmd, size_t argc, char *const
*const argv)
 static char *
 debug_ll (const char *subcmd, size_t argc, char *const *const argv)
 {
-  const size_t len = count_strings (argv);
+  const size_t len = guestfs_int_count_strings (argv);
   CLEANUP_FREE const char **cargv = NULL;
   size_t i;
   int r;
diff --git a/daemon/echo-daemon.c b/daemon/echo-daemon.c
index d566a9bb2..15429f072 100644
--- a/daemon/echo-daemon.c
+++ b/daemon/echo-daemon.c
@@ -28,7 +28,7 @@ do_echo_daemon (char *const *argv)
 {
   char *out;

-  out = join_strings (" ", argv);
+  out = guestfs_int_join_strings (" ", argv);
   if (out == NULL) {
     reply_with_perror ("malloc");
     return NULL;
diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
index db2bb702f..b3f40628b 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -583,16 +583,6 @@ take_stringsbuf (struct stringsbuf *sb)
   return ret;
 }

-size_t
-count_strings (char *const *argv)
-{
-  size_t argc;
-
-  for (argc = 0; argv[argc] != NULL; ++argc)
-    ;
-  return argc;
-}
-
 /**
  * Returns true if C<v> is a power of 2.
  *
@@ -620,19 +610,6 @@ sort_strings (char **argv, size_t len)
 }

 void
-free_strings (char **argv)
-{
-  size_t argc;
-
-  if (!argv)
-    return;
-
-  for (argc = 0; argv[argc] != NULL; ++argc)
-    free (argv[argc]);
-  free (argv);
-}
-
-void
 free_stringslen (char **argv, size_t len)
 {
   size_t i;
@@ -720,47 +697,6 @@ sort_device_names (char **argv, size_t len)
   qsort (argv, len, sizeof (char *), compare_device_names_vp);
 }

-char *
-concat_strings (char *const *argv)
-{
-  return join_strings ("", argv);
-}
-
-char *
-join_strings (const char *separator, char *const *argv)
-{
-  size_t i, len, seplen, rlen;
-  char *r;
-
-  seplen = strlen (separator);
-
-  len = 0;
-  for (i = 0; argv[i] != NULL; ++i) {
-    if (i > 0)
-      len += seplen;
-    len += strlen (argv[i]);
-  }
-  len++; /* for final \0 */
-
-  r = malloc (len);
-  if (r == NULL)
-    return NULL;
-
-  rlen = 0;
-  for (i = 0; argv[i] != NULL; ++i) {
-    if (i > 0) {
-      memcpy (&r[rlen], separator, seplen);
-      rlen += seplen;
-    }
-    len = strlen (argv[i]);
-    memcpy (&r[rlen], argv[i], len);
-    rlen += len;
-  }
-  r[rlen] = '\0';
-
-  return r;
-}
-
 /**
  * Split an output string into a NULL-terminated list of lines,
  * wrapped into a stringsbuf.
diff --git a/daemon/ldm.c b/daemon/ldm.c
index 7753b0d82..75418e8d3 100644
--- a/daemon/ldm.c
+++ b/daemon/ldm.c
@@ -320,7 +320,7 @@ do_ldmtool_scan_devices (char * const * devices)
   int r;
   CLEANUP_FREE char *out = NULL, *err = NULL;

-  nr_devices = count_strings (devices);
+  nr_devices = guestfs_int_count_strings (devices);
   argv = malloc ((3 + nr_devices) * sizeof (char *));
   if (argv == NULL) {
     reply_with_perror ("malloc");
diff --git a/daemon/lvm.c b/daemon/lvm.c
index 6c57046ff..5d12b009f 100644
--- a/daemon/lvm.c
+++ b/daemon/lvm.c
@@ -337,7 +337,7 @@ do_vgcreate (const char *volgroup, char *const *physvols)
   CLEANUP_FREE char *err = NULL;
   CLEANUP_FREE const char **argv = NULL;

-  argc = count_strings (physvols) + 3;
+  argc = guestfs_int_count_strings (physvols) + 3;
   argv = malloc (sizeof (char *) * (argc + 1));
   if (argv == NULL) {
     reply_with_perror ("malloc");
@@ -643,7 +643,7 @@ do_vg_activate (int activate, char *const *volgroups)
   CLEANUP_FREE char *err = NULL;
   CLEANUP_FREE const char **argv = NULL;

-  argc = count_strings (volgroups) + 4;
+  argc = guestfs_int_count_strings (volgroups) + 4;
   argv = malloc (sizeof (char *) * (argc+1));
   if (argv == NULL) {
     reply_with_perror ("malloc");
diff --git a/daemon/md.c b/daemon/md.c
index 3f31529e2..64d98fae5 100644
--- a/daemon/md.c
+++ b/daemon/md.c
@@ -97,7 +97,8 @@ do_md_create (const char *name, char *const *devices,
     }
   }
   else
-    nrdevices = count_strings (devices) + count_bits (umissingbitmap);
+    nrdevices +      guestfs_int_count_strings (devices) + count_bits
(umissingbitmap);

   if (optargs_bitmask & GUESTFS_MD_CREATE_LEVEL_BITMASK) {
     if (STRNEQ (level, "linear") && STRNEQ (level,
"raid0") &&
@@ -124,10 +125,11 @@ do_md_create (const char *name, char *const *devices,
   }

   /* Check invariant. */
-  if (count_strings (devices) + count_bits (umissingbitmap) !+  if
(guestfs_int_count_strings (devices) + count_bits (umissingbitmap) !
(size_t) (nrdevices + spare)) {
     reply_with_error ("devices (%zu) + bits set in missingbitmap (%zu) is
not equal to nrdevices (%d) + spare (%d)",
-                      count_strings (devices), count_bits (umissingbitmap),
+                      guestfs_int_count_strings (devices),
+                      count_bits (umissingbitmap),
                       nrdevices, spare);
     return -1;
   }
diff --git a/daemon/stat.c b/daemon/stat.c
index 73f19226b..a1cd49245 100644
--- a/daemon/stat.c
+++ b/daemon/stat.c
@@ -127,7 +127,7 @@ do_internal_lstatnslist (const char *path, char *const
*names)
   guestfs_int_statns_list *ret;
   size_t i, nr_names;

-  nr_names = count_strings (names);
+  nr_names = guestfs_int_count_strings (names);

   ret = malloc (sizeof *ret);
   if (!ret) {
diff --git a/generator/daemon.ml b/generator/daemon.ml
index 0300dc54b..2ae462864 100644
--- a/generator/daemon.ml
+++ b/generator/daemon.ml
@@ -397,7 +397,7 @@ let generate_daemon_stubs actions ()          | RStringList
(RPlainString, n)
         | RHashtable (RPlainString, RPlainString, n) ->
             pr "  struct guestfs_%s_ret ret;\n" name;
-            pr "  ret.%s.%s_len = count_strings (r);\n" n n;
+            pr "  ret.%s.%s_len = guestfs_int_count_strings (r);\n" n
n;
             pr "  ret.%s.%s_val = r;\n" n n;
             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *)
&ret);\n"
               name
@@ -413,7 +413,7 @@ let generate_daemon_stubs actions ()              pr "
free (r[i]);\n";
             pr "    r[i] = rr;\n";
             pr "  }\n";
-            pr "  ret.%s.%s_len = count_strings (r);\n" n n;
+            pr "  ret.%s.%s_len = guestfs_int_count_strings (r);\n" n
n;
             pr "  ret.%s.%s_val = r;\n" n n;
             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *)
&ret);\n"
               name
@@ -428,7 +428,7 @@ let generate_daemon_stubs actions ()              pr "
free (r[i]);\n";
             pr "    r[i] = rr;\n";
             pr "  }\n";
-            pr "  ret.%s.%s_len = count_strings (r);\n" n n;
+            pr "  ret.%s.%s_len = guestfs_int_count_strings (r);\n" n
n;
             pr "  ret.%s.%s_val = r;\n" n n;
             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *)
&ret);\n"
               name
@@ -443,7 +443,7 @@ let generate_daemon_stubs actions ()              pr "
free (r[i+1]);\n";
             pr "    r[i+1] = rr;\n";
             pr "  }\n";
-            pr "  ret.%s.%s_len = count_strings (r);\n" n n;
+            pr "  ret.%s.%s_len = guestfs_int_count_strings (r);\n" n
n;
             pr "  ret.%s.%s_val = r;\n" n n;
             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *)
&ret);\n"
               name
--
2.13.0