akaros/tools/compilers/gcc-glibc/glibc-2.19-akaros/sysdeps/akaros/errno.c
<<
>>
Prefs
   1/* Definition of `errno' variable.  Canonical version.
   2   Copyright (C) 2002, 2004 Free Software Foundation, Inc.
   3   This file is part of the GNU C Library.
   4
   5   The GNU C Library is free software; you can redistribute it and/or
   6   modify it under the terms of the GNU Lesser General Public
   7   License as published by the Free Software Foundation; either
   8   version 2.1 of the License, or (at your option) any later version.
   9
  10   The GNU C Library is distributed in the hope that it will be useful,
  11   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13   Lesser General Public License for more details.
  14
  15   You should have received a copy of the GNU Lesser General Public
  16   License along with the GNU C Library; if not, write to the Free
  17   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18   02111-1307 USA.  */
  19
  20#include <errno.h>
  21#include <tls.h>
  22#include <dl-sysdep.h>
  23#undef errno
  24
  25#if RTLD_PRIVATE_ERRNO
  26
  27/* Code compiled for rtld has errno #defined to rtld_errno. */
  28int rtld_errno attribute_hidden;
  29#define errno rtld_errno
  30char __errstr_tls[MAX_ERRSTR_LEN] = {0};
  31
  32#else
  33
  34__thread int errno;
  35extern __thread int __libc_errno __attribute__ ((alias ("errno")))
  36  attribute_hidden;
  37__thread char __errstr_tls[MAX_ERRSTR_LEN] = {0};
  38
  39#endif
  40
  41/* this is the glibc default, exported so uthread.c can use them */
  42int *__errno_location_tls(void)
  43{
  44        return &errno;
  45}
  46
  47char *__errstr_location_tls(void)
  48{
  49        return __errstr_tls;
  50}
  51
  52/* These func ptrs will be used to access errno_loc and errstr_loc.  can be
  53 * overriden at runtime (uthread code will do this) */
  54int *(*ros_errno_loc)(void) = __errno_location_tls;
  55char *(*ros_errstr_loc)(void) = __errstr_location_tls;
  56
  57/* errno calls this (and derefs the result).  it's also called internally */
  58int *__errno_location(void)
  59{
  60        return ros_errno_loc();
  61}
  62
  63/* libc doesn't really know about errstr, but we'll use it in syscall.c */
  64char *errstr(void)
  65{
  66        return ros_errstr_loc();
  67}
  68
  69/* Using this instead of strcpy/memcpy to avoid dependencies. */
  70static void simple_strcpy(char *dst, char *src, size_t size)
  71{
  72        for (size_t i = 0; (i < size) && *src; i++)
  73                *dst++ = *src++;
  74}
  75
  76/* Helpers to save and restore errno and errstr into temporary locations.  Make
  77 * sure str is MAX_ERRSTR_LEN bytes long. */
  78void save_err(int *no, char *str)
  79{
  80        *no = errno;
  81        simple_strcpy(str, errstr(), MAX_ERRSTR_LEN);
  82}
  83
  84void restore_err(int *no, char *str)
  85{
  86        errno = *no;
  87        simple_strcpy(errstr(), str, MAX_ERRSTR_LEN);
  88}
  89
  90/* Don't try to hidden_data_def the function pointers.  Won't allow us to
  91 * switch, or otherwise track the right location. */
  92libc_hidden_def(__errno_location_tls)
  93libc_hidden_def(__errstr_location_tls)
  94libc_hidden_def(__errno_location)
  95libc_hidden_def(errstr)
  96