xref: /onnv-gate/usr/src/lib/libc/port/gen/getauxv.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53864Sraf  * Common Development and Distribution License (the "License").
63864Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
29*6812Sraf #include "lint.h"
300Sstevel@tonic-gate #include <libc.h>
310Sstevel@tonic-gate #include <fcntl.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/auxv.h>
370Sstevel@tonic-gate #include <mtlib.h>
380Sstevel@tonic-gate #include <thread.h>
390Sstevel@tonic-gate #include <synch.h>
403864Sraf #include <atomic.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static mutex_t auxlock = DEFAULTMUTEX;
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Get auxiliary entry.
460Sstevel@tonic-gate  * Returns pointer to entry, or 0 if entry does not exist.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate static auxv_t *
_getaux(int type)490Sstevel@tonic-gate _getaux(int type)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	static auxv_t *auxb = NULL;
520Sstevel@tonic-gate 	static size_t nauxv = 0;
530Sstevel@tonic-gate 	ssize_t i;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	/*
560Sstevel@tonic-gate 	 * The first time through, read the initial aux vector that was
570Sstevel@tonic-gate 	 * passed to the process at exec(2).  Only do this once.
580Sstevel@tonic-gate 	 */
590Sstevel@tonic-gate 	if (auxb == NULL) {
603864Sraf 		lmutex_lock(&auxlock);
613864Sraf 		if (auxb == NULL) {
623864Sraf 			struct stat statb;
633864Sraf 			auxv_t *buf = NULL;
643864Sraf 			int fd;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 			if ((fd = open("/proc/self/auxv", O_RDONLY)) != -1 &&
670Sstevel@tonic-gate 			    fstat(fd, &statb) != -1)
683864Sraf 				buf = libc_malloc(
690Sstevel@tonic-gate 				    statb.st_size + sizeof (auxv_t));
700Sstevel@tonic-gate 
713864Sraf 			if (buf != NULL) {
723864Sraf 				i = read(fd, buf, statb.st_size);
730Sstevel@tonic-gate 				if (i != -1) {
740Sstevel@tonic-gate 					nauxv = i / sizeof (auxv_t);
753864Sraf 					buf[nauxv].a_type = AT_NULL;
760Sstevel@tonic-gate 				} else {
773864Sraf 					libc_free(buf);
783864Sraf 					buf = NULL;
790Sstevel@tonic-gate 				}
800Sstevel@tonic-gate 			}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 			if (fd != -1)
830Sstevel@tonic-gate 				(void) close(fd);
843864Sraf 
853864Sraf 			membar_producer();
863864Sraf 			auxb = buf;
870Sstevel@tonic-gate 		}
880Sstevel@tonic-gate 		lmutex_unlock(&auxlock);
890Sstevel@tonic-gate 	}
903864Sraf 	membar_consumer();
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	/*
930Sstevel@tonic-gate 	 * Scan the auxiliary entries looking for the required type.
940Sstevel@tonic-gate 	 */
950Sstevel@tonic-gate 	for (i = 0; i < nauxv; i++)
960Sstevel@tonic-gate 		if (auxb[i].a_type == type)
970Sstevel@tonic-gate 			return (&auxb[i]);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 * No auxiliary array (static executable) or entry not found.
1010Sstevel@tonic-gate 	 */
1020Sstevel@tonic-gate 	return ((auxv_t *)0);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * These two routines are utilities exported to the rest of libc.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate long
___getauxval(int type)1100Sstevel@tonic-gate ___getauxval(int type)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	auxv_t *auxp;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if ((auxp = _getaux(type)) != (auxv_t *)0)
1150Sstevel@tonic-gate 		return (auxp->a_un.a_val);
1160Sstevel@tonic-gate 	return (0);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate void *
___getauxptr(int type)1200Sstevel@tonic-gate ___getauxptr(int type)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	auxv_t *auxp;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if ((auxp = _getaux(type)) != (auxv_t *)0)
1250Sstevel@tonic-gate 		return (auxp->a_un.a_ptr);
1260Sstevel@tonic-gate 	return (0);
1270Sstevel@tonic-gate }
128