>From 5da70f1199f38de67229e0961bfe82f8bebb4537 Mon Sep 17 00:00:00 2001
From: Nathan Kinder <nkinder@redhat.com>
Date: Wed, 7 Oct 2009 11:00:06 -0700
Subject: [PATCH] Get rundir from sysconfig script.

The dsalib function used by the CGIs to get the DS rundir was
getting the data from the start-slapd script, which no longer
works since that script is just a thin wrapper around the
start-dirsrv script now.  This patch changes the logic to get
the rundir from the instance sysconfig script.
---
 Makefile.am                  |    5 +++-
 lib/libdsa/dsalib_location.c |   59 +++++++++++++++++++++++++++++++++++++-----
 2 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 29b6b26..35f5e5d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -29,6 +29,9 @@ BUILDNUM := $(shell perl $(srcdir)/buildnum.pl)
 NQBUILDNUM := $(subst \,,$(subst $(QUOTE),,$(BUILDNUM)))
 
 DEBUG_DEFINES = @debug_defs@
+
+PATH_DEFINES = -DINITCONFIGDIR="\"$(initconfigdir)\""
+
 # look for included m4 files in the ./m4/ directory
 ACLOCAL_AMFLAGS = -I m4
 
@@ -100,7 +103,7 @@ libadmin_a_SOURCES = lib/libadmin/cluster.c lib/libadmin/dllglue.c \
 	lib/libadmin/error.c lib/libadmin/form_get.c lib/libadmin/httpcon.c lib/libadmin/install.c \
 	lib/libadmin/referer.c lib/libadmin/template.c lib/libadmin/util.c
 
-AM_CPPFLAGS = $(DEBUG_DEFINES) @adminutil_inc@ @icu_inc@ @ldapsdk_inc@ @nss_inc@ @nspr_inc@ -DUSE_ADMSERV=1 \
+AM_CPPFLAGS = $(DEBUG_DEFINES) $(PATH_DEFINES) @adminutil_inc@ @icu_inc@ @ldapsdk_inc@ @nss_inc@ @nspr_inc@ -DUSE_ADMSERV=1 \
 	-I$(srcdir)/include -I$(srcdir)/include/base
 if WINNT
 AM_CPPFLAGS += -DXP_WINNT
diff --git a/lib/libdsa/dsalib_location.c b/lib/libdsa/dsalib_location.c
index 026e696..9237cd9 100644
--- a/lib/libdsa/dsalib_location.c
+++ b/lib/libdsa/dsalib_location.c
@@ -92,26 +92,68 @@ ds_get_run_dir()
     char *rundir_env = NULL;
     static char rundir[PATH_MAX];
     char *inst_dir = NULL;
+    char *inst_name = NULL;
     char *start_script = NULL;
+    char *sysconfig_script = NULL;
     char *p = NULL;
+    char *start = NULL;
     char line[BIG_LINE];
     FILE *fp = NULL;
 
     if ((rundir_env = getenv("DS_RUN_DIR"))) {
         return (rundir_env);
     } else {
-        /* Find the run dir from the start script */
+        /* Use the instance name to locate the instance sysconfig script */
         inst_dir = ds_get_instance_dir();
         start_script = PR_smprintf("%s%cstart-slapd", inst_dir, FILE_PATHSEP);
         fp = fopen(start_script, "r");
         if (fp) {
             while(fgets(line, BIG_LINE, fp)) {
-                /* Find line starting with PIDFILE */
-                if (strncmp(line, "PIDFILE", 7) == 0) {
-                    /* Chop off the pidfile name to get the run dir */
-                    if ((p = strrchr(line, '/'))) {
-                        *p = '\0';
-                        PR_snprintf(rundir, sizeof(rundir), "%s", line + 8);
+                /* Find the line that calls start-dirsrv */
+                if ((start = strstr(line, "start-dirsrv"))) {
+                    /* skip any spaces after start-dirsrv */
+                    start += strlen("start-dirsrv");
+                    while (isspace(*start)) {
+                        start++;
+                    }
+
+                    /* find the end of the instance name */
+                    p = start;
+                    while (!isspace(*p)) {
+                        p++;
+                    }
+                    *p = '\0';
+
+                    if (strlen(start) > 0) {
+                        inst_name = PR_smprintf("%s", start);
+                    }
+                }
+            }
+            fclose(fp);
+        }
+
+        /* We're done if we didn't parse the instance name. */
+        if ((inst_name == NULL) || (strlen(inst_name) == 0)) {
+            goto free_and_exit;
+        }
+
+        /* Get the RUN_DIR line from the instance sysconfig script */
+        sysconfig_script = PR_smprintf("%s%cdirsrv-%s", INITCONFIGDIR, FILE_PATHSEP, inst_name);
+        fp = fopen(sysconfig_script, "r");
+        if (fp) {
+            while(fgets(line, BIG_LINE, fp)) {
+                /* Find line starting with RUN_DIR */
+                if (strncmp(line, "RUN_DIR", 7) == 0) {
+                    /* Chop off the variable name and export of the variable */
+                    if ((start = strchr(line, '='))) {
+                        start++;
+                        if ((p = strrchr(start, ';'))) {
+                            *p = '\0';
+                            for (--p; isspace(*p); p--) {
+                                *p = '\0';
+                            }
+                            PR_snprintf(rundir, sizeof(rundir), "%s", start);
+                        }
                     }
                     break;
                 }
@@ -119,7 +161,10 @@ ds_get_run_dir()
             fclose(fp);
         }
 
+free_and_exit:
         PR_smprintf_free(start_script);
+        PR_smprintf_free(sysconfig_script);
+        PR_smprintf_free(inst_name);
 
         if (rundir[0] != '\0') {
             return (rundir);
-- 
1.6.2.5

