xref: /titanic_41/usr/src/lib/libc/port/gen/getauxv.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb620785Sraf  * Common Development and Distribution License (the "License").
6cb620785Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e8031f0aSraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
29*7257d1b4Sraf #include "lint.h"
307c478bd9Sstevel@tonic-gate #include <libc.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
377c478bd9Sstevel@tonic-gate #include <mtlib.h>
387c478bd9Sstevel@tonic-gate #include <thread.h>
397c478bd9Sstevel@tonic-gate #include <synch.h>
40cb620785Sraf #include <atomic.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static mutex_t auxlock = DEFAULTMUTEX;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Get auxiliary entry.
467c478bd9Sstevel@tonic-gate  * Returns pointer to entry, or 0 if entry does not exist.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate static auxv_t *
_getaux(int type)497c478bd9Sstevel@tonic-gate _getaux(int type)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	static auxv_t *auxb = NULL;
527c478bd9Sstevel@tonic-gate 	static size_t nauxv = 0;
537c478bd9Sstevel@tonic-gate 	ssize_t i;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	/*
567c478bd9Sstevel@tonic-gate 	 * The first time through, read the initial aux vector that was
577c478bd9Sstevel@tonic-gate 	 * passed to the process at exec(2).  Only do this once.
587c478bd9Sstevel@tonic-gate 	 */
597c478bd9Sstevel@tonic-gate 	if (auxb == NULL) {
60cb620785Sraf 		lmutex_lock(&auxlock);
61cb620785Sraf 		if (auxb == NULL) {
627c478bd9Sstevel@tonic-gate 			struct stat statb;
63cb620785Sraf 			auxv_t *buf = NULL;
647c478bd9Sstevel@tonic-gate 			int fd;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 			if ((fd = open("/proc/self/auxv", O_RDONLY)) != -1 &&
677c478bd9Sstevel@tonic-gate 			    fstat(fd, &statb) != -1)
68cb620785Sraf 				buf = libc_malloc(
697c478bd9Sstevel@tonic-gate 				    statb.st_size + sizeof (auxv_t));
707c478bd9Sstevel@tonic-gate 
71cb620785Sraf 			if (buf != NULL) {
72cb620785Sraf 				i = read(fd, buf, statb.st_size);
737c478bd9Sstevel@tonic-gate 				if (i != -1) {
747c478bd9Sstevel@tonic-gate 					nauxv = i / sizeof (auxv_t);
75cb620785Sraf 					buf[nauxv].a_type = AT_NULL;
767c478bd9Sstevel@tonic-gate 				} else {
77cb620785Sraf 					libc_free(buf);
78cb620785Sraf 					buf = NULL;
797c478bd9Sstevel@tonic-gate 				}
807c478bd9Sstevel@tonic-gate 			}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 			if (fd != -1)
837c478bd9Sstevel@tonic-gate 				(void) close(fd);
847c478bd9Sstevel@tonic-gate 
85cb620785Sraf 			membar_producer();
86cb620785Sraf 			auxb = buf;
87cb620785Sraf 		}
887c478bd9Sstevel@tonic-gate 		lmutex_unlock(&auxlock);
897c478bd9Sstevel@tonic-gate 	}
90cb620785Sraf 	membar_consumer();
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * Scan the auxiliary entries looking for the required type.
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	for (i = 0; i < nauxv; i++)
967c478bd9Sstevel@tonic-gate 		if (auxb[i].a_type == type)
977c478bd9Sstevel@tonic-gate 			return (&auxb[i]);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * No auxiliary array (static executable) or entry not found.
1017c478bd9Sstevel@tonic-gate 	 */
1027c478bd9Sstevel@tonic-gate 	return ((auxv_t *)0);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * These two routines are utilities exported to the rest of libc.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate long
___getauxval(int type)1107c478bd9Sstevel@tonic-gate ___getauxval(int type)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	auxv_t *auxp;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if ((auxp = _getaux(type)) != (auxv_t *)0)
1157c478bd9Sstevel@tonic-gate 		return (auxp->a_un.a_val);
1167c478bd9Sstevel@tonic-gate 	return (0);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate void *
___getauxptr(int type)1207c478bd9Sstevel@tonic-gate ___getauxptr(int type)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	auxv_t *auxp;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if ((auxp = _getaux(type)) != (auxv_t *)0)
1257c478bd9Sstevel@tonic-gate 		return (auxp->a_un.a_ptr);
1267c478bd9Sstevel@tonic-gate 	return (0);
1277c478bd9Sstevel@tonic-gate }
128