yash_env/semantics/command.rs
1// This file is part of yash, an extended POSIX shell.
2// Copyright (C) 2025 WATANABE Yuki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17//! Command execution components
18//!
19//! This module provides functionality related to command execution semantics.
20
21pub mod search;
22
23use crate::Env;
24use crate::function::Function;
25use crate::job::{RunBlocking, RunUnblocking, add_job_if_suspended};
26use crate::semantics::{ExitStatus, Field, Result};
27use crate::source::Location;
28use crate::source::pretty::{Report, ReportType, Snippet};
29use crate::subshell::{BlockSignals, Config};
30use crate::system::concurrency::WaitForSignals;
31use crate::system::resource::SetRlimit;
32use crate::system::{
33 Close, Dup, Errno, Exec, Exit, Fork, GetPid, Open, SendSignal, SetPgid, ShellPath, TcSetPgrp,
34 Wait,
35};
36use crate::trap::SignalSystem;
37use itertools::Itertools as _;
38use std::convert::Infallible;
39use std::ffi::CString;
40use std::ops::ControlFlow::Continue;
41use std::pin::Pin;
42use std::rc::Rc;
43use thiserror::Error;
44
45type PinFuture<'a, T = ()> = Pin<Box<dyn Future<Output = T> + 'a>>;
46type FutureResult<'a, T = ()> = PinFuture<'a, Result<T>>;
47
48type EnvPrepHook<S> = fn(&mut Env<S>) -> PinFuture<'_, ()>;
49
50/// Wrapper for a function that runs a shell function
51///
52/// This struct declares a function type that runs a shell function.
53/// It is used to inject command execution behavior into the shell environment.
54/// An instance of this struct can be stored in the shell environment
55/// ([`Env::any`]) and used by modules that need to run shell functions.
56///
57/// The wrapped function takes the following arguments:
58///
59/// 1. A mutable reference to the shell environment (`&'a mut Env`)
60/// 2. A reference-counted pointer to the shell function to be executed (`Rc<Function>`)
61/// 3. A vector of fields representing the arguments to be passed to the function (`Vec<Field>`)
62/// - This should not be empty; the first element is the function name and
63/// the rest are the actual arguments.
64/// 4. An optional environment preparation hook
65/// (`Option<fn(&mut Env) -> Pin<Box<dyn Future<Output = ()>>>>`)
66/// - This hook is called after setting up the local variable context. It can inject
67/// additional setup logic or modify the environment before the function is executed.
68///
69/// The function returns a future that resolves to a [`Result`] indicating the
70/// outcome of the function execution.
71///
72/// The most standard implementation of this type is provided in the
73/// [`yash-semantics` crate](https://crates.io/crates/yash-semantics):
74///
75/// ```
76/// # use yash_env::Env;
77/// # use yash_env::semantics::command::RunFunction;
78/// fn register_run_function<S: 'static>(env: &mut Env<S>) {
79/// env.any.insert(Box::new(RunFunction::<S>(|env, function, fields, env_prep_hook| {
80/// Box::pin(async move {
81/// yash_semantics::command::simple_command::execute_function_body(
82/// env, function, fields, env_prep_hook
83/// ).await
84/// })
85/// })));
86/// }
87/// # register_run_function(&mut Env::new_virtual());
88/// ```
89pub struct RunFunction<S>(
90 #[allow(clippy::type_complexity, reason = "we can't make this simpler")]
91 pub for<'a> fn(
92 &'a mut Env<S>,
93 Rc<Function<S>>,
94 Vec<Field>,
95 Option<EnvPrepHook<S>>,
96 ) -> FutureResult<'a>,
97);
98
99// Not derived automatically because S may not implement Clone, Copy or Debug.
100impl<S> Clone for RunFunction<S> {
101 fn clone(&self) -> Self {
102 *self
103 }
104}
105
106impl<S> Copy for RunFunction<S> {}
107
108impl<S> std::fmt::Debug for RunFunction<S> {
109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110 f.debug_tuple("RunFunction").field(&self.0).finish()
111 }
112}
113
114/// Error returned when [replacing the current process](replace_current_process) fails
115#[derive(Clone, Debug, Error)]
116#[error("cannot execute external utility {path:?}: {errno}")]
117pub struct ReplaceCurrentProcessError {
118 /// Path of the external utility attempted to be executed
119 pub path: CString,
120 /// Error returned by the [`execve`](Exec::execve) system call
121 pub errno: Errno,
122}
123
124/// Substitutes the currently executing shell process with the external utility.
125///
126/// This function performs the very last step of the simple command execution.
127/// It disables the internal signal dispositions and calls the
128/// [`execve`](Exec::execve) system call. If the call fails, it updates
129/// `env.exit_status` and returns an error, in which case the caller should
130/// print an error message and terminate the current process with the exit
131/// status.
132///
133/// If the `execve` call fails with [`ENOEXEC`](Errno::ENOEXEC), this function
134/// falls back on invoking the shell with the given arguments, so that the shell
135/// can interpret the script. The path to the shell executable is taken from
136/// [`ShellPath::shell_path`].
137///
138/// If the `execve` call succeeds, the future returned by this function never
139/// resolves.
140///
141/// This function is for implementing the simple command execution semantics and
142/// the `exec` built-in utility.
143pub async fn replace_current_process<S: Exec + ShellPath + SignalSystem>(
144 env: &mut Env<S>,
145 path: CString,
146 args: Vec<Field>,
147) -> std::result::Result<Infallible, ReplaceCurrentProcessError> {
148 env.traps
149 .disable_internal_dispositions(&env.system)
150 .await
151 .ok();
152
153 let args = to_c_strings(args);
154 let envs = env.variables.env_c_strings();
155 let Err(errno) = env.system.execve(path.as_c_str(), &args, &envs).await;
156 env.exit_status = match errno {
157 Errno::ENOEXEC => {
158 fall_back_on_sh(&env.system, path.clone(), args, envs).await;
159 ExitStatus::NOEXEC
160 }
161 Errno::ENOENT | Errno::ENOTDIR => ExitStatus::NOT_FOUND,
162 _ => ExitStatus::NOEXEC,
163 };
164 Err(ReplaceCurrentProcessError { path, errno })
165}
166
167/// Converts fields to C strings.
168fn to_c_strings(s: Vec<Field>) -> Vec<CString> {
169 s.into_iter()
170 .filter_map(|f| {
171 let bytes = f.value.into_bytes();
172 // TODO Handle interior null bytes more gracefully
173 CString::new(bytes).ok()
174 })
175 .collect()
176}
177
178/// Invokes the shell with the given arguments.
179async fn fall_back_on_sh<S: ShellPath + Exec>(
180 system: &S,
181 mut script_path: CString,
182 mut args: Vec<CString>,
183 envs: Vec<CString>,
184) {
185 // Prevent the path to be regarded as an option
186 if script_path.as_bytes().starts_with("-".as_bytes()) {
187 let mut bytes = script_path.into_bytes();
188 bytes.splice(0..0, "./".bytes());
189 script_path = CString::new(bytes).unwrap();
190 }
191
192 args.insert(1, script_path);
193
194 // Some shells change their behavior depending on args[0].
195 // We set it to "sh" for the maximum portability.
196 c"sh".clone_into(&mut args[0]);
197
198 let sh_path = system.shell_path();
199 system.execve(&sh_path, &args, &envs).await.ok();
200}
201
202/// Error returned when starting a subshell fails in [`run_external_utility_in_subshell`]
203#[derive(Clone, Debug, Error)]
204#[error("cannot start subshell for utility {utility:?}: {errno}")]
205pub struct StartSubshellError {
206 pub utility: Field,
207 pub errno: Errno,
208}
209
210impl<'a> From<&'a StartSubshellError> for Report<'a> {
211 fn from(error: &'a StartSubshellError) -> Self {
212 let mut report = Report::new();
213 report.r#type = ReportType::Error;
214 report.title = format!(
215 "cannot start subshell for utility {:?}",
216 error.utility.value
217 )
218 .into();
219 report.snippets = Snippet::with_primary_span(
220 &error.utility.origin,
221 format!("{:?}: {}", error.utility.value, error.errno).into(),
222 );
223 report
224 }
225}
226
227/// Starts an external utility in a subshell and waits for it to finish.
228///
229/// `path` is the path to the external utility. `args` are the command line
230/// words of the utility. The first field must exist and be the name of the
231/// utility as it may be used in error messages.
232///
233/// This function starts the utility in a subshell and waits for it to finish.
234/// The subshell will be a foreground job if job control is enabled.
235///
236/// This function returns the exit status of the utility. In case of an error,
237/// one of the error handling functions will be called before returning an
238/// appropriate exit status. `handle_start_subshell_error` is called in the
239/// parent shell if starting the subshell fails.
240/// `handle_replace_current_process_error` is called in the subshell if
241/// replacing the subshell process with the utility fails. Both functions
242/// should print appropriate error messages.
243///
244/// This function is for implementing the simple command execution semantics and
245/// the `command` built-in utility. This function internally uses
246/// [`replace_current_process`] to execute the utility in the subshell.
247pub async fn run_external_utility_in_subshell<S>(
248 env: &mut Env<S>,
249 path: CString,
250 args: Vec<Field>,
251 handle_start_subshell_error: fn(&mut Env<S>, StartSubshellError) -> PinFuture<'_>,
252 handle_replace_current_process_error: fn(
253 &mut Env<S>,
254 ReplaceCurrentProcessError,
255 Location,
256 ) -> PinFuture<'_>,
257) -> Result<ExitStatus>
258where
259 S: BlockSignals
260 + Close
261 + Dup
262 + Exec
263 + Exit
264 + Fork
265 + GetPid
266 + Open
267 + RunBlocking
268 + RunUnblocking
269 + SendSignal
270 + SetPgid
271 + SetRlimit
272 + ShellPath
273 + SignalSystem
274 + TcSetPgrp
275 + Wait
276 + WaitForSignals
277 + 'static,
278{
279 let utility = args[0].clone();
280
281 let job_name = if env.controls_jobs() {
282 to_job_name(&args)
283 } else {
284 String::new()
285 };
286 let subshell_result =
287 Config::foreground().start_and_wait(env, async move |env, _job_control| {
288 let location = args[0].origin.clone();
289 let Err(e) = replace_current_process(env, path, args).await;
290 handle_replace_current_process_error(env, e, location).await;
291 });
292
293 match subshell_result.await {
294 Ok((pid, result)) => add_job_if_suspended(env, pid, result, || job_name),
295 Err(errno) => {
296 handle_start_subshell_error(env, StartSubshellError { utility, errno }).await;
297 Continue(ExitStatus::NOEXEC)
298 }
299 }
300}
301
302fn to_job_name(fields: &[Field]) -> String {
303 fields
304 .iter()
305 .format_with(" ", |field, f| f(&format_args!("{}", field.value)))
306 .to_string()
307}