]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Add option not to report errors in virGetUserEnt
authorPeter Krempa <pkrempa@redhat.com>
Fri, 17 Jun 2016 11:54:52 +0000 (13:54 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 20 Jun 2016 14:51:10 +0000 (16:51 +0200)
In some cases it will be necessary to ignore errors reported from this
function. This allows suppressing them to avoid spamming logs.

src/util/virutil.c

index ff58054f5c41ace199c2071767b140a42ba0faa5..392091b366470bc9aef690e9a2dc9965ea8f7c05 100644 (file)
@@ -755,9 +755,10 @@ virGetUserDirectory(void)
 
 #ifdef HAVE_GETPWUID_R
 /* Look up fields from the user database for the given user.  On
- * error, set errno, report the error, and return -1.  */
+ * error, set errno, report the error if not instructed otherwise via @quiet,
+ * and return -1.  */
 static int
-virGetUserEnt(uid_t uid, char **name, gid_t *group, char **dir, char **shell)
+virGetUserEnt(uid_t uid, char **name, gid_t *group, char **dir, char **shell, bool quiet)
 {
     char *strbuf;
     struct passwd pwbuf;
@@ -792,12 +793,19 @@ virGetUserEnt(uid_t uid, char **name, gid_t *group, char **dir, char **shell)
         if (VIR_RESIZE_N(strbuf, strbuflen, strbuflen, strbuflen) < 0)
             goto cleanup;
     }
+
     if (rc != 0) {
+        if (quiet)
+            goto cleanup;
+
         virReportSystemError(rc,
                              _("Failed to find user record for uid '%u'"),
                              (unsigned int) uid);
         goto cleanup;
     } else if (pw == NULL) {
+        if (quiet)
+            goto cleanup;
+
         virReportError(VIR_ERR_SYSTEM_ERROR,
                        _("Failed to find user record for uid '%u'"),
                        (unsigned int) uid);
@@ -882,7 +890,7 @@ char *
 virGetUserDirectoryByUID(uid_t uid)
 {
     char *ret;
-    virGetUserEnt(uid, NULL, NULL, &ret, NULL);
+    virGetUserEnt(uid, NULL, NULL, &ret, NULL, false);
     return ret;
 }
 
@@ -890,7 +898,7 @@ virGetUserDirectoryByUID(uid_t uid)
 char *virGetUserShell(uid_t uid)
 {
     char *ret;
-    virGetUserEnt(uid, NULL, NULL, NULL, &ret);
+    virGetUserEnt(uid, NULL, NULL, NULL, &ret, false);
     return ret;
 }
 
@@ -940,7 +948,7 @@ char *virGetUserRuntimeDirectory(void)
 char *virGetUserName(uid_t uid)
 {
     char *ret;
-    virGetUserEnt(uid, &ret, NULL, NULL, NULL);
+    virGetUserEnt(uid, &ret, NULL, NULL, NULL, false);
     return ret;
 }
 
@@ -1126,7 +1134,7 @@ virGetGroupList(uid_t uid, gid_t gid, gid_t **list)
     if (uid == (uid_t)-1)
         return 0;
 
-    if (virGetUserEnt(uid, &user, &primary, NULL, NULL) < 0)
+    if (virGetUserEnt(uid, &user, &primary, NULL, NULL, false) < 0)
         return -1;
 
     ret = mgetgroups(user, primary, list);