Back to index

glibc  2.9
openat.c
Go to the documentation of this file.
00001 /* Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <fcntl.h>
00021 #include <stdarg.h>
00022 #include <stddef.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <sys/stat.h>
00026 #include <kernel-features.h>
00027 #include <sysdep-cancel.h>
00028 #include <not-cancel.h>
00029 
00030 
00031 #ifndef OPENAT
00032 # define OPENAT openat
00033 # define __OPENAT_2 __openat_2
00034 
00035 # ifndef __ASSUME_ATFCTS
00036 /* Set errno after a failed call.  If BUF is not null,
00037    it is a /proc/self/fd/ path name we just tried to use.  */
00038 void
00039 attribute_hidden
00040 __atfct_seterrno (int errval, int fd, const char *buf)
00041 {
00042   if (buf != NULL)
00043     {
00044       struct stat64 st;
00045 
00046       if (errval == ENOTDIR || errval == ENOENT)
00047        {
00048          /* This can mean either the file descriptor is invalid or
00049             /proc is not mounted.  */
00050          if (__fxstat64 (_STAT_VER, fd, &st) != 0)
00051            /* errno is already set correctly.  */
00052            return;
00053 
00054          /* If /proc is not mounted there is nothing we can do.  */
00055          if ((errval != ENOTDIR || S_ISDIR (st.st_mode))
00056              && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
00057                 || !S_ISDIR (st.st_mode)))
00058            errval = ENOSYS;
00059        }
00060     }
00061 
00062   __set_errno (errval);
00063 }
00064 
00065 int __have_atfcts;
00066 # endif
00067 #endif
00068 
00069 
00070 #define OPENAT_NOT_CANCEL CONCAT (OPENAT)
00071 #define CONCAT(name) CONCAT2 (name)
00072 #define CONCAT2(name) __##name##_nocancel
00073 
00074 
00075 int
00076 OPENAT_NOT_CANCEL (fd, file, oflag, mode)
00077      int fd;
00078      const char *file;
00079      int oflag;
00080      mode_t mode;
00081 {
00082 
00083   /* We have to add the O_LARGEFILE flag for openat64.  */
00084 #ifdef MORE_OFLAGS
00085   oflag |= MORE_OFLAGS;
00086 #endif
00087 
00088   INTERNAL_SYSCALL_DECL (err);
00089   int res;
00090 
00091 #ifdef __NR_openat
00092 # ifndef __ASSUME_ATFCTS
00093   if (__have_atfcts >= 0)
00094 # endif
00095     {
00096       res = INLINE_SYSCALL (openat, 4, fd, file, oflag, mode);
00097 
00098 # ifndef __ASSUME_ATFCTS
00099       if (res == -1 && errno == ENOSYS)
00100        __have_atfcts = -1;
00101       else
00102 # endif
00103        return res;
00104     }
00105 #endif
00106 
00107 #ifndef __ASSUME_ATFCTS
00108   char *buf = NULL;
00109 
00110   if (fd != AT_FDCWD && file[0] != '/')
00111     {
00112       size_t filelen = strlen (file);
00113       static const char procfd[] = "/proc/self/fd/%d/%s";
00114       /* Buffer for the path name we are going to use.  It consists of
00115         - the string /proc/self/fd/
00116         - the file descriptor number
00117         - the file name provided.
00118         The final NUL is included in the sizeof.   A bit of overhead
00119         due to the format elements compensates for possible negative
00120         numbers.  */
00121       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00122       buf = alloca (buflen);
00123 
00124       /* Note: snprintf cannot be canceled.  */
00125       __snprintf (buf, buflen, procfd, fd, file);
00126       file = buf;
00127     }
00128 
00129   res = INTERNAL_SYSCALL (open, err, 3, file, oflag, mode);
00130 
00131   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (res, err), 0))
00132     {
00133       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (res, err), fd, buf);
00134       res = -1;
00135     }
00136 
00137   return res;
00138 #endif
00139 }
00140 
00141 #define UNDERIZE(name) UNDERIZE_1 (name)
00142 #define UNDERIZE_1(name) __##name
00143 #define __OPENAT UNDERIZE (OPENAT)
00144 
00145 
00146 /* Open FILE with access OFLAG.  Interpret relative paths relative to
00147    the directory associated with FD.  If OFLAG includes O_CREAT, a
00148    third argument is the file protection.  */
00149 int
00150 __OPENAT (fd, file, oflag)
00151      int fd;
00152      const char *file;
00153      int oflag;
00154 {
00155   mode_t mode = 0;
00156   if (oflag & O_CREAT)
00157     {
00158       va_list arg;
00159       va_start (arg, oflag);
00160       mode = va_arg (arg, mode_t);
00161       va_end (arg);
00162     }
00163 
00164   if (SINGLE_THREAD_P)
00165     return OPENAT_NOT_CANCEL (fd, file, oflag, mode);
00166 
00167   int oldtype = LIBC_CANCEL_ASYNC ();
00168 
00169   int res = OPENAT_NOT_CANCEL (fd, file, oflag, mode);
00170 
00171   LIBC_CANCEL_RESET (oldtype);
00172 
00173   return res;
00174 }
00175 libc_hidden_def (__OPENAT)
00176 weak_alias (__OPENAT, OPENAT)
00177 
00178 
00179 int
00180 __OPENAT_2 (fd, file, oflag)
00181      int fd;
00182      const char *file;
00183      int oflag;
00184 {
00185   if (oflag & O_CREAT)
00186 #define MSG(s) MSG2 (s)
00187 #define MSG2(s) "invalid " #s " call: O_CREAT without mode"
00188     __fortify_fail (MSG (OPENAT));
00189 
00190   return __OPENAT (fd, file, oflag);
00191 }