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