yash_env/system/virtual/
process.rs

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2021 WATANABE Yuki
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Processes in a virtual system.

use super::io::FdBody;
use super::signal::{self, SignalEffect};
use super::Disposition;
use super::Gid;
use super::Mode;
use super::SigmaskOp;
use super::Uid;
use crate::io::Fd;
use crate::job::Pid;
use crate::job::ProcessResult;
use crate::job::ProcessState;
use crate::path::Path;
use crate::path::PathBuf;
use crate::system::resource::LimitPair;
use crate::system::resource::Resource;
use crate::system::resource::INFINITY;
use crate::system::SelectSystem;
use std::cell::Cell;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::ffi::CString;
use std::fmt::Debug;
use std::ops::BitOr;
use std::ops::BitOrAssign;
use std::rc::Weak;
use std::task::Waker;

/// Process in a virtual system
#[derive(Clone, Debug)]
pub struct Process {
    /// Process ID of the parent process
    pub(crate) ppid: Pid,

    /// Process group ID of this process
    pub(crate) pgid: Pid,

    /// Real user ID of this process
    uid: Uid,

    /// Effective user ID of this process
    euid: Uid,

    /// Real group ID of this process
    gid: Gid,

    /// Effective group ID of this process
    egid: Gid,

    /// Set of file descriptors open in this process
    pub(crate) fds: BTreeMap<Fd, FdBody>,

    /// File creation mask
    pub(crate) umask: Mode,

    /// Working directory path
    pub(crate) cwd: PathBuf,

    /// Execution state of the process
    pub(crate) state: ProcessState,

    /// True when `state` has changed but not yet reported to the parent
    /// process.
    ///
    /// The change of `state` is reported when the parent `wait`s for this
    /// process.
    state_has_changed: bool,

    /// Wakers waiting for the state of this process to change to `Running`
    resumption_awaiters: Vec<Weak<Cell<Option<Waker>>>>,

    /// Current signal dispositions
    ///
    /// For signals not contained in this hash map, the default disposition is
    /// assumed.
    dispositions: HashMap<signal::Number, Disposition>,

    /// Set of blocked signals
    blocked_signals: BTreeSet<signal::Number>,

    /// Set of pending signals
    pending_signals: BTreeSet<signal::Number>,

    /// List of signals that have been delivered and caught
    pub(crate) caught_signals: Vec<signal::Number>,

    /// Limits for system resources
    pub(crate) resource_limits: HashMap<Resource, LimitPair>,

    /// Weak reference to the `SelectSystem` for this process
    ///
    /// This weak reference is empty for the initial process of a
    /// `VirtualSystem`.  When a new child process is created, a weak reference
    /// to the `SelectSystem` for the child is set.
    pub(crate) selector: Weak<RefCell<SelectSystem>>,

    /// Copy of arguments passed to [`execve`](crate::System::execve)
    pub(crate) last_exec: Option<(CString, Vec<CString>, Vec<CString>)>,
}

/// Finds the minimum available FD.
///
/// The returned FD is the minimum that is equal to or greater than `min` and
/// not included in `existings`. Items of `existings` must be sorted.
fn min_unused_fd<'a, I: IntoIterator<Item = &'a Fd>>(min: Fd, existings: I) -> Fd {
    let candidates = (min.0..).map(Fd);
    let rejections = existings
        .into_iter()
        .skip_while(|fd| **fd < min)
        .map(Some)
        .chain(std::iter::repeat(None));
    candidates
        .zip(rejections)
        .skip_while(|(candidate, rejection)| Some(candidate) == *rejection)
        .map(|(candidate, _rejection)| candidate)
        .next()
        .unwrap()
}

