Skip to contentSkip to conversation

bridge helper: includedir conf arg

PATCH Superseded Doug Goldstein 4 participants 18 messages Mar 2, 2013 – Mar 18, 2013 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

v2 cover bridge helper: includedir conf arg

The goal is to support an 'includedir' to include all files within a directory specified in the bridge.conf file. The rationale is to allow libvirt to be able to configure interfaces to for use by unprivileged users by just simply generating a new configuration file to the directory.

Change from v1:
- Reversed patch order to make the series clearer
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Doug Goldstein (2):
bridge helper: unified error cleanup for parse_acl_file bridge helper: support conf dirs

 qemu-bridge-helper.c | 75 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 66 insertions(+), 9 deletions(-)
Signature · 1 line
1.7.12.4
#2 lore raw

PATCH v2 1/2 bridge helper: unified error cleanup for parse_acl_file

Handle errors and cleanup from the error in a unified place for parse_acl_file().

CC Anthony Liguori <[email protected]>
CC Richa Marwaha <[email protected]>
CC Corey Bryant <[email protected]>

TO: [email protected]

---
 qemu-bridge-helper.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
qemu-bridge-helper.c +11 -9
index 287bfd5..ee67740 100644
@@ -74,11 +74,12 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 {
     FILE *f;
     char line[4096];
+    int ret = -EINVAL;
     ACLRule *acl_rule;
 
     f = fopen(filename, "r");
     if (f == NULL) {
-        return -1;
+        return -errno;
     }
 
     while (fgets(line, sizeof(line), f) != NULL) {
@@ -102,9 +103,8 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 
         if (arg == NULL) {
             fprintf(stderr, "Invalid config line:\n  %s\n", line);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;
         }
 
         *arg = 0;
@@ -142,15 +142,17 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
             parse_acl_file(arg, acl_list);
         } else {
             fprintf(stderr, "Unknown command `%s'\n", cmd);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;
         }
     }
 
+    ret = 0;
+
+failure:
     fclose(f);
 
-    return 0;
+    return ret;
 }
 
 static bool has_vnet_hdr(int fd)
@@ -272,7 +274,7 @@ int main(int argc, char **argv)
 
     /* parse default acl file */
     QSIMPLEQ_INIT(&acl_list);
-    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) == -1) {
+    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) < 0) {
         fprintf(stderr, "failed to parse default acl file `%s'\n",
                 DEFAULT_ACL_FILE);
         ret = EXIT_FAILURE;
Signature · 1 line
1.7.12.4
#3 lore raw

Re: [Qemu-devel] [PATCHv2 1/2] bridge helper: unified error cleanup for parse_acl_file

Quoted 31 lines · Doug Goldstein

Handle errors and cleanup from the error in a unified place for parse_acl_file().

CC Anthony Liguori <[email protected]>
CC Richa Marwaha <[email protected]>
CC Corey Bryant <[email protected]>

TO: [email protected]

---
  qemu-bridge-helper.c | 20 +++++++++++---------
  1 file changed, 11 insertions(+), 9 deletions(-)
qemu-bridge-helper.c +2 -1
index 287bfd5..ee67740 100644
@@ -74,11 +74,12 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
  {
      FILE *f;
      char line[4096];
+    int ret = -EINVAL;
      ACLRule *acl_rule;
 
      f = fopen(filename, "r");
      if (f == NULL) {
-        return -1;
+        return -errno;
      }
 
      while (fgets(line, sizeof(line), f) != NULL) {

qemu-bridge-helper.c static int parse_acl_file(const char *filename, ACLList *acl_list) · line 107

Quoted 8 lines
qemu-bridge-helper.c +2 -3
@@ -102,9 +103,8 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 
          if (arg == NULL) {
              fprintf(stderr, "Invalid config line:\n  %s\n", line);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;

I would stick with setting errno here rather than ret..

qemu-bridge-helper.c static int parse_acl_file(const char *filename, ACLList *acl_list) · line 146

Quoted 12 lines
qemu-bridge-helper.c +2 -3
          }
 
          *arg = 0;
@@ -142,15 +142,17 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
              parse_acl_file(arg, acl_list);
          } else {
              fprintf(stderr, "Unknown command `%s'\n", cmd);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;

And do the same here..

Quoted 14 lines
qemu-bridge-helper.c +4 -1
          }
      }
 
+    ret = 0;
+
+failure:
      fclose(f);
 
-    return 0;
+    return ret;
  }
 
  static bool has_vnet_hdr(int fd)

qemu-bridge-helper.c int main(int argc, char **argv) · line 279

