xref: /illumos-gate/usr/src/lib/libc/port/gen/getauxv.c (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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 
41 static mutex_t auxlock = DEFAULTMUTEX;
42 
43 /*
44  * Get auxiliary entry.
45  * Returns pointer to entry, or 0 if entry does not exist.
46  */
47 static auxv_t *
48 _getaux(int type)
49 {
50 	static auxv_t *auxb = NULL;
51 	static size_t nauxv = 0;
52 	ssize_t i;
53 
54 	/*
55 	 * The first time through, read the initial aux vector that was
56 	 * passed to the process at exec(2).  Only do this once.
57 	 */
58 	if (auxb == NULL) {
59 		struct stat statb;
60 		int fd;
61 
62 		lmutex_lock(&auxlock);
63 
64 		if (auxb == NULL) {
65 			if ((fd = open("/proc/self/auxv", O_RDONLY)) != -1 &&
66 			    fstat(fd, &statb) != -1)
67 				auxb = libc_malloc(
68 				    statb.st_size + sizeof (auxv_t));
69 
70 			if (auxb != NULL) {
71 				i = read(fd, auxb, statb.st_size);
72 				if (i != -1) {
73 					nauxv = i / sizeof (auxv_t);
74 					auxb[nauxv].a_type = AT_NULL;
75 				} else {
76 					libc_free(auxb);
77 					auxb = NULL;
78 				}
79 			}
80 
81 			if (fd != -1)
82 				(void) close(fd);
83 		}
84 
85 		lmutex_unlock(&auxlock);
86 	}
87 
88 	/*
89 	 * Scan the auxiliary entries looking for the required type.
90 	 */
91 	for (i = 0; i < nauxv; i++)
92 		if (auxb[i].a_type == type)
93 			return (&auxb[i]);
94 
95 	/*
96 	 * No auxiliary array (static executable) or entry not found.
97 	 */
98 	return ((auxv_t *)0);
99 }
100 
101 /*
102  * These two routines are utilities exported to the rest of libc.
103  */
104 
105 long
106 ___getauxval(int type)
107 {
108 	auxv_t *auxp;
109 
110 	if ((auxp = _getaux(type)) != (auxv_t *)0)
111 		return (auxp->a_un.a_val);
112 	return (0);
113 }
114 
115 void *
116 ___getauxptr(int type)
117 {
118 	auxv_t *auxp;
119 
120 	if ((auxp = _getaux(type)) != (auxv_t *)0)
121 		return (auxp->a_un.a_ptr);
122 	return (0);
123 }
124