Subject: bin/4031: add processing of /etc/ld.so.conf to ldconfig
To: None <gnats-bugs@gnats.netbsd.org>
From: None <jbernard@tater.mines.edu>
List: netbsd-bugs
Date: 08/24/1997 09:58:02
>Number:         4031
>Category:       bin
>Synopsis:       add processing of /etc/ld.so.conf to ldconfig
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sun Aug 24 09:05:01 1997
>Last-Modified:
>Originator:     Jim Bernard
>Organization:
	speaking for myself
>Release:        August 24, 1997
>Environment:
System: NetBSD zoo 1.2G NetBSD 1.2G (ZOO) #0: Sat Jul 19 12:48:58 MDT 1997 jim@zoo:/jaz/home/local/compile/sys/arch/i386/compile/ZOO i386


>Description:
	/etc/ld.so.conf, was recently added to make local configuration of
	shared-library hints easy, but the file is not processed directly
	by ldconfig (instead, one must do "ldconfig `cat /etc/ld.so.conf`").
	Thus, whenever libraries are changed or added, it is not sufficient
	to just run "ldconfig" to have the configured directories scanned.
>How-To-Repeat:
	Add libraries.  Run "ldconfig".  Lose hints for directories
	configured in /etc/ld.so.conf.  Cut and paste from /etc/rc to
	include ld.so.conf directories.  Get tired of doing that.
>Fix:
	The patches below add internal processing of /etc/ld.so.conf to
	ldconfig, together with stripping of comments.  The ldconfig.8
	and ld.so.conf.5 man pages, and the /etc/rc script are also
	updated to match.  Briefly, the changes relative to Aug. 24, 1997
	sources are:

	* gnu/usr.bin/ld/ldconfig/ldconfig.c
	  - added do_conf function and call to it in main to implement internal
	    processing of /etc/ld.so.conf
	  - moved maintenance of dir_list to dodir, with an added argument to
	    dodir to specify whether dir_list should be updated
	  - added option '-c' to suppress processing of /etc/ld.so.conf
	  - added option '-S' to suppress processing of std directories (but not
	    /etc/ld.so.conf)
	  - modified option -s to suppress processing of _both_ std directories
	    and /etc/ld.so.conf (i.e., it is equivalent to -cS)--this was done
	    so that users and scripts that interpret -s as meaning "process
	    only directories specified on the command line" would not be
	    startled
	  - initialize the variables set by command-line options

	* gnu/usr.bin/ld/ldconfig/ldconfig.8
	  - document internal processing of /etc/ld.so.conf
	  - document new options and "change" to -s functionality
	  - update date
	  - add xref to ld.so(1)
	  - miscellaneous corrections

	* share/man/man5/ld.so.conf.5
	  - update date
	  - documented syntax supported by internal processing in ldconfig

	* etc/rc
	  - unconditionally run ldconfig with no arguments

--- ldconfig.c-dist	Thu Apr 17 05:11:34 1997
+++ ldconfig.c	Sun Aug 24 07:36:45 1997
@@ -49,19 +49,22 @@
 #include <string.h>
 #include <unistd.h>
 
 #include "ld.h"
 
+#define _PATH_LD_SO_CONF "/etc/ld.so.conf"
+
 #undef major
 #undef minor
 
 extern char			*__progname;
 