Quoted 7 lines
qemu-bridge-helper.c +1 -1
@@ -272,7 +274,7 @@ int main(int argc, char **argv)
 
      /* parse default acl file */
      QSIMPLEQ_INIT(&acl_list);
-    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) == -1) {
+    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) < 0) {
          fprintf(stderr, "failed to parse default acl file `%s'\n",
                  DEFAULT_ACL_FILE);

.. and then you can append strerror(errno) to this message, which I admit should have been here before you touched this code. This will keep this error path consistent with many of the others in this file.

Quoted 2 lines
qemu-bridge-helper.c +0 -0
          ret = EXIT_FAILURE;
Signature · 2 lines
Regards,
Corey Bryant
#4 lore raw

Re: [Qemu-devel] [PATCHv2 1/2] bridge helper: unified error cleanup for parse_acl_file

Quoted 83 lines · Corey Bryant
Quoted 33 lines · Doug Goldstein

Handle errors and cleanup from the error in a unified place for parse_acl_file().

CC Anthony Liguori <[email protected]>
CC Richa Marwaha <[email protected]>
CC Corey Bryant <[email protected]>

TO: [email protected]

---
  qemu-bridge-helper.c | 20 +++++++++++---------
  1 file changed, 11 insertions(+), 9 deletions(-)
qemu-bridge-helper.c +2 -1
index 287bfd5..ee67740 100644
@@ -74,11 +74,12 @@ static int parse_acl_file(const char *filename,
 ACLList *acl_list)
  {
      FILE *f;
      char line[4096];
+    int ret = -EINVAL;
      ACLRule *acl_rule;
 
      f = fopen(filename, "r");
      if (f == NULL) {
-        return -1;
+        return -errno;
      }
 
      while (fgets(line, sizeof(line), f) != NULL) {

qemu-bridge-helper.c static int parse_acl_file(const char *filename, · line 108

Quoted 9 lines
qemu-bridge-helper.c +2 -3
@@ -102,9 +103,8 @@ static int parse_acl_file(const char *filename,
 ACLList *acl_list)
 
          if (arg == NULL) {
              fprintf(stderr, "Invalid config line:\n  %s\n", line);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;

I would stick with setting errno here rather than ret..

qemu-bridge-helper.c static int parse_acl_file(const char *filename, · line 147

Quoted 13 lines
qemu-bridge-helper.c +0 -0
          }
 
          *arg = 0;
qemu-bridge-helper.c +2 -3
@@ -142,15 +142,17 @@ static int parse_acl_file(const char *filename,
 ACLList *acl_list)
              parse_acl_file(arg, acl_list);
          } else {
              fprintf(stderr, "Unknown command `%s'\n", cmd);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;

And do the same here..

Quoted 15 lines
qemu-bridge-helper.c +4 -1
          }
      }
 
+    ret = 0;
+
+failure:
      fclose(f);
 
-    return 0;
+    return ret;
  }
 

static bool has_vnet_hdr(int fd)

qemu-bridge-helper.c +0 -0
@@ -272,7 +274,7 @@ int main(int argc, char **argv)
 
Quoted 11 lines

qemu-bridge-helper.c int main(int argc, char **argv) · line 279

Quoted 6 lines
qemu-bridge-helper.c +1 -1
      /* parse default acl file */
      QSIMPLEQ_INIT(&acl_list);
-    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) == -1) {
+    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) < 0) {
          fprintf(stderr, "failed to parse default acl file `%s'\n",
                  DEFAULT_ACL_FILE);

.. and then you can append strerror(errno) to this message, which I admit should have been here before you touched this code. This will keep this error path consistent with many of the others in this file.

Would you consider the return value then being passed on to strerror()? Seems like it'd be a little bit safer from the stand point of someone coming through and adding something new the the cleanup case or any other cases which calls a glibc function which then blows away errno.

Signature · 1 line
Doug Goldstein
#5 lore raw

Re: [Qemu-devel] [PATCHv2 1/2] bridge helper: unified error cleanup for parse_acl_file

Quoted 96 lines · Doug Goldstein
Quoted 83 lines · Corey Bryant
Quoted 33 lines · Doug Goldstein

Handle errors and cleanup from the error in a unified place for parse_acl_file().

CC Anthony Liguori <[email protected]>
CC Richa Marwaha <[email protected]>
CC Corey Bryant <[email protected]>

TO: [email protected]

---
   qemu-bridge-helper.c | 20 +++++++++++---------
   1 file changed, 11 insertions(+), 9 deletions(-)
qemu-bridge-helper.c +2 -1
index 287bfd5..ee67740 100644
@@ -74,11 +74,12 @@ static int parse_acl_file(const char *filename,
 ACLList *acl_list)
   {
       FILE *f;
       char line[4096];
+    int ret = -EINVAL;
       ACLRule *acl_rule;
 
       f = fopen(filename, "r");
       if (f == NULL) {
-        return -1;
+        return -errno;
       }
 
       while (fgets(line, sizeof(line), f) != NULL) {

qemu-bridge-helper.c static int parse_acl_file(const char *filename, · line 108

Quoted 9 lines
qemu-bridge-helper.c +2 -3
@@ -102,9 +103,8 @@ static int parse_acl_file(const char *filename,
 ACLList *acl_list)
 
           if (arg == NULL) {
               fprintf(stderr, "Invalid config line:\n  %s\n", line);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;

I would stick with setting errno here rather than ret..

qemu-bridge-helper.c static int parse_acl_file(const char *filename, · line 147

Quoted 13 lines
qemu-bridge-helper.c +0 -0
           }
 
           *arg = 0;
qemu-bridge-helper.c +2 -3
@@ -142,15 +142,17 @@ static int parse_acl_file(const char *filename,
 ACLList *acl_list)
               parse_acl_file(arg, acl_list);
           } else {
               fprintf(stderr, "Unknown command `%s'\n", cmd);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;

And do the same here..

Quoted 15 lines
qemu-bridge-helper.c +4 -1
           }
       }
 
+    ret = 0;
+
+failure:
       fclose(f);
 
-    return 0;
+    return ret;
   }
 

static bool has_vnet_hdr(int fd)

qemu-bridge-helper.c +0 -0
@@ -272,7 +274,7 @@ int main(int argc, char **argv)
 
Quoted 11 lines

qemu-bridge-helper.c int main(int argc, char **argv) · line 279

Quoted 6 lines
qemu-bridge-helper.c +1 -1
       /* parse default acl file */
       QSIMPLEQ_INIT(&acl_list);
-    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) == -1) {
+    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) < 0) {
           fprintf(stderr, "failed to parse default acl file `%s'\n",
                   DEFAULT_ACL_FILE);

.. and then you can append strerror(errno) to this message, which I admit should have been here before you touched this code. This will keep this error path consistent with many of the others in this file.

Quoted 6 lines

Would you consider the return value then being passed on to strerror()? Seems like it'd be a little bit safer from the stand point of someone coming through and adding something new the the cleanup case or any other cases which calls a glibc function which then blows away errno.

Yes that makes sense. Or you could save and restore errno at the beginning and end of the cleanup path. Either way.

Signature · 2 lines
Regards,
Corey Bryant
#6 lore raw

PATCH v2 2/2 bridge helper: support conf dirs

In reply to Doug Goldstein

Allow the bridge helper to take a config directory rather than having to specify every file in the directory manually via an include statement.

CC Anthony Liguori <[email protected]>
CC Richa Marwaha <[email protected]>
CC Corey Bryant <[email protected]>

TO: [email protected]

---
 qemu-bridge-helper.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
qemu-bridge-helper.c +55 -0
index ee67740..39d343c 100644
@@ -16,6 +16,7 @@
 #include "config-host.h"
 
 #include <stdio.h>
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -70,12 +71,27 @@ static void usage(void)
             "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
 }
 
