Print this page
    
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/lib/libc/port/gen/getauxv.c
          +++ new/usr/src/lib/libc/port/gen/getauxv.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
  25   25   */
  26   26  
  27   27  #include "lint.h"
  28   28  #include "thr_uberdata.h"
  29   29  #include <libc.h>
  30   30  #include <fcntl.h>
  31   31  #include <stdlib.h>
  32   32  #include <unistd.h>
  33   33  #include <sys/types.h>
  34   34  #include <sys/stat.h>
  35   35  #include <sys/auxv.h>
  36   36  #include <mtlib.h>
  37   37  #include <thread.h>
  38   38  #include <synch.h>
  39   39  #include <atomic.h>
  40   40  #include <limits.h>
  41   41  
  42   42  static mutex_t auxlock = DEFAULTMUTEX;
  43   43  
  44   44  /*
  45   45   * Get auxiliary entry.
  46   46   * Returns pointer to entry, or 0 if entry does not exist.
  47   47   */
  48   48  static auxv_t *
  49   49  _getaux(int type)
  50   50  {
  51   51          static auxv_t *auxb = NULL;
  52   52          static size_t nauxv = 0;
  53   53          ssize_t i;
  54   54  
  55   55          /*
  56   56           * The first time through, read the initial aux vector that was
  57   57           * passed to the process at exec(2).  Only do this once.
  58   58           */
  59   59          if (auxb == NULL) {
  60   60                  lmutex_lock(&auxlock);
  61   61                  if (auxb == NULL) {
  62   62                          uberdata_t *udp = curthread->ul_uberdata;
  63   63                          struct stat statb;
  64   64                          auxv_t *buf = NULL;
  65   65                          char *path = "/proc/self/auxv";
  66   66                          char pbuf[PATH_MAX];
  67   67                          int fd;
  68   68  
  69   69                          if (udp->ub_broot != NULL) {
  70   70                                  (void) snprintf(pbuf, sizeof (pbuf),
  71   71                                      "%s/proc/self/auxv", udp->ub_broot);
  72   72                                  path = pbuf;
  73   73                          }
  74   74  
  75   75                          if ((fd = open(path, O_RDONLY)) != -1 &&
  76   76                              fstat(fd, &statb) != -1)
  77   77                                  buf = libc_malloc(
  78   78                                      statb.st_size + sizeof (auxv_t));
  79   79  
  80   80                          if (buf != NULL) {
  81   81                                  i = read(fd, buf, statb.st_size);
  82   82                                  if (i != -1) {
  83   83                                          nauxv = i / sizeof (auxv_t);
  84   84                                          buf[nauxv].a_type = AT_NULL;
  85   85                                  } else {
  86   86                                          libc_free(buf);
  87   87                                          buf = NULL;
  88   88                                  }
  89   89                          }
  90   90  
  91   91                          if (fd != -1)
  92   92                                  (void) close(fd);
  93   93  
  94   94                          membar_producer();
  95   95                          auxb = buf;
  96   96                  }
  97   97                  lmutex_unlock(&auxlock);
  98   98          }
  99   99          membar_consumer();
 100  100  
 101  101          /*
 102  102           * Scan the auxiliary entries looking for the required type.
 103  103           */
 104  104          for (i = 0; i < nauxv; i++)
 105  105                  if (auxb[i].a_type == type)
 106  106                          return (&auxb[i]);
 107  107  
 108  108          /*
 109  109           * No auxiliary array (static executable) or entry not found.
 110  110           */
 111  111          return ((auxv_t *)0);
 112  112  }
 113  113  
 114  114  /*
 115  115   * These two routines are utilities exported to the rest of libc.
 116  116   */
 117  117  
 118  118  long
 119  119  ___getauxval(int type)
 120  120  {
 121  121          auxv_t *auxp;
 122  122  
 123  123          if ((auxp = _getaux(type)) != (auxv_t *)0)
 124  124                  return (auxp->a_un.a_val);
 125  125          return (0);
 126  126  }
 127  127  
 128  128  void *
 129  129  ___getauxptr(int type)
 130  130  {
 131  131          auxv_t *auxp;
 132  132  
 133  133          if ((auxp = _getaux(type)) != (auxv_t *)0)
 134  134                  return (auxp->a_un.a_ptr);
 135  135          return (0);
 136  136  }
  
    | 
      ↓ open down ↓ | 
    136 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX