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