+static int filter_bridge_conf_dir(const struct dirent *entry)
+{
+    ssize_t len = strlen(entry->d_name);
+
+    /* We only want files ending in .conf */
+    if (len > 5 &&
+            strcmp(".conf", &entry->d_name[len-5]) == 0)
+        return 1;
+
+    return 0;
+}
+
 static int parse_acl_file(const char *filename, ACLList *acl_list)
 {
     FILE *f;
     char line[4096];
     int ret = -EINVAL;
     ACLRule *acl_rule;
+    struct dirent **include_list = NULL;
+    int i, include_count = 0;
+    char *conf_file;
 
     f = fopen(filename, "r");
     if (f == NULL) {
@@ -137,6 +153,37 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
                 snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
             }
             QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
+        } else if (strcmp(cmd, "includedir") == 0) {
+            include_count = scandir(arg, &include_list,
+                                    filter_bridge_conf_dir, alphasort);
+            if (include_count < 0) {
+                ret = -errno;
+                fprintf(stderr, "Unable to retrieve conf files from '%s': %s\n",
+                        arg, strerror(errno));
+                goto failure;
+            }
+
+            for (i = 0; i < include_count; i++) {
+                if (asprintf(&conf_file, "%s/%s", arg,
+                             include_list[i]->d_name) < 0) {
+                    fprintf(stderr, "Failed to allocate memory for "
+                            "file path: %s/%s\n",
+                            arg, include_list[i]->d_name);
+                    ret = -ENOMEM;
+                    goto failure;
+                }
+
+                /* ignore errors like 'include' cmd */
+                parse_acl_file(conf_file, acl_list);
+
+                free(conf_file);
+                free(include_list[i]);
+                include_list[i] = NULL;
+            }
+            free(include_list);
+            include_list = NULL;
+            include_count = 0;
+
         } else if (strcmp(cmd, "include") == 0) {
             /* ignore errors */
             parse_acl_file(arg, acl_list);
@@ -152,6 +199,14 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 failure:
     fclose(f);
 
+    if (include_list) {
+        for (i = 0; i < include_count; i++) {
+            if (include_list[i])
+                free(include_list[i]);
+        }
+        free(include_list);
+    }
+
     return ret;
 }
 
