diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /ipc/glue/FileDescriptorShuffle.h | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ipc/glue/FileDescriptorShuffle.h')
-rw-r--r-- | ipc/glue/FileDescriptorShuffle.h | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/ipc/glue/FileDescriptorShuffle.h b/ipc/glue/FileDescriptorShuffle.h new file mode 100644 index 0000000000..4afa8e0474 --- /dev/null +++ b/ipc/glue/FileDescriptorShuffle.h @@ -0,0 +1,68 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_ipc_FileDescriptorShuffle_h +#define mozilla_ipc_FileDescriptorShuffle_h + +#include "mozilla/Span.h" +#include "nsTArray.h" + +#include <functional> +#include <utility> + +// This class converts a set of file descriptor mapping, which may +// contain conflicts (like {a->b, b->c} or {a->b, b->a}) into a +// sequence of dup2() operations that can be performed between fork +// and exec, or with posix_spawn_file_actions_adddup2. It may create +// temporary duplicates of fds to use as the source of a dup2; they +// are closed on destruction. +// +// The dup2 sequence is guaranteed to not contain dup2(x, x) for any +// x; if such an element is present in the input, it will be dup2()ed +// from a temporary fd to ensure that the close-on-exec bit is cleared. +// +// In general, this is *not* guaranteed to minimize the use of +// temporary fds. + +namespace mozilla { +namespace ipc { + +class FileDescriptorShuffle { + public: + FileDescriptorShuffle() = default; + ~FileDescriptorShuffle(); + + using MappingRef = mozilla::Span<const std::pair<int, int>>; + + // Translate the given mapping, creating temporary fds as needed. + // Can fail (return false) on failure to duplicate fds. + bool Init(MappingRef aMapping); + + // Accessor for the dup2() sequence. Do not use the returned value + // or the fds contained in it after this object is destroyed. + MappingRef Dup2Sequence() const { return mMapping; } + + // Tests whether the given fd is used as a destination in this mapping. + // Can be used to close other fds after performing the dup2()s. + bool MapsTo(int aFd) const; + + // Forget the information, so that it's destructor will not try to + // delete FDs duped by itself. + void Forget() { mTempFds.Clear(); } + + private: + nsTArray<std::pair<int, int>> mMapping; + nsTArray<int> mTempFds; + int mMaxDst; + + FileDescriptorShuffle(const FileDescriptorShuffle&) = delete; + void operator=(const FileDescriptorShuffle&) = delete; +}; + +} // namespace ipc +} // namespace mozilla + +#endif // mozilla_ipc_FileDescriptorShuffle_h |