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