Signature · 1 line
1.7.12.4
#7 lore raw

Re: [Qemu-devel] [PATCHv2 2/2] bridge helper: support conf dirs

Quoted 26 lines · Doug Goldstein

Allow the bridge helper to take a config directory rather than having to specify every file in the directory manually via an include statement.

CC Anthony Liguori <[email protected]>
CC Richa Marwaha <[email protected]>
CC Corey Bryant <[email protected]>

TO: [email protected]

---
  qemu-bridge-helper.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 55 insertions(+)
qemu-bridge-helper.c +1 -0
index ee67740..39d343c 100644
@@ -16,6 +16,7 @@
  #include "config-host.h"
 
  #include <stdio.h>
+#include <dirent.h>
  #include <errno.h>
  #include <fcntl.h>
  #include <unistd.h>
@@ -70,12 +71,27 @@ static void usage(void)
              "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");

qemu-bridge-helper.c static void usage(void) · line 81

Quoted 10 lines
qemu-bridge-helper.c +8 -0
  }
 
+static int filter_bridge_conf_dir(const struct dirent *entry)
+{
+    ssize_t len = strlen(entry->d_name);
+
+    /* We only want files ending in .conf */
+    if (len > 5 &&
+            strcmp(".conf", &entry->d_name[len-5]) == 0)
+        return 1;

QEMU prefers braces on single statement blocks. Check out the CODING_STYLE file. Also you'll want to run scripts/checkpatch.pl against your patches and make sure it's not flagging any issues. It'll catch things like this.

Quoted 55 lines
qemu-bridge-helper.c +38 -0
+
+    return 0;
+}
+
  static int parse_acl_file(const char *filename, ACLList *acl_list)
  {
      FILE *f;
      char line[4096];
      int ret = -EINVAL;
      ACLRule *acl_rule;
+    struct dirent **include_list = NULL;
+    int i, include_count = 0;
+    char *conf_file;
 
      f = fopen(filename, "r");
      if (f == NULL) {
@@ -137,6 +153,37 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
                  snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
              }
              QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
+        } else if (strcmp(cmd, "includedir") == 0) {
+            include_count = scandir(arg, &include_list,
+                                    filter_bridge_conf_dir, alphasort);
+            if (include_count < 0) {
+                ret = -errno;
+                fprintf(stderr, "Unable to retrieve conf files from '%s': %s\n",
+                        arg, strerror(errno));
+                goto failure;
+            }
+
+            for (i = 0; i < include_count; i++) {
+                if (asprintf(&conf_file, "%s/%s", arg,
+                             include_list[i]->d_name) < 0) {
+                    fprintf(stderr, "Failed to allocate memory for "
+                            "file path: %s/%s\n",
+                            arg, include_list[i]->d_name);
+                    ret = -ENOMEM;
+                    goto failure;
+                }
+
+                /* ignore errors like 'include' cmd */
+                parse_acl_file(conf_file, acl_list);
+
+                free(conf_file);
+                free(include_list[i]);
+                include_list[i] = NULL;
+            }
+            free(include_list);
+            include_list = NULL;
+            include_count = 0;
+
          } else if (strcmp(cmd, "include") == 0) {
              /* ignore errors */
              parse_acl_file(arg, acl_list);

qemu-bridge-helper.c static int parse_acl_file(const char *filename, ACLList *acl_list) · line 205

Quoted 7 lines
qemu-bridge-helper.c +4 -0
@@ -152,6 +199,14 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
  failure:
      fclose(f);
 
+    if (include_list) {
+        for (i = 0; i < include_count; i++) {
+            if (include_list[i])
+                free(include_list[i]);

Same comment here.

Quoted 7 lines
qemu-bridge-helper.c +4 -0
+        }
+        free(include_list);
+    }
+
      return ret;
  }
 
Signature · 2 lines
Regards,
Corey Bryant
#8 lore raw