impl Process {
    /// Creates a new running process.
    pub fn with_parent_and_group(ppid: Pid, pgid: Pid) -> Process {
        Process {
            ppid,
            pgid,
            uid: Uid(1),
            euid: Uid(1),
            gid: Gid(1),
            egid: Gid(1),
            fds: BTreeMap::new(),
            umask: Mode::default(),
            cwd: PathBuf::new(),
            state: ProcessState::Running,
            state_has_changed: false,
            resumption_awaiters: Vec::new(),
            dispositions: HashMap::new(),
            blocked_signals: BTreeSet::new(),
            pending_signals: BTreeSet::new(),
            caught_signals: Vec::new(),
            resource_limits: HashMap::new(),
            selector: Weak::new(),
            last_exec: None,
        }
    }

    /// Creates a new running process as a child of the given parent.
    ///
    /// Some part of the parent process state is copied to the new process.
    pub fn fork_from(ppid: Pid, parent: &Process) -> Process {
        let mut child = Self::with_parent_and_group(ppid, parent.pgid);
        child.uid = parent.uid;
        child.euid = parent.euid;
        child.gid = parent.gid;
        child.egid = parent.egid;
        child.fds = parent.fds.clone();
        child.dispositions.clone_from(&parent.dispositions);
        child.blocked_signals.clone_from(&parent.blocked_signals);
        child.pending_signals = BTreeSet::new();
        child
    }

    /// Returns the process ID of the parent process.
    #[inline(always)]
    #[must_use]
    pub fn ppid(&self) -> Pid {
        self.ppid
    }

    /// Returns the process group ID of this process.
    #[inline(always)]
    #[must_use]
    pub fn pgid(&self) -> Pid {
        self.pgid
    }

    /// Returns the real user ID of this process.
    #[inline(always)]
    #[must_use]
    pub fn uid(&self) -> Uid {
        self.uid
    }

    /// Sets the real user ID of this process.
    #[inline(always)]
    pub fn set_uid(&mut self, uid: Uid) {
        self.uid = uid;
    }

    /// Returns the effective user ID of this process.
    #[inline(always)]
    #[must_use]
    pub fn euid(&self) -> Uid {
        self.euid
    }

    /// Sets the effective user ID of this process.
    #[inline(always)]
    pub fn set_euid(&mut self, euid: Uid) {
        self.euid = euid;
    }

    /// Returns the real group ID of this process.
    #[inline(always)]
    #[must_use]
    pub fn gid(&self) -> Gid {
        self.gid
    }

    /// Sets the real group ID of this process.
    #[inline(always)]
    pub fn set_gid(&mut self, gid: Gid) {
        self.gid = gid;
    }

    /// Returns the effective group ID of this process.
    #[inline(always)]
    #[must_use]
    pub fn egid(&self) -> Gid {
        self.egid
    }

    /// Sets the effective group ID of this process.
    #[inline(always)]
    pub fn set_egid(&mut self, egid: Gid) {
        self.egid = egid;
    }

    /// Returns FDs open in this process.
    #[inline(always)]
    #[must_use]
    pub fn fds(&self) -> &BTreeMap<Fd, FdBody> {
        &self.fds
    }

    /// Returns the body for the given FD.
    #[inline]
    #[must_use]
    pub fn get_fd(&self, fd: Fd) -> Option<&FdBody> {
        self.fds.get(&fd)
    }

    /// Returns the body for the given FD.
    #[inline]
    #[must_use]
    pub fn get_fd_mut(&mut self, fd: Fd) -> Option<&mut FdBody> {
        self.fds.get_mut(&fd)
    }

    /// Assigns the given FD to the body.
    ///
    /// If successful, returns an `Ok` value containing the previous body for
    /// the FD. If the FD is equal to or greater than the current soft limit for
    /// `Resource::NOFILE`, returns `Err(body)`.
    pub fn set_fd(&mut self, fd: Fd, body: FdBody) -> Result<Option<FdBody>, FdBody> {
        let limit = self
            .resource_limits
            .get(&Resource::NOFILE)
            .map(|l| l.soft)
            .unwrap_or(INFINITY);

        #[allow(clippy::unnecessary_cast)]
        if limit == INFINITY || (fd.0 as u64) < limit as u64 {
            Ok(self.fds.insert(fd, body))
        } else {
            Err(body)
        }
    }

