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 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
// 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/>.
//! Type definitions for job management.
//!
//! A [`JobList`] manages the state of jobs executed by the shell.
//! Each [`Job`] in the job list remembers the latest state of the child process
//! performing the job's task.
//!
//! The job list stores jobs in an internal array. The index of a job in the
//! array never changes once the [job is added](JobList::add) to the job list.
//! The index of the other jobs does not change when you [remove a
//! job](JobList::remove). Note that the job list may reuse the index of a
//! removed job for another job added later.
//!
//! When the [wait system call](crate::System::wait) returns a new state of a
//! child process, the caller should pass it to [`JobList::update_status`],
//! which modifies the state of the corresponding job. The `state_changed` flag
//! of the job is set when the job is updated and should be
//! [reset when reported](JobRefMut::state_reported).
//!
//! The job list remembers the selection of two special jobs called the "current
//! job" and "previous job." The previous job is chosen automatically, so there
//! is no function to modify it. You can change the current job by
//! [`JobList::set_current_job`].
//!
//! The [`JobList::set_last_async_pid`] function remembers the process ID of the
//! last executed asynchronous command, which will be the value of the `$!`
//! special parameter.
use crate::semantics::ExitStatus;
use crate::signal;
use slab::Slab;
use std::collections::HashMap;
use std::iter::FusedIterator;
use std::ops::Deref;
use thiserror::Error;
/// Process ID
///
/// A process ID is an integer that identifies a process in the system. This
/// type implements the newtype pattern around the raw integral type `pid_t`
/// declared in the [`libc`] crate. The exact representation of this type
/// depends on the target platform.
///
/// Although genuine process IDs are always positive integers, this type allows
/// zero or negative values for the purpose of specifying a group of processes
/// when used as a parameter to the [`kill`] and [`wait`] system calls. The
/// [`setpgid`] system call also uses process ID zero to specify the process
/// ID of the calling process.
///
/// This type may also be used to represent process group IDs, session IDs, etc.
///
/// [`libc`]: nix::libc
/// [`kill`]: crate::system::System::kill
/// [`wait`]: crate::system::System::wait
/// [`setpgid`]: crate::system::System::setpgid
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Pid(pub nix::libc::pid_t);
impl std::fmt::Display for Pid {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::ops::Neg for Pid {
type Output = Self;
fn neg(self) -> Self {
Self(-self.0)
}
}
/// This conversion depends a type declared in the `nix` crate, which is not
/// covered by the semantic versioning policy of this crate.
impl From<Pid> for nix::unistd::Pid {
fn from(pid: Pid) -> Self {
Self::from_raw(pid.0)
}
}
/// This conversion depends a type declared in the `nix` crate, which is not
/// covered by the semantic versioning policy of this crate.
impl From<nix::unistd::Pid> for Pid {
fn from(pid: nix::unistd::Pid) -> Self {
Self(pid.as_raw())
}
}
impl Pid {
/// Sentinel value for the [`kill`] and [`wait`]system calls specifying all
/// processes in the process group of the calling process.
///
/// [`kill`]: crate::system::System::kill
/// [`wait`]: crate::system::System::wait
pub const MY_PROCESS_GROUP: Self = Pid(0);
/// Sentinel value for the [`kill`] and [`wait`] system calls specifying all
/// possible processes.
///
/// [`kill`]: crate::system::System::kill
/// [`wait`]: crate::system::System::wait
pub const ALL: Self = Pid(-1);
}
/// Execution state of a process from which the exit status can be computed
///
/// This type is used to represent the result of a process execution. It is
/// similar to the `WaitStatus` type defined in the `nix` crate, but it is
/// simplified to represent only the states that are relevant to the shell.
///
/// This type only contains the states the process's exit status can be computed
/// from. See also [`ProcessState`], which is a more general type that includes
/// the states that are not directly related to the exit status.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProcessResult {
/// The process has been stopped by a signal.
Stopped(signal::Number),
/// The process has exited.
Exited(ExitStatus),
/// The process has been terminated by a signal.
Signaled {
signal: signal::Number,
core_dump: bool,
},
}
impl ProcessResult {
/// Creates a new `ProcessResult` instance representing an exited process.
#[inline]
#[must_use]
pub fn exited<S: Into<ExitStatus>>(exit_status: S) -> Self {
Self::Exited(exit_status.into())
}
/// Whether the process is stopped
#[must_use]
pub fn is_stopped(&self) -> bool {
matches!(self, ProcessResult::Stopped(_))
}
}
/// Converts `ProcessResult` to `ExitStatus`.
impl From<ProcessResult> for ExitStatus {
fn from(result: ProcessResult) -> Self {
match result {
ProcessResult::Exited(exit_status) => exit_status,
ProcessResult::Stopped(signal) | ProcessResult::Signaled { signal, .. } => {
ExitStatus::from(signal)
}
}
}
}
/// Execution state of a process, either running or halted
///
/// This type is used to represent the current state of a process. It is similar
/// to the `WaitStatus` type defined in the `nix` crate, but it is simplified to
/// represent only the states that are relevant to the shell.
///
/// This type can represent all possible states of a process, including running,
/// stopped, exited, and signaled states. When the process is not running, the
/// state is represented by a [`ProcessResult`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProcessState {
/// The process is running.
Running,
/// The process has exited, stopped, or been terminated by a signal.
Halted(ProcessResult),
}
impl ProcessState {
/// Creates a new `ProcessState` instance representing a stopped process.
#[inline]
#[must_use]
pub fn stopped(signal: signal::Number) -> Self {
Self::Halted(ProcessResult::Stopped(signal))
}
/// Creates a new `ProcessState` instance representing an exited process.
#[inline]
#[must_use]
pub fn exited<S: Into<ExitStatus>>(exit_status: S) -> Self {
Self::Halted(ProcessResult::exited(exit_status))
}
/// Whether the process is not yet terminated
#[must_use]
pub fn is_alive(&self) -> bool {
match self {
ProcessState::Running => true,
ProcessState::Halted(result) => result.is_stopped(),
}
}
/// Whether the process is stopped
#[must_use]
pub fn is_stopped(&self) -> bool {
matches!(self, Self::Halted(result) if result.is_stopped())
}
}
impl From<ProcessResult> for ProcessState {
#[inline]
fn from(result: ProcessResult) -> Self {
Self::Halted(result)
}
}
/// Error value indicating that the process is running.
///
/// This error value may be returned by [`TryFrom<ProcessState>::try_from`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct RunningProcess;
/// Converts `ProcessState` to `ExitStatus`.
///
/// For the `Running` state, the conversion fails with [`RunningProcess`].
impl TryFrom<ProcessState> for ExitStatus {
type Error = RunningProcess;
fn try_from(state: ProcessState) -> Result<Self, RunningProcess> {
match state {
ProcessState::Halted(result) => Ok(result.into()),
ProcessState::Running => Err(RunningProcess),
}
}
}
/// Set of one or more processes executing a pipeline
///
/// In the current implementation, a job contains the process ID of one child
/// process of the shell. Though there may be more processes involved in the
/// execution of the pipeline, the shell takes care of only one process of the
/// job.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct Job {
/// Process ID
///
/// If the job is job-controlled, this is also the process group ID.
pub pid: Pid,
/// Whether the job is job-controlled.
///
/// If the job is job-controlled, the job processes run in their own process
/// group.
pub job_controlled: bool,
/// Current state of the process
pub state: ProcessState,
/// State of the process expected in the next update
///
/// See [`JobRefMut::expect`] and [`JobList::update_status`] for details.
pub expected_state: Option<ProcessState>,
/// Indicator of state change
///
/// This flag is true if the `state` has been changed since the state was
/// last reported to the user.
pub state_changed: bool,
/// Whether this job is a true child of the current shell
///
/// When a subshell is created, the jobs inherited from the parent shell are
/// marked as not owned by the current shell. The shell cannot wait for
/// these jobs to finish.
pub is_owned: bool,
/// String representation of this process
pub name: String,
}
impl Job {
/// Creates a new job instance.
///
/// This function requires a process ID to initialize the new job. The other
/// members of the job are defaulted.
pub fn new(pid: Pid) -> Self {
Job {
pid,
job_controlled: false,
state: ProcessState::Running,
expected_state: None,
state_changed: true,
is_owned: true,
name: String::new(),
}
}
/// Whether the job is suspended
#[must_use]
fn is_suspended(&self) -> bool {
self.state.is_stopped()
}
}
/// Partially mutable reference to [`Job`].
///
/// This struct is a specialized reference type for `Job`. It provides limited
/// mutability over the `Job` instance through its methods. It also allows
/// unlimited immutable access through the `Deref` implementation.
#[derive(Debug, Eq, PartialEq)]
pub struct JobRefMut<'a>(&'a mut Job);
impl JobRefMut<'_> {
/// Sets the `expected_state` of the job.
///
/// This method remembers the argument as the expected state of the job.
/// If the job is [updated] with the same state, the `state_changed` flag
/// is not set then.
///
/// This method may be used to suppress a change report of a job state,
/// especially when the state is reported before it is actually changed.
///
/// [updated]: JobList::update_status
pub fn expect<S>(&mut self, state: S)
where
S: Into<Option<ProcessState>>,
{
self.0.expected_state = state.into();
}
/// Clears the `state_changed` flag of the job.
///
/// Normally, this method should be called when the shell printed a job
/// status report.
pub fn state_reported(&mut self) {
self.0.state_changed = false
}
}
impl Deref for JobRefMut<'_> {
type Target = Job;
fn deref(&self) -> &Job {
self.0
}
}
/// Indexed iterator of jobs.
///
/// Call [`JobList::iter`] to get an instance of `Iter`.
#[derive(Clone, Debug)]
pub struct Iter<'a>(slab::Iter<'a, Job>);
impl<'a> Iterator for Iter<'a> {
type Item = (usize, &'a Job);
#[inline(always)]
fn next(&mut self) -> Option<(usize, &'a Job)> {
self.0.next()
}
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<'a> DoubleEndedIterator for Iter<'a> {
#[inline(always)]
fn next_back(&mut self) -> Option<(usize, &'a Job)> {
self.0.next_back()
}
}
impl ExactSizeIterator for Iter<'_> {
#[inline(always)]
fn len(&self) -> usize {
self.0.len()
}
}
impl FusedIterator for Iter<'_> {}
/// Indexed iterator of partially mutable jobs.
///
/// Call [`JobList::iter_mut`] to get an instance of `IterMut`.
#[derive(Debug)]
pub struct IterMut<'a>(slab::IterMut<'a, Job>);
impl<'a> Iterator for IterMut<'a> {
type Item = (usize, JobRefMut<'a>);
#[inline]
fn next(&mut self) -> Option<(usize, JobRefMut<'a>)> {
self.0.next().map(|(index, job)| (index, JobRefMut(job)))
}
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<'a> DoubleEndedIterator for IterMut<'a> {
fn next_back(&mut self) -> Option<(usize, JobRefMut<'a>)> {
self.0
.next_back()
.map(|(index, job)| (index, JobRefMut(job)))
}
}
impl ExactSizeIterator for IterMut<'_> {
#[inline(always)]
fn len(&self) -> usize {
self.0.len()
}
}
impl FusedIterator for IterMut<'_> {}
/// Collection of jobs.
///
/// See the [module documentation](self) for details.
#[derive(Clone, Debug)]
pub struct JobList {
/// Jobs managed by the shell
jobs: Slab<Job>,
/// Map from process IDs to indices of `jobs`
///
/// This is a shortcut to quickly find jobs by process ID.
pids_to_indices: HashMap<Pid, usize>,
/// Index of the current job. (Only valid when the list is non-empty)
current_job_index: usize,
/// Index of the previous job. (Only valid when the list is non-empty)
previous_job_index: usize,
/// Process ID of the most recently executed asynchronous command.
last_async_pid: Pid,
}
impl Default for JobList {
fn default() -> Self {
JobList {
jobs: Slab::new(),
pids_to_indices: HashMap::new(),
current_job_index: usize::default(),
previous_job_index: usize::default(),
last_async_pid: Pid(0),
}
}
}
impl JobList {
/// Creates an empty job list.
#[inline]
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Returns the job at the specified index.
///
/// The result is `None` if there is no job for the index.
#[inline]
pub fn get(&self, index: usize) -> Option<&Job> {
self.jobs.get(index)
}
/// Returns a partially mutable reference to the job at the specified index.
///
/// The result is `None` if there is no job for the index.
#[inline]
pub fn get_mut(&mut self, index: usize) -> Option<JobRefMut> {
self.jobs.get_mut(index).map(JobRefMut)
}
/// Returns the number of jobs in this job list.
#[inline]
pub fn len(&self) -> usize {
self.jobs.len()
}
/// Returns true if this job list contains no jobs.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns an indexed iterator of jobs.
///
/// The item type of the returned iterator is `(usize, &Job)`.
/// Jobs are iterated in the order of indices.
#[inline]
pub fn iter(&self) -> Iter {
Iter(self.jobs.iter())
}
/// Returns an indexed iterator of partially mutable jobs.
///
/// The item type of the returned iterator is `(usize, JobRefMut)`.
/// Note that the iterator does not yield raw mutable references to jobs.
/// [`JobRefMut`] allows mutating only part of jobs.
///
/// Jobs are iterated in the order of indices.
#[inline]
pub fn iter_mut(&mut self) -> IterMut {
IterMut(self.jobs.iter_mut())
}
/// Finds a job by the process ID.
///
/// This function returns the index of the job whose process ID is `pid`.
/// The result is `None` if no such job is found.
///
/// A `JobList` maintains an internal hash map to quickly find jobs by
/// process ID.
pub fn find_by_pid(&self, pid: Pid) -> Option<usize> {
self.pids_to_indices.get(&pid).copied()
}
}
impl<'a> IntoIterator for &'a JobList {
type Item = (usize, &'a Job);
type IntoIter = Iter<'a>;
#[inline(always)]
fn into_iter(self) -> Iter<'a> {
self.iter()
}
}
impl<'a> IntoIterator for &'a mut JobList {
type Item = (usize, JobRefMut<'a>);
type IntoIter = IterMut<'a>;
#[inline(always)]
fn into_iter(self) -> IterMut<'a> {
self.iter_mut()
}
}
/// Supports indexing operation on `JobList`.
impl std::ops::Index<usize> for JobList {
type Output = Job;
/// Returns a reference to the specified job.
///
/// This function will panic if the job does not exist.
fn index(&self, index: usize) -> &Job {
&self.jobs[index]
}
}
/// Iterator that conditionally removes jobs from a job list.
///
/// Call [`JobList::extract_if`] to get an instance of `ExtractIf`.
#[derive(Debug)]
pub struct ExtractIf<'a, F>
where
F: FnMut(usize, JobRefMut) -> bool,
{
list: &'a mut JobList,
should_remove: F,
next_index: usize,
len: usize,
}
impl<F> Iterator for ExtractIf<'_, F>
where
F: FnMut(usize, JobRefMut) -> bool,
{
type Item = (usize, Job);
fn next(&mut self) -> Option<(usize, Job)> {
while self.len > 0 {
let index = self.next_index;
self.next_index += 1;
if let Some(job) = self.list.get_mut(index) {
self.len -= 1;
if (self.should_remove)(index, job) {
let job = self.list.remove(index).unwrap();
return Some((index, job));
}
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.len))
}
}
impl<F> FusedIterator for ExtractIf<'_, F> where F: FnMut(usize, JobRefMut) -> bool {}
impl JobList {
/// Adds a job to this job list.
///
/// This function returns a unique index assigned to the job.
///
/// If there already is a job that has the same process ID as that of the
/// new job, the existing job is silently removed.
///
/// If the new job is suspended and the [current job](Self::current_job) is
/// not, the new job becomes the current job. If the new job and the current
/// job are suspended but the [previous job](Self::previous_job) is not, the
/// new job becomes the previous job.
pub fn add(&mut self, job: Job) -> usize {
let new_job_is_suspended = job.is_suspended();
let ex_current_job_is_suspended =
self.current_job().map(|index| self[index].is_suspended());
let ex_previous_job_is_suspended =
self.previous_job().map(|index| self[index].is_suspended());
// Add the job to `self.jobs` and `self.pids_to_indices`.
use std::collections::hash_map::Entry::*;
let index = match self.pids_to_indices.entry(job.pid) {
Vacant(entry) => {
let index = self.jobs.insert(job);
entry.insert(index);
index
}
Occupied(entry) => {
let index = *entry.get();
self.jobs[index] = job;
index
}
};
debug_assert_eq!(self.jobs.len(), self.pids_to_indices.len());
// Reselect the current and previous job.
match ex_current_job_is_suspended {
None => self.current_job_index = index,
Some(false) if new_job_is_suspended => self.set_current_job(index).unwrap(),
Some(_) => match ex_previous_job_is_suspended {
None => self.previous_job_index = index,
Some(false) if new_job_is_suspended => self.previous_job_index = index,
Some(_) => (),
},
}
index
}
/// Removes a job from this job list.
///
/// This function returns the job removed from the job list.
/// The result is `None` if there is no job for the index.
///
/// If the removed job is the [current job](Self::current_job), the
/// [previous job](Self::previous_job) becomes the current job and another
/// job is selected for the new previous job, if any.
/// If the removed job is the previous job, another job is selected for the
/// new previous job, if any.
pub fn remove(&mut self, index: usize) -> Option<Job> {
let job = self.jobs.try_remove(index);
if let Some(job) = &job {
// Keep `pids_to_indices` in sync
self.pids_to_indices.remove(&job.pid);
if self.jobs.is_empty() {
// Clearing an already empty slab may seem redundant, but this
// operation purges the slab's internal cache of unused indices,
// so that jobs added later have indices starting from 0.
self.jobs.clear();
}
// Reselect the current and previous job
let previous_job_becomes_current_job = index == self.current_job_index;
if previous_job_becomes_current_job {
self.current_job_index = self.previous_job_index;
}
if previous_job_becomes_current_job || index == self.previous_job_index {
self.previous_job_index = self
.any_suspended_job_but_current()
.unwrap_or_else(|| self.any_job_but_current().unwrap_or_default());
}
}
debug_assert_eq!(self.jobs.len(), self.pids_to_indices.len());
job
}
/// Removes jobs that satisfy the predicate.
///
/// This function uses the `should_remove` function to decide whether to
/// remove jobs. If it returns true, the job is removed and yielded from the
/// iterator. Otherwise, the job remains in the list.
///
/// You can reset the `state_changed` flag of a job
/// ([`JobRefMut::state_reported`]) regardless of whether you choose to
/// remove it or not.
///
/// This function is a simplified version of [`JobList::extract_if`] that
/// does not return removed jobs.
pub fn remove_if<F>(&mut self, should_remove: F)
where
F: FnMut(usize, JobRefMut) -> bool,
{
self.extract_if(should_remove).for_each(drop)
}
/// Returns an iterator that conditionally removes jobs.
///
/// The iterator uses the `should_remove` function to decide whether to
/// remove jobs. If it returns true, the job is removed and yielded from the
/// iterator. Otherwise, the job remains in the list.
///
/// You can reset the `state_changed` flag of a job
/// ([`JobRefMut::state_reported`]) regardless of whether you choose to
/// remove it or not.
///
/// If the returned iterator is dropped before iterating all jobs, the
/// remaining jobs are retained in the list.
///
/// If you don't need to take the ownership of removed jobs, consider using
/// [`JobList::remove_if`] instead.
pub fn extract_if<F>(&mut self, should_remove: F) -> ExtractIf<'_, F>
where
F: FnMut(usize, JobRefMut) -> bool,
{
let len = self.len();
ExtractIf {
list: self,
should_remove,
next_index: 0,
len,
}
}
}
impl JobList {
/// Updates the state of a job.
///
/// The result of a [`wait`](crate::System::wait) call should be passed to
/// this function. It looks up the job for the given process ID, updates the
/// state of the job to the given `state`, and sets the `state_changed` flag
/// in the job. As an exception, if `state` is equal to the `expected_state`
/// of the job, the `state_changed` flag is not set. The `expected_state` is
/// cleared in any case. (See also [`JobRefMut::expect`] for the usage of
/// `expected_state`.)
///
/// Returns the index of the job updated. If there is no job for the given
/// process ID, the result is `None`.
///
/// When a job is suspended (i.e., `state` is `Stopped`), the job becomes
/// the [current job](Self::current_job) and the old current job becomes the
/// [previous job](Self::previous_job). When a suspended job gets a state
/// update:
///
/// - If the updated job is the current job and the previous job is
/// suspended, the previous job becomes the current job and the new
/// previous job is chosen from other suspended jobs. If there is no
/// suspended jobs, the new previous jobs is the old current job.
/// - If the updated job is the previous job and there is a suspended job
/// other than the current job, it becomes the previous job.
pub fn update_status(&mut self, pid: Pid, state: ProcessState) -> Option<usize> {
let index = self.find_by_pid(pid)?;
// Update the job state.
let job = &mut self.jobs[index];
let was_suspended = job.is_suspended();
job.state = state;
job.state_changed |= job.expected_state != Some(state);
job.expected_state = None;
// Reselect the current and previous job.
if !was_suspended && job.is_suspended() {
if index != self.current_job_index {
self.previous_job_index = std::mem::replace(&mut self.current_job_index, index);
}
} else if was_suspended && !job.is_suspended() {
if let Some(prev_index) = self.previous_job() {
let previous_job_becomes_current_job =
index == self.current_job_index && self[prev_index].is_suspended();
if previous_job_becomes_current_job {
self.current_job_index = prev_index;
}
if previous_job_becomes_current_job || index == prev_index {
self.previous_job_index = self.any_suspended_job_but_current().unwrap_or(index);
}
}
}
Some(index)
}
/// Disowns all jobs.
///
/// This function sets the `is_owned` flag of all jobs to `false`.
pub fn disown_all(&mut self) {
for (_, job) in &mut self.jobs {
job.is_owned = false;
}
}
}
/// Error type for [`JobList::set_current_job`].
#[derive(Clone, Copy, Debug, Eq, Error, Hash, PartialEq)]
pub enum SetCurrentJobError {
/// The specified index does not refer to any job.
#[error("no such job")]
NoSuchJob,
/// The specified job is not a suspended job and there is another suspended
/// job.
#[error("the current job must be selected from suspended jobs")]
NotSuspended,
}
impl JobList {
/// Selects the current job.
///
/// This function changes the current job to the job specified by the index
/// and the previous job to the old current job.
///
/// If there is one or more suspended jobs, the current job must be selected
/// from them. If the index does not refer to a suspended job, the
/// `NotSuspended` error is returned.
///
/// If the index does not refer to any job, the `NoSuchJob` error is
/// returned.
pub fn set_current_job(&mut self, index: usize) -> Result<(), SetCurrentJobError> {
let job = self.get(index).ok_or(SetCurrentJobError::NoSuchJob)?;
if !job.is_suspended() && self.iter().any(|(_, job)| job.is_suspended()) {
return Err(SetCurrentJobError::NotSuspended);
}
if index != self.current_job_index {
self.previous_job_index = std::mem::replace(&mut self.current_job_index, index);
}
Ok(())
}
/// Returns the index of the current job.
///
/// If the job list contains at least one job, there is a current job. This
/// function returns its index. If the job list is empty, the result is
/// `None`.
///
/// If there is any suspended jobs, the current job must be a suspended job.
/// Running or terminated jobs can be the current job if there is no
/// suspended job. You can [change the current job](Self::set_current_job)
/// as long as the above rules are met.
///
/// See also [`previous_job`](Self::previous_job).
pub fn current_job(&self) -> Option<usize> {
if self.jobs.contains(self.current_job_index) {
Some(self.current_job_index)
} else {
None
}
}
/// Returns the index of the previous job.
///
/// If the job list contains two or more jobs, there is a previous job. This
/// function returns its index. If the job list has zero or one job, the
/// result is `None`.
///
/// The previous job is never the same job as the [current
/// job](Self::current_job).
///
/// If there are two or more suspended jobs, the previous job must be a
/// suspended job. Running or terminated jobs can be the previous job if
/// there is zero or one suspended job.
///
/// You cannot directly select the previous job. When the current job is
/// selected, the old current job becomes the previous job.
pub fn previous_job(&self) -> Option<usize> {
if self.previous_job_index != self.current_job_index
&& self.jobs.contains(self.previous_job_index)
{
Some(self.previous_job_index)
} else {
None
}
}
/// Finds a suspended job other than the current job.
fn any_suspended_job_but_current(&self) -> Option<usize> {
self.iter()
.filter(|&(index, job)| index != self.current_job_index && job.is_suspended())
.map(|(index, _)| index)
.next()
}
/// Finds a job other than the current job.
fn any_job_but_current(&self) -> Option<usize> {
self.iter()
.filter(|&(index, _)| index != self.current_job_index)
.map(|(index, _)| index)
.next()
}
}
impl JobList {
/// Returns the process ID of the most recently executed asynchronous
/// command.
///
/// This function returns the value that has been set by
/// [`set_last_async_pid`](Self::set_last_async_pid), or 0 if no value has
/// been set.
pub fn last_async_pid(&self) -> Pid {
self.last_async_pid
}
/// Sets the process ID of the most recently executed asynchronous command.
///
/// This function affects the result of
/// [`last_async_pid`](Self::last_async_pid).
pub fn set_last_async_pid(&mut self, pid: Pid) {
self.last_async_pid = pid;
}
}
pub mod fmt;
pub mod id;
#[cfg(test)]
mod tests {
use super::*;
use crate::system::r#virtual::{SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU};
#[test]
fn job_list_find_by_pid() {
let mut list = JobList::default();
assert_eq!(list.find_by_pid(Pid(10)), None);
let i10 = list.add(Job::new(Pid(10)));
let i20 = list.add(Job::new(Pid(20)));
let i30 = list.add(Job::new(Pid(30)));
assert_eq!(list.find_by_pid(Pid(10)), Some(i10));
assert_eq!(list.find_by_pid(Pid(20)), Some(i20));
assert_eq!(list.find_by_pid(Pid(30)), Some(i30));
assert_eq!(list.find_by_pid(Pid(40)), None);
list.remove(i10);
assert_eq!(list.find_by_pid(Pid(10)), None);
}
#[test]
fn job_list_add_and_remove() {
// This test case depends on how Slab reuses the index of removed items.
let mut list = JobList::default();
assert_eq!(list.add(Job::new(Pid(10))), 0);
assert_eq!(list.add(Job::new(Pid(11))), 1);
assert_eq!(list.add(Job::new(Pid(12))), 2);
assert_eq!(list.remove(0).unwrap().pid, Pid(10));
assert_eq!(list.remove(1).unwrap().pid, Pid(11));
// Indices are reused in the reverse order of removals.
assert_eq!(list.add(Job::new(Pid(13))), 1);
assert_eq!(list.add(Job::new(Pid(14))), 0);
assert_eq!(list.remove(0).unwrap().pid, Pid(14));
assert_eq!(list.remove(1).unwrap().pid, Pid(13));
assert_eq!(list.remove(2).unwrap().pid, Pid(12));
// Once the job list is empty, indices start from 0 again.
assert_eq!(list.add(Job::new(Pid(13))), 0);
assert_eq!(list.add(Job::new(Pid(14))), 1);
}
#[test]
fn job_list_add_same_pid() {
let mut list = JobList::default();
let mut job = Job::new(Pid(10));
job.name = "first job".to_string();
let i_first = list.add(job);
let mut job = Job::new(Pid(10));
job.name = "second job".to_string();
let i_second = list.add(job);
let job = &list[i_second];
assert_eq!(job.pid, Pid(10));
assert_eq!(job.name, "second job");
assert_ne!(
list.get(i_first).map(|job| job.name.as_str()),
Some("first job")
);
}
#[test]
fn job_list_extract_if() {
let mut list = JobList::default();
let i21 = list.add(Job::new(Pid(21)));
let i22 = list.add(Job::new(Pid(22)));
let i23 = list.add(Job::new(Pid(23)));
let i24 = list.add(Job::new(Pid(24)));
let i25 = list.add(Job::new(Pid(25)));
let i26 = list.add(Job::new(Pid(26)));
list.remove(i23).unwrap();
let mut i = list.extract_if(|index, mut job| {
assert_ne!(index, i23);
if index % 2 == 0 {
job.state_reported();
}
index == 0 || job.pid == Pid(26)
});
let mut expected_job_21 = Job::new(Pid(21));
expected_job_21.state_changed = false;
assert_eq!(i.next(), Some((i21, expected_job_21)));
assert_eq!(i.next(), Some((i26, Job::new(Pid(26)))));
assert_eq!(i.next(), None);
assert_eq!(i.next(), None); // ExtractIf is fused.
let indices: Vec<usize> = list.iter().map(|(index, _)| index).collect();
assert_eq!(indices, [i22, i24, i25]);
assert!(list[i22].state_changed);
assert!(list[i24].state_changed);
assert!(!list[i25].state_changed);
}
#[test]
#[allow(clippy::bool_assert_comparison)]
fn updating_job_status_without_expected_state() {
let mut list = JobList::default();
let state = ProcessState::exited(15);
assert_eq!(list.update_status(Pid(20), state), None);
let i10 = list.add(Job::new(Pid(10)));
let i20 = list.add(Job::new(Pid(20)));
let i30 = list.add(Job::new(Pid(30)));
assert_eq!(list[i20].state, ProcessState::Running);
list.get_mut(i20).unwrap().state_reported();
assert_eq!(list[i20].state_changed, false);
assert_eq!(list.update_status(Pid(20), state), Some(i20));
assert_eq!(list[i20].state, ProcessState::exited(15));
assert_eq!(list[i20].state_changed, true);
assert_eq!(list[i10].state, ProcessState::Running);
assert_eq!(list[i30].state, ProcessState::Running);
}
#[test]
#[allow(clippy::bool_assert_comparison)]
fn updating_job_status_with_matching_expected_state() {
let mut list = JobList::default();
let pid = Pid(20);
let mut job = Job::new(pid);
job.expected_state = Some(ProcessState::Running);
job.state_changed = false;
let i20 = list.add(job);
assert_eq!(list.update_status(pid, ProcessState::Running), Some(i20));
let job = &list[i20];
assert_eq!(job.state, ProcessState::Running);
assert_eq!(job.expected_state, None);
assert_eq!(job.state_changed, false);
}
#[test]
#[allow(clippy::bool_assert_comparison)]
fn updating_job_status_with_unmatched_expected_state() {
let mut list = JobList::default();
let pid = Pid(20);
let mut job = Job::new(pid);
job.expected_state = Some(ProcessState::Running);
job.state_changed = false;
let i20 = list.add(job);
let result = list.update_status(pid, ProcessState::exited(0));
assert_eq!(result, Some(i20));
let job = &list[i20];
assert_eq!(job.state, ProcessState::exited(0));
assert_eq!(job.expected_state, None);
assert_eq!(job.state_changed, true);
}
#[test]
#[allow(clippy::bool_assert_comparison)]
fn disowning_jobs() {
let mut list = JobList::default();
let i10 = list.add(Job::new(Pid(10)));
let i20 = list.add(Job::new(Pid(20)));
let i30 = list.add(Job::new(Pid(30)));
list.disown_all();
assert_eq!(list[i10].is_owned, false);
assert_eq!(list[i20].is_owned, false);
assert_eq!(list[i30].is_owned, false);
}
#[test]
fn no_current_and_previous_job_in_empty_job_list() {
let list = JobList::default();
assert_eq!(list.current_job(), None);
assert_eq!(list.previous_job(), None);
}
#[test]
fn current_and_previous_job_in_job_list_with_one_job() {
let mut list = JobList::default();
let i10 = list.add(Job::new(Pid(10)));
assert_eq!(list.current_job(), Some(i10));
assert_eq!(list.previous_job(), None);
}
#[test]
fn current_and_previous_job_in_job_list_with_two_job() {
// If one job is suspended and the other is not, the current job is the
// suspended one.
let mut list = JobList::default();
let mut suspended = Job::new(Pid(10));
suspended.state = ProcessState::stopped(SIGSTOP);
let running = Job::new(Pid(20));
let i10 = list.add(suspended.clone());
let i20 = list.add(running.clone());
assert_eq!(list.current_job(), Some(i10));
assert_eq!(list.previous_job(), Some(i20));
// The order of adding jobs does not matter in this case.
list = JobList::default();
let i20 = list.add(running);
let i10 = list.add(suspended);
assert_eq!(list.current_job(), Some(i10));
assert_eq!(list.previous_job(), Some(i20));
}
#[test]
fn adding_suspended_job_with_running_current_and_previous_job() {
let mut list = JobList::default();
let running_1 = Job::new(Pid(11));
let running_2 = Job::new(Pid(12));
list.add(running_1);
list.add(running_2);
let ex_current_job_index = list.current_job().unwrap();
let ex_previous_job_index = list.previous_job().unwrap();
assert_ne!(ex_current_job_index, ex_previous_job_index);
let mut suspended = Job::new(Pid(20));
suspended.state = ProcessState::stopped(SIGSTOP);
let i20 = list.add(suspended);
let now_current_job_index = list.current_job().unwrap();
let now_previous_job_index = list.previous_job().unwrap();
assert_eq!(now_current_job_index, i20);
assert_eq!(now_previous_job_index, ex_current_job_index);
}
#[test]
fn adding_suspended_job_with_suspended_current_and_running_previous_job() {
let mut list = JobList::default();
let running = Job::new(Pid(18));
let i18 = list.add(running);
let mut suspended_1 = Job::new(Pid(19));
suspended_1.state = ProcessState::stopped(SIGSTOP);
let i19 = list.add(suspended_1);
let ex_current_job_index = list.current_job().unwrap();
let ex_previous_job_index = list.previous_job().unwrap();
assert_eq!(ex_current_job_index, i19);
assert_eq!(ex_previous_job_index, i18);
let mut suspended_2 = Job::new(Pid(20));
suspended_2.state = ProcessState::stopped(SIGSTOP);
let i20 = list.add(suspended_2);
let now_current_job_index = list.current_job().unwrap();
let now_previous_job_index = list.previous_job().unwrap();
assert_eq!(now_current_job_index, ex_current_job_index);
assert_eq!(now_previous_job_index, i20);
}
#[test]
fn removing_current_job() {
let mut list = JobList::default();
let running = Job::new(Pid(10));
let i10 = list.add(running);
let mut suspended_1 = Job::new(Pid(11));
let mut suspended_2 = Job::new(Pid(12));
let mut suspended_3 = Job::new(Pid(13));
suspended_1.state = ProcessState::stopped(SIGSTOP);
suspended_2.state = ProcessState::stopped(SIGSTOP);
suspended_3.state = ProcessState::stopped(SIGSTOP);
list.add(suspended_1);
list.add(suspended_2);
list.add(suspended_3);
let current_job_index_1 = list.current_job().unwrap();
let previous_job_index_1 = list.previous_job().unwrap();
assert_ne!(current_job_index_1, i10);
assert_ne!(previous_job_index_1, i10);
list.remove(current_job_index_1);
let current_job_index_2 = list.current_job().unwrap();
let previous_job_index_2 = list.previous_job().unwrap();
assert_eq!(current_job_index_2, previous_job_index_1);
assert_ne!(previous_job_index_2, current_job_index_2);
// The new previous job is chosen from suspended jobs other than the current job.
let previous_job_2 = &list[previous_job_index_2];
assert!(
previous_job_2.is_suspended(),
"previous_job_2 = {previous_job_2:?}"
);
list.remove(current_job_index_2);
let current_job_index_3 = list.current_job().unwrap();
let previous_job_index_3 = list.previous_job().unwrap();
assert_eq!(current_job_index_3, previous_job_index_2);
// There is no other suspended job, so the new previous job is a running job.
assert_eq!(previous_job_index_3, i10);
list.remove(current_job_index_3);
let current_job_index_4 = list.current_job().unwrap();
assert_eq!(current_job_index_4, i10);
// No more job to be selected for the previous job.
assert_eq!(list.previous_job(), None);
}
#[test]
fn removing_previous_job_with_suspended_job() {
let mut list = JobList::default();
let running = Job::new(Pid(10));
let i10 = list.add(running);
let mut suspended_1 = Job::new(Pid(11));
let mut suspended_2 = Job::new(Pid(12));
let mut suspended_3 = Job::new(Pid(13));
suspended_1.state = ProcessState::stopped(SIGSTOP);
suspended_2.state = ProcessState::stopped(SIGSTOP);
suspended_3.state = ProcessState::stopped(SIGSTOP);
list.add(suspended_1);
list.add(suspended_2);
list.add(suspended_3);
let ex_current_job_index = list.current_job().unwrap();
let ex_previous_job_index = list.previous_job().unwrap();
assert_ne!(ex_current_job_index, i10);
assert_ne!(ex_previous_job_index, i10);
list.remove(ex_previous_job_index);
let now_current_job_index = list.current_job().unwrap();
let now_previous_job_index = list.previous_job().unwrap();
assert_eq!(now_current_job_index, ex_current_job_index);
assert_ne!(now_previous_job_index, now_current_job_index);
// The new previous job is chosen from suspended jobs other than the current job.
let now_previous_job = &list[now_previous_job_index];
assert!(
now_previous_job.is_suspended(),
"now_previous_job = {now_previous_job:?}"
);
}
#[test]
fn removing_previous_job_with_running_job() {
let mut list = JobList::default();
let running = Job::new(Pid(10));
let i10 = list.add(running);
let mut suspended_1 = Job::new(Pid(11));
let mut suspended_2 = Job::new(Pid(12));
suspended_1.state = ProcessState::stopped(SIGSTOP);
suspended_2.state = ProcessState::stopped(SIGSTOP);
list.add(suspended_1);
list.add(suspended_2);
let ex_current_job_index = list.current_job().unwrap();
let ex_previous_job_index = list.previous_job().unwrap();
assert_ne!(ex_current_job_index, i10);
assert_ne!(ex_previous_job_index, i10);
list.remove(ex_previous_job_index);
let now_current_job_index = list.current_job().unwrap();
let now_previous_job_index = list.previous_job().unwrap();
assert_eq!(now_current_job_index, ex_current_job_index);
// When there is no suspended job other than the current job,
// then the new previous job can be any job other than the current.
assert_eq!(now_previous_job_index, i10);
}
#[test]
fn set_current_job_with_running_jobs_only() {
let mut list = JobList::default();
let i21 = list.add(Job::new(Pid(21)));
let i22 = list.add(Job::new(Pid(22)));
assert_eq!(list.set_current_job(i21), Ok(()));
assert_eq!(list.current_job(), Some(i21));
assert_eq!(list.set_current_job(i22), Ok(()));
assert_eq!(list.current_job(), Some(i22));
}
#[test]
fn set_current_job_to_suspended_job() {
let mut list = JobList::default();
list.add(Job::new(Pid(20)));
let mut suspended_1 = Job::new(Pid(21));
let mut suspended_2 = Job::new(Pid(22));
suspended_1.state = ProcessState::stopped(SIGSTOP);
suspended_2.state = ProcessState::stopped(SIGSTOP);
let i21 = list.add(suspended_1);
let i22 = list.add(suspended_2);
assert_eq!(list.set_current_job(i21), Ok(()));
assert_eq!(list.current_job(), Some(i21));
assert_eq!(list.set_current_job(i22), Ok(()));
assert_eq!(list.current_job(), Some(i22));
}
#[test]
fn set_current_job_no_such_job() {
let mut list = JobList::default();
assert_eq!(list.set_current_job(0), Err(SetCurrentJobError::NoSuchJob));
assert_eq!(list.set_current_job(1), Err(SetCurrentJobError::NoSuchJob));
assert_eq!(list.set_current_job(2), Err(SetCurrentJobError::NoSuchJob));
}
#[test]
fn set_current_job_not_suspended() {
let mut list = JobList::default();
let mut suspended = Job::new(Pid(10));
suspended.state = ProcessState::stopped(SIGTSTP);
let running = Job::new(Pid(20));
let i10 = list.add(suspended);
let i20 = list.add(running);
assert_eq!(
list.set_current_job(i20),
Err(SetCurrentJobError::NotSuspended)
);
assert_eq!(list.current_job(), Some(i10));
}
#[test]
fn set_current_job_no_change() {
let mut list = JobList::default();
list.add(Job::new(Pid(5)));
list.add(Job::new(Pid(6)));
let old_current_job_index = list.current_job().unwrap();
let old_previous_job_index = list.previous_job().unwrap();
list.set_current_job(old_current_job_index).unwrap();
let new_current_job_index = list.current_job().unwrap();
let new_previous_job_index = list.previous_job().unwrap();
assert_eq!(new_current_job_index, old_current_job_index);
assert_eq!(new_previous_job_index, old_previous_job_index);
}
#[test]
fn resuming_current_job_without_other_suspended_jobs() {
let mut list = JobList::default();
let mut suspended = Job::new(Pid(10));
suspended.state = ProcessState::stopped(SIGTSTP);
let running = Job::new(Pid(20));
let i10 = list.add(suspended);
let i20 = list.add(running);
list.update_status(Pid(10), ProcessState::Running);
assert_eq!(list.current_job(), Some(i10));
assert_eq!(list.previous_job(), Some(i20));
}
#[test]
fn resuming_current_job_with_another_suspended_job() {
let mut list = JobList::default();
let mut suspended_1 = Job::new(Pid(10));
let mut suspended_2 = Job::new(Pid(20));
suspended_1.state = ProcessState::stopped(SIGTSTP);
suspended_2.state = ProcessState::stopped(SIGTSTP);
let i10 = list.add(suspended_1);
let i20 = list.add(suspended_2);
list.set_current_job(i10).unwrap();
list.update_status(Pid(10), ProcessState::Running);
// The current job must be a suspended job, if any.
assert_eq!(list.current_job(), Some(i20));
assert_eq!(list.previous_job(), Some(i10));
}
#[test]
fn resuming_current_job_with_other_suspended_jobs() {
let mut list = JobList::default();
let mut suspended_1 = Job::new(Pid(10));
let mut suspended_2 = Job::new(Pid(20));
let mut suspended_3 = Job::new(Pid(30));
suspended_1.state = ProcessState::stopped(SIGTSTP);
suspended_2.state = ProcessState::stopped(SIGTSTP);
suspended_3.state = ProcessState::stopped(SIGTSTP);
list.add(suspended_1);
list.add(suspended_2);
list.add(suspended_3);
let ex_current_job_pid = list[list.current_job().unwrap()].pid;
let ex_previous_job_index = list.previous_job().unwrap();
list.update_status(ex_current_job_pid, ProcessState::Running);
let now_current_job_index = list.current_job().unwrap();
let now_previous_job_index = list.previous_job().unwrap();
assert_eq!(now_current_job_index, ex_previous_job_index);
assert_ne!(now_previous_job_index, now_current_job_index);
// The new previous job is chosen from suspended jobs other than the current job.
let now_previous_job = &list[now_previous_job_index];
assert!(
now_previous_job.is_suspended(),
"now_previous_job = {now_previous_job:?}"
);
}
#[test]
fn resuming_previous_job() {
let mut list = JobList::default();
let mut suspended_1 = Job::new(Pid(10));
let mut suspended_2 = Job::new(Pid(20));
let mut suspended_3 = Job::new(Pid(30));
suspended_1.state = ProcessState::stopped(SIGTSTP);
suspended_2.state = ProcessState::stopped(SIGTSTP);
suspended_3.state = ProcessState::stopped(SIGTSTP);
list.add(suspended_1);
list.add(suspended_2);
list.add(suspended_3);
let ex_current_job_index = list.current_job().unwrap();
let ex_previous_job_pid = list[list.previous_job().unwrap()].pid;
list.update_status(ex_previous_job_pid, ProcessState::Running);
let now_current_job_index = list.current_job().unwrap();
let now_previous_job_index = list.previous_job().unwrap();
assert_eq!(now_current_job_index, ex_current_job_index);
assert_ne!(now_previous_job_index, now_current_job_index);
// The new previous job is chosen from suspended jobs other than the current job.
let now_previous_job = &list[now_previous_job_index];
assert!(
now_previous_job.is_suspended(),
"now_previous_job = {now_previous_job:?}"
);
}
#[test]
fn resuming_other_job() {
let mut list = JobList::default();
let mut suspended_1 = Job::new(Pid(10));
let mut suspended_2 = Job::new(Pid(20));
let mut suspended_3 = Job::new(Pid(30));
suspended_1.state = ProcessState::stopped(SIGTSTP);
suspended_2.state = ProcessState::stopped(SIGTSTP);
suspended_3.state = ProcessState::stopped(SIGTSTP);
let i10 = list.add(suspended_1);
let i20 = list.add(suspended_2);
let _i30 = list.add(suspended_3);
list.set_current_job(i20).unwrap();
list.set_current_job(i10).unwrap();
list.update_status(Pid(30), ProcessState::Running);
assert_eq!(list.current_job(), Some(i10));
assert_eq!(list.previous_job(), Some(i20));
}
#[test]
fn suspending_current_job() {
let mut list = JobList::default();
let i11 = list.add(Job::new(Pid(11)));
let i12 = list.add(Job::new(Pid(12)));
list.set_current_job(i11).unwrap();
list.update_status(Pid(11), ProcessState::stopped(SIGTTOU));
assert_eq!(list.current_job(), Some(i11));
assert_eq!(list.previous_job(), Some(i12));
}
#[test]
fn suspending_previous_job() {
let mut list = JobList::default();
let i11 = list.add(Job::new(Pid(11)));
let i12 = list.add(Job::new(Pid(12)));
list.set_current_job(i11).unwrap();
list.update_status(Pid(12), ProcessState::stopped(SIGTTOU));
assert_eq!(list.current_job(), Some(i12));
assert_eq!(list.previous_job(), Some(i11));
}
#[test]
fn suspending_job_with_running_current_job() {
let mut list = JobList::default();
let i10 = list.add(Job::new(Pid(10)));
let _i11 = list.add(Job::new(Pid(11)));
let i12 = list.add(Job::new(Pid(12)));
list.set_current_job(i10).unwrap();
list.update_status(Pid(12), ProcessState::stopped(SIGTTIN));
assert_eq!(list.current_job(), Some(i12));
assert_eq!(list.previous_job(), Some(i10));
}
#[test]
fn suspending_job_with_running_previous_job() {
let mut list = JobList::default();
let i11 = list.add(Job::new(Pid(11)));
let i12 = list.add(Job::new(Pid(12)));
let mut suspended = Job::new(Pid(10));
suspended.state = ProcessState::stopped(SIGTTIN);
let i10 = list.add(suspended);
assert_eq!(list.current_job(), Some(i10));
assert_eq!(list.previous_job(), Some(i11));
list.update_status(Pid(12), ProcessState::stopped(SIGTTOU));
assert_eq!(list.current_job(), Some(i12));
assert_eq!(list.previous_job(), Some(i10));
}
}