Re: [Qemu-devel] [PATCHv2 2/2] bridge helper: support conf dirs

In reply to Doug Goldstein

qemu-bridge-helper.c static int parse_acl_file(const char *filename, ACLList *acl_list) · line 205

Quoted 8 lines · Doug Goldstein
qemu-bridge-helper.c +4 -0
@@ -152,6 +199,14 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 failure:
     fclose(f);
 
+    if (include_list) {
+        for (i = 0; i < include_count; i++) {
+            if (include_list[i])
+                free(include_list[i]);

free(NULL) is a nop so the if isn't necessary - you can free(include_list[i]) unconditionally.

#9 lore raw

v3 cover bridge helper: includedir conf arg

In reply to Doug Goldstein

The goal is to support an 'includedir' to include all files within a directory specified in the bridge.conf file. The rationale is to allow libvirt to be able to configure interfaces to for use by unprivileged users by just simply generating a new configuration file to the directory.

Change from v2:
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Change from v1:
- Reversed patch order to make the series clearer
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Doug Goldstein (2):
bridge helper: unified error cleanup for parse_acl_file bridge helper: support conf dirs

 qemu-bridge-helper.c | 83 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 71 insertions(+), 12 deletions(-)
Signature · 1 line
1.7.12.4
#10 lore raw

PATCH v3 1/2 bridge helper: unified error cleanup for parse_acl_file

Handle errors and cleanup from the error in a unified place for parse_acl_file().

CC Anthony Liguori <[email protected]>
CC Richa Marwaha <[email protected]>
CC Corey Bryant <[email protected]>

TO: [email protected]

---
 qemu-bridge-helper.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
qemu-bridge-helper.c +16 -12
index 287bfd5..95486e7 100644
@@ -74,11 +74,12 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 {
     FILE *f;
     char line[4096];
+    int ret = -EINVAL;
     ACLRule *acl_rule;
 
     f = fopen(filename, "r");
     if (f == NULL) {
-        return -1;
+        return -errno;
     }
 
     while (fgets(line, sizeof(line), f) != NULL) {
@@ -102,9 +103,8 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 
         if (arg == NULL) {
             fprintf(stderr, "Invalid config line:\n  %s\n", line);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;
         }
 
         *arg = 0;
@@ -142,15 +142,17 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
             parse_acl_file(arg, acl_list);
         } else {
             fprintf(stderr, "Unknown command `%s'\n", cmd);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;
         }
     }
 
+    ret = 0;
+
+failure:
     fclose(f);
 
-    return 0;
+    return ret;
 }
 
 static bool has_vnet_hdr(int fd)
@@ -238,7 +240,7 @@ int main(int argc, char **argv)
     ACLRule *acl_rule;
     ACLList acl_list;
     int access_allowed, access_denied;
-    int ret = EXIT_SUCCESS;
+    int ret;
 
 #ifdef CONFIG_LIBCAP
     /* if we're run from an suid binary, immediately drop privileges preserving
@@ -272,9 +274,10 @@ int main(int argc, char **argv)
 
     /* parse default acl file */
     QSIMPLEQ_INIT(&acl_list);
-    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) == -1) {
-        fprintf(stderr, "failed to parse default acl file `%s'\n",
-                DEFAULT_ACL_FILE);
+    ret = parse_acl_file(DEFAULT_ACL_FILE, &acl_list);
+    if (ret < 0) {
+        fprintf(stderr, "failed to parse default acl file `%s': %s\n",
+                DEFAULT_ACL_FILE, strerror(ret));
         ret = EXIT_FAILURE;
         goto cleanup;
     }
@@ -416,6 +419,7 @@ int main(int argc, char **argv)
     /* ... */
 
     /* profit! */
+    ret = EXIT_SUCCESS;
 
 cleanup:
 
Signature · 1 line
1.7.12.4
#11 lore raw

PATCH v3 2/2 bridge helper: support conf dirs

In reply to Doug Goldstein

Allow the bridge helper to take a config directory rather than having to specify every file in the directory manually via an include statement.

CC Anthony Liguori <[email protected]>
CC Richa Marwaha <[email protected]>
CC Corey Bryant <[email protected]>

TO: [email protected]

---
 qemu-bridge-helper.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
qemu-bridge-helper.c +55 -0
index 95486e7..cebfcf8 100644
@@ -16,6 +16,7 @@
 #include "config-host.h"
 
 #include <stdio.h>
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -70,12 +71,28 @@ static void usage(void)
             "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
 }
 
+static int filter_bridge_conf_dir(const struct dirent *entry)
+{
+    ssize_t len = strlen(entry->d_name);
+
+    /* We only want files ending in .conf */
+    if (len > 5 &&
+            strcmp(".conf", &entry->d_name[len-5]) == 0) {
+        return 1;
+    }
+
+    return 0;
+}
+
 static int parse_acl_file(const char *filename, ACLList *acl_list)
 {
     FILE *f;
     char line[4096];
     int ret = -EINVAL;
     ACLRule *acl_rule;
+    struct dirent **include_list = NULL;
+    int i, include_count = 0;
+    char *conf_file;
 
     f = fopen(filename, "r");
     if (f == NULL) {
@@ -137,6 +154,37 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
                 snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
             }
             QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
+        } else if (strcmp(cmd, "includedir") == 0) {
+            include_count = scandir(arg, &include_list,
+                                    filter_bridge_conf_dir, alphasort);
+            if (include_count < 0) {
+                ret = -errno;
+                fprintf(stderr, "Unable to retrieve conf files from '%s': %s\n",
+                        arg, strerror(errno));
+                goto failure;
+            }
+
+            for (i = 0; i < include_count; i++) {
+                if (asprintf(&conf_file, "%s/%s", arg,
+                             include_list[i]->d_name) < 0) {
+                    fprintf(stderr, "Failed to allocate memory for "
+                            "file path: %s/%s\n",
+                            arg, include_list[i]->d_name);
+                    ret = -ENOMEM;
+                    goto failure;
+                }
+
+                /* ignore errors like 'include' cmd */
+                parse_acl_file(conf_file, acl_list);
+
+                free(conf_file);
+                free(include_list[i]);
+                include_list[i] = NULL;
+            }
+            free(include_list);
+            include_list = NULL;
+            include_count = 0;
+
         } else if (strcmp(cmd, "include") == 0) {
             /* ignore errors */
             parse_acl_file(arg, acl_list);
@@ -152,6 +200,13 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 failure:
     fclose(f);
 
+    if (include_list) {
+        for (i = 0; i < include_count; i++) {
+            free(include_list[i]);
+        }
+        free(include_list);
+    }
+
     return ret;
 }
 
Signature · 1 line
1.7.12.4
#12 lore raw

Re: [Qemu-devel] [PATCHv3 2/2] bridge helper: support conf dirs

Quoted 59 lines · Doug Goldstein

Allow the bridge helper to take a config directory rather than having to specify every file in the directory manually via an include statement.

CC Anthony Liguori <[email protected]>
CC Richa Marwaha <[email protected]>
CC Corey Bryant <[email protected]>

TO: [email protected]

---
 qemu-bridge-helper.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
qemu-bridge-helper.c +19 -0
index 95486e7..cebfcf8 100644
@@ -16,6 +16,7 @@
 #include "config-host.h"
 
 #include <stdio.h>
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -70,12 +71,28 @@ static void usage(void)
             "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
 }
 
+static int filter_bridge_conf_dir(const struct dirent *entry)
+{
+    ssize_t len = strlen(entry->d_name);
+
+    /* We only want files ending in .conf */
+    if (len > 5 &&
+            strcmp(".conf", &entry->d_name[len-5]) == 0) {
+        return 1;
+    }
+
+    return 0;
+}
+
 static int parse_acl_file(const char *filename, ACLList *acl_list)
 {
     FILE *f;
     char line[4096];
     int ret = -EINVAL;
     ACLRule *acl_rule;
+    struct dirent **include_list = NULL;
+    int i, include_count = 0;
+    char *conf_file;
 
     f = fopen(filename, "r");
     if (f == NULL) {
@@ -137,6 +154,37 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
                 snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
             }
             QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
+        } else if (strcmp(cmd, "includedir") == 0) {
+            include_count = scandir(arg, &include_list,

qemu-bridge-helper.c static int parse_acl_file(const char *filename, ACLList *acl_list) · line 168

Quoted 10 lines
qemu-bridge-helper.c +10 -0
+                                    filter_bridge_conf_dir, alphasort);
+            if (include_count < 0) {
+                ret = -errno;
+                fprintf(stderr, "Unable to retrieve conf files from '%s': %s\n",
+                        arg, strerror(errno));
+                goto failure;
+            }
+
+            for (i = 0; i < include_count; i++) {
+                if (asprintf(&conf_file, "%s/%s", arg,

Please use g_strdup_printf() and g_free() instead. This will make the check go away too since it will not fail.

Quoted 23 lines
qemu-bridge-helper.c +19 -0
+                             include_list[i]->d_name) < 0) {
+                    fprintf(stderr, "Failed to allocate memory for "
+                            "file path: %s/%s\n",
+                            arg, include_list[i]->d_name);
+                    ret = -ENOMEM;
+                    goto failure;
+                }
+
+                /* ignore errors like 'include' cmd */
+                parse_acl_file(conf_file, acl_list);
+
+                free(conf_file);
+                free(include_list[i]);
+                include_list[i] = NULL;
+            }
+            free(include_list);
+            include_list = NULL;
+            include_count = 0;
+
         } else if (strcmp(cmd, "include") == 0) {
             /* ignore errors */
             parse_acl_file(arg, acl_list);

qemu-bridge-helper.c static int parse_acl_file(const char *filename, ACLList *acl_list) · line 203

Quoted 4 lines
qemu-bridge-helper.c +1 -0
@@ -152,6 +200,13 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 failure:
     fclose(f);
 
+    if (include_list) {

This check is somewhat redundant since the for loop below will also do nothing for cases where include_count is either zero (freed) or < 0 (failure).

Quoted 13 lines
qemu-bridge-helper.c +6 -1
+        for (i = 0; i < include_count; i++) {
+            free(include_list[i]);
+        }
+        free(include_list);
+    }
+
     return ret;
 }
 