    /// Assigns a new FD to the given body.
    ///
    /// The new FD will be the minimum unused FD equal to or greater than
    /// `min_fd`.
    ///
    /// If successful, the new FD is returned in `Ok`.
    /// If no more FD can be opened, returns `Err(body)`.
    pub fn open_fd_ge(&mut self, min_fd: Fd, body: FdBody) -> Result<Fd, FdBody> {
        let fd = min_unused_fd(min_fd, self.fds.keys());
        let old_body = self.set_fd(fd, body)?;
        debug_assert_eq!(old_body, None);
        Ok(fd)
    }

    /// Assigns a new FD to the given body.
    ///
    /// The new FD will be the minimum unused FD, which will be returned as an
    /// `Ok` value.
    ///
    /// If no more FD can be opened, returns `Err(body)`.
    pub fn open_fd(&mut self, body: FdBody) -> Result<Fd, FdBody> {
        self.open_fd_ge(Fd(0), body)
    }

    /// Removes the FD body for the given FD.
    pub fn close_fd(&mut self, fd: Fd) -> Option<FdBody> {
        self.fds.remove(&fd)
    }

    /// Removes all FD bodies in this process.
    pub fn close_fds(&mut self) {
        self.fds.clear();
    }

    /// Returns the working directory path.
    pub fn getcwd(&self) -> &Path {
        &self.cwd
    }

    /// Changes the working directory.
    ///
    /// This function does not check if the directory exists and is accessible.
    pub fn chdir(&mut self, path: PathBuf) {
        self.cwd = path
    }

    /// Registers a waker that will be woken up when this process resumes.
    ///
    /// The given waker will be woken up when this process is resumed by
    /// [`set_state`](Self::set_state) or [`raise_signal`](Self::raise_signal).
    /// A strong reference to the waker must be held by the caller until the
    /// waker is woken up, when the waker is consumed and the `Cell` content is
    /// set to `None`.
    ///
    /// This function does nothing if the process is not stopped.
    pub fn wake_on_resumption(&mut self, waker: Weak<Cell<Option<Waker>>>) {
        if self.state.is_stopped() {
            self.resumption_awaiters.push(waker);
        }
    }

    /// Returns the process state.
    #[inline(always)]
    #[must_use]
    pub fn state(&self) -> ProcessState {
        self.state
    }

    /// Sets the state of this process.
    ///
    /// If the new state is `Exited` or `Signaled`, all file descriptors in this
    /// process are closed.
    ///
    /// This function returns whether the state did change. If true, the
    /// [`state_has_changed`](Self::state_has_changed) flag is set and the
    /// caller must notify the state change by sending `SIGCHLD` to the parent
    /// process.
    #[must_use = "You must send SIGCHLD to the parent if set_state returns true"]
    pub fn set_state(&mut self, state: ProcessState) -> bool {
        let old_state = std::mem::replace(&mut self.state, state);

        if old_state == state {
            false
        } else {
            match state {
                ProcessState::Running => {
                    for weak in self.resumption_awaiters.drain(..) {
                        if let Some(strong) = weak.upgrade() {
                            if let Some(waker) = strong.take() {
                                waker.wake();
                            }
                        }
                    }
                }
                ProcessState::Halted(result) => {
                    if !result.is_stopped() {
                        self.close_fds()
                    }
                }
            }
            self.state_has_changed = true;
            true
        }
    }

    /// Returns true if a new state has been [set](Self::set_state) but not yet
    /// [taken](Self::take_state).
    #[must_use]
    pub fn state_has_changed(&self) -> bool {
        self.state_has_changed
    }

    /// Returns the process state and clears the
    /// [`state_has_changed`](Self::state_has_changed) flag.
    pub fn take_state(&mut self) -> ProcessState {
        self.state_has_changed = false;
        self.state
    }

    /// Returns the currently blocked signals.
    pub fn blocked_signals(&self) -> &BTreeSet<signal::Number> {
        &self.blocked_signals
    }

    /// Returns the currently pending signals.
    ///
    /// A signal is pending when it has been raised but not yet delivered
    /// because it is being blocked.
    pub fn pending_signals(&self) -> &BTreeSet<signal::Number> {
        &self.pending_signals
    }

