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 5*3864Sraf * Common Development and Distribution License (the "License"). 6*3864Sraf * 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*3864Sraf * Copyright 2007 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 291219Sraf #include "synonyms.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> 40*3864Sraf #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 * 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) { 60*3864Sraf lmutex_lock(&auxlock); 61*3864Sraf if (auxb == NULL) { 62*3864Sraf struct stat statb; 63*3864Sraf auxv_t *buf = NULL; 64*3864Sraf int fd; 650Sstevel@tonic-gate 660Sstevel@tonic-gate if ((fd = open("/proc/self/auxv", O_RDONLY)) != -1 && 670Sstevel@tonic-gate fstat(fd, &statb) != -1) 68*3864Sraf buf = libc_malloc( 690Sstevel@tonic-gate statb.st_size + sizeof (auxv_t)); 700Sstevel@tonic-gate 71*3864Sraf if (buf != NULL) { 72*3864Sraf i = read(fd, buf, statb.st_size); 730Sstevel@tonic-gate if (i != -1) { 740Sstevel@tonic-gate nauxv = i / sizeof (auxv_t); 75*3864Sraf buf[nauxv].a_type = AT_NULL; 760Sstevel@tonic-gate } else { 77*3864Sraf libc_free(buf); 78*3864Sraf buf = NULL; 790Sstevel@tonic-gate } 800Sstevel@tonic-gate } 810Sstevel@tonic-gate 820Sstevel@tonic-gate if (fd != -1) 830Sstevel@tonic-gate (void) close(fd); 84*3864Sraf 85*3864Sraf membar_producer(); 86*3864Sraf auxb = buf; 870Sstevel@tonic-gate } 880Sstevel@tonic-gate lmutex_unlock(&auxlock); 890Sstevel@tonic-gate } 90*3864Sraf 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 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 * 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