Subject: patch to allow /etc/ld.so.conf configure directories for ELF
To: None <tech-userlevel@netbsd.org>
From: matthew green <mrg@eterna.com.au>
List: tech-userlevel
Date: 03/12/1999 00:43:46
hi folks.


it has always annoyed me that the only way to change the shared
library directory search for our ELF ports was LD_LIBRARY_PATH.
this has two problems in my mind:

	- it isn't "system wide".  i can't configure it simply
	  so that it works for everyone, all the time.

	- it doesn't work for set-id binaries.

these are fatal flaws to me.


after discussions several months ago, it came to my attention
that it was considered a feature that there was no elf ld.so.conf.
this is because people don't like the fact that the cache gets
stale.  well, i don't care too much about the cache.  infact, i
agree that it can be painful with stale caches (it could stat(2)
dirs, but then you're stretching it's ability to be an effective
cache, arne't you?).


below is a patch that simply adds the directories found in
/etc/ld.so.conf to the search path after the default, but
before LD_LIBRARY_PATH (same as a.out).  infact, most of
this patch is ripped directly out of the a.out ldconfig(8)
program, so it uses exactly the same file format -- it just
does not create or use a cache file.

it adds support to both ld.elf_so and ldd_elf to read
/etc/ld.so.conf (yay!  ldd_elf is NOT a complete nightmare!)


i'd really like this to go in.  comments?



Index: libexec/ld.elf_so/paths.c
===================================================================
RCS file: /cvsroot/src/libexec/ld.elf_so/paths.c,v
retrieving revision 1.4
diff -p -c -r1.4 paths.c
*** paths.c	1999/03/01 16:40:07	1.4
--- paths.c	1999/03/11 13:40:49
***************
*** 1,5 ****
--- 1,41 ----
  /*	$NetBSD: paths.c,v 1.4 1999/03/01 16:40:07 christos Exp $	 */
  
+ /*-
+  * Copyright (c) 1998 The NetBSD Foundation, Inc.
+  * All rights reserved.
+  *
+  * This code is derived from software contributed to The NetBSD Foundation
+  * by Paul Kranenburg.
+  *
+  * Redistribution and use in source and binary forms, with or without
+  * modification, are permitted provided that the following conditions
+  * are met:
+  * 1. Redistributions of source code must retain the above copyright
+  *    notice, this list of conditions and the following disclaimer.
+  * 2. Redistributions in binary form must reproduce the above copyright
+  *    notice, this list of conditions and the following disclaimer in the
+  *    documentation and/or other materials provided with the distribution.
+  * 3. All advertising materials mentioning features or use of this software
+  *    must display the following acknowledgement:
+  *        This product includes software developed by the NetBSD
+  *        Foundation, Inc. and its contributors.
+  * 4. Neither the name of The NetBSD Foundation nor the names of its
+  *    contributors may be used to endorse or promote products derived
+  *    from this software without specific prior written permission.
+  *
+  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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.
+  */
+ 
  /*
   * Copyright 1996 Matt Thomas <matt@3am-software.com>
   * All rights reserved.
***************
*** 27,33 ****
--- 63,73 ----
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   */
  
+ #include <sys/types.h>
+ #include <sys/mman.h>
  
+ #include <ctype.h>
+ #include <dirent.h>
  #include <err.h>
  #include <errno.h>
  #include <fcntl.h>
***************
*** 36,44 ****
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
- #include <sys/types.h>
- #include <sys/mman.h>
- #include <dirent.h>
  
  #include "debug.h"
  #include "rtld.h"
--- 76,81 ----
*************** _rtld_add_paths(path_p, pathstr, dodebug
*** 104,108 ****
--- 141,200 ----
  		if (ep[0] == '\0')
  			break;
  		pathstr = ep + 1;
+ 	}
+ }
+ 
+ /*
+  * this is mostly ripped off src/sbin/ldconfig/ldconfig.c:
+  *	NetBSD: ldconfig.c,v 1.22 1998/12/15 22:49:42 pk Exp
+  */
+ void
+ _rtld_setup_paths(path_p, pathstr, confstr, dodebug)
+ 	Search_Path ** path_p;
+ 	const char *pathstr;
+ 	const char *confstr;
+ 	bool dodebug;
+ {
+ 	FILE	*conf;
+ 	char	*line, *c;
+ 	char	*cline = NULL;
+ 	size_t	len;
+ 
+ 	/* first, setup the default paths */
+ 	_rtld_add_paths(path_p, pathstr, dodebug);
+ 
+ 	/* now, fill in the bits from /etc/ld.so.conf */
+ 	if ((conf = fopen(confstr, "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))
+ 			;
+ 		if (c >= line) {
+ 			*++c = '\0';
+ 			_rtld_add_paths(path_p, line, dodebug);
+ 		}
+ 
+ 		if (cline) {
+ 			free(cline);
+ 			cline = NULL;
+ 		}
  	}
  }
Index: libexec/ld.elf_so/rtld.c
===================================================================
RCS file: /cvsroot/src/libexec/ld.elf_so/rtld.c,v
retrieving revision 1.17
diff -p -c -r1.17 rtld.c
*** rtld.c	1999/03/08 10:44:25	1.17
--- rtld.c	1999/03/11 13:40:51
*************** _rtld_init(mapbase)
*** 194,200 ****
  	_rtld_objself = objself;
  
  	_rtld_objself.path = _rtld_path;
! 	_rtld_add_paths(&_rtld_paths, RTLD_DEFAULT_LIBRARY_PATH, true);
  
  	/*
  	 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
--- 194,201 ----
  	_rtld_objself = objself;
  
  	_rtld_objself.path = _rtld_path;
! 	_rtld_setup_paths(&_rtld_paths, RTLD_DEFAULT_LIBRARY_PATH,
! 	    _PATH_LD_SO_CONF, true);
  
  	/*
  	 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
Index: libexec/ld.elf_so/rtld.h
===================================================================
RCS file: /cvsroot/src/libexec/ld.elf_so/rtld.h,v
retrieving revision 1.8
diff -p -c -r1.8 rtld.h
*** rtld.h	1999/03/01 16:40:07	1.8
--- rtld.h	1999/03/11 13:40:52
***************
*** 42,47 ****
--- 42,48 ----
  #include "link.h"
  
  #define	RTLD_DEFAULT_LIBRARY_PATH	"/usr/lib:/usr/local/lib"
+ #define	_PATH_LD_SO_CONF		"/etc/ld.so.conf"
  
  #if 0
  #define SVR4_LIBDIR	"/usr/lib"
*************** int _rtld_load_needed_objects __P((Obj_E
*** 197,202 ****
--- 198,204 ----
  
  /* path.c */
  void _rtld_add_paths __P((Search_Path **, const char *, bool));
+ void _rtld_setup_paths __P((Search_Path **, const char *, const char *, bool));
  
  /* reloc.c */
  int _rtld_do_copy_relocations __P((const Obj_Entry *, bool));
Index: usr.bin/ldd/ldd_elf/ldd.c
===================================================================
RCS file: /cvsroot/src/usr.bin/ldd/ldd_elf/ldd.c,v
retrieving revision 1.2
diff -p -c -r1.2 ldd.c
*** ldd.c	1999/02/25 16:26:51	1.2
--- ldd.c	1999/03/11 13:40:52
*************** main(
*** 85,91 ****
  #ifdef DEBUG
      debug = 1;
  #endif
!     _rtld_add_paths(&_rtld_paths, RTLD_DEFAULT_LIBRARY_PATH, true);
  
  
      _rtld_trust = geteuid() == getuid() && getegid() == getgid();
--- 85,92 ----
  #ifdef DEBUG
      debug = 1;
  #endif
!     _rtld_setup_paths(&_rtld_paths, RTLD_DEFAULT_LIBRARY_PATH,
!       _PATH_LD_SO_CONF, true);
  
  
      _rtld_trust = geteuid() == getuid() && getegid() == getgid();