--

1.7.12.4

Reviewed-by on the v3 series
#13 lore raw
In reply to Doug Goldstein
Quoted 14 lines · Doug Goldstein

The goal is to support an 'includedir' to include all files within a directory specified in the bridge.conf file. The rationale is to allow libvirt to be able to configure interfaces to for use by unprivileged users by just simply generating a new configuration file to the directory.

Change from v2:
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Change from v1:
- Reversed patch order to make the series clearer
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Quoted 11 lines

Doug Goldstein (2):
bridge helper: unified error cleanup for parse_acl_file bridge helper: support conf dirs

 qemu-bridge-helper.c | 83 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 71 insertions(+), 12 deletions(-)
Signature · 1 line
1.7.12.4
Reviewed-by on the v3 series
#14 lore raw
In reply to Doug Goldstein
Quoted 14 lines · Doug Goldstein

The goal is to support an 'includedir' to include all files within a directory specified in the bridge.conf file. The rationale is to allow libvirt to be able to configure interfaces to for use by unprivileged users by just simply generating a new configuration file to the directory.

Change from v2:
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Change from v1:
- Reversed patch order to make the series clearer
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Quoted 7 lines

Doug Goldstein (2):
bridge helper: unified error cleanup for parse_acl_file bridge helper: support conf dirs

  qemu-bridge-helper.c | 83 ++++++++++++++++++++++++++++++++++++++++++++--------
  1 file changed, 71 insertions(+), 12 deletions(-)
Signature · 2 lines
Regards,
Corey Bryant
#15 lore raw

v3 cover bridge helper: includedir conf arg

In reply to Doug Goldstein

The goal is to support an 'includedir' to include all files within a directory specified in the bridge.conf file. The rationale is to allow libvirt to be able to configure interfaces to for use by unprivileged users by just simply generating a new configuration file to the directory.

Change from v3:
- Integreated review changes from Blue Swirl

Change from v2:
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Change from v1:
- Reversed patch order to make the series clearer
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Doug Goldstein (2):
bridge helper: unified error cleanup for parse_acl_file bridge helper: support conf dirs

 qemu-bridge-helper.c | 75 +++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 63 insertions(+), 12 deletions(-)
Signature · 1 line
1.8.1.5
#16 lore raw

PATCH v3 1/2 bridge helper: unified error cleanup for parse_acl_file

Handle errors and cleanup from the error in a unified place for parse_acl_file().

TO: [email protected]

---
 qemu-bridge-helper.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
qemu-bridge-helper.c +16 -12
index 287bfd5..95486e7 100644
@@ -74,11 +74,12 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 {
     FILE *f;
     char line[4096];
+    int ret = -EINVAL;
     ACLRule *acl_rule;
 
     f = fopen(filename, "r");
     if (f == NULL) {
-        return -1;
+        return -errno;
     }
 
     while (fgets(line, sizeof(line), f) != NULL) {
@@ -102,9 +103,8 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 
         if (arg == NULL) {
             fprintf(stderr, "Invalid config line:\n  %s\n", line);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;
         }
 
         *arg = 0;
@@ -142,15 +142,17 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
             parse_acl_file(arg, acl_list);
         } else {
             fprintf(stderr, "Unknown command `%s'\n", cmd);
-            fclose(f);
-            errno = EINVAL;
-            return -1;
+            ret = -EINVAL;
+            goto failure;
         }
     }
 
+    ret = 0;
+
+failure:
     fclose(f);
 
-    return 0;
+    return ret;
 }
 
 static bool has_vnet_hdr(int fd)
@@ -238,7 +240,7 @@ int main(int argc, char **argv)
     ACLRule *acl_rule;
     ACLList acl_list;
     int access_allowed, access_denied;
-    int ret = EXIT_SUCCESS;
+    int ret;
 
 #ifdef CONFIG_LIBCAP
     /* if we're run from an suid binary, immediately drop privileges preserving
@@ -272,9 +274,10 @@ int main(int argc, char **argv)
 
     /* parse default acl file */
     QSIMPLEQ_INIT(&acl_list);
-    if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) == -1) {
-        fprintf(stderr, "failed to parse default acl file `%s'\n",
-                DEFAULT_ACL_FILE);
+    ret = parse_acl_file(DEFAULT_ACL_FILE, &acl_list);
+    if (ret < 0) {
+        fprintf(stderr, "failed to parse default acl file `%s': %s\n",
+                DEFAULT_ACL_FILE, strerror(ret));
         ret = EXIT_FAILURE;
         goto cleanup;
     }
@@ -416,6 +419,7 @@ int main(int argc, char **argv)
     /* ... */
 
     /* profit! */
+    ret = EXIT_SUCCESS;
 
 cleanup:
 
Signature · 1 line
1.8.1.5
#17 lore raw

PATCH v3 2/2 bridge helper: support conf dirs

In reply to Doug Goldstein

Allow the bridge helper to take a config directory rather than having to specify every file in the directory manually via an include statement.

TO: [email protected]

---
 qemu-bridge-helper.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
qemu-bridge-helper.c +47 -0
index 95486e7..b647848 100644
@@ -16,6 +16,7 @@
 #include "config-host.h"
 
 #include <stdio.h>
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -70,12 +71,28 @@ static void usage(void)
             "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
 }
 
+static int filter_bridge_conf_dir(const struct dirent *entry)
+{
+    ssize_t len = strlen(entry->d_name);
+
+    /* We only want files ending in .conf */
+    if (len > 5 &&
+            strcmp(".conf", &entry->d_name[len-5]) == 0) {
+        return 1;
+    }
+
+    return 0;
+}
+
 static int parse_acl_file(const char *filename, ACLList *acl_list)
 {
     FILE *f;
     char line[4096];
     int ret = -EINVAL;
     ACLRule *acl_rule;
+    struct dirent **include_list = NULL;
+    int i, include_count = 0;
+    char *conf_file;
 
     f = fopen(filename, "r");
     if (f == NULL) {
@@ -137,6 +154,31 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
                 snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
             }
             QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
+        } else if (strcmp(cmd, "includedir") == 0) {
+            include_count = scandir(arg, &include_list,
+                                    filter_bridge_conf_dir, alphasort);
+            if (include_count < 0) {
+                ret = -errno;
+                fprintf(stderr, "Unable to retrieve conf files from '%s': %s\n",
+                        arg, strerror(errno));
+                goto failure;
+            }
+
+            for (i = 0; i < include_count; i++) {
+                conf_file = g_strdup_printf("%s/%s", arg,
+                                            include_list[i]->d_name);
+
+                /* ignore errors like 'include' cmd */
+                parse_acl_file(conf_file, acl_list);
+
+                g_free(conf_file);
+                free(include_list[i]);
+                include_list[i] = NULL;
+            }
+            free(include_list);
+            include_list = NULL;
+            include_count = 0;
+
         } else if (strcmp(cmd, "include") == 0) {
             /* ignore errors */
             parse_acl_file(arg, acl_list);
@@ -152,6 +194,11 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 failure:
     fclose(f);
 
+    for (i = 0; i < include_count; i++) {
+        free(include_list[i]);
+    }
+    free(include_list);
+
     return ret;
 }
 
Signature · 1 line
1.8.1.5
Reviewed-by on the v3 series
#18 lore raw
In reply to Doug Goldstein
Quoted 17 lines · Doug Goldstein

The goal is to support an 'includedir' to include all files within a directory specified in the bridge.conf file. The rationale is to allow libvirt to be able to configure interfaces to for use by unprivileged users by just simply generating a new configuration file to the directory.

Change from v3:
- Integreated review changes from Blue Swirl

Change from v2:
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Change from v1:
- Reversed patch order to make the series clearer
- Integrated review changes from Corey Bryant
- Integrated review changes from Stefan Hajnoczi

Quoted 11 lines

Doug Goldstein (2):
bridge helper: unified error cleanup for parse_acl_file bridge helper: support conf dirs

 qemu-bridge-helper.c | 75 +++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 63 insertions(+), 12 deletions(-)
Signature · 1 line
1.8.1.5