    /// Updates the signal blocking mask for this process.
    ///
    /// If this function unblocks a signal, any pending signal is delivered.
    ///
    /// If the signal changes the execution state of the process, this function
    /// returns a `SignalResult` with `process_state_changed` being `true`. In
    /// that case, the caller must send a SIGCHLD to the parent process of this
    /// process.
    #[must_use = "send SIGCHLD if process state has changed"]
    pub fn block_signals(&mut self, how: SigmaskOp, signals: &[signal::Number]) -> SignalResult {
        match how {
            SigmaskOp::Set => self.blocked_signals = signals.iter().copied().collect(),
            SigmaskOp::Add => self.blocked_signals.extend(signals),
            SigmaskOp::Remove => {
                for signal in signals {
                    self.blocked_signals.remove(signal);
                }
            }
        }

        let signals_to_deliver = self.pending_signals.difference(&self.blocked_signals);
        let signals_to_deliver = signals_to_deliver.copied().collect::<Vec<signal::Number>>();
        let mut result = SignalResult::default();
        for signal in signals_to_deliver {
            self.pending_signals.remove(&signal);
            result |= self.deliver_signal(signal);
        }
        result
    }

    /// Returns the current disposition for a signal.
    ///
    /// If no disposition is set for the signal, the default disposition is
    /// returned.
    pub fn disposition(&self, number: signal::Number) -> Disposition {
        self.dispositions.get(&number).copied().unwrap_or_default()
    }

    /// Gets and sets the disposition for a signal.
    ///
    /// This function sets the disposition to the given value and returns the
    /// previous disposition.
    pub fn set_disposition(
        &mut self,
        number: signal::Number,
        disposition: Disposition,
    ) -> Disposition {
        let old_disposition = self.dispositions.insert(number, disposition);
        old_disposition.unwrap_or_default()
    }

    /// Delivers a signal to this process.
    ///
    /// The action taken on the delivery depends on the current signal
    /// disposition for the signal.
    ///
    /// If the signal changes the execution state of the process, this function
    /// returns a `SignalResult` with `process_state_changed` being `true`. In
    /// that case, the caller must send a SIGCHLD to the parent process of this
    /// process.
    #[must_use = "send SIGCHLD if process state has changed"]
    fn deliver_signal(&mut self, signal: signal::Number) -> SignalResult {
        let disposition = if signal == signal::SIGKILL || signal == signal::SIGSTOP {
            Disposition::Default
        } else {
            self.disposition(signal)
        };

        match disposition {
            Disposition::Default => {
                let name = signal::Name::try_from_raw_virtual(signal.as_raw())
                    .unwrap_or(signal::Name::Sys);
                let process_state_changed = match SignalEffect::of(name) {
                    SignalEffect::None | SignalEffect::Resume => false,
                    SignalEffect::Terminate { core_dump } => {
                        let result = ProcessResult::Signaled { signal, core_dump };
                        self.set_state(ProcessState::Halted(result))
                    }
                    SignalEffect::Suspend => self.set_state(ProcessState::stopped(signal)),
                };
                SignalResult {
                    delivered: true,
                    caught: false,
                    process_state_changed,
                }
            }
            Disposition::Ignore => SignalResult {
                delivered: true,
                caught: false,
                process_state_changed: false,
            },
            Disposition::Catch => {
                self.caught_signals.push(signal);
                SignalResult {
                    delivered: true,
                    caught: true,
                    process_state_changed: false,
                }
            }
        }
    }

    /// Sends a signal to this process.
    ///
    /// If the signal is being blocked, it will remain pending. Otherwise, it is
    /// immediately delivered.
    ///
    /// If the signal changes the execution state of the process, this function
    /// returns a `SignalResult` with `process_state_changed` being `true`. In
    /// that case, the caller must send a SIGCHLD to the parent process of this
    /// process.
    #[must_use = "send SIGCHLD if process state has changed"]
    pub fn raise_signal(&mut self, signal: signal::Number) -> SignalResult {
        let process_state_changed =
            signal == signal::SIGCONT && self.set_state(ProcessState::Running);

        let mut result = if signal != signal::SIGKILL
            && signal != signal::SIGSTOP
            && self.blocked_signals().contains(&signal)
        {
            self.pending_signals.insert(signal);
            SignalResult::default()
        } else {
            self.deliver_signal(signal)
        };

        result.process_state_changed |= process_state_changed;
        result
    }

