Blame SOURCES/0018-Ticket-48939-nsslapd-workingdir-is-empty-when-ns-sla.patch

7c7f29
From c78cee0747aaa7f3ba7ad77d683d382308992952 Mon Sep 17 00:00:00 2001
7c7f29
From: Noriko Hosoi <nhosoi@redhat.com>
7c7f29
Date: Tue, 26 Jul 2016 16:51:41 -0700
7c7f29
Subject: [PATCH 18/29] Ticket #48939 - nsslapd-workingdir is empty when
7c7f29
 ns-slapd is started by systemd
7c7f29
7c7f29
Description: If the Type of the service is notify in systemd, the server
7c7f29
process does not fork.  Setting nsslapd-workingdir was missing in the not-
7c7f29
fork path.  This patch adds it.
7c7f29
7c7f29
https://fedorahosted.org/389/ticket/48939
7c7f29
7c7f29
Reviewed by wibrown@redhat.com (Thank you, William!!)
7c7f29
7c7f29
(cherry picked from commit a06cb4269613224e1454ed8c1ad6f702cc247b2b)
7c7f29
---
7c7f29
 ldap/servers/slapd/detach.c | 81 ++++++++++++++++++++++++++-------------------
7c7f29
 1 file changed, 47 insertions(+), 34 deletions(-)
7c7f29
7c7f29
diff --git a/ldap/servers/slapd/detach.c b/ldap/servers/slapd/detach.c
7c7f29
index 84a9eef..cd13a99 100644
7c7f29
--- a/ldap/servers/slapd/detach.c
7c7f29
+++ b/ldap/servers/slapd/detach.c
7c7f29
@@ -44,16 +44,50 @@
7c7f29
 #include <unistd.h>
7c7f29
 #endif /* USE_SYSCONF */
7c7f29
 
