xref: /illumos-gate/usr/src/lib/libc/port/gen/getauxv.c (revision 1da57d55)
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 
27*7257d1b4Sraf #include "lint.h"
287c478bd9Sstevel@tonic-gate #include <libc.h>
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
357c478bd9Sstevel@tonic-gate #include <mtlib.h>
367c478bd9Sstevel@tonic-gate #include <thread.h>
377c478bd9Sstevel@tonic-gate #include <synch.h>
38cb620785Sraf #include <atomic.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static mutex_t auxlock = DEFAULTMUTEX;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Get auxiliary entry.
447c478bd9Sstevel@tonic-gate  * Returns pointer to entry, or 0 if entry does not exist.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate static auxv_t *
_getaux(int type)477c478bd9Sstevel@tonic-gate _getaux(int type)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	static auxv_t *auxb = NULL;
507c478bd9Sstevel@tonic-gate 	static size_t nauxv = 0;
517c478bd9Sstevel@tonic-gate 	ssize_t i;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	/*
547c478bd9Sstevel@tonic-gate 	 * The first time through, read the initial aux vector that was
557c478bd9Sstevel@tonic-gate 	 * passed to the process at exec(2).  Only do this once.
567c478bd9Sstevel@tonic-gate 	 */
577c478bd9Sstevel@tonic-gate 	if (auxb == NULL) {
587c478bd9Sstevel@tonic-gate 		lmutex_lock(&auxlock);
597c478bd9Sstevel@tonic-gate 		if (auxb == NULL) {
60cb620785Sraf 			struct stat statb;
61cb620785Sraf 			auxv_t *buf = NULL;
62cb620785Sraf 			int fd;
63cb620785Sraf 
647c478bd9Sstevel@tonic-gate 			if ((fd = open("/proc/self/auxv", O_RDONLY)) != -1 &&
657c478bd9Sstevel@tonic-gate 			    fstat(fd, &statb) != -1)
66cb620785Sraf 				buf = libc_malloc(
677c478bd9Sstevel@tonic-gate 				    statb.st_size + sizeof (auxv_t));
687c478bd9Sstevel@tonic-gate 
69cb620785Sraf 			if (buf != NULL) {
70cb620785Sraf 				i = read(fd, buf, statb.st_size);
717c478bd9Sstevel@tonic-gate 				if (i != -1) {
727c478bd9Sstevel@tonic-gate 					nauxv = i / sizeof (auxv_t);
73cb620785Sraf 					buf[nauxv].a_type = AT_NULL;
747c478bd9Sstevel@tonic-gate 				} else {
75cb620785Sraf 					libc_free(buf);
76cb620785Sraf 					buf = NULL;
777c478bd9Sstevel@tonic-gate 				}
787c478bd9Sstevel@tonic-gate 			}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 			if (fd != -1)
817c478bd9Sstevel@tonic-gate 				(void) close(fd);
827c478bd9Sstevel@tonic-gate 
83cb620785Sraf 			membar_producer();
84cb620785Sraf 			auxb = buf;
85cb620785Sraf 		}
867c478bd9Sstevel@tonic-gate 		lmutex_unlock(&auxlock);
877c478bd9Sstevel@tonic-gate 	}
88cb620785Sraf 	membar_consumer();
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 * Scan the auxiliary entries looking for the required type.
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	for (i = 0; i < nauxv; i++)
947c478bd9Sstevel@tonic-gate 		if (auxb[i].a_type == type)
957c478bd9Sstevel@tonic-gate 			return (&auxb[i]);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * No auxiliary array (static executable) or entry not found.
997c478bd9Sstevel@tonic-gate 	 */
1007c478bd9Sstevel@tonic-gate 	return ((auxv_t *)0);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * These two routines are utilities exported to the rest of libc.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate long
___getauxval(int type)1087c478bd9Sstevel@tonic-gate ___getauxval(int type)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	auxv_t *auxp;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if ((auxp = _getaux(type)) != (auxv_t *)0)
1137c478bd9Sstevel@tonic-gate 		return (auxp->a_un.a_val);
1147c478bd9Sstevel@tonic-gate 	return (0);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate void *
___getauxptr(int type)1187c478bd9Sstevel@tonic-gate ___getauxptr(int type)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	auxv_t *auxp;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if ((auxp = _getaux(type)) != (auxv_t *)0)
1237c478bd9Sstevel@tonic-gate 		return (auxp->a_un.a_ptr);
1247c478bd9Sstevel@tonic-gate 	return (0);
1257c478bd9Sstevel@tonic-gate }
126