1/*
| 1/*
|
2 * $FreeBSD: head/libexec/rtld-elf/libmap.c 115150 2003-05-19 07:10:12Z mdodd $
| 2 * $FreeBSD: head/libexec/rtld-elf/libmap.c 115396 2003-05-29 22:58:26Z kan $
|
3 */ 4 5#include <stdio.h> 6#include <ctype.h> 7#include <string.h> 8#include <stdlib.h> 9#include <sys/queue.h> 10#include <sys/param.h> 11 12#include "debug.h" 13#include "rtld.h" 14 15#ifndef _PATH_LIBMAP_CONF 16#define _PATH_LIBMAP_CONF "/etc/libmap.conf" 17#endif 18 19TAILQ_HEAD(lm_list, lm); 20struct lm { 21 char *f; 22 char *t; 23 24 TAILQ_ENTRY(lm) lm_link; 25}; 26 27TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head); 28struct lmp { 29 char *p; 30 struct lm_list lml; 31 TAILQ_ENTRY(lmp) lmp_link; 32}; 33 34static void lm_add (char *, char *, char *); 35static void lm_free (struct lm_list *); 36static char * lml_find (struct lm_list *, const char *); 37static struct lm_list * lmp_find (const char *); 38static struct lm_list * lmp_init (char *); 39 40#define iseol(c) (((c) == '#') || ((c) == '\0') || \ 41 ((c) == '\n') || ((c) == '\r')) 42 43void 44lm_init (void) 45{ 46 FILE *fp; 47 char *cp; 48 char *f, *t, *p; 49 char prog[MAXPATHLEN]; 50 char line[MAXPATHLEN + 2]; 51 52 TAILQ_INIT(&lmp_head); 53 54 if ((fp = fopen(_PATH_LIBMAP_CONF, "r")) == NULL) 55 return; 56 57 p = NULL; 58 while ((cp = fgets(line, MAXPATHLEN + 1, fp)) != NULL) { 59 t = f = NULL; 60 61 /* Skip over leading space */ 62 while (isspace(*cp)) cp++; 63 64 /* Found a comment or EOL */ 65 if (iseol(*cp)) continue; 66 67 /* Found a constraint selector */ 68 if (*cp == '[') { 69 cp++; 70 71 /* Skip leading space */ 72 while (isspace(*cp)) cp++; 73 74 /* Found comment, EOL or end of selector */ 75 if (iseol(*cp) || *cp == ']') 76 continue; 77 78 p = cp++; 79 /* Skip to end of word */ 80 while (!isspace(*cp) && !iseol(*cp) && *cp != ']') 81 cp++; 82 83 /* Skip and zero out trailing space */ 84 while (isspace(*cp)) *cp++ = '\0'; 85 86 /* Check if there is a closing brace */ 87 if (*cp != ']') continue; 88 89 /* Terminate string if there was no trailing space */ 90 *cp++ = '\0'; 91 92 /* 93 * There should be nothing except whitespace or comment
| 3 */ 4 5#include <stdio.h> 6#include <ctype.h> 7#include <string.h> 8#include <stdlib.h> 9#include <sys/queue.h> 10#include <sys/param.h> 11 12#include "debug.h" 13#include "rtld.h" 14 15#ifndef _PATH_LIBMAP_CONF 16#define _PATH_LIBMAP_CONF "/etc/libmap.conf" 17#endif 18 19TAILQ_HEAD(lm_list, lm); 20struct lm { 21 char *f; 22 char *t; 23 24 TAILQ_ENTRY(lm) lm_link; 25}; 26 27TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head); 28struct lmp { 29 char *p; 30 struct lm_list lml; 31 TAILQ_ENTRY(lmp) lmp_link; 32}; 33 34static void lm_add (char *, char *, char *); 35static void lm_free (struct lm_list *); 36static char * lml_find (struct lm_list *, const char *); 37static struct lm_list * lmp_find (const char *); 38static struct lm_list * lmp_init (char *); 39 40#define iseol(c) (((c) == '#') || ((c) == '\0') || \ 41 ((c) == '\n') || ((c) == '\r')) 42 43void 44lm_init (void) 45{ 46 FILE *fp; 47 char *cp; 48 char *f, *t, *p; 49 char prog[MAXPATHLEN]; 50 char line[MAXPATHLEN + 2]; 51 52 TAILQ_INIT(&lmp_head); 53 54 if ((fp = fopen(_PATH_LIBMAP_CONF, "r")) == NULL) 55 return; 56 57 p = NULL; 58 while ((cp = fgets(line, MAXPATHLEN + 1, fp)) != NULL) { 59 t = f = NULL; 60 61 /* Skip over leading space */ 62 while (isspace(*cp)) cp++; 63 64 /* Found a comment or EOL */ 65 if (iseol(*cp)) continue; 66 67 /* Found a constraint selector */ 68 if (*cp == '[') { 69 cp++; 70 71 /* Skip leading space */ 72 while (isspace(*cp)) cp++; 73 74 /* Found comment, EOL or end of selector */ 75 if (iseol(*cp) || *cp == ']') 76 continue; 77 78 p = cp++; 79 /* Skip to end of word */ 80 while (!isspace(*cp) && !iseol(*cp) && *cp != ']') 81 cp++; 82 83 /* Skip and zero out trailing space */ 84 while (isspace(*cp)) *cp++ = '\0'; 85 86 /* Check if there is a closing brace */ 87 if (*cp != ']') continue; 88 89 /* Terminate string if there was no trailing space */ 90 *cp++ = '\0'; 91 92 /* 93 * There should be nothing except whitespace or comment
|
94 * from this point to the end of the line.
| 94 from this point to the end of the line.
|
95 */ 96 while(isspace(*cp++)); 97 if (!iseol(*cp)) continue; 98 99 strcpy(prog, p); 100 p = prog; 101 continue; 102 } 103 104 /* Parse the 'from' candidate. */ 105 f = cp++; 106 while (!isspace(*cp) && !iseol(*cp)) cp++; 107 108 /* Skip and zero out the trailing whitespace */ 109 while (isspace(*cp)) *cp++ = '\0'; 110 111 /* Found a comment or EOL */ 112 if (iseol(*cp)) continue; 113 114 /* Parse 'to' mapping */ 115 t = cp++; 116 while (!isspace(*cp) && !iseol(*cp)) cp++;
| 95 */ 96 while(isspace(*cp++)); 97 if (!iseol(*cp)) continue; 98 99 strcpy(prog, p); 100 p = prog; 101 continue; 102 } 103 104 /* Parse the 'from' candidate. */ 105 f = cp++; 106 while (!isspace(*cp) && !iseol(*cp)) cp++; 107 108 /* Skip and zero out the trailing whitespace */ 109 while (isspace(*cp)) *cp++ = '\0'; 110 111 /* Found a comment or EOL */ 112 if (iseol(*cp)) continue; 113 114 /* Parse 'to' mapping */ 115 t = cp++; 116 while (!isspace(*cp) && !iseol(*cp)) cp++;
|
117
| 117
|
118 /* Skip and zero out the trailing whitespace */ 119 while (isspace(*cp)) *cp++ = '\0'; 120 121 /* Should be no extra tokens at this point */ 122 if (!iseol(*cp)) continue; 123 124 *cp = '\0'; 125 lm_add(p, xstrdup(f), xstrdup(t)); 126 } 127 fclose(fp); 128 return; 129} 130 131static void 132lm_free (struct lm_list *lml) 133{ 134 struct lm *lm; 135 136 while (!TAILQ_EMPTY(lml)) { 137 lm = TAILQ_FIRST(lml); 138 TAILQ_REMOVE(lml, lm, lm_link); 139 free(lm->f); 140 free(lm->t); 141 free(lm); 142 } 143 return; 144} 145 146void 147lm_fini (void) 148{ 149 struct lmp *lmp; 150 151 while (!TAILQ_EMPTY(&lmp_head)) { 152 lmp = TAILQ_FIRST(&lmp_head); 153 TAILQ_REMOVE(&lmp_head, lmp, lmp_link); 154 free(lmp->p); 155 lm_free(&lmp->lml); 156 free(lmp); 157 } 158 return; 159} 160 161static void 162lm_add (char *p, char *f, char *t) 163{ 164 struct lm_list *lml; 165 struct lm *lm; 166 167 if (p == NULL) 168 p = "$DEFAULT$"; 169
| 118 /* Skip and zero out the trailing whitespace */ 119 while (isspace(*cp)) *cp++ = '\0'; 120 121 /* Should be no extra tokens at this point */ 122 if (!iseol(*cp)) continue; 123 124 *cp = '\0'; 125 lm_add(p, xstrdup(f), xstrdup(t)); 126 } 127 fclose(fp); 128 return; 129} 130 131static void 132lm_free (struct lm_list *lml) 133{ 134 struct lm *lm; 135 136 while (!TAILQ_EMPTY(lml)) { 137 lm = TAILQ_FIRST(lml); 138 TAILQ_REMOVE(lml, lm, lm_link); 139 free(lm->f); 140 free(lm->t); 141 free(lm); 142 } 143 return; 144} 145 146void 147lm_fini (void) 148{ 149 struct lmp *lmp; 150 151 while (!TAILQ_EMPTY(&lmp_head)) { 152 lmp = TAILQ_FIRST(&lmp_head); 153 TAILQ_REMOVE(&lmp_head, lmp, lmp_link); 154 free(lmp->p); 155 lm_free(&lmp->lml); 156 free(lmp); 157 } 158 return; 159} 160 161static void 162lm_add (char *p, char *f, char *t) 163{ 164 struct lm_list *lml; 165 struct lm *lm; 166 167 if (p == NULL) 168 p = "$DEFAULT$"; 169
|
170#if 0 171 printf("%s(\"%s\", \"%s\", \"%s\")\n", __func__, p, f, t); 172#endif 173
| |
174 if ((lml = lmp_find(p)) == NULL) 175 lml = lmp_init(xstrdup(p)); 176 177 lm = xmalloc(sizeof(struct lm)); 178 lm->f = f; 179 lm->t = t; 180 TAILQ_INSERT_HEAD(lml, lm, lm_link); 181} 182 183char * 184lm_find (const char *p, const char *f) 185{ 186 struct lm_list *lml; 187 char *t; 188 189 if (p != NULL && (lml = lmp_find(p)) != NULL) { 190 t = lml_find(lml, f); 191 if (t != NULL) { 192 /* 193 * Add a global mapping if we have 194 * a successful constrained match. 195 */ 196 lm_add(NULL, xstrdup(f), xstrdup(t)); 197 return (t); 198 } 199 } 200 lml = lmp_find("$DEFAULT$"); 201 if (lml != NULL) 202 return (lml_find(lml, f)); 203 else 204 return (NULL); 205} 206 207static char * 208lml_find (struct lm_list *lmh, const char *f) 209{ 210 struct lm *lm; 211 212 TAILQ_FOREACH(lm, lmh, lm_link) 213 if ((strncmp(f, lm->f, strlen(lm->f)) == 0) && 214 (strlen(f) == strlen(lm->f))) 215 return (lm->t); 216 return NULL; 217} 218 219static struct lm_list * 220lmp_find (const char *n) 221{ 222 struct lmp *lmp; 223 224 TAILQ_FOREACH(lmp, &lmp_head, lmp_link) 225 if ((strncmp(n, lmp->p, strlen(lmp->p)) == 0) && 226 (strlen(n) == strlen(lmp->p))) 227 return (&lmp->lml); 228 return (NULL); 229} 230 231static struct lm_list * 232lmp_init (char *n) 233{ 234 struct lmp *lmp; 235 236 lmp = xmalloc(sizeof(struct lmp)); 237 lmp->p = n; 238 TAILQ_INIT(&lmp->lml); 239 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link); 240 241 return (&lmp->lml); 242}
| 170 if ((lml = lmp_find(p)) == NULL) 171 lml = lmp_init(xstrdup(p)); 172 173 lm = xmalloc(sizeof(struct lm)); 174 lm->f = f; 175 lm->t = t; 176 TAILQ_INSERT_HEAD(lml, lm, lm_link); 177} 178 179char * 180lm_find (const char *p, const char *f) 181{ 182 struct lm_list *lml; 183 char *t; 184 185 if (p != NULL && (lml = lmp_find(p)) != NULL) { 186 t = lml_find(lml, f); 187 if (t != NULL) { 188 /* 189 * Add a global mapping if we have 190 * a successful constrained match. 191 */ 192 lm_add(NULL, xstrdup(f), xstrdup(t)); 193 return (t); 194 } 195 } 196 lml = lmp_find("$DEFAULT$"); 197 if (lml != NULL) 198 return (lml_find(lml, f)); 199 else 200 return (NULL); 201} 202 203static char * 204lml_find (struct lm_list *lmh, const char *f) 205{ 206 struct lm *lm; 207 208 TAILQ_FOREACH(lm, lmh, lm_link) 209 if ((strncmp(f, lm->f, strlen(lm->f)) == 0) && 210 (strlen(f) == strlen(lm->f))) 211 return (lm->t); 212 return NULL; 213} 214 215static struct lm_list * 216lmp_find (const char *n) 217{ 218 struct lmp *lmp; 219 220 TAILQ_FOREACH(lmp, &lmp_head, lmp_link) 221 if ((strncmp(n, lmp->p, strlen(lmp->p)) == 0) && 222 (strlen(n) == strlen(lmp->p))) 223 return (&lmp->lml); 224 return (NULL); 225} 226 227static struct lm_list * 228lmp_init (char *n) 229{ 230 struct lmp *lmp; 231 232 lmp = xmalloc(sizeof(struct lmp)); 233 lmp->p = n; 234 TAILQ_INIT(&lmp->lml); 235 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link); 236 237 return (&lmp->lml); 238}
|
| 239
|
| |