Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/libexec/ld.elf_so Don't use normal environment handling func...



details:   https://anonhg.NetBSD.org/src/rev/b101e2f73f0e
branches:  trunk
changeset: 759805:b101e2f73f0e
user:      joerg <joerg%NetBSD.org@localhost>
date:      Thu Dec 16 22:47:27 2010 +0000

description:
Don't use normal environment handling functions from libc, but iterate
once over the array and clean out entries as needed.

diffstat:

 libexec/ld.elf_so/Makefile |   4 +-
 libexec/ld.elf_so/rtld.c   |  75 +++++++++++++++++++++++++++++++++---------
 libexec/ld.elf_so/xenv.c   |  81 ----------------------------------------------
 3 files changed, 61 insertions(+), 99 deletions(-)

diffs (242 lines):

diff -r 40196fe8c158 -r b101e2f73f0e libexec/ld.elf_so/Makefile
--- a/libexec/ld.elf_so/Makefile        Thu Dec 16 22:19:37 2010 +0000
+++ b/libexec/ld.elf_so/Makefile        Thu Dec 16 22:47:27 2010 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.99 2010/12/05 00:56:06 joerg Exp $
+#      $NetBSD: Makefile,v 1.100 2010/12/16 22:47:27 joerg Exp $
 #
 # NOTE: when changing ld.so, ensure that ldd still compiles.
 #
@@ -58,7 +58,7 @@
 
 CLIBOBJ!=      cd ${NETBSDSRCDIR}/lib/libc && ${PRINTOBJDIR}
 
-SRCS+=         rtld.c reloc.c symbol.c xenv.c xmalloc.c xprintf.c debug.c \
+SRCS+=         rtld.c reloc.c symbol.c xmalloc.c xprintf.c debug.c \
                map_object.c load.c search.c headers.c paths.c expand.c
 
 .if ${USE_FORT} == "yes"
diff -r 40196fe8c158 -r b101e2f73f0e libexec/ld.elf_so/rtld.c
--- a/libexec/ld.elf_so/rtld.c  Thu Dec 16 22:19:37 2010 +0000
+++ b/libexec/ld.elf_so/rtld.c  Thu Dec 16 22:47:27 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rtld.c,v 1.133 2010/12/16 19:59:39 skrll Exp $  */
+/*     $NetBSD: rtld.c,v 1.134 2010/12/16 22:47:27 joerg Exp $  */
 
 /*
  * Copyright 1996 John D. Polstra.
@@ -40,7 +40,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: rtld.c,v 1.133 2010/12/16 19:59:39 skrll Exp $");
+__RCSID("$NetBSD: rtld.c,v 1.134 2010/12/16 22:47:27 joerg Exp $");
 #endif /* not lint */
 
 #include <err.h>
@@ -312,20 +312,21 @@
                       *pAUX_phent, *pAUX_phnum, *pAUX_euid, *pAUX_egid,
                       *pAUX_ruid, *pAUX_rgid;
        const AuxInfo  *pAUX_pagesz;
-       char          **env;
+       char          **env, **oenvp;
        const AuxInfo  *aux;
        const AuxInfo  *auxp;
        Elf_Addr       *const osp = sp;
        bool            bind_now = 0;
-       const char     *ld_bind_now;
+       const char     *ld_bind_now, *ld_preload, *ld_library_path;
        const char    **argv;
        const char     *execname;
        long            argc;
        const char **real___progname;
        const Obj_Entry **real___mainprog_obj;
        char ***real_environ;
-#if defined(RTLD_DEBUG)
+#ifdef DEBUG
        int i = 0;
+       const char     *ld_debug;
 #endif
 
        /*
@@ -428,24 +429,68 @@
            ((pAUX_egid ? (gid_t)pAUX_egid->a_v : getegid()) ==
            (pAUX_rgid ? (gid_t)pAUX_rgid->a_v : getgid()));
 
-       ld_bind_now = getenv("LD_BIND_NOW");
+#ifdef DEBUG
+       ld_debug = NULL;
+#endif
+       ld_bind_now = NULL;
+       ld_library_path = NULL;
+       ld_preload = NULL;
+       /*
+        * Inline avoid using normal getenv/unsetenv here as the libc
+        * code is quite a bit more complicated.
+        */
+       for (oenvp = env; *env != NULL; ++env) {
+               static const char bind_var[] = "LD_BIND_NOW=";
+               static const char debug_var[] =  "LD_DEBUG=";
+               static const char path_var[] = "LD_LIBRARY_PATH=";
+               static const char preload_var[] = "LD_PRELOAD=";
+#define LEN(x) (sizeof(x) - 1)
+
+               if ((*env)[0] != 'L' || (*env)[1] != 'D') {
+                       /*
+                        * Special case to skip most entries without
+                        * the more expensive calls to strncmp.
+                        */
+                       *oenvp++ = *env;
+               } else if (strncmp(*env, debug_var, LEN(debug_var)) == 0) {
+                       if (_rtld_trust) {
+#ifdef DEBUG
+                               ld_debug = *env + LEN(debug_var);
+#endif
+                               *oenvp++ = *env;
+                       }
+               } else if (strncmp(*env, bind_var, LEN(bind_var)) == 0) {
+                       ld_bind_now = *env + LEN(bind_var);
+               } else if (strncmp(*env, path_var, LEN(path_var)) == 0) {
+                       if (_rtld_trust) {
+                               ld_library_path = *env + LEN(path_var);
+                               *oenvp++ = *env;
+                       }
+               } else if (strncmp(*env, preload_var, LEN(preload_var)) == 0) {
+                       if (_rtld_trust) {
+                               ld_preload = *env + LEN(preload_var);
+                               *oenvp++ = *env;
+                       }
+               } else {
+                       *oenvp++ = *env;
+               }
+#undef LEN
+       }
+       *oenvp++ = NULL;
+
        if (ld_bind_now != NULL && *ld_bind_now != '\0')
                bind_now = true;
        if (_rtld_trust) {
 #ifdef DEBUG
-               const char     *ld_debug = getenv("LD_DEBUG");
 #ifdef RTLD_DEBUG
                debug = 0;
 #endif
                if (ld_debug != NULL && *ld_debug != '\0')
                        debug = 1;
 #endif
-               _rtld_add_paths(execname, &_rtld_paths,
-                   getenv("LD_LIBRARY_PATH"));
+               _rtld_add_paths(execname, &_rtld_paths, ld_library_path);
        } else {
                execname = NULL;
-               if (xunsetenv("LD_DEBUG") || xunsetenv("LD_LIBRARY_PATH"))
-                       _rtld_die();
        }
        _rtld_process_hints(execname, &_rtld_paths, &_rtld_xforms,
            _PATH_LD_HINTS);
@@ -512,17 +557,15 @@
        _rtld_objmain->mainref = 1;
        _rtld_objlist_push_tail(&_rtld_list_main, _rtld_objmain);
 
-       if (_rtld_trust) {
+       if (ld_preload) {
                /*
                 * Pre-load user-specified objects after the main program
                 * but before any shared object dependencies.
                 */
                dbg(("preloading objects"));
-               if (_rtld_preload(getenv("LD_PRELOAD")) == -1)
+               if (_rtld_preload(ld_preload) == -1)
                        _rtld_die();
-       } else
-               if (xunsetenv("LD_PRELOAD"))
-                       _rtld_die();
+       }
 
        dbg(("loading needed objects"));
        if (_rtld_load_needed_objects(_rtld_objmain, RTLD_MAIN) == -1)
diff -r 40196fe8c158 -r b101e2f73f0e libexec/ld.elf_so/xenv.c
--- a/libexec/ld.elf_so/xenv.c  Thu Dec 16 22:19:37 2010 +0000
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,81 +0,0 @@
-/*     $NetBSD: xenv.c,v 1.2 2010/11/14 22:09:16 tron Exp $    */
-
-/*
- * Copyright (c) 1987, 1993
- *     The Regents of the University of California.  All rights reserved.
- *
- * 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. Neither the name of the University 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 REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char sccsid[] = "from: @(#)setenv.c     8.1 (Berkeley) 6/4/93";
-#else
-__RCSID("$NetBSD: xenv.c,v 1.2 2010/11/14 22:09:16 tron Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
-#include <errno.h>
-#include <string.h>
-#include "rtldenv.h"
-
-extern char **environ;
-
-#include <unistd.h>
-
-/*
- * xunsetenv(name) --
- *     Delete environmental variable "name".
- */
-int
-xunsetenv(const char *name)
-{
-       size_t l_name, r_offset, w_offset;
-
-       if (name == NULL) {
-               errno = EINVAL;
-               return -1;
-       }
-
-       l_name = strcspn(name, "=");
-       if (l_name == 0 || name[l_name] == '=') {
-               errno = EINVAL;
-               return -1;
-       }
-
-       for (r_offset = 0, w_offset = 0; environ[r_offset] != NULL;
-           r_offset++) {
-               if (strncmp(name, environ[r_offset], l_name) != 0 ||
-                   environ[r_offset][l_name] != '=') {
-                       environ[w_offset++] = environ[r_offset];
-               }
-       }
-
-       while (w_offset < r_offset)
-               environ[w_offset++] = NULL;
-
-       return 0;
-}



Home | Main Index | Thread Index | Old Index