[libc-commits] [libc] [llvm] [libc] implement popen/pclose (PR #232846)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Mon Oct 19 02:04:31 PDT 2026
================
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of the Linux File specialization for popen streams.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/File/linux/popen_file.h"
+
+#include "src/__support/CPP/new.h"
+#include "src/__support/File/file.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/threads/mutex.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+PopenFile *PopenFile::popen_list = nullptr;
+Mutex PopenFile::popen_list_lock(/*timed=*/false, /*recursive=*/false,
+ /*robust=*/false, /*pshared=*/false);
+
+void PopenFile::add_to_popen_list_unlocked(PopenFile *f) {
+ f->popen_prev = nullptr;
+ f->popen_next = popen_list;
+ if (popen_list != nullptr)
+ popen_list->popen_prev = f;
+ popen_list = f;
+}
+
+void PopenFile::close_popen_list_fds_unlocked() {
+ for (PopenFile *f = popen_list; f != nullptr; f = f->popen_next)
+ f->close_fd();
+}
+
+int PopenFile::popen_file_close(File *f) {
+ auto *pf = reinterpret_cast<PopenFile *>(f);
+
+ lock_popen_list();
+ if (pf->popen_prev != nullptr)
+ pf->popen_prev->popen_next = pf->popen_next;
+ if (pf->popen_next != nullptr)
+ pf->popen_next->popen_prev = pf->popen_prev;
+ if (popen_list == pf)
+ popen_list = pf->popen_next;
+ unlock_popen_list();
----------------
labath wrote:
I think the close call has to be inside the critical section. Otherwise, a concurrent popen will inherit the FD, but it will not find it in the popen list.
https://github.com/llvm/llvm-project/pull/232846
More information about the libc-commits
mailing list