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