Back to index

glibc  2.9
chown.c
Go to the documentation of this file.
00001 /* chown() compatibility.
00002    Copyright (C) 1998, 2000, 2002, 2003, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <string.h>
00022 #include <unistd.h>
00023 #include <limits.h>
00024 #include <sysdep.h>
00025 #include <stdlib.h>
00026 
00027 #include <kernel-features.h>
00028 
00029 /*
00030   In Linux 2.1.x the chown functions have been changed.  A new function lchown
00031   was introduced.  The new chown now follows symlinks - the old chown and the
00032   new lchown do not follow symlinks.
00033   This file emulates chown() under the old kernels.
00034 */
00035 
00036 int
00037 __chown (const char *file, uid_t owner, gid_t group)
00038 {
00039 #if __ASSUME_LCHOWN_SYSCALL
00040   return INLINE_SYSCALL (chown, 3, file, owner, group);
00041 #else
00042   int err;
00043   int old_errno;
00044   char link[PATH_MAX + 2];
00045   char path[2 * PATH_MAX + 4];
00046   int loopct;
00047   size_t filelen;
00048   static int libc_old_chown = 0 /* -1=old linux, 1=new linux, 0=unknown */;
00049 
00050   if (libc_old_chown == 1)
00051     return INLINE_SYSCALL (chown, 3, __ptrvalue (file), owner, group);
00052 
00053   old_errno = errno;
00054 
00055 # ifdef __NR_lchown
00056   if (libc_old_chown == 0)
00057     {
00058       err = INLINE_SYSCALL (chown, 3, __ptrvalue (file), owner, group);
00059       if (err != -1 || errno != ENOSYS)
00060        {
00061          libc_old_chown = 1;
00062          return err;
00063        }
00064       libc_old_chown = -1;
00065     }
00066 # endif
00067 
00068   err = __readlink (file, link, PATH_MAX + 1);
00069   if (err == -1)
00070     {
00071       __set_errno (old_errno);
00072       return __lchown (file, owner, group);
00073     }
00074 
00075   filelen = strlen (file) + 1;
00076   if (filelen > sizeof (path))
00077     {
00078       __set_errno (ENAMETOOLONG);
00079       return -1;
00080     }
00081   memcpy (path, file, filelen);
00082 
00083   /* 'The system has an arbitrary limit...'  In practise, we'll hit
00084      ENAMETOOLONG before this, usually.  */
00085   for (loopct = 0; loopct < 128; ++loopct)
00086     {
00087       size_t linklen;
00088 
00089       if (err >= PATH_MAX + 1)
00090        {
00091          __set_errno (ENAMETOOLONG);
00092          return -1;
00093        }
00094 
00095       link[err] = 0;  /* Null-terminate string, just-in-case.  */
00096 
00097       linklen = strlen (link) + 1;
00098 
00099       if (link[0] == '/')
00100        memcpy (path, link, linklen);
00101       else
00102        {
00103          filelen = strlen (path);
00104 
00105          while (filelen > 1 && path[filelen - 1] == '/')
00106            --filelen;
00107          while (filelen > 0 && path[filelen - 1] != '/')
00108            --filelen;
00109          if (filelen + linklen > sizeof (path))
00110            {
00111              errno = ENAMETOOLONG;
00112              return -1;
00113            }
00114          memcpy (path + filelen, link, linklen);
00115        }
00116 
00117       err = __readlink (path, link, PATH_MAX + 1);
00118 
00119       if (err == -1)
00120        {
00121          __set_errno (old_errno);
00122          return __lchown (path, owner, group);
00123        }
00124     }
00125   __set_errno (ELOOP);
00126   return -1;
00127 #endif
00128 }
00129 libc_hidden_def (__chown)
00130 
00131 #include <shlib-compat.h>
00132 versioned_symbol (libc, __chown, chown, GLIBC_2_1);