1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #include "lint.h"
28 #include "thr_uberdata.h"
29 #include <libc.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/auxv.h>
36 #include <mtlib.h>
37 #include <thread.h>
38 #include <synch.h>
39 #include <atomic.h>
40 #include <limits.h>
41
42 static mutex_t auxlock = DEFAULTMUTEX;
43
44 /*
45 * Get auxiliary entry.
46 * Returns pointer to entry, or 0 if entry does not exist.
47 */
48 static auxv_t *
49 _getaux(int type)
50 {
51 static auxv_t *auxb = NULL;
52 static size_t nauxv = 0;
53 ssize_t i;
54
55 /*
56 * The first time through, read the initial aux vector that was
57 * passed to the process at exec(2). Only do this once.
58 */
59 if (auxb == NULL) {
60 lmutex_lock(&auxlock);
61 if (auxb == NULL) {
62 uberdata_t *udp = curthread->ul_uberdata;
63 struct stat statb;
64 auxv_t *buf = NULL;
65 char *path = "/proc/self/auxv";
66 char pbuf[PATH_MAX];
67 int fd;
68
69 if (udp->ub_broot != NULL) {
70 (void) snprintf(pbuf, sizeof (pbuf),
71 "%s/proc/self/auxv", udp->ub_broot);
72 path = pbuf;
73 }
74
75 if ((fd = open(path, O_RDONLY)) != -1 &&
76 fstat(fd, &statb) != -1)
77 buf = libc_malloc(
78 statb.st_size + sizeof (auxv_t));
79
80 if (buf != NULL) {
81 i = read(fd, buf, statb.st_size);
82 if (i != -1) {
83 nauxv = i / sizeof (auxv_t);
84 buf[nauxv].a_type = AT_NULL;
85 } else {
86 libc_free(buf);
87 buf = NULL;
88 }
89 }
90
91 if (fd != -1)
92 (void) close(fd);
93
94 membar_producer();
95 auxb = buf;
96 }
97 lmutex_unlock(&auxlock);
98 }
99 membar_consumer();
100
101 /*
102 * Scan the auxiliary entries looking for the required type.
103 */
104 for (i = 0; i < nauxv; i++)
105 if (auxb[i].a_type == type)
106 return (&auxb[i]);
107
108 /*
109 * No auxiliary array (static executable) or entry not found.
110 */
111 return ((auxv_t *)0);
112 }
113
114 /*
115 * These two routines are utilities exported to the rest of libc.
116 */
117
118 long
119 ___getauxval(int type)
120 {
121 auxv_t *auxp;
122
123 if ((auxp = _getaux(type)) != (auxv_t *)0)
124 return (auxp->a_un.a_val);
125 return (0);
126 }
127
128 void *
129 ___getauxptr(int type)
130 {
131 auxv_t *auxp;
132
133 if ((auxp = _getaux(type)) != (auxv_t *)0)
134 return (auxp->a_un.a_ptr);
135 return (0);
136 }