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 <stddef.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 #include <sys/types.h>
00026 #include <alloca.h>
00027 #include <sysdep.h>
00028 #include <kernel-features.h>
00029 
00030 
00031 /* Change the owner and group of FILE.  */
00032 int
00033 fchownat (fd, file, owner, group, flag)
00034      int fd;
00035      const char *file;
00036      uid_t owner;
00037      gid_t group;
00038      int flag;
00039 {
00040   int result;
00041 
00042 #ifdef __NR_fchownat
00043 # ifndef __ASSUME_ATFCTS
00044   if (__have_atfcts >= 0)
00045 # endif
00046     {
00047       result = INLINE_SYSCALL (fchownat, 5, fd, file, owner, group, flag);
00048 # ifndef __ASSUME_ATFCTS
00049       if (result == -1 && errno == ENOSYS)
00050        __have_atfcts = -1;
00051       else
00052 # endif
00053        return result;
00054     }
00055 #endif
00056 
00057 #ifndef __ASSUME_ATFCTS
00058   if (flag & ~AT_SYMLINK_NOFOLLOW)
00059     {
00060       __set_errno (EINVAL);
00061       return -1;
00062     }
00063 
00064   char *buf = NULL;
00065 
00066   if (fd != AT_FDCWD && file[0] != '/')
00067     {
00068       size_t filelen = strlen (file);
00069       static const char procfd[] = "/proc/self/fd/%d/%s";
00070       /* Buffer for the path name we are going to use.  It consists of
00071         - the string /proc/self/fd/
00072         - the file descriptor number
00073         - the file name provided.
00074         The final NUL is included in the sizeof.   A bit of overhead
00075         due to the format elements compensates for possible negative
00076         numbers.  */
00077       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
00078       buf = alloca (buflen);
00079 
00080       __snprintf (buf, buflen, procfd, fd, file);
00081       file = buf;
00082     }
00083 
00084   INTERNAL_SYSCALL_DECL (err);
00085 
00086   if (flag & AT_SYMLINK_NOFOLLOW)
00087     result = INTERNAL_SYSCALL (lchown, err, 3, file, owner, group);
00088   else
00089     result = INTERNAL_SYSCALL (chown, err, 3, file, owner, group);
00090 
00091   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
00092     {
00093       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
00094       result = -1;
00095     }
00096 
00097   return result;
00098 #endif
00099 }