Back to index

glibc  2.9
fchmodat.c
Go to the documentation of this file.
00001 /* Change the protections of file relative to open directory.  Linux version.
00002    Copyright (C) 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <fcntl.h>
00022 #include <stddef.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 #include <sys/types.h>
00027 #include <alloca.h>
00028 #include <kernel-features.h>
00029 #include <sysdep.h>
00030 
00031 int
00032 fchmodat (fd, file, mode, flag)
00033      int fd;
00034      const char *file;
00035      mode_t mode;
00036      int flag;
00037 {
00038   if (flag & ~AT_SYMLINK_NOFOLLOW)
00039     {
00040       __set_errno (EINVAL);
00041       return -1;
00042     }
00043 #ifndef __NR_lchmod         /* Linux so far has no lchmod syscall.  */
00044   if (flag & AT_SYMLINK_NOFOLLOW)
00045     {
00046       __set_errno (ENOTSUP);
00047       return -1;
00048     }
00049 #endif
00050 
00051   int result;
00052 
00053 #ifdef __NR_fchmodat
00054 # ifndef __ASSUME_ATFCTS
00055   if (__have_atfcts >= 0)
00056 # endif
00057     {
00058       result = INLINE_SYSCALL (fchmodat, 3, fd, file, mode);
00059 # ifndef __ASSUME_ATFCTS
00060       if (result == -1 && errno == ENOSYS)
00061        __have_atfcts = -1;
00062       else
00063 # endif
00064        return result;
00065     }
00066 #endif
00067 
00068 #ifndef __ASSUME_ATFCTS
00069   char *buf = NULL;
00070 
00071   if (fd != AT_FDCWD && file[0] != '/')
00072     {
00073       size_t filelen = strlen (file);
00074       static const char procfd[] = "/proc/self/fd/%d/%s";
00075       /* Buffer for the path name we are going to use.  It consists of
00076         - the string /proc/self/fd/
00077         - the file descriptor number
00078         - the file name provided.
00079         The final NUL is included in the sizeof.   A bit of overhead
00080         due to the format elements compensates for possible negative
00081         numbers.  */
00082       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00083       buf = alloca (buflen);
00084 
00085       __snprintf (buf, buflen, procfd, fd, file);
00086       file = buf;
00087     }
00088 
00089   INTERNAL_SYSCALL_DECL (err);
00090 
00091 # ifdef __NR_lchmod
00092   if (flag & AT_SYMLINK_NOFOLLOW)
00093     result = INTERNAL_SYSCALL (lchmod, err, 2, file, mode);
00094   else
00095 # endif
00096     result = INTERNAL_SYSCALL (chmod, err, 2, file, mode);
00097 
00098   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
00099     {
00100       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
00101       result = -1;
00102     }
00103 
00104   return result;
00105 #endif
00106 }