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