xref: /aosp_15_r20/external/cronet/base/mac/close_nocancel.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker // https://crbug.com/269623
6*6777b538SAndroid Build Coastguard Worker // https://openradar.appspot.com/14999594
7*6777b538SAndroid Build Coastguard Worker //
8*6777b538SAndroid Build Coastguard Worker // When the default version of close used on Mac OS X fails with EINTR, the
9*6777b538SAndroid Build Coastguard Worker // file descriptor is not in a deterministic state. It may have been closed,
10*6777b538SAndroid Build Coastguard Worker // or it may not have been. This makes it impossible to gracefully recover
11*6777b538SAndroid Build Coastguard Worker // from the error. If the close is retried after the FD has been closed, the
12*6777b538SAndroid Build Coastguard Worker // subsequent close can report EBADF, or worse, it can close an unrelated FD
13*6777b538SAndroid Build Coastguard Worker // opened by another thread. If the close is not retried after the FD has been
14*6777b538SAndroid Build Coastguard Worker // left open, the FD is leaked. Neither of these are good options.
15*6777b538SAndroid Build Coastguard Worker //
16*6777b538SAndroid Build Coastguard Worker // Mac OS X provides an alternate version of close, close$NOCANCEL. This
17*6777b538SAndroid Build Coastguard Worker // version will never fail with EINTR before the FD is actually closed. With
18*6777b538SAndroid Build Coastguard Worker // this version, it is thus safe to call close without checking for EINTR (as
19*6777b538SAndroid Build Coastguard Worker // the HANDLE_EINTR macro does) and not risk leaking the FD. In fact, mixing
20*6777b538SAndroid Build Coastguard Worker // this verison of close with HANDLE_EINTR is hazardous.
21*6777b538SAndroid Build Coastguard Worker //
22*6777b538SAndroid Build Coastguard Worker // The $NOCANCEL variants of various system calls are activated by compiling
23*6777b538SAndroid Build Coastguard Worker // with __DARWIN_NON_CANCELABLE, which prevents them from being pthread
24*6777b538SAndroid Build Coastguard Worker // cancellation points. Rather than taking such a heavy-handed approach, this
25*6777b538SAndroid Build Coastguard Worker // file implements an alternative: to use the $NOCANCEL variant of close (thus
26*6777b538SAndroid Build Coastguard Worker // preventing it from being a pthread cancellation point) without affecting
27*6777b538SAndroid Build Coastguard Worker // any other system calls.
28*6777b538SAndroid Build Coastguard Worker //
29*6777b538SAndroid Build Coastguard Worker // This file operates by providing a close function with the non-$NOCANCEL
30*6777b538SAndroid Build Coastguard Worker // symbol name expected for the compilation environment as set by <unistd.h>
31*6777b538SAndroid Build Coastguard Worker // and <sys/cdefs.h> (the DARWIN_ALIAS_C macro). That name is set by an asm
32*6777b538SAndroid Build Coastguard Worker // label on the declaration of the close function, so the definition of that
33*6777b538SAndroid Build Coastguard Worker // function receives that name. The function calls the $NOCANCEL variant, which
34*6777b538SAndroid Build Coastguard Worker // is resolved from libsyscall. By linking with this version of close prior to
35*6777b538SAndroid Build Coastguard Worker // the libsyscall version, close's implementation is overridden.
36*6777b538SAndroid Build Coastguard Worker 
37*6777b538SAndroid Build Coastguard Worker #include <sys/cdefs.h>
38*6777b538SAndroid Build Coastguard Worker #include <unistd.h>
39*6777b538SAndroid Build Coastguard Worker 
40*6777b538SAndroid Build Coastguard Worker // If the non-cancelable variants of all system calls have already been
41*6777b538SAndroid Build Coastguard Worker // chosen, do nothing.
42*6777b538SAndroid Build Coastguard Worker #if !__DARWIN_NON_CANCELABLE
43*6777b538SAndroid Build Coastguard Worker 
44*6777b538SAndroid Build Coastguard Worker extern "C" {
45*6777b538SAndroid Build Coastguard Worker 
46*6777b538SAndroid Build Coastguard Worker #if !__DARWIN_ONLY_UNIX_CONFORMANCE
47*6777b538SAndroid Build Coastguard Worker 
48*6777b538SAndroid Build Coastguard Worker // When there's a choice between UNIX2003 and pre-UNIX2003. There's no
49*6777b538SAndroid Build Coastguard Worker // close$NOCANCEL symbol in this case, so use close$NOCANCEL$UNIX2003 as the
50*6777b538SAndroid Build Coastguard Worker // implementation. It does the same thing that close$NOCANCEL would do.
51*6777b538SAndroid Build Coastguard Worker #define close_implementation close$NOCANCEL$UNIX2003
52*6777b538SAndroid Build Coastguard Worker 
53*6777b538SAndroid Build Coastguard Worker #else  // __DARWIN_ONLY_UNIX_CONFORMANCE
54*6777b538SAndroid Build Coastguard Worker 
55*6777b538SAndroid Build Coastguard Worker // When only UNIX2003 is supported:
56*6777b538SAndroid Build Coastguard Worker #define close_implementation close$NOCANCEL
57*6777b538SAndroid Build Coastguard Worker 
58*6777b538SAndroid Build Coastguard Worker #endif
59*6777b538SAndroid Build Coastguard Worker 
60*6777b538SAndroid Build Coastguard Worker int close_implementation(int fd);
61*6777b538SAndroid Build Coastguard Worker 
close(int fd)62*6777b538SAndroid Build Coastguard Worker int close(int fd) {
63*6777b538SAndroid Build Coastguard Worker   return close_implementation(fd);
64*6777b538SAndroid Build Coastguard Worker }
65*6777b538SAndroid Build Coastguard Worker 
66*6777b538SAndroid Build Coastguard Worker #undef close_implementation
67*6777b538SAndroid Build Coastguard Worker 
68*6777b538SAndroid Build Coastguard Worker }  // extern "C"
69*6777b538SAndroid Build Coastguard Worker 
70*6777b538SAndroid Build Coastguard Worker #endif  // !__DARWIN_NON_CANCELABLE
71