[Libguestfs] [PATCH supermin 3/3] init: Refactor for-loop which waits for root device to show up.

Richard W.M. Jones rjones at redhat.com
Wed Apr 19 10:04:30 UTC 2017


On Wed, Apr 19, 2017 at 10:57:28AM +0100, Richard W.M. Jones wrote:
> +  struct timespec t;
> +#define NANOSLEEP(ns) do {                      \
> +    t.tv_sec = delay_ns / 1000000000;           \
> +    t.tv_nsec = delay_ns % 1000000000;          \
> +    nanosleep (&t, NULL);                       \
> +  } while(0)
> +

Defining 't' outside its context is a bit clumsy.  See attached
version of 3/3 which fixes this.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW
-------------- next part --------------
>From b5eb18caf63223d58910d1953dd3531022fbc984 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones at redhat.com>
Date: Tue, 18 Apr 2017 20:46:08 +0100
Subject: [PATCH] init: Refactor for-loop which waits for root device to show
 up.

---
 init/init.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/init/init.c b/init/init.c
index 473a5c5..ddfc437 100644
--- a/init/init.c
+++ b/init/init.c
@@ -101,13 +101,19 @@ main ()
   char *root, *path;
   size_t len;
   int dax = 0;
-  uint64_t delay_ns = 250000;
+  uint64_t delay_ns;
   int virtio_message = 0;
-  struct timespec t;
   int major, minor;
   char *p;
   const char *mount_options = "";
 
+#define NANOSLEEP(ns) do {                      \
+    struct timespec t;                          \
+    t.tv_sec = delay_ns / 1000000000;           \
+    t.tv_nsec = delay_ns % 1000000000;          \
+    nanosleep (&t, NULL);                       \
+  } while(0)
+
   mount_proc ();
 
   fprintf (stderr, "supermin: ext2 mini initrd starting up: "
@@ -184,7 +190,9 @@ main ()
 
   asprintf (&path, "/sys/block/%s/dev", root);
 
-  while (delay_ns <= MAX_ROOT_WAIT * UINT64_C(1000000000)) {
+  for (delay_ns = 250000;
+       delay_ns <= MAX_ROOT_WAIT * UINT64_C(1000000000);
+       delay_ns *= 2) {
     fp = fopen (path, "r");
     if (fp != NULL)
       break;
@@ -201,10 +209,7 @@ main ()
       }
     }
 
-    t.tv_sec = delay_ns / 1000000000;
-    t.tv_nsec = delay_ns % 1000000000;
-    nanosleep (&t, NULL);
-    delay_ns *= 2;
+    NANOSLEEP (delay_ns);
   }
 
   if (!quiet)
-- 
2.12.0



More information about the Libguestfs mailing list