import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / gen / getauxv.c
blob500675719c9c65c20d0e8fa7b76e8829ffcc2747
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "lint.h"
30 #include <libc.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/auxv.h>
37 #include <mtlib.h>
38 #include <thread.h>
39 #include <synch.h>
40 #include <atomic.h>
42 static mutex_t auxlock = DEFAULTMUTEX;
45 * Get auxiliary entry.
46 * Returns pointer to entry, or 0 if entry does not exist.
48 static auxv_t *
49 _getaux(int type)
51 static auxv_t *auxb = NULL;
52 static size_t nauxv = 0;
53 ssize_t i;
56 * The first time through, read the initial aux vector that was
57 * passed to the process at exec(2). Only do this once.
59 if (auxb == NULL) {
60 lmutex_lock(&auxlock);
61 if (auxb == NULL) {
62 struct stat statb;
63 auxv_t *buf = NULL;
64 int fd;
66 if ((fd = open("/proc/self/auxv", O_RDONLY)) != -1 &&
67 fstat(fd, &statb) != -1)
68 buf = libc_malloc(
69 statb.st_size + sizeof (auxv_t));
71 if (buf != NULL) {
72 i = read(fd, buf, statb.st_size);
73 if (i != -1) {
74 nauxv = i / sizeof (auxv_t);
75 buf[nauxv].a_type = AT_NULL;
76 } else {
77 libc_free(buf);
78 buf = NULL;
82 if (fd != -1)
83 (void) close(fd);
85 membar_producer();
86 auxb = buf;
88 lmutex_unlock(&auxlock);
90 membar_consumer();
93 * Scan the auxiliary entries looking for the required type.
95 for (i = 0; i < nauxv; i++)
96 if (auxb[i].a_type == type)
97 return (&auxb[i]);
100 * No auxiliary array (static executable) or entry not found.
102 return ((auxv_t *)0);
106 * These two routines are utilities exported to the rest of libc.
109 long
110 ___getauxval(int type)
112 auxv_t *auxp;
114 if ((auxp = _getaux(type)) != (auxv_t *)0)
115 return (auxp->a_un.a_val);
116 return (0);
119 void *
120 ___getauxptr(int type)
122 auxv_t *auxp;
124 if ((auxp = _getaux(type)) != (auxv_t *)0)
125 return (auxp->a_un.a_ptr);
126 return (0);