Skip to main content

task/manager/
metadata.rs

1// Metadata module - handles task metadata and data structures
2
3use super::*;
4use alloc::{string::String, vec::Vec};
5use users::{GroupIdentifier, UserIdentifier};
6
7/// Internal representation of a task.
8pub(crate) struct Metadata {
9    pub(crate) generation: u64,
10    /// Internal identifier of the task.
11    pub(crate) internal_identifier: usize,
12    /// Name of the task.
13    pub(crate) name: String,
14    /// The children of the task.
15    pub(crate) parent: TaskIdentifier,
16    /// The identifier of the user that owns the task.
17    pub(crate) user: UserIdentifier,
18    /// The identifier of the group that owns the task.
19    pub(crate) group: GroupIdentifier,
20    /// Whether the task, or one of its ancestors, was given its identity by
21    /// its launcher (see [`Manager::grant_identity`]). Copied into every task
22    /// it spawns afterwards and never cleared.
23    pub(crate) identity_granted: bool,
24    /// Environment variables of the task.
25    pub(crate) environment_variables: Vec<EnvironmentVariable>,
26    /// Signals
27    pub(crate) signals: SignalAccumulator,
28    /// Index of the spawner that spawned this task (for tracking completion)
29    pub(crate) spawner_identifier: usize,
30    /// Poll counters of the task's storage slot and its executor; `None` for
31    /// adopted tasks.
32    #[cfg(feature = "metrics")]
33    pub(crate) instrumentation: Option<crate::metrics::TaskInstrumentation>,
34}
35
36/// Authority bound to one task registration in one manager. Copies retain that
37/// registration; they never inherit the identity of a reused numeric task ID.
38#[derive(Clone, Copy)]
39pub struct TaskAuthority<'a> {
40    pub(super) manager: &'a Manager,
41    pub(super) task: TaskIdentifier,
42    pub(super) generation: u64,
43}
44
45impl TaskAuthority<'_> {
46    fn validate(&self, inner: &Inner) -> Result<UserIdentifier> {
47        let metadata = inner
48            .tasks
49            .get(&self.task)
50            .ok_or(Error::InvalidTaskIdentifier)?;
51        if metadata.generation != self.generation {
52            return Err(Error::InvalidTaskIdentifier);
53        }
54        Ok(metadata.user)
55    }
56
57    /// Read current credentials; exit or ID reuse revokes the authority.
58    pub async fn user(&self) -> Result<UserIdentifier> {
59        self.validate(&*self.manager.0.read().await)
60    }
61
62    /// Single-attempt lookup for synchronous adapters. `None` means contention.
63    pub fn try_user(&self) -> Result<Option<UserIdentifier>> {
64        match self.manager.0.try_read() {
65            Ok(inner) => self.validate(&inner).map(Some),
66            Err(_) => Ok(None),
67        }
68    }
69}
70
71impl Manager {
72    pub async fn authority(&self, task: TaskIdentifier) -> Result<TaskAuthority<'_>> {
73        let inner = self.0.read().await;
74        let metadata = inner.tasks.get(&task).ok_or(Error::InvalidTaskIdentifier)?;
75        Ok(TaskAuthority {
76            manager: self,
77            task,
78            generation: metadata.generation,
79        })
80    }
81}