1From 1b90c61f1fd4d83054a60ed5d21c6f76f1d23925 Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Wed, 11 Aug 2021 20:08:33 -0700
4Subject: [PATCH] provide __close on musl
5
6Upstream-Status: Pending
7Signed-off-by: Khem Raj <raj.khem@gmail.com>
8
9---
10 base/files/scoped_file_linux.cc | 14 ++++++++++++--
11 1 file changed, 12 insertions(+), 2 deletions(-)
12
13diff --git a/base/files/scoped_file_linux.cc b/base/files/scoped_file_linux.cc
14index e72b5b7248..958f7c2823 100644
15--- a/base/files/scoped_file_linux.cc
16+++ b/base/files/scoped_file_linux.cc
17@@ -7,6 +7,7 @@
18 #include <algorithm>
19 #include <array>
20 #include <atomic>
21+#include <dlfcn.h>
22
23 #include "base/compiler_specific.h"
24 #include "base/debug/stack_trace.h"
25@@ -81,9 +82,18 @@ bool IsFDOwned(int fd) {
26
27 extern "C" {
28
29-int __close(int);
30-
31 __attribute__((visibility("default"), noinline)) int close(int fd) {
32+  static int (*__close)(int) = nullptr;
33+
34+  if (__close == nullptr) {
35+    __close = (int (*)(int))dlsym(RTLD_NEXT, "close");
36+
37+    if (__close == nullptr) {
38+      RAW_LOG(ERROR, "musl close not found\n");
39+      IMMEDIATE_CRASH();
40+    }
41+  }
42+
43   if (base::IsFDOwned(fd) && g_is_ownership_enforced)
44     CrashOnFdOwnershipViolation();
45   return __close(fd);
46