1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! The grant a request brings a session, which the daemon holds on the
//! session's job (#1780): what the session's turn may do through session
//! control (`crate::caller_context`).
//!
//! A request names its grant, as the session-control core's do, or leaves it
//! to its client's environment, as a person's `rho run --detach` inside a
//! session's shell does. Either way the daemon decides the grant once,
//! records it, and writes the session's `RHO_SESSION_*` variables from it,
//! when it opens or reopens the session and when a prompt starts a turn, so
//! the record and what the session's processes see never differ.
//!
//! A session that opens under a limit, as every session an agent starts
//! through session control does, is held to the grant it opened with
//! (#1796), itself no more than its parent's turn could give: its job keeps
//! that as the limit of every later grant, so a prompt from a person, or
//! from a session with more, cannot widen it. A session that opens without
//! one, as a person's does, has no limit, and each prompt grants it anew.
use std::{collections::BTreeMap, sync::Arc};
use rho_protocol::{ClientEnvironment, ErrorCode, RpcError, SessionGrant, SessionSetup};
use super::open::{session_environment, OpenWith};
use crate::{
caller_context::{CallerContext, ENV_NAMES},
session_environment::Ambient,
};
/// The grant a request asks for: the one it names, else the one its
/// client's `environment` carries. A grant the environment carries
/// malformed is invalid params, never the unlimited default.
pub(super) fn asked(
named: Option<SessionGrant>,
environment: &ClientEnvironment,
) -> Result<CallerContext, RpcError> {
match named {
Some(grant) => Ok(grant.into()),
None => CallerContext::from_env(|name| {
environment
.iter()
.find(|(variable, _)| *variable == name)
.map(|(_, value)| value.into())
})
.map_err(|error| RpcError::new(ErrorCode::InvalidParams, format!("{error:#}"))),
}
}
/// The grant a new session opens with when its request asks for `asked`:
/// `asked`, and when another session's turn starts it, no more than a
/// session started under `parents`, the grant of the job writing that
/// session, may have (#1796). A session with no parent, or with one no job
/// of the daemon writes, has no record to hold it to and opens with what
/// was asked.
///
/// `parents` is the grant of the parent's turn under way, or of its last
/// one under this daemon when it runs elsewhere now, which can only make
/// the new session's grant less.
pub(super) fn opening(asked: CallerContext, parents: Option<&CallerContext>) -> CallerContext {
match parents {
Some(parents) => asked.within(&parents.child()),
None => asked,
}
}
/// `environment` carrying `grant`: its variables, with the grant's in place
/// of any it had.
pub(super) fn carrying(grant: &CallerContext, environment: &Ambient) -> Ambient {
let mut variables = match environment {
Ambient::Client(variables) => BTreeMap::clone(variables),
// Never a daemon session's: each runs in a client's variables.
Ambient::ThisProcess => return Ambient::ThisProcess,
};
for name in ENV_NAMES {
variables.remove(name);
}
// A grant without limits is carried by no variable.
if !grant.is_root() {
variables.extend(
grant
.env()
.into_iter()
.map(|(name, value)| (name.to_owned(), value)),
);
}
Ambient::Client(Arc::new(variables))
}
impl OpenWith {
/// What a request with `environment` and `setup` brings a session it
/// runs under `grant`: the client's variables, with the grant's in place
/// of any the client's carry.
pub(super) fn under(
grant: CallerContext,
environment: &ClientEnvironment,
setup: SessionSetup,
) -> Result<Self, RpcError> {
Ok(Self {
environment: carrying(&grant, &session_environment(environment)?),
setup,
grant,
})
}
/// What this request brings a session it reopens whose job gave it
/// `grant` instead of the one it asked for.
pub(super) fn granted(&self, grant: CallerContext) -> Self {
Self {
environment: carrying(&grant, &self.environment),
setup: self.setup.clone(),
grant,
}
}
}
#[cfg(test)]
#[path = "grant_tests.rs"]
mod tests;