[Pkg-libvirt-commits] [libguestfs] 22/29: daemon: Add -l / --listen flag.

Hilko Bengen bengen at moszumanska.debian.org
Sun Nov 1 17:14:18 UTC 2015


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag upstream/1.29.48
in repository libguestfs.

commit 64ff631127bd682e7311ae20328fdafb2ca1d4da
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Tue Jun 23 15:29:15 2015 +0100

    daemon: Add -l / --listen flag.
    
    This option, used for testing, causes the daemon to create the Unix
    domain socket (from guestfs_channel), listen on it, and accept a
    single connection.
---
 daemon/guestfsd.c   | 66 ++++++++++++++++++++++++++++++++++++++---------------
 daemon/guestfsd.pod |  9 ++++++++
 2 files changed, 57 insertions(+), 18 deletions(-)

diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
index 8285d27..4995a2b 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -139,15 +139,17 @@ usage (void)
 int
 main (int argc, char *argv[])
 {
-  static const char *options = "rtv?";
+  static const char *options = "lrtv?";
   static const struct option long_options[] = {
     { "help", 0, 0, '?' },
+    { "listen", 0, 0, 'l' },
     { "test", 0, 0, 't' },
     { "verbose", 0, 0, 'v' },
     { 0, 0, 0, 0 }
   };
   int c;
   char *cmdline;
+  int listen_mode = 0;
 
   ignore_value (chdir ("/"));
 
@@ -186,6 +188,10 @@ main (int argc, char *argv[])
     if (c == -1) break;
 
     switch (c) {
+    case 'l':
+      listen_mode = 1;
+      break;
+
       /* The -r flag is used when running standalone.  It changes
        * several aspects of the daemon.
        */
@@ -292,23 +298,47 @@ main (int argc, char *argv[])
   if (verbose)
     printf ("trying to open virtio-serial channel '%s'\n", channel);
 
-  int sock = open (channel, O_RDWR|O_CLOEXEC);
-  if (sock == -1) {
-    fprintf (stderr,
-             "\n"
-             "Failed to connect to virtio-serial channel.\n"
-             "\n"
-             "This is a fatal error and the appliance will now exit.\n"
-             "\n"
-             "Usually this error is caused by either QEMU or the appliance\n"
-             "kernel not supporting the vmchannel method that the\n"
-             "libguestfs library chose to use.  Please run\n"
-             "'libguestfs-test-tool' and provide the complete, unedited\n"
-             "output to the libguestfs developers, either in a bug report\n"
-             "or on the libguestfs redhat com mailing list.\n"
-             "\n");
-    perror (channel);
-    exit (EXIT_FAILURE);
+  int sock;
+  if (!listen_mode) {
+    sock = open (channel, O_RDWR|O_CLOEXEC);
+    if (sock == -1) {
+      fprintf (stderr,
+               "\n"
+               "Failed to connect to virtio-serial channel.\n"
+               "\n"
+               "This is a fatal error and the appliance will now exit.\n"
+               "\n"
+               "Usually this error is caused by either QEMU or the appliance\n"
+               "kernel not supporting the vmchannel method that the\n"
+               "libguestfs library chose to use.  Please run\n"
+               "'libguestfs-test-tool' and provide the complete, unedited\n"
+               "output to the libguestfs developers, either in a bug report\n"
+               "or on the libguestfs redhat com mailing list.\n"
+               "\n");
+      perror (channel);
+      exit (EXIT_FAILURE);
+    }
+  }
+  else {
+    struct sockaddr_un addr;
+
+    sock = socket (AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
+    if (sock == -1)
+      error (EXIT_FAILURE, errno, "socket");
+    addr.sun_family = AF_UNIX;
+    if (strlen (channel) > UNIX_PATH_MAX-1)
+      error (EXIT_FAILURE, 0, "%s: socket path is too long", channel);
+    strcpy (addr.sun_path, channel);
+
+    if (bind (sock, (struct sockaddr *) &addr, sizeof addr) == -1)
+      error (EXIT_FAILURE, errno, "bind: %s", channel);
+
+    if (listen (sock, 4) == -1)
+      error (EXIT_FAILURE, errno, "listen");
+
+    sock = accept4 (sock, NULL, NULL, SOCK_CLOEXEC);
+    if (sock == -1)
+      error (EXIT_FAILURE, errno, "accept");
   }
 
   /* If it's a serial-port like device then it probably has echoing
diff --git a/daemon/guestfsd.pod b/daemon/guestfsd.pod
index 1ed31a9..22c8003 100644
--- a/daemon/guestfsd.pod
+++ b/daemon/guestfsd.pod
@@ -50,6 +50,15 @@ removed.
 
 Display brief help.
 
+=item B<-l>
+
+=item B<--listen>
+
+Instead of opening the C<guestfs_channel> and thus expecting that it
+already exists, create the channel as a Unix domain socket, listen on
+it, and accept a single connection.  This is mainly used for testing
+the daemon.
+
 =item B<-r>
 
 Set the root filesystem to be F</> (instead of the default which is

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list