-static int			verbose;
-static int			nostd;
-static int			justread;
-static int			merge;
+static int			verbose = 0;
+static int			nostd = 0;
+static int			noconf = 0;
+static int			justread = 0;
+static int			merge = 0;
 
 struct shlib_list {
 	/* Internal list of shared libraries found */
 	char			*name;
 	char			*path;
@@ -74,11 +77,12 @@
 
 static struct shlib_list	*shlib_head = NULL, **shlib_tail = &shlib_head;
 static char			*dir_list;
 
 static void	enter __P((char *, char *, char *, int *, int));
-static int	dodir __P((char *, int));
+static int	dodir __P((char *, int, int));
+static int	do_conf __P((void));
 static int	buildhints __P((void));
 static int	readhints __P((void));
 static void	listhints __P((void));
 
 int
@@ -87,26 +91,33 @@
 char	*argv[];
 {
 	int		i, c;
 	int		rval = 0;
 
-	while ((c = getopt(argc, argv, "mrsv")) != EOF) {
+	while ((c = getopt(argc, argv, "cmrsSv")) != EOF) {
 		switch (c) {
+		case 'c':
+			noconf = 1;
+			break;
 		case 'm':
 			merge = 1;
 			break;
 		case 'r':
 			justread = 1;
 			break;
 		case 's':
 			nostd = 1;
+			noconf = 1;
+			break;
+		case 'S':
+			nostd = 1;
 			break;
 		case 'v':
 			verbose = 1;
 			break;
 		default:
-			errx(1, "Usage: %s [-m][-r][-s][-v][dir ...]",
+			errx(1, "Usage: %s [-c][-m][-r][-s][-S][-v][dir ...]",
 				__progname);
 			break;
 		}
 	}
 
@@ -124,39 +135,89 @@
 
 	if (!nostd && !merge)
 		std_search_path();
 
 	for (i = 0; i < n_search_dirs; i++)
-		rval |= dodir(search_dirs[i], 1);
+		rval |= dodir(search_dirs[i], 1, 0);
+
+	if (!noconf && !merge)
+		rval |= do_conf();
 
 	for (i = optind; i < argc; i++) {
-		/* Check for duplicates? */
-		char *cp = concat(dir_list, *dir_list?":":"", argv[i]);
-		free(dir_list);
-		dir_list = cp;
-		rval |= dodir(argv[i], 0);
+		rval |= dodir(argv[i], 0, 1);
 	}
 
 	rval |= buildhints();
 
 	return rval;
 }
 
 int
-dodir(dir, silent)
+do_conf ()
+{
+	FILE		*conf;
+	char		*line, *c;
+	char		*cline = NULL;
+	size_t		len;
+	int		rval = 0;
+
+	if ((conf = fopen(_PATH_LD_SO_CONF, "r")) == NULL)
+		return;
+
+	while ((line = fgetln(conf, &len)) != NULL) {
+		if (*line == '#' || *line == '\n')
+			continue;
+
+		if (line[len-1] == '\n') {
+			line[--len] = '\0';
+		} else {
+			cline = xmalloc(len+1);
+			bcopy(line, cline, len);
+			line = cline;
+			line[len] = '\0';
+		}
+
+		while (isblank(*line)) { line++; len--; }
+		if ((c = strchr(line, '#')) == NULL)
+			c = line + len;
+		while (--c >= line && isblank(*c)) continue;
+		if (c >= line) {
+			*++c = '\0';
+			rval |= dodir(line, 0, 1);
+		}
+
+		if (cline) {
+			free(cline);
+			cline = NULL;
+		}
+	}
+
+	return rval;
+}
+
+int
+dodir(dir, silent, update_dir_list)
 char	*dir;
 int	silent;
+int	update_dir_list;
 {
 	DIR		*dd;
 	struct dirent	*dp;
 	char		name[MAXPATHLEN];
 	int		dewey[MAXDEWEY], ndewey;
 
 	if ((dd = opendir(dir)) == NULL) {
 		if (!silent || errno != ENOENT)
 			warn("%s", dir);
 		return -1;
+	}
+
+	if (update_dir_list) {
+		/* Check for duplicates? */
+		char *cp = concat(dir_list, *dir_list?":":"", dir);
+		free(dir_list);
+		dir_list = cp;
 	}
 
 	while ((dp = readdir(dd)) != NULL) {
 		register int n;
 		register char *cp;
--- ldconfig.8-dist	Sun Jan  5 05:18:43 1997
+++ ldconfig.8	Sun Aug 24 09:23:50 1997
@@ -26,107 +26,118 @@
 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd October 3, 1993
+.Dd August 24, 1997
 .Dt LDCONFIG 8
 .Os NetBSD
 .Sh NAME
 .Nm ldconfig
 .Nd configure the shared library cache
 .Sh SYNOPSIS
 .Nm ldconfig
-.Op Fl mrsv
+.Op Fl cmrsSv
 .Op Ar directory Ar ...
 .Sh DESCRIPTION
 .Nm
 is used to prepare a set of
 .Dq hints
 for use by the run-time linker
-.Xr ld.so
+.Nm ld.so
 to facilitate quick lookup of shared libraries available in multiple
-directories.  It scans a set of built-in system directories and any
+directories.  By default, it scans a set of built-in system directories,
+directories listed in
+.Pa /etc/ld.so.conf ,
+and any
 .Ar directories
 specified on the command line (in the given order) looking for shared
 libraries and stores the results in the file
-.Xr /var/run/ld.so.hints
+.Pa /var/run/ld.so.hints
 to forestall the overhead that would otherwise result from the
 directory search operations
-.Xr ld.so
-would have to perform to load the required shared libraries.  The system
-reads the file
-.Pa /etc/ld.so.conf
-as a list of directories to pass to
-.Nm
-at boot time.
+.Nm ld.so
+would have to perform to load required shared libraries.
 .Pp
 The shared libraries so found will be automatically available for loading
-if needed by the program being prepared for execution. This obviates the need
+if needed by the program being prepared for execution.  This obviates the need
 for storing search paths within the executable.
 .Pp
 The
 .Ev LD_LIBRARY_PATH
 environment variable can be used to override the use of
 directories (or the order thereof) from the cache or to specify additional
 directories where shared libraries might be found.
 .Ev LD_LIBRARY_PATH
 is a
 .Sq \:
-separated list of directory paths which are searched by
-.Xr ld.so
-when it needs to load a shared library. It can be viewed as the run-time
+separated list of directory paths that are searched by
+.Nm ld.so
+when it needs to load a shared library.  It can be viewed as the run-time
 equivalent of the
 .Fl L
 switch of
-.Xr ld.
+.Nm ld .
 .Pp
-.Nm Ldconfig
+.Nm
 is typically run as part of the boot sequence.
 .Pp
 The following options are recognized by
-.Nm ldconfig:
+.Nm ldconfig :
 .Bl -tag -width indent
+.It Fl c
+Do not scan directories listed in
+.Pa /etc/ld.so.conf
+for shared libraries.
 .It Fl m
 Merge the result of the scan of the directories given as arguments into
-the existing hints file. The default action is to build the hints file afresh.
+the existing hints file.  The default action is to build the hints file afresh.
 .It Fl r
 Lists the current contents of
-.Xr ld.so.hints
-on the standard output. The hints file will not be modified.
+.Pa ld.so.hints
+on the standard output.  The hints file will not be modified.
 .It Fl s
 Do not scan the built-in system directory
-.Pq Dq /usr/lib
+.Pq Dq /usr/lib ,
+nor any directories listed in
+.Pa /etc/ld.so.conf
 for shared libraries.
+.It Fl S
+Do not scan the built-in system directory
+.Pq Dq /usr/lib ,
+for shared libraries.  (Directories listed in
+.Pa /etc/ld.so.conf
+are still scanned.)
 .It Fl v
 Switch on verbose mode.
 .Sh Security
 Special care must be taken when loading shared libraries into the address
 space of
 .Ev set-user-Id
-programs. Whenever such a program is run,
-.Xr ld.so
+programs.  Whenever such a program is run,
+.Nm ld.so
 will only load shared libraries from the
-.Ev ld.so.hints
-file. In particular, the
+.Pa ld.so.hints
+file.  In particular, the
 .Ev LD_LIBRARY_PATH
-is not used to search for libraries. Thus, the role of ldconfig is dual. In
+is not used to search for libraries.  Thus, the role of ldconfig is dual.  In
 addition to building a set of hints for quick lookup, it also serves to
 specify the trusted collection of directories from which shared objects can
-be safely loaded. It is presumed that the set of directories specified to
-.Nm ldconfig
-are under control of the system's administrator.
-.Xr ld.so
+be safely loaded.  It is presumed that the set of directories specified to
+.Nm
+is under control of the system's administrator.
+.Nm ld.so
 further assists set-user-Id programs by erasing the
 .Ev LD_LIBRARY_PATH
 from the environment.
 
 .Sh FILES
-.Xr /var/run/ld.so.hints ,
-.Xr /etc/ld.so.conf
+.Pa /var/run/ld.so.hints ,
+.Pa /etc/ld.so.conf
 .Sh SEE ALSO
 .Xr ld 1 ,
+.Xr ld.so 1 ,
 .Xr ld.so.conf 5 ,
 .Xr link 5
 .Sh HISTORY
 A
 .Nm
--- ld.so.conf.5-dist	Sat Jun  7 05:32:59 1997
+++ ld.so.conf.5	Sun Aug 24 08:54:28 1997
@@ -28,34 +28,43 @@
 .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd December 30, 1996
+.Dd August 24, 1997
 .Dt LD.SO.CONF 5
 .Os NetBSD 1.3
 .Sh NAME
 .Nm ld.so.conf
 .Nd run-time link-editor configuration file
 .Sh DESCRIPTION
 The
 .Nm
-file specifies the directories to be added to the hints file used by the
-run-time linker
-.Pa /usr/libexec/ld.so
-at system boot time.  Each line in
-.Nm
-is treated as a directory to be added to the hints.
+file specifies additional default directories (beyond the standard set,
+normally
+.Dq /usr/lib )
+to be scanned by
+.Nm ldconfig
+for shared libraries to include in the hints file used by the run-time linker
+.Pa /usr/libexec/ld.so .
+.Pp
+Lines beginning with
+.Dq #
+are treated as comments and ignored.  Any other non-blank lines
+are stripped of leading whitespace and trailing comments (introduced with
+.Dq # )
+together with any preceding whitespace, then treated as directories to be
+scanned for shared libraries to add to the hints.
 .Sh FILES
 .Pa /etc/ld.so.conf
 .Sh SEE ALSO
 .Xr ldconfig 8
 .Sh BUGS
-The
-.Nm
-file should allow comments with # symbol, but the current implementation
-does not allow this.
+Directory names containing the comment character
+.Pq Dq #
+and/or leading or trailing whitespace cannot be included.  (Embedded blanks
+are allowed, however.)
 .Sh HISTORY
 The
 .Nm
 file appeared in
 .Nx 1.3 .
--- rc-dist	Tue Aug  5 05:09:54 1997
+++ rc	Sun Aug 24 09:18:03 1997
@@ -235,15 +235,11 @@
 
 echo '.'
 
 if [ -f /sbin/ldconfig ]; then
 	echo 'creating runtime link editor directory cache.'
-	if [ -f /etc/ld.so.conf ]; then
-		ldconfig `cat /etc/ld.so.conf`
-	else
-		ldconfig
-	fi
+	ldconfig
 fi 
 
 # load kernel modules specified in /etc/lkm.conf
 if [ "$lkm_init" != NO ] && [ -f /etc/rc.lkm ]; then
 	lkmstage=AFTERMOUNT
>Audit-Trail:
>Unformatted: