1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2024 Christian Brauner <brauner@kernel.org>
7 //! C header: [`include/linux/pid_namespace.h`](srctree/include/linux/pid_namespace.h) and
8 //! [`include/linux/pid.h`](srctree/include/linux/pid.h)
12 types::{AlwaysRefCounted, Opaque},
16 /// Wraps the kernel's `struct pid_namespace`. Thread safe.
18 /// This structure represents the Rust abstraction for a C `struct pid_namespace`. This
19 /// implementation abstracts the usage of an already existing C `struct pid_namespace` within Rust
20 /// code that we get passed from the C side.
22 pub struct PidNamespace {
23 inner: Opaque<bindings::pid_namespace>,
27 /// Returns a raw pointer to the inner C struct.
29 pub fn as_ptr(&self) -> *mut bindings::pid_namespace {
33 /// Creates a reference to a [`PidNamespace`] from a valid pointer.
37 /// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the
38 /// returned [`PidNamespace`] reference.
39 pub unsafe fn from_ptr<'a>(ptr: *const bindings::pid_namespace) -> &'a Self {
40 // SAFETY: The safety requirements guarantee the validity of the dereference, while the
41 // `PidNamespace` type being transparent makes the cast ok.
42 unsafe { &*ptr.cast() }
46 // SAFETY: Instances of `PidNamespace` are always reference-counted.
47 unsafe impl AlwaysRefCounted for PidNamespace {
50 // SAFETY: The existence of a shared reference means that the refcount is nonzero.
51 unsafe { bindings::get_pid_ns(self.as_ptr()) };
55 unsafe fn dec_ref(obj: ptr::NonNull<PidNamespace>) {
56 // SAFETY: The safety requirements guarantee that the refcount is non-zero.
57 unsafe { bindings::put_pid_ns(obj.cast().as_ptr()) }
62 // - `PidNamespace::dec_ref` can be called from any thread.
63 // - It is okay to send ownership of `PidNamespace` across thread boundaries.
64 unsafe impl Send for PidNamespace {}
66 // SAFETY: It's OK to access `PidNamespace` through shared references from other threads because
67 // we're either accessing properties that don't change or that are properly synchronised by C code.
68 unsafe impl Sync for PidNamespace {}