7c7f29
-int
7c7f29
-detach( int slapd_exemode, int importexport_encrypt,
7c7f29
-        int s_port, daemon_ports_t *ports_info )
7c7f29
+static int
7c7f29
+set_workingdir()
7c7f29
 {
7c7f29
-	int i, sd;
7c7f29
 	int rc = 0;
7c7f29
-	char *workingdir = 0;
7c7f29
+	char *workingdir = config_get_workingdir();
7c7f29
 	char *errorlog = 0;
7c7f29
 	char *ptr = 0;
7c7f29
 	extern char *config_get_errorlog(void);
7c7f29
+	extern int config_set_workingdir(const char *attrname, char *value, char *errorbuf, int apply);
7c7f29
+	char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE];
7c7f29
+
7c7f29
+	if ( NULL == workingdir ) {
7c7f29
+		errorlog = config_get_errorlog();
7c7f29
+		if (NULL == errorlog) {
7c7f29
+			rc = chdir("/");
7c7f29
+		} else {
7c7f29
+			ptr = strrchr(errorlog, '/');
7c7f29
+			if (ptr) {
7c7f29
+				*ptr = '\0';
7c7f29
+			}
7c7f29
+			rc = chdir(errorlog);
7c7f29
+			if (config_set_workingdir(CONFIG_WORKINGDIR_ATTRIBUTE, errorlog, errorbuf, 1) == LDAP_OPERATIONS_ERROR) {
7c7f29
+				LDAPDebug1Arg(LDAP_DEBUG_ANY, "detach: set workingdir failed with \"%s\"\n", errorbuf);
7c7f29
+			}
7c7f29
+			slapi_ch_free_string(&errorlog);
7c7f29
+		}
7c7f29
+	} else {
7c7f29
+		/* calling config_set_workingdir to check for validity of directory, don't apply */
7c7f29
+		if (config_set_workingdir(CONFIG_WORKINGDIR_ATTRIBUTE, workingdir, errorbuf, 0) == LDAP_OPERATIONS_ERROR) {
7c7f29
+			LDAPDebug1Arg(LDAP_DEBUG_ANY, "detach: set workingdir failed with \"%s\"\n", errorbuf);
7c7f29
+			rc = chdir("/");
7c7f29
+		} else {
7c7f29
+			rc = chdir(workingdir);
7c7f29
+		}
7c7f29
+		slapi_ch_free_string(&workingdir);
7c7f29
+	}
7c7f29
+	return rc;
7c7f29
+}
7c7f29
+
7c7f29
+int
7c7f29
+detach( int slapd_exemode, int importexport_encrypt,
7c7f29
+        int s_port, daemon_ports_t *ports_info )
7c7f29
+{
7c7f29
+	int i, sd;
7c7f29
 
7c7f29
 	if ( should_detach ) {
7c7f29
 		for ( i = 0; i < 5; i++ ) {
7c7f29
@@ -76,35 +110,12 @@ detach( int slapd_exemode, int importexport_encrypt,
7c7f29
 		}
7c7f29
 
7c7f29
 		/* call this right after the fork, but before closing stdin */
7c7f29
-		if (slapd_do_all_nss_ssl_init(slapd_exemode, importexport_encrypt,
7c7f29
-									  s_port, ports_info)) {
7c7f29
+		if (slapd_do_all_nss_ssl_init(slapd_exemode, importexport_encrypt, s_port, ports_info)) {
7c7f29
 			return 1;
7c7f29
 		}
7c7f29
 
7c7f29
-		workingdir = config_get_workingdir();
7c7f29
-		if ( NULL == workingdir ) {
7c7f29
-			errorlog = config_get_errorlog();
7c7f29
-			if ( NULL == errorlog ) {
7c7f29
-				rc = chdir( "/" );
7c7f29
-				PR_ASSERT(rc == 0);
7c7f29
-			} else {
7c7f29
-				if ((ptr = strrchr(errorlog, '/')) ||
7c7f29
-					(ptr = strrchr(errorlog, '\\'))) {
7c7f29
-					*ptr = 0;
7c7f29
-				}
7c7f29
-				rc = chdir( errorlog );
7c7f29
-				PR_ASSERT(rc == 0);
7c7f29
-				config_set_workingdir(CONFIG_WORKINGDIR_ATTRIBUTE, errorlog, NULL, 1);
7c7f29
-				slapi_ch_free_string(&errorlog);
7c7f29
-			}
7c7f29
-		} else {
7c7f29
-			/* calling config_set_workingdir to check for validity of directory, don't apply */
7c7f29
-			if (config_set_workingdir(CONFIG_WORKINGDIR_ATTRIBUTE, workingdir, NULL, 0) == LDAP_OPERATIONS_ERROR) {
7c7f29
-				return 1;
7c7f29
-			}
7c7f29
-			rc = chdir( workingdir );
7c7f29
-			PR_ASSERT(rc == 0);
7c7f29
-			slapi_ch_free_string(&workingdir);
7c7f29
+		if (set_workingdir()) {
7c7f29
+			LDAPDebug0Args(LDAP_DEBUG_ANY, "detach: chdir to workingdir failed.\n");
7c7f29
 		}
7c7f29
 
7c7f29
 		if ( (sd = open( "/dev/null", O_RDWR )) == -1 ) {
7c7f29
@@ -127,14 +138,16 @@ detach( int slapd_exemode, int importexport_encrypt,
7c7f29
 
7c7f29
 		g_set_detached(1);
7c7f29
 	} else { /* not detaching - call nss/ssl init */
7c7f29
-		if (slapd_do_all_nss_ssl_init(slapd_exemode, importexport_encrypt,
7c7f29
-									  s_port, ports_info)) {
7c7f29
+		if (slapd_do_all_nss_ssl_init(slapd_exemode, importexport_encrypt, s_port, ports_info)) {
7c7f29
 			return 1;
7c7f29
 		}
7c7f29
+		if (set_workingdir()) {
7c7f29
+			LDAPDebug0Args(LDAP_DEBUG_ANY, "detach: chdir to workingdir failed.\n");
7c7f29
+		}
7c7f29
 	}
7c7f29
 
7c7f29
 	(void) SIGNAL( SIGPIPE, SIG_IGN );
7c7f29
-	return rc;
7c7f29
+	return 0;
7c7f29
 }
7c7f29
 
7c7f29
 /*
7c7f29
-- 
7c7f29
2.4.11
7c7f29