summaryrefslogtreecommitdiff
path: root/sysdeps/unix/sysv/solaris2/kopensolaris-gnu/faccessat.c
blob: 9648352ca546998300a602a0d57624c32b4e7c8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Test for access to file, relative to open directory.  OpenSolaris version.
   Copyright (C) 2006, 2009 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   OpenSolaris bits contributed by David Bartley
    <dtbartle@csclub.uwaterloo.ca>, 2009.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <unistd.h>
#include <sys/types.h>

#define E_OK 010

DECLARE_INLINE_SYSCALL (int, accessat, int, const char *, int);

int
faccessat (fd, file, type, flag)
     int fd;
     const char *file;
     int type;
     int flag;
{
  if (file == NULL || (flag & ~(AT_EACCESS)) != 0
      || (type & ~(R_OK|W_OK|X_OK|F_OK|E_OK)) != 0)
    {
      __set_errno (EINVAL);
      return -1;
    }

  if (fd < 0 && fd != AT_FDCWD)
    {
      __set_errno (EBADF);
      return -1;
    }

  if (flag & AT_EACCESS)
    {
      type |= E_OK;
    }

  int res = INLINE_SYSCALL (accessat, 3, fd, file, type);

  /* XXX: This should really be fixed in the kernel.  */
  if (res == 0 && (type & X_OK))
    {
      uid_t uid;
      gid_t gid;

      uid = (type & E_OK) ? geteuid () : getuid ();
      gid = (type & E_OK) ? getegid () : getgid ();
      if (uid == 0 || gid == 0)
        {
          struct stat buf;

          if (fstatat (fd, file, &buf, 0) != 0)
            return -1;

          /* Test user bit.  */
          if (uid == buf.st_uid)
            {
              if ((buf.st_mode & S_IXUSR) == 0)
                {
                  __set_errno (EACCES);
                  return -1;
                }
              return 0;
            }

          /* Test gid bit.  */
          if (gid == buf.st_gid)
            {
              if ((buf.st_mode & S_IXGRP) == 0)
                {
                  __set_errno (EACCES);
                  return -1;
                }
              return 0;
            }

          /* Test other bit.  */
          if ((buf.st_mode & S_IXOTH) == 0)
            {
              __set_errno (EACCES);
              return -1;
            }
          return 0;
        }
    }

  return res;
}