Blame SOURCES/0024-Make-the-daemon-also-try-to-give-better-errors-on-EP.patch

65f427
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
793dd5
From: Peter Jones <pjones@redhat.com>
793dd5
Date: Tue, 8 Aug 2017 17:28:19 -0400
65f427
Subject: [PATCH] Make the daemon also try to give better errors on -EPERM etc.
793dd5
793dd5
Basically 6796e5f but also for the daemon.  This also tries to fix them
793dd5
up to save errno better, for more accurate reporting.
793dd5
793dd5
Signed-off-by: Peter Jones <pjones@redhat.com>
793dd5
---
793dd5
 src/daemon.c | 27 +++++++++++++++++++++++++--
793dd5
 src/pesign.c |  8 ++++++--
793dd5
 2 files changed, 31 insertions(+), 4 deletions(-)
793dd5
793dd5
diff --git a/src/daemon.c b/src/daemon.c
793dd5
index 7f694b2..942d576 100644
793dd5
--- a/src/daemon.c
793dd5
+++ b/src/daemon.c
793dd5
@@ -19,6 +19,7 @@
793dd5
 
793dd5
 #include <errno.h>
793dd5
 #include <fcntl.h>
793dd5
+#include <glob.h>
793dd5
 #include <poll.h>
793dd5
 #include <pwd.h>
793dd5
 #include <signal.h>
793dd5
@@ -1104,10 +1105,32 @@ daemonize(cms_context *cms_ctx, char *certdir, int do_fork)
793dd5
 		"pesignd starting (pid %d)", ctx.pid);
793dd5
 
793dd5
 	SECStatus status = NSS_Init(certdir);
793dd5
+	int error = errno;
793dd5
 	if (status != SECSuccess) {
793dd5
+		char *globpattern = NULL;
793dd5
+		rc = asprintf(&globpattern, "%s/cert*.db",
793dd5
+			      certdir);
793dd5
+		if (rc > 0) {
793dd5
+			glob_t globbuf;
793dd5
+			memset(&globbuf, 0, sizeof(globbuf));
793dd5
+			rc = glob(globpattern, GLOB_ERR, NULL,
793dd5
+				  &globbuf);
793dd5
+			if (rc != 0) {
793dd5
+				errno = error;
793dd5
+				ctx.backup_cms->log(ctx.backup_cms,
793dd5
+					ctx.priority|LOG_NOTICE,
793dd5
+					"Could not open NSS database (\"%s\"): %m",
793dd5
+					PORT_ErrorToString(PORT_GetError()));
793dd5
+				exit(1);
793dd5
+			}
793dd5
+		}
793dd5
+	}
793dd5
+	if (status != SECSuccess) {
793dd5
+		errno = error;
793dd5
 		ctx.backup_cms->log(ctx.backup_cms, ctx.priority|LOG_NOTICE,
793dd5
-			"Could not initialize nss: %s\n",
793dd5
-			PORT_ErrorToString(PORT_GetError()));
793dd5
+				    "Could not initialize nss.\n"
793dd5
+				    "NSS says \"%s\" errno says \"%m\"\n",
793dd5
+				    PORT_ErrorToString(PORT_GetError()));
793dd5
 		exit(1);
793dd5
 	}
793dd5
 
793dd5
diff --git a/src/pesign.c b/src/pesign.c
793dd5
index 5879cfc..6ceda34 100644
793dd5
--- a/src/pesign.c
793dd5
+++ b/src/pesign.c
793dd5
@@ -660,10 +660,12 @@ main(int argc, char *argv[])
793dd5
 
793dd5
 	if (!daemon) {
793dd5
 		SECStatus status;
793dd5
+		int error;
793dd5
 		if (need_db) {
793dd5
 			status = NSS_Init(certdir);
793dd5
 			if (status != SECSuccess) {
793dd5
 				char *globpattern = NULL;
793dd5
+				error = errno;
793dd5
 				rc = asprintf(&globpattern, "%s/cert*.db",
793dd5
 					      certdir);
793dd5
 				if (rc > 0) {
793dd5
@@ -680,8 +682,10 @@ main(int argc, char *argv[])
793dd5
 		} else
793dd5
 			status = NSS_NoDB_Init(NULL);
793dd5
 		if (status != SECSuccess) {
793dd5
-			errx(1, "Could not initialize nss. NSS says \"%s\" errno says \"%m\"\n",
793dd5
-				PORT_ErrorToString(PORT_GetError()));
793dd5
+			errno = error;
793dd5
+			errx(1, "Could not initialize nss.\n"
793dd5
+			        "NSS says \"%s\" errno says \"%m\"\n",
793dd5
+			     PORT_ErrorToString(PORT_GetError()));
793dd5
 		}
793dd5
 
793dd5
 		status = register_oids(ctxp->cms_ctx);