1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2020 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. Please obtain a copy of the License at
10*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this
11*c54f35caSApple OSS Distributions * file.
12*c54f35caSApple OSS Distributions *
13*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
14*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
19*c54f35caSApple OSS Distributions * limitations under the License.
20*c54f35caSApple OSS Distributions *
21*c54f35caSApple OSS Distributions * @APPLE_LICENSE_HEADER_END@
22*c54f35caSApple OSS Distributions */
23*c54f35caSApple OSS Distributions
24*c54f35caSApple OSS Distributions #include <fcntl.h>
25*c54f35caSApple OSS Distributions #include <stdarg.h>
26*c54f35caSApple OSS Distributions #include <sys/param.h>
27*c54f35caSApple OSS Distributions #include <sys/types.h>
28*c54f35caSApple OSS Distributions #include <TargetConditionals.h>
29*c54f35caSApple OSS Distributions #include "system-version-compat-support.h"
30*c54f35caSApple OSS Distributions
31*c54f35caSApple OSS Distributions #if !defined(__i386__)
32*c54f35caSApple OSS Distributions
33*c54f35caSApple OSS Distributions #if SYSTEM_VERSION_COMPAT_ENABLED
34*c54f35caSApple OSS Distributions #include <stdbool.h>
35*c54f35caSApple OSS Distributions
36*c54f35caSApple OSS Distributions extern bool (*system_version_compat_check_path_suffix)(const char *orig_path);
37*c54f35caSApple OSS Distributions extern int (*system_version_compat_open_shim)(int opened_fd, int openat_fd, const char *orig_path, int oflag, mode_t mode,
38*c54f35caSApple OSS Distributions int (*close_syscall)(int), int (*open_syscall)(const char *, int, mode_t),
39*c54f35caSApple OSS Distributions int (*openat_syscall)(int, const char *, int, mode_t),
40*c54f35caSApple OSS Distributions int (*fcntl_syscall)(int, int, long));
41*c54f35caSApple OSS Distributions #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
42*c54f35caSApple OSS Distributions
43*c54f35caSApple OSS Distributions #ifdef VARIANT_CANCELABLE
44*c54f35caSApple OSS Distributions int __open(const char *path, int oflag, mode_t mode);
45*c54f35caSApple OSS Distributions int __openat(int fd, const char *path, int oflag, mode_t mode);
46*c54f35caSApple OSS Distributions
47*c54f35caSApple OSS Distributions #define OPEN_SYSCALL __open
48*c54f35caSApple OSS Distributions #define OPENAT_SYSCALL __openat
49*c54f35caSApple OSS Distributions
50*c54f35caSApple OSS Distributions #if SYSTEM_VERSION_COMPAT_ENABLED
51*c54f35caSApple OSS Distributions int __fcntl(int fd, int cmd, long arg);
52*c54f35caSApple OSS Distributions int close(int fd);
53*c54f35caSApple OSS Distributions
54*c54f35caSApple OSS Distributions #define FCNTL_SYSCALL __fcntl
55*c54f35caSApple OSS Distributions #define CLOSE_SYSCALL close
56*c54f35caSApple OSS Distributions #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
57*c54f35caSApple OSS Distributions
58*c54f35caSApple OSS Distributions #else /* VARIANT_CANCELABLE */
59*c54f35caSApple OSS Distributions int __open_nocancel(const char *path, int oflag, mode_t mode);
60*c54f35caSApple OSS Distributions int __openat_nocancel(int fd, const char *path, int oflag, mode_t mode);
61*c54f35caSApple OSS Distributions
62*c54f35caSApple OSS Distributions #define OPEN_SYSCALL __open_nocancel
63*c54f35caSApple OSS Distributions #define OPENAT_SYSCALL __openat_nocancel
64*c54f35caSApple OSS Distributions
65*c54f35caSApple OSS Distributions #if SYSTEM_VERSION_COMPAT_ENABLED
66*c54f35caSApple OSS Distributions int __fcntl_nocancel(int fd, int cmd, long arg);
67*c54f35caSApple OSS Distributions int __close_nocancel(int fd);
68*c54f35caSApple OSS Distributions
69*c54f35caSApple OSS Distributions #define FCNTL_SYSCALL __fcntl_nocancel
70*c54f35caSApple OSS Distributions #define CLOSE_SYSCALL __close_nocancel
71*c54f35caSApple OSS Distributions #endif /* SYSTEM_VERSION_COMPAT_ENABLED */
72*c54f35caSApple OSS Distributions #endif /* VARIANT_CANCELABLE */
73*c54f35caSApple OSS Distributions
74*c54f35caSApple OSS Distributions #ifdef VARIANT_CANCELABLE
75*c54f35caSApple OSS Distributions int
open(const char * path,int oflag,...)76*c54f35caSApple OSS Distributions open(const char *path, int oflag, ...)
77*c54f35caSApple OSS Distributions #else /* VARIANT_CANCELABLE */
78*c54f35caSApple OSS Distributions int
79*c54f35caSApple OSS Distributions open$NOCANCEL(const char *path, int oflag, ...)
80*c54f35caSApple OSS Distributions #endif
81*c54f35caSApple OSS Distributions {
82*c54f35caSApple OSS Distributions int opened_fd = 0;
83*c54f35caSApple OSS Distributions mode_t mode = 0;
84*c54f35caSApple OSS Distributions
85*c54f35caSApple OSS Distributions if (oflag & O_CREAT) {
86*c54f35caSApple OSS Distributions va_list ap;
87*c54f35caSApple OSS Distributions va_start(ap, oflag);
88*c54f35caSApple OSS Distributions /* compiler warns to pass int (not mode_t) to va_arg */
89*c54f35caSApple OSS Distributions mode = va_arg(ap, int);
90*c54f35caSApple OSS Distributions va_end(ap);
91*c54f35caSApple OSS Distributions }
92*c54f35caSApple OSS Distributions
93*c54f35caSApple OSS Distributions opened_fd = OPEN_SYSCALL(path, oflag, mode);
94*c54f35caSApple OSS Distributions #if !SYSTEM_VERSION_COMPAT_ENABLED
95*c54f35caSApple OSS Distributions return opened_fd;
96*c54f35caSApple OSS Distributions #else /* SYSTEM_VERSION_COMPAT_ENABLED */
97*c54f35caSApple OSS Distributions if (opened_fd < 0) {
98*c54f35caSApple OSS Distributions return opened_fd;
99*c54f35caSApple OSS Distributions }
100*c54f35caSApple OSS Distributions
101*c54f35caSApple OSS Distributions /* check to see if system_version_compat is enabled for this process */
102*c54f35caSApple OSS Distributions if (system_version_compat_check_path_suffix == NULL) {
103*c54f35caSApple OSS Distributions return opened_fd;
104*c54f35caSApple OSS Distributions }
105*c54f35caSApple OSS Distributions
106*c54f35caSApple OSS Distributions /* check to see if the suffix of the path we opened matches one we are shimming */
107*c54f35caSApple OSS Distributions if (!system_version_compat_check_path_suffix(path)) {
108*c54f35caSApple OSS Distributions return opened_fd;
109*c54f35caSApple OSS Distributions }
110*c54f35caSApple OSS Distributions
111*c54f35caSApple OSS Distributions /* at this point we call into the version compat open shim and return values from there */
112*c54f35caSApple OSS Distributions return system_version_compat_open_shim(opened_fd, -1, path, oflag, mode, CLOSE_SYSCALL, OPEN_SYSCALL,
113*c54f35caSApple OSS Distributions NULL, FCNTL_SYSCALL);
114*c54f35caSApple OSS Distributions #endif /* !SYSTEM_VERSION_COMPAT_ENABLED */
115*c54f35caSApple OSS Distributions }
116*c54f35caSApple OSS Distributions
117*c54f35caSApple OSS Distributions #ifdef VARIANT_CANCELABLE
118*c54f35caSApple OSS Distributions int
openat(int fd,const char * path,int oflag,...)119*c54f35caSApple OSS Distributions openat(int fd, const char *path, int oflag, ...)
120*c54f35caSApple OSS Distributions #else /* VARIANT_CANCELABLE */
121*c54f35caSApple OSS Distributions int
122*c54f35caSApple OSS Distributions openat$NOCANCEL(int fd, const char *path, int oflag, ...)
123*c54f35caSApple OSS Distributions #endif
124*c54f35caSApple OSS Distributions {
125*c54f35caSApple OSS Distributions int opened_fd = 0;
126*c54f35caSApple OSS Distributions mode_t mode = 0;
127*c54f35caSApple OSS Distributions
128*c54f35caSApple OSS Distributions if (oflag & O_CREAT) {
129*c54f35caSApple OSS Distributions va_list ap;
130*c54f35caSApple OSS Distributions va_start(ap, oflag);
131*c54f35caSApple OSS Distributions // compiler warns to pass int (not mode_t) to va_arg
132*c54f35caSApple OSS Distributions mode = va_arg(ap, int);
133*c54f35caSApple OSS Distributions va_end(ap);
134*c54f35caSApple OSS Distributions }
135*c54f35caSApple OSS Distributions
136*c54f35caSApple OSS Distributions opened_fd = OPENAT_SYSCALL(fd, path, oflag, mode);
137*c54f35caSApple OSS Distributions #if !SYSTEM_VERSION_COMPAT_ENABLED
138*c54f35caSApple OSS Distributions return opened_fd;
139*c54f35caSApple OSS Distributions #else
140*c54f35caSApple OSS Distributions if (opened_fd < 0) {
141*c54f35caSApple OSS Distributions return opened_fd;
142*c54f35caSApple OSS Distributions }
143*c54f35caSApple OSS Distributions
144*c54f35caSApple OSS Distributions /* check to see if system_version_compat is enabled for this process */
145*c54f35caSApple OSS Distributions if (system_version_compat_check_path_suffix == NULL) {
146*c54f35caSApple OSS Distributions return opened_fd;
147*c54f35caSApple OSS Distributions }
148*c54f35caSApple OSS Distributions
149*c54f35caSApple OSS Distributions /* check to see if the suffix of the path we opened matches one we are shimming */
150*c54f35caSApple OSS Distributions if (!system_version_compat_check_path_suffix(path)) {
151*c54f35caSApple OSS Distributions return opened_fd;
152*c54f35caSApple OSS Distributions }
153*c54f35caSApple OSS Distributions
154*c54f35caSApple OSS Distributions /* at this point we call into the version compat open shim and return values from there */
155*c54f35caSApple OSS Distributions return system_version_compat_open_shim(opened_fd, fd, path, oflag, mode, CLOSE_SYSCALL, NULL,
156*c54f35caSApple OSS Distributions OPENAT_SYSCALL, FCNTL_SYSCALL);
157*c54f35caSApple OSS Distributions #endif /* !SYSTEM_VERSION_COMPAT_ENABLED */
158*c54f35caSApple OSS Distributions }
159*c54f35caSApple OSS Distributions #endif /* !defined(__i386__) */
160