xref: /illumos-gate/usr/src/lib/libc/port/gen/getauxv.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include <libc.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/auxv.h>
37 #include <mtlib.h>
38 #include <thread.h>
39 #include <synch.h>
40 #include <atomic.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 			struct stat statb;
63 			auxv_t *buf = NULL;
64 			int fd;
65 
66 			if ((fd = open("/proc/self/auxv", O_RDONLY)) != -1 &&
67 			    fstat(fd, &statb) != -1)
68 				buf = libc_malloc(
69 				    statb.st_size + sizeof (auxv_t));
70 
71 			if (buf != NULL) {
72 				i = read(fd, buf, statb.st_size);
73 				if (i != -1) {
74 					nauxv = i / sizeof (auxv_t);
75 					buf[nauxv].a_type = AT_NULL;
76 				} else {
77 					libc_free(buf);
78 					buf = NULL;
79 				}
80 			}
81 
82 			if (fd != -1)
83 				(void) close(fd);
84 
85 			membar_producer();
86 			auxb = buf;
87 		}
88 		lmutex_unlock(&auxlock);
89 	}
90 	membar_consumer();
91 
92 	/*
93 	 * Scan the auxiliary entries looking for the required type.
94 	 */
95 	for (i = 0; i < nauxv; i++)
96 		if (auxb[i].a_type == type)
97 			return (&auxb[i]);
98 
99 	/*
100 	 * No auxiliary array (static executable) or entry not found.
101 	 */
102 	return ((auxv_t *)0);
103 }
104 
105 /*
106  * These two routines are utilities exported to the rest of libc.
107  */
108 
109 long
110 ___getauxval(int type)
111 {
112 	auxv_t *auxp;
113 
114 	if ((auxp = _getaux(type)) != (auxv_t *)0)
115 		return (auxp->a_un.a_val);
116 	return (0);
117 }
118 
119 void *
120 ___getauxptr(int type)
121 {
122 	auxv_t *auxp;
123 
124 	if ((auxp = _getaux(type)) != (auxv_t *)0)
125 		return (auxp->a_un.a_ptr);
126 	return (0);
127 }
128