]>
git.saurik.com Git - apple/libc.git/blob - sys/statx_np.c
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/types.h>
28 #include <sys/syscall.h>
35 #define ACL_MIN_SIZE_HEURISTIC (sizeof(struct kauth_filesec) + 16 * sizeof(struct kauth_ace))
37 static int statx_syscall(void *obj
, struct stat
*sb
, void *fsacl
, size_t *fsacl_size
);
38 static int fstatx_syscall(void *obj
, struct stat
*sb
, void *fsacl
, size_t *fsacl_size
);
39 static int lstatx_syscall(void *obj
, struct stat
*sb
, void *fsacl
, size_t *fsacl_size
);
41 static int statx1(void *obj
,
42 int (* stat_syscall
)(void *obj
, struct stat
*sb
, void *fsacl
, size_t *fsacl_size
),
43 struct stat
*sb
, filesec_t fsec
);
49 statx_np(const char *path
, struct stat
*sb
, filesec_t fsec
)
52 return(stat(path
, sb
));
53 return(statx1((void *)&path
, statx_syscall
, sb
, fsec
));
57 fstatx_np(int fd
, struct stat
*sb
, filesec_t fsec
)
60 return(fstat(fd
, sb
));
61 return(statx1((void *)&fd
, fstatx_syscall
, sb
, fsec
));
65 lstatx_np(const char *path
, struct stat
*sb
, filesec_t fsec
)
68 return(lstat(path
, sb
));
69 return(statx1((void *)&path
, lstatx_syscall
, sb
, fsec
));
76 statx_syscall(void *obj
, struct stat
*sb
, void *fsacl
, size_t *fsacl_size
)
78 const char *path
= *(const char **)obj
;
80 return(syscall(SYS_stat_extended
, path
, sb
, fsacl
, fsacl_size
));
84 fstatx_syscall(void *obj
, struct stat
*sb
, void *fsacl
, size_t *fsacl_size
)
87 return(syscall(SYS_fstat_extended
, fd
, sb
, fsacl
, fsacl_size
));
91 lstatx_syscall(void *obj
, struct stat
*sb
, void *fsacl
, size_t *fsacl_size
)
93 const char *path
= *(const char **)obj
;
94 return(syscall(SYS_lstat_extended
, path
, sb
, fsacl
, fsacl_size
));
102 int (* stat_syscall
)(void *obj
, struct stat
*sb
, void *fsacl
, size_t *fsacl_size
),
103 struct stat
*sb
, filesec_t fsec
)
105 struct kauth_filesec
*fsacl
, *ofsacl
;
106 size_t fsacl_size
, buffer_size
;
113 * Allocate an initial buffer.
115 if ((fsacl
= malloc(ACL_MIN_SIZE_HEURISTIC
)) == NULL
) {
119 buffer_size
= ACL_MIN_SIZE_HEURISTIC
;
122 * Loop until we have the ACL.
125 fsacl_size
= buffer_size
;
126 if ((error
= stat_syscall(obj
, sb
, fsacl
, &fsacl_size
)) != 0)
130 * No error, did we get the ACL?
132 if (fsacl_size
<= buffer_size
)
135 /* no, use supplied buffer size plus some padding */
137 fsacl
= realloc(fsacl
, fsacl_size
+ sizeof(struct kauth_ace
) * 2);
143 buffer_size
= fsacl_size
;
146 /* populate filesec with values from stat */
147 filesec_set_property(fsec
, FILESEC_OWNER
, &(sb
->st_uid
));
148 filesec_set_property(fsec
, FILESEC_GROUP
, &(sb
->st_gid
));
149 filesec_set_property(fsec
, FILESEC_MODE
, &(sb
->st_mode
));
151 /* if we got a kauth_filesec, take values from there too */
152 if (fsacl_size
>= sizeof(struct kauth_filesec
)) {
153 filesec_set_property(fsec
, FILESEC_UUID
, &fsacl
->fsec_owner
);
154 filesec_set_property(fsec
, FILESEC_GRPUUID
, &fsacl
->fsec_group
);
156 /* check to see whether there's actually an ACL here */
157 if (fsacl
->fsec_acl
.acl_entrycount
!= KAUTH_FILESEC_NOACL
) {
158 filesec_set_property(fsec
, FILESEC_ACL_ALLOCSIZE
, &fsacl_size
);
159 filesec_set_property(fsec
, FILESEC_ACL_RAW
, &fsacl
);
160 fsacl
= NULL
; /* avoid freeing it below */
162 filesec_set_property(fsec
, FILESEC_ACL_ALLOCSIZE
, NULL
);
163 filesec_set_property(fsec
, FILESEC_ACL_RAW
, NULL
);
166 filesec_set_property(fsec
, FILESEC_UUID
, NULL
);
167 filesec_set_property(fsec
, FILESEC_GRPUUID
, NULL
);
168 filesec_set_property(fsec
, FILESEC_ACL_ALLOCSIZE
, NULL
);
169 filesec_set_property(fsec
, FILESEC_ACL_RAW
, NULL
);