Skip to main content

yash_env/system/concurrency/
delegates.rs

1// This file is part of yash, an extended POSIX shell.
2// Copyright (C) 2026 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//! Trait implementations for `Concurrent<S>` that delegate to the inner system `S`
18
19use super::super::resource::{LimitPair, Resource};
20use super::super::{
21    Chdir, ChildProcessStarter, Clock, Close, CpuTimes, Dir, Dup, Exec, Exit, Fcntl, FdFlag, Fork,
22    Fstat, GetCwd, GetPid, GetPw, GetRlimit, GetSigaction, GetUid, Gid, IsExecutableFile, Isatty,
23    Mode, OfdAccess, Open, OpenFlag, Pipe, Result, Seek, SendSignal, SetPgid, SetRlimit, ShellPath,
24    Sigaction, Sigmask, SigmaskOp, Signals, Sysconf, TcGetPgrp, TcSetPgrp, Times, Uid, Umask, Wait,
25    signal,
26};
27use super::Concurrent;
28use crate::io::Fd;
29use crate::job::{Pid, ProcessState};
30use crate::path::PathBuf;
31use crate::semantics::ExitStatus;
32use enumset::EnumSet;
33use std::convert::Infallible;
34use std::ffi::{CStr, CString};
35use std::io::SeekFrom;
36use std::ops::RangeInclusive;
37use std::time::Instant;
38use unix_str::UnixString;
39
40impl<S> Fstat for Concurrent<S>
41where
42    S: Fstat,
43{
44    type Stat = S::Stat;
45
46    #[inline]
47    fn fstat(&self, fd: Fd) -> Result<Self::Stat> {
48        self.inner.fstat(fd)
49    }
50    #[inline]
51    fn fstatat(&self, dir_fd: Fd, path: &CStr, follow_symlinks: bool) -> Result<Self::Stat> {
52        self.inner.fstatat(dir_fd, path, follow_symlinks)
53    }
54    #[inline]
55    fn is_directory(&self, path: &CStr) -> bool {
56        self.inner.is_directory(path)
57    }
58    #[inline]
59    fn fd_is_pipe(&self, fd: Fd) -> bool {
60        self.inner.fd_is_pipe(fd)
61    }
62}
63
64impl<S> IsExecutableFile for Concurrent<S>
65where
66    S: IsExecutableFile,
67{
68    #[inline]
69    fn is_executable_file(&self, path: &CStr) -> bool {
70        self.inner.is_executable_file(path)
71    }
72}
73
74impl<S> Pipe for Concurrent<S>
75where
76    S: Pipe,
77{
78    #[inline]
79    fn pipe(&self) -> Result<(Fd, Fd)> {
80        self.inner.pipe()
81    }
82}
83
84impl<S> Dup for Concurrent<S>
85where
86    S: Dup,
87{
88    #[inline]
89    fn dup(&self, from: Fd, to_min: Fd, flags: EnumSet<FdFlag>) -> Result<Fd> {
90        self.inner.dup(from, to_min, flags)
91    }
92
93    #[inline]
94    fn dup2(&self, from: Fd, to: Fd) -> Result<Fd> {
95        self.inner.dup2(from, to)
96    }
97}
98
99/// This implementation does not (yet) support non-blocking open operations.
100impl<S> Open for Concurrent<S>
101where
102    S: Open,
103{
104    #[inline]
105    fn open(
106        &self,
107        path: &CStr,
108        access: OfdAccess,
109        flags: EnumSet<OpenFlag>,
110        mode: Mode,
111    ) -> impl Future<Output = Result<Fd>> + use<S> {
112        self.inner.open(path, access, flags, mode)
113    }
114
115    #[inline]
116    fn open_tmpfile(&self, parent_dir: &unix_path::Path) -> Result<Fd> {
117        self.inner.open_tmpfile(parent_dir)
118    }
119
120    #[inline]
121    fn fdopendir(&self, fd: Fd) -> Result<impl Dir + use<S>> {
122        self.inner.fdopendir(fd)
123    }
124
125    #[inline]
126    fn opendir(&self, path: &CStr) -> Result<impl Dir + use<S>> {
127        self.inner.opendir(path)
128    }
129}
130
131impl<S> Close for Concurrent<S>
132where
133    S: Close,
134{
135    #[inline]
136    fn close(&self, fd: Fd) -> Result<()> {
137        self.inner.close(fd)
138    }
139}
140
141impl<S> Fcntl for Concurrent<S>
142where
143    S: Fcntl,
144{
145    #[inline]
146    fn ofd_access(&self, fd: Fd) -> Result<OfdAccess> {
147        self.inner.ofd_access(fd)
148    }
149
150    #[inline]
151    fn get_and_set_nonblocking(&self, fd: Fd, nonblocking: bool) -> Result<bool> {
152        self.inner.get_and_set_nonblocking(fd, nonblocking)
153    }
154
155    #[inline]
156    fn fcntl_getfd(&self, fd: Fd) -> Result<EnumSet<FdFlag>> {
157        self.inner.fcntl_getfd(fd)
158    }
159
160    #[inline]
161    fn fcntl_setfd(&self, fd: Fd, flags: EnumSet<FdFlag>) -> Result<()> {
162        self.inner.fcntl_setfd(fd, flags)
163    }
164}
165
166impl<S> Seek for Concurrent<S>
167where
168    S: Seek,
169{
170    #[inline]
171    fn lseek(&self, fd: Fd, position: SeekFrom) -> Result<u64> {
172        self.inner.lseek(fd, position)
173    }
174}
175
176impl<S> Umask for Concurrent<S>
177where
178    S: Umask,
179{
180    #[inline]
181    fn umask(&self, new_mask: Mode) -> Mode {
182        self.inner.umask(new_mask)
183    }
184}
185
186impl<S> GetCwd for Concurrent<S>
187where
188    S: GetCwd,
189{
190    #[inline]
191    fn getcwd(&self) -> Result<PathBuf> {
192        self.inner.getcwd()
193    }
194}
195
196impl<S> Chdir for Concurrent<S>
197where
198    S: Chdir,
199{
200    #[inline]
201    fn chdir(&self, path: &CStr) -> Result<()> {
202        self.inner.chdir(path)
203    }
204}
205
206impl<S> Clock for Concurrent<S>
207where
208    S: Clock,
209{
210    #[inline]
211    fn now(&self) -> Instant {
212        self.inner.now()
213    }
214}
215
216impl<S> Times for Concurrent<S>
217where
218    S: Times,
219{
220    #[inline]
221    fn times(&self) -> Result<CpuTimes> {
222        self.inner.times()
223    }
224}
225
226impl<S> GetPid for Concurrent<S>
227where
228    S: GetPid,
229{
230    #[inline]
231    fn getpid(&self) -> Pid {
232        self.inner.getpid()
233    }
234    #[inline]
235    fn getppid(&self) -> Pid {
236        self.inner.getppid()
237    }
238    #[inline]
239    fn getpgrp(&self) -> Pid {
240        self.inner.getpgrp()
241    }
242    #[inline]
243    fn getsid(&self, pid: Pid) -> Result<Pid> {
244        self.inner.getsid(pid)
245    }
246}
247
248impl<S> SetPgid for Concurrent<S>
249where
250    S: SetPgid,
251{
252    #[inline]
253    fn setpgid(&self, pid: Pid, pgid: Pid) -> Result<()> {
254        self.inner.setpgid(pid, pgid)
255    }
256}
257
258impl<S> Signals for Concurrent<S>
259where
260    S: Signals,
261{
262    const SIGABRT: signal::Number = S::SIGABRT;
263    const SIGALRM: signal::Number = S::SIGALRM;
264    const SIGBUS: signal::Number = S::SIGBUS;
265    const SIGCHLD: signal::Number = S::SIGCHLD;
266    const SIGCLD: Option<signal::Number> = S::SIGCLD;
267    const SIGCONT: signal::Number = S::SIGCONT;
268    const SIGEMT: Option<signal::Number> = S::SIGEMT;
269    const SIGFPE: signal::Number = S::SIGFPE;
270    const SIGHUP: signal::Number = S::SIGHUP;
271    const SIGILL: signal::Number = S::SIGILL;
272    const SIGINFO: Option<signal::Number> = S::SIGINFO;
273    const SIGINT: signal::Number = S::SIGINT;
274    const SIGIO: Option<signal::Number> = S::SIGIO;
275    const SIGIOT: signal::Number = S::SIGIOT;
276    const SIGKILL: signal::Number = S::SIGKILL;
277    const SIGLOST: Option<signal::Number> = S::SIGLOST;
278    const SIGPIPE: signal::Number = S::SIGPIPE;
279    const SIGPOLL: Option<signal::Number> = S::SIGPOLL;
280    const SIGPROF: signal::Number = S::SIGPROF;
281    const SIGPWR: Option<signal::Number> = S::SIGPWR;
282    const SIGQUIT: signal::Number = S::SIGQUIT;
283    const SIGSEGV: signal::Number = S::SIGSEGV;
284    const SIGSTKFLT: Option<signal::Number> = S::SIGSTKFLT;
285    const SIGSTOP: signal::Number = S::SIGSTOP;
286    const SIGSYS: signal::Number = S::SIGSYS;
287    const SIGTERM: signal::Number = S::SIGTERM;
288    const SIGTHR: Option<signal::Number> = S::SIGTHR;
289    const SIGTRAP: signal::Number = S::SIGTRAP;
290    const SIGTSTP: signal::Number = S::SIGTSTP;
291    const SIGTTIN: signal::Number = S::SIGTTIN;
292    const SIGTTOU: signal::Number = S::SIGTTOU;
293    const SIGURG: signal::Number = S::SIGURG;
294    const SIGUSR1: signal::Number = S::SIGUSR1;
295    const SIGUSR2: signal::Number = S::SIGUSR2;
296    const SIGVTALRM: signal::Number = S::SIGVTALRM;
297    const SIGWINCH: signal::Number = S::SIGWINCH;
298    const SIGXCPU: signal::Number = S::SIGXCPU;
299    const SIGXFSZ: signal::Number = S::SIGXFSZ;
300
301    #[inline]
302    fn sigrt_range(&self) -> Option<RangeInclusive<signal::Number>> {
303        self.inner.sigrt_range()
304    }
305
306    const NAMED_SIGNALS: &'static [(&'static str, Option<signal::Number>)] = S::NAMED_SIGNALS;
307
308    #[inline]
309    fn iter_sigrt(&self) -> impl DoubleEndedIterator<Item = signal::Number> + use<S> {
310        self.inner.iter_sigrt()
311    }
312    #[inline]
313    fn to_signal_number<N: Into<signal::RawNumber>>(&self, number: N) -> Option<signal::Number> {
314        self.inner.to_signal_number(number)
315    }
316    #[inline]
317    fn sig2str<N: Into<signal::RawNumber>>(
318        &self,
319        signal: N,
320    ) -> Option<std::borrow::Cow<'static, str>> {
321        self.inner.sig2str(signal)
322    }
323    #[inline]
324    fn str2sig(&self, name: &str) -> Option<signal::Number> {
325        self.inner.str2sig(name)
326    }
327    #[inline]
328    fn validate_signal(&self, number: signal::RawNumber) -> Option<(signal::Name, signal::Number)> {
329        self.inner.validate_signal(number)
330    }
331    #[inline]
332    fn signal_name_from_number(&self, number: signal::Number) -> signal::Name {
333        self.inner.signal_name_from_number(number)
334    }
335    #[inline]
336    fn signal_number_from_name(&self, name: signal::Name) -> Option<signal::Number> {
337        self.inner.signal_number_from_name(name)
338    }
339}
340
341/// Exposes the inner system's `sigmask` method.
342///
343/// This implementation of `Sigmask` simply delegates to the inner system's
344/// `sigmask` method, which bypasses the internal state of `Concurrent` and may
345/// prevent the [`peek`](Concurrent::peek) and [`select`](Concurrent::select)
346/// methods from responding to received signals without race conditions. To
347/// ensure that the signal mask is configured in a way that allows `Concurrent`
348/// to respond to signals correctly, direct calls to `sigmask` should be
349/// avoided, and, if necessary, only used to temporarily change the signal mask
350/// for specific operations while ensuring that the original mask is restored
351/// afterward before a next call to `peek`, `select`, or `set_disposition`.
352impl<S> Sigmask for Concurrent<S>
353where
354    S: Sigmask,
355{
356    #[inline]
357    fn sigmask(
358        &self,
359        op_and_signals: Option<(SigmaskOp, &[signal::Number])>,
360        old_mask: Option<&mut Vec<signal::Number>>,
361    ) -> impl Future<Output = Result<()>> + use<S> {
362        self.inner.sigmask(op_and_signals, old_mask)
363    }
364}
365
366impl<S> GetSigaction for Concurrent<S>
367where
368    S: GetSigaction,
369{
370    #[inline]
371    fn get_sigaction(&self, signal: signal::Number) -> Result<signal::Disposition> {
372        self.inner.get_sigaction(signal)
373    }
374}
375
376/// Exposes the inner system's `sigaction` method.
377///
378/// This implementation of `Sigaction` simply delegates to the inner system's
379/// `sigaction` method, which bypasses the internal state of `Concurrent` and
380/// may prevent the [`peek`](Concurrent::peek) and
381/// [`select`](Concurrent::select) methods from responding to received signals
382/// without race conditions. To ensure that signal dispositions are configured
383/// in a way that allows `Concurrent` to respond to signals correctly, direct
384/// calls to `sigaction` should be avoided, and, if necessary, only used to
385/// temporarily change the signal disposition for specific operations while
386/// ensuring that the original disposition is restored afterward before a next
387/// call to `peek`, `select`, or `set_disposition`.
388///
389/// The standard way to set a signal disposition to `Concurrent` is to use the
390/// `set_disposition` method provided by the [`SignalSystem`] trait, which
391/// ensures that the signal disposition and the signal mask are updated
392/// consistently.
393///
394/// [`SignalSystem`]: crate::trap::SignalSystem
395impl<S> Sigaction for Concurrent<S>
396where
397    S: Sigaction,
398{
399    #[inline]
400    fn sigaction(
401        &self,
402        signal: signal::Number,
403        disposition: signal::Disposition,
404    ) -> Result<signal::Disposition> {
405        self.inner.sigaction(signal, disposition)
406    }
407}
408
409// CaughtSignals is not implemented for Concurrent<S> because Concurrent needs to
410// control the signal dispositions and the signal mask itself to ensure that the
411// select method can respond to received signals without race conditions. Instead,
412// Concurrent<S> implements the SignalSystem trait, which provides the necessary
413// methods for configuring signal dispositions and masks in a controlled manner.
414//
415// Note: Sigmask, GetSigaction, and Sigaction are implemented above as delegating
416// implementations. However, direct calls to sigmask() and sigaction() bypass
417// Concurrent's internal state and may prevent peek() and select() from responding
418// to received signals without race conditions. To ensure correct behavior, use the
419// set_disposition method from the SignalSystem trait instead. If direct sigmask or
420// sigaction calls are necessary, temporarily change the mask/disposition and restore
421// it afterward before calling peek(), select(), or set_disposition().
422
423impl<S> SendSignal for Concurrent<S>
424where
425    S: SendSignal,
426{
427    #[inline]
428    fn kill(
429        &self,
430        pid: Pid,
431        signal: Option<signal::Number>,
432    ) -> impl Future<Output = Result<()>> + use<S> {
433        self.inner.kill(pid, signal)
434    }
435    #[inline]
436    fn raise(&self, signal: signal::Number) -> impl Future<Output = Result<()>> + use<S> {
437        self.inner.raise(signal)
438    }
439}
440
441impl<S> Isatty for Concurrent<S>
442where
443    S: Isatty,
444{
445    #[inline]
446    fn isatty(&self, fd: Fd) -> bool {
447        self.inner.isatty(fd)
448    }
449}
450
451impl<S> TcGetPgrp for Concurrent<S>
452where
453    S: TcGetPgrp,
454{
455    #[inline]
456    fn tcgetpgrp(&self, fd: Fd) -> Result<Pid> {
457        self.inner.tcgetpgrp(fd)
458    }
459}
460
461impl<S> TcSetPgrp for Concurrent<S>
462where
463    S: TcSetPgrp,
464{
465    #[inline]
466    fn tcsetpgrp(&self, fd: Fd, pgid: Pid) -> impl Future<Output = Result<()>> + use<S> {
467        self.inner.tcsetpgrp(fd, pgid)
468    }
469}
470
471// Concurrent<S> cannot implement Fork because the return type of the fork
472// method does not match with that of the inner system S. Instead, Concurrent<S>
473// provides an inherent method `new_child_process` that returns a
474// `ChildProcessStarter<S>` as returned by the inner method.
475// impl<S> Fork for Concurrent<S>
476// where
477//     S: Fork,
478// {
479//     #[inline]
480//     fn new_child_process(&self) -> Result<ChildProcessStarter<Self>>
481//     where
482//         Self: Sized,
483//     {
484//         todo!()
485//     }
486// }
487
488impl<S> Concurrent<S>
489where
490    S: Fork,
491{
492    /// Creates a new child process.
493    ///
494    /// Returns the `ChildProcessStarter<S>` returned by the inner system's
495    /// [`Fork::new_child_process`] method. This method is an inherent method of
496    /// `Concurrent<S>` instead of an implementation of the `Fork` trait because
497    /// the return type does not match with that of the inner system `S`.
498    #[inline]
499    pub fn new_child_process(&self) -> Result<ChildProcessStarter<S>> {
500        self.inner.new_child_process()
501    }
502}
503
504impl<S> Wait for Concurrent<S>
505where
506    S: Wait,
507{
508    #[inline]
509    fn wait(&self, target: Pid) -> Result<Option<(Pid, ProcessState)>> {
510        self.inner.wait(target)
511    }
512}
513
514impl<S> Exec for Concurrent<S>
515where
516    S: Exec,
517{
518    #[inline]
519    fn execve(
520        &self,
521        path: &CStr,
522        args: &[CString],
523        envs: &[CString],
524    ) -> impl Future<Output = Result<Infallible>> + use<S> {
525        self.inner.execve(path, args, envs)
526    }
527}
528
529impl<S> Exit for Concurrent<S>
530where
531    S: Exit,
532{
533    #[inline]
534    fn exit(&self, exit_status: ExitStatus) -> impl Future<Output = Infallible> + use<S> {
535        self.inner.exit(exit_status)
536    }
537}
538
539impl<S> GetUid for Concurrent<S>
540where
541    S: GetUid,
542{
543    #[inline]
544    fn getuid(&self) -> Uid {
545        self.inner.getuid()
546    }
547    #[inline]
548    fn geteuid(&self) -> Uid {
549        self.inner.geteuid()
550    }
551    #[inline]
552    fn getgid(&self) -> Gid {
553        self.inner.getgid()
554    }
555    #[inline]
556    fn getegid(&self) -> Gid {
557        self.inner.getegid()
558    }
559}
560
561impl<S> GetPw for Concurrent<S>
562where
563    S: GetPw,
564{
565    #[inline]
566    fn getpwnam_dir(&self, name: &CStr) -> Result<Option<PathBuf>> {
567        self.inner.getpwnam_dir(name)
568    }
569}
570
571impl<S> Sysconf for Concurrent<S>
572where
573    S: Sysconf,
574{
575    #[inline]
576    fn confstr_path(&self) -> Result<UnixString> {
577        self.inner.confstr_path()
578    }
579}
580
581impl<S> ShellPath for Concurrent<S>
582where
583    S: ShellPath,
584{
585    #[inline]
586    fn shell_path(&self) -> CString {
587        self.inner.shell_path()
588    }
589}
590
591impl<S> GetRlimit for Concurrent<S>
592where
593    S: GetRlimit,
594{
595    #[inline]
596    fn getrlimit(&self, resource: Resource) -> Result<LimitPair> {
597        self.inner.getrlimit(resource)
598    }
599}
600
601impl<S> SetRlimit for Concurrent<S>
602where
603    S: SetRlimit,
604{
605    #[inline]
606    fn setrlimit(&self, resource: Resource, limits: LimitPair) -> Result<()> {
607        self.inner.setrlimit(resource, limits)
608    }
609}