* elf/rtld-Rules (subdir-args): New variable.
[jlayton/glibc.git] / sysdeps / unix / sysv / linux / m68k / fchownat.c
1 /* Copyright (C) 2005, 2006 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24
25 #include <sysdep.h>
26 #include <sys/syscall.h>
27 #include <bp-checks.h>
28
29 #include <linux/posix_types.h>
30 #include <kernel-features.h>
31
32 #ifdef __NR_chown32
33 # if __ASSUME_32BITUIDS == 0
34 /* This variable is shared with all files that need to check for 32bit
35    uids.  */
36 extern int __libc_missing_32bit_uids;
37 # endif
38 #endif /* __NR_chown32 */
39
40 int
41 fchownat (int fd, const char *file, uid_t owner, gid_t group, int flag)
42 {
43   if (flag & ~AT_SYMLINK_NOFOLLOW)
44     {
45       __set_errno (EINVAL);
46       return -1;
47     }
48
49   char *buf = NULL;
50
51   if (fd != AT_FDCWD && file[0] != '/')
52     {
53       size_t filelen = strlen (file);
54       static const char procfd[] = "/proc/self/fd/%d/%s";
55       /* Buffer for the path name we are going to use.  It consists of
56          - the string /proc/self/fd/
57          - the file descriptor number
58          - the file name provided.
59          The final NUL is included in the sizeof.   A bit of overhead
60          due to the format elements compensates for possible negative
61          numbers.  */
62       size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
63       buf = alloca (buflen);
64
65       __snprintf (buf, buflen, procfd, fd, file);
66       file = buf;
67     }
68
69   int result;
70   INTERNAL_SYSCALL_DECL (err);
71
72 #if __ASSUME_32BITUIDS > 0
73   if (flag & AT_SYMLINK_NOFOLLOW)
74     result = INTERNAL_SYSCALL (lchown32, err, 3, CHECK_STRING (file), owner,
75                                group);
76   else
77     result = INTERNAL_SYSCALL (chown32, err, 3, CHECK_STRING (file), owner,
78                                group);
79 #else
80 # ifdef __NR_chown32
81   if (__libc_missing_32bit_uids <= 0)
82     {
83       if (flag & AT_SYMLINK_NOFOLLOW)
84         result = INTERNAL_SYSCALL (lchown32, err, 3, CHECK_STRING (file),
85                                    owner, group);
86       else
87         result = INTERNAL_SYSCALL (chown32, err, 3, CHECK_STRING (file), owner,
88                                    group);
89
90       if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result, err), 1))
91         return result;
92       if (INTERNAL_SYSCALL_ERRNO (result, err) != ENOSYS)
93         goto fail;
94
95       __libc_missing_32bit_uids = 1;
96     }
97 # endif /* __NR_chown32 */
98
99   if (((owner + 1) > (gid_t) ((__kernel_uid_t) -1U))
100       || ((group + 1) > (gid_t) ((__kernel_gid_t) -1U)))
101     {
102       __set_errno (EINVAL);
103       return -1;
104     }
105
106   if (flag & AT_SYMLINK_NOFOLLOW)
107     result = INTERNAL_SYSCALL (lchown, err, 3, CHECK_STRING (file), owner,
108                                group);
109   else
110     result = INTERNAL_SYSCALL (chown, err, 3, CHECK_STRING (file), owner,
111                                group);
112 #endif
113
114   if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
115     {
116     fail:
117       __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
118       result = -1;
119     }
120
121   return result;
122 }