1 /* Copyright (C) 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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
28 #include <kernel-features.h>
31 In Linux 2.1.x the chown functions have been changed. A new function lchown
32 was introduced. The new chown now follows symlinks - the old chown and the
33 new lchown do not follow symlinks.
34 This file emulates chown() under the old kernels.
38 fchownat (int fd
, const char *file
, uid_t owner
, gid_t group
, int flag
)
40 if (flag
& ~AT_SYMLINK_NOFOLLOW
)
48 if (fd
!= AT_FDCWD
&& file
[0] != '/')
50 size_t filelen
= strlen (file
);
51 static const char procfd
[] = "/proc/self/fd/%d/%s";
52 /* Buffer for the path name we are going to use. It consists of
53 - the string /proc/self/fd/
54 - the file descriptor number
55 - the file name provided.
56 The final NUL is included in the sizeof. A bit of overhead
57 due to the format elements compensates for possible negative
59 size_t buflen
= sizeof (procfd
) + sizeof (int) * 3 + filelen
;
60 buf
= alloca (buflen
);
62 __snprintf (buf
, buflen
, procfd
, fd
, file
);
67 INTERNAL_SYSCALL_DECL (err
);
69 #if __ASSUME_LCHOWN_SYSCALL
70 if (flag
& AT_SYMLINK_NOFOLLOW
)
71 result
= INTERNAL_SYSCALL (lchown
, err
, 3, file
, owner
, group
);
73 result
= INTERNAL_SYSCALL (chown
, err
, 3, file
, owner
, group
);
75 char link
[PATH_MAX
+ 2];
76 char path
[2 * PATH_MAX
+ 4];
79 static int libc_old_chown
= 0 /* -1=old linux, 1=new linux, 0=unknown */;
81 if (libc_old_chown
== 1)
83 if (flag
& AT_SYMLINK_NOFOLLOW
)
84 result
= INTERNAL_SYSCALL (lchown
, err
, 3, __ptrvalue (file
), owner
,
87 result
= INTERNAL_SYSCALL (chown
, err
, 3, __ptrvalue (file
), owner
,
93 if (flag
& AT_SYMLINK_NOFOLLOW
)
95 result
= INTERNAL_SYSCALL (lchown
, err
, 3, __ptrvalue (file
), owner
,
100 if (libc_old_chown
== 0)
102 result
= INTERNAL_SYSCALL (chown
, err
, 3, __ptrvalue (file
), owner
,
104 if (__builtin_expect (!INTERNAL_SYSCALL_ERROR_P (result
, err
), 1))
106 if (INTERNAL_SYSCALL_ERRNO (result
, err
) != ENOSYS
)
114 if (flag
& AT_SYMLINK_NOFOLLOW
)
116 result
= INTERNAL_SYSCALL (chown
, err
, 3, __ptrvalue (file
), owner
,
122 result
= __readlink (file
, link
, PATH_MAX
+ 1);
126 result
= INTERNAL_SYSCALL (lchown
, err
, 3, __ptrvalue (file
), owner
,
129 result
= INTERNAL_SYSCALL (chown
, err
, 3, __ptrvalue (file
), owner
,
135 filelen
= strlen (file
) + 1;
136 if (filelen
> sizeof (path
))
138 errno
= ENAMETOOLONG
;
141 memcpy (path
, file
, filelen
);
143 /* 'The system has an arbitrary limit...' In practise, we'll hit
144 ENAMETOOLONG before this, usually. */
145 for (loopct
= 0; loopct
< 128; ++loopct
)
149 if (result
>= PATH_MAX
+ 1)
151 errno
= ENAMETOOLONG
;
155 link
[result
] = 0; /* Null-terminate string, just-in-case. */
157 linklen
= strlen (link
) + 1;
160 memcpy (path
, link
, linklen
);
163 filelen
= strlen (path
);
165 while (filelen
> 1 && path
[filelen
- 1] == '/')
167 while (filelen
> 0 && path
[filelen
- 1] != '/')
169 if (filelen
+ linklen
> sizeof (path
))
171 errno
= ENAMETOOLONG
;
174 memcpy (path
+ filelen
, link
, linklen
);
177 result
= __readlink (path
, link
, PATH_MAX
+ 1);
182 result
= INTERNAL_SYSCALL (lchown
, err
, 3, path
, owner
, group
);
184 result
= INTERNAL_SYSCALL (chown
, err
, 3, path
, owner
, group
);
195 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result
, err
), 0))
197 #if !__ASSUME_LCHOWN_SYSCALL
200 __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result
, err
), fd
, buf
);