root/maint/gnulib/lib/fchownat.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. rpl_fchownat

   1 /* This function serves as replacement for a missing fchownat function,
   2    as well as a work around for the fchownat bug in glibc-2.4:
   3     <https://lists.ubuntu.com/archives/ubuntu-users/2006-September/093218.html>
   4    when the buggy fchownat-with-AT_SYMLINK_NOFOLLOW operates on a symlink, it
   5    mistakenly affects the symlink referent, rather than the symlink itself.
   6 
   7    Copyright (C) 2006-2007, 2009-2021 Free Software Foundation, Inc.
   8 
   9    This program is free software: you can redistribute it and/or modify
  10    it under the terms of the GNU General Public License as published by
  11    the Free Software Foundation; either version 3 of the License, or
  12    (at your option) any later version.
  13 
  14    This program is distributed in the hope that it will be useful,
  15    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17    GNU General Public License for more details.
  18 
  19    You should have received a copy of the GNU General Public License
  20    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  21 
  22 /* written by Jim Meyering */
  23 
  24 #include <config.h>
  25 
  26 /* Specification.  */
  27 #include <unistd.h>
  28 
  29 #include <errno.h>
  30 #include <stdlib.h>
  31 #include <string.h>
  32 
  33 #include "openat.h"
  34 
  35 #if !HAVE_FCHOWNAT
  36 
  37 /* Replacement for Solaris' function by the same name.
  38    Invoke chown or lchown on file, FILE, using OWNER and GROUP, in the
  39    directory open on descriptor FD.  If FLAG is AT_SYMLINK_NOFOLLOW, then
  40    use lchown, otherwise, use chown.  If possible, do it without changing
  41    the working directory.  Otherwise, resort to using save_cwd/fchdir,
  42    then (chown|lchown)/restore_cwd.  If either the save_cwd or the
  43    restore_cwd fails, then give a diagnostic and exit nonzero.  */
  44 
  45 # define AT_FUNC_NAME fchownat
  46 # define AT_FUNC_F1 lchown
  47 # define AT_FUNC_F2 chown
  48 # define AT_FUNC_USE_F1_COND AT_SYMLINK_NOFOLLOW
  49 # define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group, int flag
  50 # define AT_FUNC_POST_FILE_ARGS        , owner, group
  51 # include "at-func.c"
  52 # undef AT_FUNC_NAME
  53 # undef AT_FUNC_F1
  54 # undef AT_FUNC_F2
  55 # undef AT_FUNC_USE_F1_COND
  56 # undef AT_FUNC_POST_FILE_PARAM_DECLS
  57 # undef AT_FUNC_POST_FILE_ARGS
  58 
  59 #else /* HAVE_FCHOWNAT */
  60 
  61 # undef fchownat
  62 
  63 # if FCHOWNAT_NOFOLLOW_BUG
  64 
  65 /* Failure to handle AT_SYMLINK_NOFOLLOW requires the /proc/self/fd or
  66    fchdir workaround to call lchown for lchownat, but there is no need
  67    to penalize chownat.  */
  68 static int
  69 local_lchownat (int fd, char const *file, uid_t owner, gid_t group);
  70 
  71 #  define AT_FUNC_NAME local_lchownat
  72 #  define AT_FUNC_F1 lchown
  73 #  define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group
  74 #  define AT_FUNC_POST_FILE_ARGS        , owner, group
  75 #  include "at-func.c"
  76 #  undef AT_FUNC_NAME
  77 #  undef AT_FUNC_F1
  78 #  undef AT_FUNC_POST_FILE_PARAM_DECLS
  79 #  undef AT_FUNC_POST_FILE_ARGS
  80 
  81 # endif
  82 
  83 /* Work around bugs with trailing slash, using the same workarounds as
  84    chown and lchown.  */
  85 
  86 int
  87 rpl_fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89 # if FCHOWNAT_NOFOLLOW_BUG
  90   if (flag == AT_SYMLINK_NOFOLLOW)
  91     return local_lchownat (fd, file, owner, group);
  92 # endif
  93 # if FCHOWNAT_EMPTY_FILENAME_BUG
  94   if (file[0] == '\0')
  95     {
  96       errno = ENOENT;
  97       return -1;
  98     }
  99 # endif
 100 # if CHOWN_TRAILING_SLASH_BUG
 101   {
 102     size_t len = strlen (file);
 103     struct stat st;
 104     if (len && file[len - 1] == '/')
 105       {
 106         if (statat (fd, file, &st))
 107           return -1;
 108         if (flag == AT_SYMLINK_NOFOLLOW)
 109           return fchownat (fd, file, owner, group, 0);
 110       }
 111   }
 112 # endif
 113   return fchownat (fd, file, owner, group, flag);
 114 }
 115 
 116 #endif /* HAVE_FCHOWNAT */

/* [previous][next][first][last][top][bottom][index][help] */