Home | History | Annotate | Line # | Download | only in ld.elf_so
expand.c revision 1.2.4.1
      1 /*	$NetBSD: expand.c,v 1.2.4.1 2007/11/06 23:12:08 matt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 #include <sys/cdefs.h>
     39 #ifndef lint
     40 __RCSID("$NetBSD: expand.c,v 1.2.4.1 2007/11/06 23:12:08 matt Exp $");
     41 #endif /* not lint */
     42 
     43 #include <ctype.h>
     44 #include <string.h>
     45 #include <sys/sysctl.h>
     46 
     47 #ifdef DEBUG_EXPAND
     48 #include <stdio.h>
     49 #include <err.h>
     50 #define xwarn warn
     51 #define xerr err
     52 size_t _rtld_expand_path(char *, size_t, const char *, const char *,
     53     const char *);
     54 #else
     55 #include <sys/stat.h>
     56 #include "rtld.h"
     57 #endif
     58 
     59 static const struct {
     60 	const char *name;
     61 	size_t namelen;
     62 } bltn[] = {
     63 #define ADD(a)	{ #a, sizeof(#a) - 1 },
     64 	ADD(HWCAP)	/* SSE, MMX, etc */
     65 	ADD(ISALIST)	/* XXX */
     66 	ADD(ORIGIN) 	/* dirname argv[0] */
     67 	ADD(OSNAME)	/* uname -s */
     68 	ADD(OSREL)	/* uname -r */
     69 	ADD(PLATFORM)	/* uname -p */
     70 };
     71 
     72 static int mib[3][2] = {
     73 	{ CTL_KERN, KERN_OSTYPE },
     74 	{ CTL_KERN, KERN_OSRELEASE },
     75 	{ CTL_HW, HW_MACHINE_ARCH },
     76 };
     77 
     78 static size_t
     79 expand(char *buf, const char *execname, int what, size_t bl)
     80 {
     81 	const char *p, *ep;
     82 	char *bp = buf;
     83 	size_t len;
     84 	char name[32];
     85 
     86 	switch (what) {
     87 	case 0:	/* HWCAP XXX: Not yet */
     88 	case 1:	/* ISALIST XXX: Not yet */
     89 		return 0;
     90 
     91 	case 2:	/* ORIGIN */
     92 		if (execname == NULL)
     93 			xerr(1, "execname not specified in AUX vector");
     94 		if ((ep = strrchr(p = execname, '/')) == NULL)
     95 			xerr(1, "bad execname `%s' in AUX vector", execname);
     96 		break;
     97 
     98 	case 3:	/* OSNAME */
     99 	case 4:	/* OSREL */
    100 	case 5:	/* PLATFORM */
    101 		len = sizeof(name);
    102 		if (sysctl(mib[what - 3], 2, name, &len, NULL, 0) == -1) {
    103 			xwarn("sysctl");
    104 			return 0;
    105 		}
    106 		ep = (p = name) + len - 1;
    107 		break;
    108 	default:
    109 		return 0;
    110 	}
    111 
    112 	while (p != ep && bl)
    113 		*bp++ = *p++, bl--;
    114 
    115 	return bp - buf;
    116 }
    117 
    118 
    119 size_t
    120 _rtld_expand_path(char *buf, size_t bufsize, const char *execname,
    121     const char *bp, const char *ep)
    122 {
    123 	size_t i, ds = bufsize;
    124 	char *dp = buf;
    125 	const char *p;
    126 	int br;
    127 
    128 	for (p = bp; p < ep;) {
    129 		if (*p == '$') {
    130 			br = *++p == '{';
    131 
    132 			if (br)
    133 				p++;
    134 
    135 			for (i = 0; i < sizeof(bltn) / sizeof(bltn[0]); i++) {
    136 				size_t s = bltn[i].namelen;
    137 				const char *es = p + s;
    138 
    139 				if ((br && *es != '}') ||
    140 				    (!br && (es != ep &&
    141 					isalpha((unsigned char)*es))))
    142 					continue;
    143 
    144 				if (strncmp(bltn[i].name, p, s) == 0) {
    145 					size_t ls = expand(dp, execname, i, ds);
    146 					if (ls >= ds)
    147 						return bufsize;
    148 					ds -= ls;
    149 					dp += ls;
    150 					p = es + br;
    151 					goto done;
    152 				}
    153 			}
    154 			p -= br + 1;
    155 
    156 		}
    157 		*dp++ = *p++;
    158 		ds--;
    159 done:;
    160 	}
    161 	*dp = '\0';
    162 	return dp - buf;
    163 }
    164 
    165 #ifdef DEBUG_EXPAND
    166 int
    167 main(int argc, char *argv[])
    168 {
    169 	char buf[1024];
    170 	size_t i;
    171 
    172 	for (i = 1; i < argc; i++) {
    173 		char *p = argv[i], *ep = argv[i] + strlen(p);
    174 		size_t n = _rtld_expand_path(buf, sizeof(buf), argv[0], p, ep);
    175 		printf("%s\n", buf);
    176 	}
    177 	return 0;
    178 }
    179 #endif
    180