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