Back to index

glibc  2.9
fchownat.c
Go to the documentation of this file.
00001 /* Copyright (C) 2005, 2006 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 <stdio.h>
00022 #include <string.h>
00023 #include <unistd.h>
00024 
00025 #include <sysdep.h>
00026 #include <sys/syscall.h>
00027 #include <shlib-compat.h>
00028 #include <bp-checks.h>
00029 
00030 #include <linux/posix_types.h>
00031 #include <kernel-features.h>
00032 
00033 
00034 int
00035 fchownat (int fd, const char *file, uid_t owner, gid_t group, int flag)
00036 {
00037   int result;
00038 
00039 #ifdef __NR_fchownat
00040 # ifndef __ASSUME_ATFCTS
00041   if (__have_atfcts >= 0)
00042 # endif
00043     {
00044       result = INLINE_SYSCALL (fchownat, 5, fd, file, owner, group, flag);
00045 # ifndef __ASSUME_ATFCTS
00046       if (result == -1 && errno == ENOSYS)
00047        __have_atfcts = -1;
00048       else
00049 # endif
00050        return result;
00051     }
00052 #endif
00053 
00054 #ifndef __ASSUME_ATFCTS
00055   if (flag & ~AT_SYMLINK_NOFOLLOW)
00056     {
00057       __set_errno (EINVAL);
00058       return -1;
00059     }
00060 
00061   char *buf = NULL;
00062 
00063   if (fd != AT_FDCWD && file[0] != '/')
00064     {
00065       size_t filelen = strlen (file);
00066       static const char procfd[] = "/proc/self/fd/%d/%s";
00067       /* Buffer for the path name we are going to use.  It consists of
00068         - the string /proc/self/fd/
00069         - the file descriptor number
00070         - the file name provided.
00071         The final NUL is included in the sizeof.   A bit of overhead
00072         due to the format elements compensates for possible negative
00073         numbers.  */
00074       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00075       buf = alloca (buflen);
00076 
00077       __snprintf (buf, buflen, procfd, fd, file);
00078       file = buf;
00079     }
00080 
00081 # if __ASSUME_32BITUIDS > 0
00082   /* This implies __ASSUME_LCHOWN_SYSCALL.  */
00083   INTERNAL_SYSCALL_DECL (err);
00084 
00085   if (flag & AT_SYMLINK_NOFOLLOW)
00086     result = INTERNAL_SYSCALL (lchown32, err, 3, CHECK_STRING (file), owner,
00087                             group);
00088   else
00089     result = INTERNAL_SYSCALL (chown32, err, 3, CHECK_STRING (file), owner,
00090                             group);
00091 
00092   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
00093     {
00094       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
00095       return -1;
00096     }
00097 # else
00098   /* Don't inline the rest to avoid unnecessary code duplication.  */
00099   if (flag & AT_SYMLINK_NOFOLLOW)
00100     result = __lchown (file, owner, group);
00101   else
00102     result = __chown (file, owner, group);
00103   if (result < 0)
00104     __atfct_seterrno (errno, fd, buf);
00105 # endif
00106 
00107   return result;
00108 
00109 #endif
00110 }