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 <unistd.h>
00023 #include <string.h>
00024 
00025 #include <sysdep.h>
00026 #include <sys/syscall.h>
00027 #include <bp-checks.h>
00028 
00029 #include <linux/posix_types.h>
00030 #include <kernel-features.h>
00031 
00032 #ifdef __NR_chown32
00033 # if __ASSUME_32BITUIDS == 0
00034 /* This variable is shared with all files that need to check for 32bit
00035    uids.  */
00036 extern int __libc_missing_32bit_uids;
00037 # endif
00038 #endif /* __NR_chown32 */
00039 
00040 int
00041 fchownat (int fd, const char *file, uid_t owner, gid_t group, int flag)
00042 {
00043   if (flag & ~AT_SYMLINK_NOFOLLOW)
00044     {
00045       __set_errno (EINVAL);
00046       return -1;
00047     }
00048 
00049   char *buf = NULL;
00050 
00051   if (fd != AT_FDCWD && file[0] != '/')
00052     {
00053       size_t filelen = strlen (file);
00054       static const char procfd[] = "/proc/self/fd/%d/%s";
00055       /* Buffer for the path name we are going to use.  It consists of
00056         - the string /proc/self/fd/
00057         - the file descriptor number
00058         - the file name provided.
00059         The final NUL is included in the sizeof.   A bit of overhead
00060         due to the format elements compensates for possible negative
00061         numbers.  */
00062       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00063       buf = alloca (buflen);
00064 
00065       __snprintf (buf, buflen, procfd, fd, file);
00066       file = buf;
00067     }
00068 
00069   int result;
00070   INTERNAL_SYSCALL_DECL (err);
00071 
00072 #if __ASSUME_32BITUIDS > 0
00073   if (flag & AT_SYMLINK_NOFOLLOW)
00074     result = INTERNAL_SYSCALL (lchown32, err, 3, CHECK_STRING (file), owner,
00075                             group);
00076   else
00077     result = INTERNAL_SYSCALL (chown32, err, 3, CHECK_STRING (file), owner,
00078                             group);
00079 #else
00080 # ifdef __NR_chown32
00081   if (__libc_missing_32bit_uids <= 0)
00082     {
00083       if (flag & AT_SYMLINK_NOFOLLOW)
00084        result = INTERNAL_SYSCALL (lchown32, err, 3, CHECK_STRING (file),
00085                                owner, group);
00086       else
00087        result = INTERNAL_SYSCALL (chown32, err, 3, CHECK_STRING (file), owner,
00088                                group);
00089 
00090       if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
00091        return result;
00092       if (INTERNAL_SYSCALL_ERRNO (result, err) != ENOSYS)
00093        goto fail;
00094 
00095       __libc_missing_32bit_uids = 1;
00096     }
00097 # endif /* __NR_chown32 */
00098 
00099   if (((owner + 1) > (gid_t) ((__kernel_uid_t) -1U))
00100       || ((group + 1) > (gid_t) ((__kernel_gid_t) -1U)))
00101     {
00102       __set_errno (EINVAL);
00103       return -1;
00104     }
00105 
00106   if (flag & AT_SYMLINK_NOFOLLOW)
00107     result = INTERNAL_SYSCALL (lchown, err, 3, CHECK_STRING (file), owner,
00108                             group);
00109   else
00110     result = INTERNAL_SYSCALL (chown, err, 3, CHECK_STRING (file), owner,
00111                             group);
00112 #endif
00113 
00114   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
00115     {
00116     fail:
00117       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
00118       result = -1;
00119     }
00120 
00121   return result;
00122 }