[libvirt PATCH 07/19] commandhelper: Factor out parseArguments

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx>
---
 tests/commandhelper.c | 83 +++++++++++++++++++++++++++++--------------
 1 file changed, 57 insertions(+), 26 deletions(-)

diff --git a/tests/commandhelper.c b/tests/commandhelper.c
index a77bee931f..c44322502f 100644
--- a/tests/commandhelper.c
+++ b/tests/commandhelper.c
@@ -35,6 +35,51 @@ extern char **environ;
 
 # define VIR_FROM_THIS VIR_FROM_NONE
 
+struct Arguments {
+    int readfds[3];
+    int numreadfds;
+    bool daemonize_check;
+    bool close_stdin;
+};
+
+static struct Arguments *parseArguments(int argc, char** argv)
+{
+    struct Arguments* args = NULL;
+    int ret = -1;
+    size_t i;
+
+    if (!(args = calloc(1, sizeof(*args))))
+        goto cleanup;
+
+    args->numreadfds = 1;
+    args->readfds[0] = STDIN_FILENO;
+
+    for (i = 1; i < argc; i++) {
+        if (STREQ(argv[i - 1], "--readfd")) {
+            char c;
+
+            if (1 != sscanf(argv[i], "%u%c",
+                            &args->readfds[args->numreadfds++], &c)) {
+                printf("Could not parse fd %s\n", argv[i]);
+                goto cleanup;
+            }
+        } else if (STREQ(argv[i], "--check-daemonize")) {
+            args->daemonize_check = true;
+        } else if (STREQ(argv[i], "--close-stdin")) {
+            args->close_stdin = true;
+        }
+    }
+
+    ret = 0;
+
+ cleanup:
+    if (ret == 0)
+        return args;
+
+    free(args);
+    return NULL;
+}
+
 static int envsort(const void *a, const void *b)
 {
     const char *const*astrptr = a;
@@ -43,39 +88,23 @@ static int envsort(const void *a, const void *b)
 }
 
 int main(int argc, char **argv) {
+    struct Arguments *args = parseArguments(argc, argv);
     size_t i, n;
     int open_max;
     char **newenv = NULL;
     char *cwd;
     FILE *log = fopen(abs_builddir "/commandhelper.log", "w");
     int ret = EXIT_FAILURE;
-    int readfds[3] = { STDIN_FILENO, };
-    int numreadfds = 1;
     struct pollfd fds[3];
     char *buffers[3] = {NULL, NULL, NULL};
     size_t buflen[3] = {0, 0, 0};
-    char c;
-    bool daemonize_check = false;
-    bool close_stdin = false;
     size_t daemonize_retries = 3;
     char buf[1024];
     ssize_t got;
 
-    if (!log)
+    if (!log || !args)
         goto cleanup;
 
-    for (i = 1; i < argc; i++) {
-        if (STREQ(argv[i - 1], "--readfd") &&
-            sscanf(argv[i], "%u%c", &readfds[numreadfds++], &c) != 1) {
-            printf("Could not parse fd %s\n", argv[i]);
-            goto cleanup;
-        } else if (STREQ(argv[i], "--check-daemonize")) {
-            daemonize_check = true;
-        } else if (STREQ(argv[i], "--close-stdin")) {
-            close_stdin = true;
-        }
-    }
-
     for (i = 1; i < argc; i++) {
         fprintf(log, "ARG:%s\n", argv[i]);
     }
@@ -116,7 +145,7 @@ int main(int argc, char **argv) {
     while (true) {
         bool daemonized = getpgrp() != getppid();
 
-        if (daemonize_check && !daemonized && daemonize_retries-- > 0) {
+        if (args->daemonize_check && !daemonized && daemonize_retries-- > 0) {
             usleep(100*1000);
             continue;
         }
@@ -144,7 +173,7 @@ int main(int argc, char **argv) {
 
     fprintf(log, "UMASK:%04o\n", umask(0));
 
-    if (close_stdin) {
+    if (args->close_stdin) {
         if (freopen("/dev/null", "r", stdin) != stdin)
             goto cleanup;
         usleep(100*1000);
@@ -155,8 +184,8 @@ int main(int argc, char **argv) {
     fprintf(stderr, "BEGIN STDERR\n");
     fflush(stderr);
 
-    for (i = 0; i < numreadfds; i++) {
-        fds[i].fd = readfds[i];
+    for (i = 0; i < args->numreadfds; i++) {
+        fds[i].fd = args->readfds[i];
         fds[i].events = POLLIN;
         fds[i].revents = 0;
     }
@@ -164,12 +193,12 @@ int main(int argc, char **argv) {
     for (;;) {
         unsigned ctr = 0;
 
-        if (poll(fds, numreadfds, -1) < 0) {
+        if (poll(fds, args->numreadfds, -1) < 0) {
             printf("poll failed: %s\n", strerror(errno));
             goto cleanup;
         }
 
-        for (i = 0; i < numreadfds; i++) {
+        for (i = 0; i < args->numreadfds; i++) {
             short revents = POLLIN | POLLHUP | POLLERR;
 
 # ifdef __APPLE__
@@ -200,7 +229,7 @@ int main(int argc, char **argv) {
                 }
             }
         }
-        for (i = 0; i < numreadfds; i++) {
+        for (i = 0; i < args->numreadfds; i++) {
             if (fds[i].events) {
                 ctr++;
                 break;
@@ -210,7 +239,7 @@ int main(int argc, char **argv) {
             break;
     }
 
-    for (i = 0; i < numreadfds; i++) {
+    for (i = 0; i < args->numreadfds; i++) {
         if (fwrite(buffers[i], 1, buflen[i], stdout) != buflen[i])
             goto cleanup;
         if (fwrite(buffers[i], 1, buflen[i], stderr) != buflen[i])
@@ -227,6 +256,8 @@ int main(int argc, char **argv) {
  cleanup:
     for (i = 0; i < G_N_ELEMENTS(buffers); i++)
         free(buffers[i]);
+    if (args)
+        free(args);
     if (newenv)
         free(newenv);
     if (log)
-- 
2.26.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux