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
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2024 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/>.
//! Ulimit built-in
//!
//! The **`ulimit`** built-in sets or shows system resource limits.
//!
//! # Synopsis
//!
//! ```sh
//! ulimit [-SH] [-a|-b|-c|-d|-e|-f|-i|-k|-l|-m|-n|-q|-R|-r|-s|-t|-u|-v|-w|-x] [limit]
//! ```
//!
//! # Description
//!
//! The built-in sets or shows the system resource limits. The limits are
//! specified by the *limit* operand. If the *limit* operand is omitted, the
//! built-in shows the current limits.
//!
//! There are two types of limits for each resource: the soft limit and the hard
//! limit. The soft limit is the value that the kernel enforces for the process.
//! The process can increase the soft limit up to the hard limit. The hard limit
//! is the maximum value that the soft limit can be set to. Any process can
//! decrease the hard limit, but only a process with the appropriate privilege
//! can increase the hard limit.
//!
//! # Options
//!
//! The **`-S`** (**`--soft`**) option sets or shows the soft limit. The
//! **`-H`** (**`--hard`**) option sets or shows the hard limit. If neither option
//! is specified:
//!
//! - both the soft limit and the hard limit are set, or
//! - the soft limit is shown.
//!
//! You can also set both limits at once by specifying both options.
//! However, it is an error to specify both options when the *limit* operand is
//! omitted.
//!
//! ## Resources
//!
//! You use an option to specify the resource to set or show the limit on.
//! Available resources vary depending on the platform, so not all of the
//! following options are universally accepted:
//!
//! - **`-b`** (**`--sbsize`**): [`Resource::SBSIZE`] (bytes)
//! - **`-c`** (**`--core`**): [`Resource::CORE`] (512-byte blocks)
//! - **`-d`** (**`--data`**): [`Resource::DATA`] (kilobytes)
//! - **`-e`** (**`--nice`**): [`Resource::NICE`] (see below)
//! - **`-f`** (**`--fsize`**): [`Resource::FSIZE`] (512-byte blocks)
//! - **`-i`** (**`--sigpending`**): [`Resource::SIGPENDING`]
//! - **`-k`** (**`--kqueues`**): [`Resource::KQUEUES`]
//! - **`-l`** (**`--memlock`**): [`Resource::MEMLOCK`] (kilobytes)
//! - **`-m`** (**`--rss`**): [`Resource::RSS`] (kilobytes)
//! - **`-n`** (**`--nofile`**): [`Resource::NOFILE`]
//! - **`-q`** (**`--msgqueue`**): [`Resource::MSGQUEUE`]
//! - **`-R`** (**`--rttime`**): [`Resource::RTTIME`] (microseconds)
//! - **`-r`** (**`--rtprio`**): [`Resource::RTPRIO`]
//! - **`-s`** (**`--stack`**): [`Resource::STACK`] (kilobytes)
//! - **`-t`** (**`--cpu`**): [`Resource::CPU`] (seconds)
//! - **`-u`** (**`--nproc`**): [`Resource::NPROC`]
//! - **`-v`** (**`--as`**): [`Resource::AS`] (kilobytes)
//! - **`-w`** (**`--swap`**): [`Resource::SWAP`]
//! - **`-x`** (**`--locks`**): [`Resource::LOCKS`]
//!
//! Limits that are specified as the *limit* operand and are shown in the output
//! are in the unit specified in the parentheses above. For [`Resource::NICE`],
//! The limit value defines the lower bound for the nice value by the formula
//! `nice = 20 - limit`. Note that lower nice values represent higher
//! priorities.
//!
//! To show the limits for all resources, use the **`-a`** (**`--all`**) option.
//! This option cannot be used with the *limit* operand.
//!
//! # Operands
//!
//! The *limit* operand specifies the new limit to set. The value is interpreted
//! as follows:
//!
//! - If the value is a non-negative integer, the limit is set to that value.
//! - If the value is `unlimited`, the limit is set to the maximum value.
//! - If the value is `hard`, the limit is set to the current hard limit.
//! - If the value is `soft`, the limit is set to the current soft limit.
//!
//! # Standard output
//!
//! If the *limit* operand is omitted, the built-in prints the current limit for
//! the specified resource. If the **`-a`** option is effective, the built-in
//! prints the current limits for all resources in a table.
//!
//! # Errors
//!
//! The built-in may fail when:
//!
//! - The specified resource is not supported on the current platform.
//! - The specified soft limit is greater than the hard limit.
//! - The new hard limit is greater than the current hard limit and the user does
//! not have permission to raise the hard limit.
//! - The specified *limit* operand is out of range.
//!
//! # Exit status
//!
//! The built-in exits with a non-zero status when an error occurs. Otherwise, it
//! exits with zero.
//!
//! # Examples
//!
//! ```sh
//! ulimit -n 1024
//! ulimit -t unlimited
//! ulimit -v hard
//! ulimit -m soft
//! ulimit -a
//! ```
//!
//! # Portability
//!
//! The `ulimit` built-in is defined in POSIX, but only the `-f` option is
//! required. All the other options are extensions. However, many options
//! including `-H`, `-S`, `-a`, `-c`, `-d`, `-n`, `-s`, and `-t` are widely
//! supported in other shells.
//!
//! See the source code for [`Resource::as_raw_type`] to see which resources are
//! supported on which platforms.
//!
//! The behavior differs between shells when both the `-H` and `-S` options are
//! specified. This implementation sets both limits, but the previous versions
//! of yash honored only the last specified option.
//!
//! The `hard` and `soft` values for the *limit* operand are not defined in
//! POSIX.
use crate;
use Field;
use ;
use Errno;
use Env;
use System as _;
/// Type of limit to show
///
/// See [`Command`].
/// Type of limit to set
///
/// See [`Command`].
/// Value of the limit to set
///
/// See [`Command`].
/// Interpretation of command-line arguments that determine the behavior of the
/// `ulimit` built-in
pub use ResourceExt;
/// Error that may occur in [`Command::execute`]
/// Executes the `ulimit` built-in.
///
/// This is the main entry point for the `ulimit` built-in.
pub async