    /// Returns the arguments to the last call to
    /// [`execve`](crate::System::execve) on this process.
    #[inline(always)]
    #[must_use]
    pub fn last_exec(&self) -> &Option<(CString, Vec<CString>, Vec<CString>)> {
        &self.last_exec
    }
}

/// Result of operations that may deliver a signal to a process.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub struct SignalResult {
    /// Whether the signal was delivered to the target process.
    pub delivered: bool,

    /// Whether the delivered signal was caught by a signal handler.
    pub caught: bool,

    /// Whether the signal changed the execution status of the target process.
    ///
    /// This flag is true when the process was terminated, suspended, or resumed.
    pub process_state_changed: bool,
}

impl BitOr for SignalResult {
    type Output = Self;
    fn bitor(self, rhs: Self) -> Self::Output {
        Self {
            delivered: self.delivered | rhs.delivered,
            caught: self.caught | rhs.caught,
            process_state_changed: self.process_state_changed | rhs.process_state_changed,
        }
    }
}

impl BitOrAssign for SignalResult {
    fn bitor_assign(&mut self, rhs: Self) {
        *self = *self | rhs;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::system::r#virtual::file_system::{FileBody, Inode, Mode};
    use crate::system::r#virtual::io::OpenFileDescription;
    use enumset::EnumSet;
    use futures_util::task::LocalSpawnExt;
    use futures_util::FutureExt;
    use std::collections::VecDeque;
    use std::future::poll_fn;
    use std::rc::Rc;
    use std::task::Poll;

    #[test]
    fn min_unused_fd_for_various_arguments() {
        assert_eq!(min_unused_fd(Fd(0), []), Fd(0));
        assert_eq!(min_unused_fd(Fd(0), [&Fd(1)]), Fd(0));
        assert_eq!(min_unused_fd(Fd(0), [&Fd(3), &Fd(4)]), Fd(0));
        assert_eq!(min_unused_fd(Fd(0), [&Fd(0)]), Fd(1));
        assert_eq!(min_unused_fd(Fd(0), [&Fd(0), &Fd(2)]), Fd(1));
        assert_eq!(min_unused_fd(Fd(0), [&Fd(0), &Fd(1)]), Fd(2));
        assert_eq!(min_unused_fd(Fd(0), [&Fd(0), &Fd(1), &Fd(4)]), Fd(2));
        assert_eq!(min_unused_fd(Fd(0), [&Fd(0), &Fd(1), &Fd(2)]), Fd(3));

        assert_eq!(min_unused_fd(Fd(1), []), Fd(1));
        assert_eq!(min_unused_fd(Fd(1), [&Fd(1)]), Fd(2));
        assert_eq!(min_unused_fd(Fd(1), [&Fd(2)]), Fd(1));

        assert_eq!(min_unused_fd(Fd(1), [&Fd(1), &Fd(3), &Fd(4)]), Fd(2));
        assert_eq!(min_unused_fd(Fd(2), [&Fd(1), &Fd(3), &Fd(4)]), Fd(2));
        assert_eq!(min_unused_fd(Fd(3), [&Fd(1), &Fd(3), &Fd(4)]), Fd(5));
        assert_eq!(min_unused_fd(Fd(4), [&Fd(1), &Fd(3), &Fd(4)]), Fd(5));
        assert_eq!(min_unused_fd(Fd(5), [&Fd(1), &Fd(3), &Fd(4)]), Fd(5));
        assert_eq!(min_unused_fd(Fd(6), [&Fd(1), &Fd(3), &Fd(4)]), Fd(6));
    }

    fn process_with_pipe() -> (Process, Fd, Fd) {
        let mut process = Process::with_parent_and_group(Pid(10), Pid(11));

        let file = Rc::new(RefCell::new(Inode {
            body: FileBody::Fifo {
                content: VecDeque::new(),
                readers: 1,
                writers: 1,
            },
            permissions: Mode::default(),
        }));
        let reader = OpenFileDescription {
            file: Rc::clone(&file),
            offset: 0,
            is_readable: true,
            is_writable: false,
            is_appending: false,
        };
        let writer = OpenFileDescription {
            file: Rc::clone(&file),
            offset: 0,
            is_readable: false,
            is_writable: true,
            is_appending: false,
        };

        let reader = FdBody {
            open_file_description: Rc::new(RefCell::new(reader)),
            flags: EnumSet::empty(),
        };
        let writer = FdBody {
            open_file_description: Rc::new(RefCell::new(writer)),
            flags: EnumSet::empty(),
        };

        let reader = process.open_fd(reader).unwrap();
        let writer = process.open_fd(writer).unwrap();
        (process, reader, writer)
    }

    #[test]
    fn process_set_state_wakes_on_resumed() {
        let mut process = Process::with_parent_and_group(Pid(1), Pid(2));
        process.state = ProcessState::stopped(signal::SIGTSTP);
        let process = Rc::new(RefCell::new(process));
        let process2 = Rc::clone(&process);
        let waker = Rc::new(Cell::new(None));
        let task = poll_fn(move |cx| {
            let mut process = process2.borrow_mut();
            if process.state() == ProcessState::Running {
                return Poll::Ready(());
            }
            waker.set(Some(cx.waker().clone()));
            process.wake_on_resumption(Rc::downgrade(&waker));
            Poll::Pending
        });

        let mut executor = futures_executor::LocalPool::new();
        let mut handle = executor.spawner().spawn_local_with_handle(task).unwrap();
        executor.run_until_stalled();
        assert_eq!((&mut handle).now_or_never(), None);

        _ = process.borrow_mut().set_state(ProcessState::Running);
        assert!(executor.try_run_one());
        assert_eq!(handle.now_or_never(), Some(()));
    }

    #[test]
    fn process_set_state_closes_all_fds_on_exit() {
        let (mut process, _reader, _writer) = process_with_pipe();
        assert!(process.set_state(ProcessState::exited(3)));
        assert!(process.fds().is_empty(), "{:?}", process.fds());
    }

    #[test]
    fn process_set_state_closes_all_fds_on_signaled() {
        let (mut process, _reader, _writer) = process_with_pipe();
        assert!(
            process.set_state(ProcessState::Halted(ProcessResult::Signaled {
                signal: signal::SIGINT,
                core_dump: false
            }))
        );
        assert!(process.fds().is_empty(), "{:?}", process.fds());
    }

    #[test]
    fn process_default_signal_blocking_mask() {
        let process = Process::with_parent_and_group(Pid(10), Pid(11));
        let initial_set = process.blocked_signals();
        assert!(initial_set.is_empty(), "{initial_set:?}");
    }

    #[test]
    fn process_sigmask_setmask() {
        let mut process = Process::with_parent_and_group(Pid(10), Pid(11));
        let result = process.block_signals(SigmaskOp::Set, &[signal::SIGINT, signal::SIGCHLD]);
        assert_eq!(result, SignalResult::default());

        let result_set = process.blocked_signals();
        assert!(result_set.contains(&signal::SIGINT));
        assert!(result_set.contains(&signal::SIGCHLD));
        assert_eq!(result_set.len(), 2);

        let result = process.block_signals(SigmaskOp::Set, &[signal::SIGINT, signal::SIGQUIT]);
        assert_eq!(result, SignalResult::default());

        let result_set = process.blocked_signals();
        assert!(result_set.contains(&signal::SIGINT));
        assert!(result_set.contains(&signal::SIGQUIT));
        assert_eq!(result_set.len(), 2);
    }

    #[test]
    fn process_sigmask_block() {
        let mut process = Process::with_parent_and_group(Pid(10), Pid(11));
        let result = process.block_signals(SigmaskOp::Add, &[signal::SIGINT, signal::SIGCHLD]);
        assert_eq!(result, SignalResult::default());

        let result_set = process.blocked_signals();
        assert!(result_set.contains(&signal::SIGINT));
        assert!(result_set.contains(&signal::SIGCHLD));
        assert_eq!(result_set.len(), 2);

        let result = process.block_signals(SigmaskOp::Add, &[signal::SIGINT, signal::SIGQUIT]);
        assert_eq!(result, SignalResult::default());

        let result_set = process.blocked_signals();
        assert!(result_set.contains(&signal::SIGINT));
        assert!(result_set.contains(&signal::SIGQUIT));
        assert!(result_set.contains(&signal::SIGCHLD));
        assert_eq!(result_set.len(), 3);
    }

    #[test]
    fn process_sigmask_unblock() {
        let mut process = Process::with_parent_and_group(Pid(10), Pid(11));
        let result = process.block_signals(SigmaskOp::Add, &[signal::SIGINT, signal::SIGCHLD]);
        assert_eq!(result, SignalResult::default());

        let result = process.block_signals(SigmaskOp::Remove, &[signal::SIGINT, signal::SIGQUIT]);
        assert_eq!(result, SignalResult::default());

        let result_set = process.blocked_signals();
        assert!(result_set.contains(&signal::SIGCHLD));
        assert_eq!(result_set.len(), 1);
    }

    #[test]
    fn process_set_disposition() {
        let mut process = Process::with_parent_and_group(Pid(100), Pid(11));
        let old_disposition = process.set_disposition(signal::SIGINT, Disposition::Ignore);
        assert_eq!(old_disposition, Disposition::Default);
        let old_disposition = process.set_disposition(signal::SIGTERM, Disposition::Catch);
        assert_eq!(old_disposition, Disposition::Default);

        let old_disposition = process.set_disposition(signal::SIGINT, Disposition::Default);
        assert_eq!(old_disposition, Disposition::Ignore);
        let old_disposition = process.set_disposition(signal::SIGTERM, Disposition::Ignore);
        assert_eq!(old_disposition, Disposition::Catch);

        let disposition = process.disposition(signal::SIGINT);
        assert_eq!(disposition, Disposition::Default);
        let disposition = process.disposition(signal::SIGTERM);
        assert_eq!(disposition, Disposition::Ignore);
        let disposition = process.disposition(signal::SIGQUIT);
        assert_eq!(disposition, Disposition::Default);
    }

    #[test]
    fn process_raise_signal_default_nop() {
        let mut process = Process::with_parent_and_group(Pid(42), Pid(11));
        let result = process.raise_signal(signal::SIGCHLD);
        assert_eq!(
            result,
            SignalResult {
                delivered: true,
                caught: false,
                process_state_changed: false,
            }
        );
        assert_eq!(process.state(), ProcessState::Running);
    }

    #[test]
    fn process_raise_signal_default_terminating() {
        let mut process = Process::with_parent_and_group(Pid(42), Pid(11));
        let result = process.raise_signal(signal::SIGTERM);
        assert_eq!(
            result,
            SignalResult {
                delivered: true,
                caught: false,
                process_state_changed: true,
            }
        );
        assert_eq!(
            process.state(),
            ProcessState::Halted(ProcessResult::Signaled {
                signal: signal::SIGTERM,
                core_dump: false
            })
        );
        assert_eq!(process.caught_signals, []);
    }

    #[test]
    fn process_raise_signal_default_aborting() {
        let mut process = Process::with_parent_and_group(Pid(42), Pid(11));
        let result = process.raise_signal(signal::SIGABRT);
        assert_eq!(
            result,
            SignalResult {
                delivered: true,
                caught: false,
                process_state_changed: true,
            }
        );
        assert_eq!(
            process.state(),
            ProcessState::Halted(ProcessResult::Signaled {
                signal: signal::SIGABRT,
                core_dump: true
            })
        );
        assert_eq!(process.caught_signals, []);
        // TODO Check if core dump file has been created
    }

    #[test]
    fn process_raise_signal_default_stopping() {
        let mut process = Process::with_parent_and_group(Pid(42), Pid(11));
        let result = process.raise_signal(signal::SIGTSTP);
        assert_eq!(
            result,
            SignalResult {
                delivered: true,
                caught: false,
                process_state_changed: true,
            }
        );
        assert_eq!(process.state(), ProcessState::stopped(signal::SIGTSTP));
        assert_eq!(process.caught_signals, []);
    }

    #[test]
    fn process_raise_signal_default_continuing() {
        let mut process = Process::with_parent_and_group(Pid(42), Pid(11));
        let _ = process.set_state(ProcessState::stopped(signal::SIGTTOU));
        let result = process.raise_signal(signal::SIGCONT);
        assert_eq!(
            result,
            SignalResult {
                delivered: true,
                caught: false,
                process_state_changed: true,
            }
        );
        assert_eq!(process.state(), ProcessState::Running);
        assert_eq!(process.caught_signals, []);
    }

    #[test]
    fn process_raise_signal_ignored() {
        let mut process = Process::with_parent_and_group(Pid(42), Pid(11));
        process.set_disposition(signal::SIGCHLD, Disposition::Ignore);
        let result = process.raise_signal(signal::SIGCHLD);
        assert_eq!(
            result,
            SignalResult {
                delivered: true,
                caught: false,
                process_state_changed: false,
            }
        );
        assert_eq!(process.state(), ProcessState::Running);
        assert_eq!(process.caught_signals, []);
    }

    #[test]
    fn process_raise_signal_ignored_and_blocked_sigcont() {
        let mut process = Process::with_parent_and_group(Pid(42), Pid(11));
        let _ = process.set_state(ProcessState::stopped(signal::SIGTTOU));
        let _ = process.set_disposition(signal::SIGCONT, Disposition::Ignore);
        let _ = process.block_signals(SigmaskOp::Add, &[signal::SIGCONT]);
        let result = process.raise_signal(signal::SIGCONT);
        assert_eq!(
            result,
            SignalResult {
                delivered: false,
                caught: false,
                process_state_changed: true,
            }
        );
        assert_eq!(process.state(), ProcessState::Running);
        assert_eq!(process.caught_signals, []);
        assert!(process.pending_signals.contains(&signal::SIGCONT));
    }

    #[test]
    fn process_raise_signal_caught() {
        let mut process = Process::with_parent_and_group(Pid(42), Pid(11));
        process.set_disposition(signal::SIGCHLD, Disposition::Catch);
        let result = process.raise_signal(signal::SIGCHLD);
        assert_eq!(
            result,
            SignalResult {
                delivered: true,
                caught: true,
                process_state_changed: false,
            }
        );
        assert_eq!(process.state(), ProcessState::Running);
        assert_eq!(process.caught_signals, [signal::SIGCHLD]);
    }

    #[test]
    fn process_raise_signal_blocked() {
        let mut process = Process::with_parent_and_group(Pid(42), Pid(11));
        process.set_disposition(signal::SIGCHLD, Disposition::Catch);
        let result = process.block_signals(SigmaskOp::Add, &[signal::SIGCHLD]);
        assert_eq!(
            result,
            SignalResult {
                delivered: false,
                caught: false,
                process_state_changed: false,
            }
        );

        let result = process.raise_signal(signal::SIGCHLD);
        assert_eq!(
            result,
            SignalResult {
                delivered: false,
                caught: false,
                process_state_changed: false,
            }
        );
        assert_eq!(process.state(), ProcessState::Running);
        assert_eq!(process.caught_signals, []);

        let result = process.block_signals(SigmaskOp::Set, &[]);
        assert_eq!(
            result,
            SignalResult {
                delivered: true,
                caught: true,
                process_state_changed: false,
            }
        );
        assert_eq!(process.state(), ProcessState::Running);
        assert_eq!(process.caught_signals, [signal::SIGCHLD]);
    }
}