xref: /onnv-gate/usr/src/lib/libc/port/gen/getauxv.c (revision 1219:f89f56c2d9ac)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*1219Sraf 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
30*1219Sraf #include "synonyms.h"
310Sstevel@tonic-gate #include <libc.h>
320Sstevel@tonic-gate #include <fcntl.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <sys/auxv.h>
380Sstevel@tonic-gate #include <mtlib.h>
390Sstevel@tonic-gate #include <thread.h>
400Sstevel@tonic-gate #include <synch.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 *
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) {
600Sstevel@tonic-gate 		struct stat statb;
610Sstevel@tonic-gate 		int fd;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 		lmutex_lock(&auxlock);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 		if (auxb == NULL) {
660Sstevel@tonic-gate 			if ((fd = open("/proc/self/auxv", O_RDONLY)) != -1 &&
670Sstevel@tonic-gate 			    fstat(fd, &statb) != -1)
680Sstevel@tonic-gate 				auxb = libc_malloc(
690Sstevel@tonic-gate 				    statb.st_size + sizeof (auxv_t));
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 			if (auxb != NULL) {
720Sstevel@tonic-gate 				i = read(fd, auxb, statb.st_size);
730Sstevel@tonic-gate 				if (i != -1) {
740Sstevel@tonic-gate 					nauxv = i / sizeof (auxv_t);
750Sstevel@tonic-gate 					auxb[nauxv].a_type = AT_NULL;
760Sstevel@tonic-gate 				} else {
770Sstevel@tonic-gate 					libc_free(auxb);
780Sstevel@tonic-gate 					auxb = NULL;
790Sstevel@tonic-gate 				}
800Sstevel@tonic-gate 			}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 			if (fd != -1)
830Sstevel@tonic-gate 				(void) close(fd);
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 		lmutex_unlock(&auxlock);
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	/*
900Sstevel@tonic-gate 	 * Scan the auxiliary entries looking for the required type.
910Sstevel@tonic-gate 	 */
920Sstevel@tonic-gate 	for (i = 0; i < nauxv; i++)
930Sstevel@tonic-gate 		if (auxb[i].a_type == type)
940Sstevel@tonic-gate 			return (&auxb[i]);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/*
970Sstevel@tonic-gate 	 * No auxiliary array (static executable) or entry not found.
980Sstevel@tonic-gate 	 */
990Sstevel@tonic-gate 	return ((auxv_t *)0);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * These two routines are utilities exported to the rest of libc.
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate long
1070Sstevel@tonic-gate ___getauxval(int type)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	auxv_t *auxp;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if ((auxp = _getaux(type)) != (auxv_t *)0)
1120Sstevel@tonic-gate 		return (auxp->a_un.a_val);
1130Sstevel@tonic-gate 	return (0);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate void *
1170Sstevel@tonic-gate ___getauxptr(int type)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	auxv_t *auxp;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if ((auxp = _getaux(type)) != (auxv_t *)0)
1220Sstevel@tonic-gate 		return (auxp->a_un.a_ptr);
1230Sstevel@tonic-gate 	return (0);
1240Sstevel@tonic-gate }
125