prlimit: add support for executing a command

prlimit.c: Alternatively to applying the limits to an existing process via the
--pid option, allow a command to be executed. Adapted usage() accordingly.

prlimit.1: mention new syntax.

Signed-off-by: Bernhard Voelker <mail@bernhard-voelker.de>
This commit is contained in:
Bernhard Voelker 2011-11-16 17:57:27 +01:00 committed by Karel Zak
parent 0459a7b389
commit 53e1f461f9
2 changed files with 26 additions and 5 deletions

View File

@ -9,12 +9,22 @@ get and set a process resource limits.
.SH SYNOPSIS .SH SYNOPSIS
.B prlimit .B prlimit
.RB [options] .RB [options]
.RB [ \-\-{resource_name}[=limits] ] .RB [ \-\-{resource_name}[=limits]
.RB [ \-\-pid\ PID]
.B prlimit
.RB [options]
.RB [ \-\-{resource_name}[=limits]]
.RB "command " [ argument ...]
.SH DESCRIPTION .SH DESCRIPTION
Given a process id and one or more resources, \fBprlimit\fP tries to retrieve Given a process id and one or more resources, \fBprlimit\fP tries to retrieve
and/or modify the limits. and/or modify the limits.
When \fIcommand\fR is given,
.B prlimit
will run this command with the given arguments.
The \fIlimits\fP format is composed by a soft and a hard (ceiling) value, separated The \fIlimits\fP format is composed by a soft and a hard (ceiling) value, separated
by a semicolon (:), in order to modify the existing value(s). If no limits are by a semicolon (:), in order to modify the existing value(s). If no limits are
used, \fBprlimit\fP will only display the current values. If one of the values used, \fBprlimit\fP will only display the current values. If one of the values
@ -92,6 +102,8 @@ of open files to 1024 and 4095, respectively.
Modify only the soft limit for the amount of processes. Modify only the soft limit for the amount of processes.
.IP "\fBprlimit \-\-pid $$ --nproc=unlimited\fP" .IP "\fBprlimit \-\-pid $$ --nproc=unlimited\fP"
Set the amount of processes for both soft and ceiling values to unlimited. Set the amount of processes for both soft and ceiling values to unlimited.
.IP "\fBprlimit --cpu=10 sort -u hugefile\fP"
Set the soft and hard CPU time limit and run 'sort'.
.SH "SEE ALSO" .SH "SEE ALSO"
.BR prlimit (2), .BR prlimit (2),

View File

@ -152,7 +152,9 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
fputs(USAGE_HEADER, out); fputs(USAGE_HEADER, out);
fprintf(out, fprintf(out,
_(" %s [options]\n"), program_invocation_short_name); _(" %s [options] [-p PID]\n"), program_invocation_short_name);
fprintf(out,
_(" %s [options] COMMAND\n"), program_invocation_short_name);
fputs(_("\nGeneral Options:\n"), out); fputs(_("\nGeneral Options:\n"), out);
fputs(_(" -p, --pid <pid> process id\n" fputs(_(" -p, --pid <pid> process id\n"
@ -521,7 +523,7 @@ int main(int argc, char **argv)
assert(MAX_RESOURCES == STACK + 1); assert(MAX_RESOURCES == STACK + 1);
while((opt = getopt_long(argc, argv, while((opt = getopt_long(argc, argv,
"c::d::e::f::i::l::m::n::q::r::s::t::u::v::x::y::p:o:vVh", "+c::d::e::f::i::l::m::n::q::r::s::t::u::v::x::y::p:o:vVh",
longopts, NULL)) != -1) { longopts, NULL)) != -1) {
switch(opt) { switch(opt) {
case 'c': case 'c':
@ -608,8 +610,9 @@ int main(int argc, char **argv)
break; break;
} }
} }
if (argc > optind) if (argc > optind && pid)
usage(stderr); errx(EXIT_FAILURE,
_("--pid option and COMMAND are mutually exclusive"));
if (!ncolumns) { if (!ncolumns) {
/* default columns */ /* default columns */
@ -633,5 +636,11 @@ int main(int argc, char **argv)
if (!list_empty(&lims)) if (!list_empty(&lims))
show_limits(&lims, tt_flags); show_limits(&lims, tt_flags);
if (argc > optind) {
/* prlimit [options] COMMAND */
execvp(argv[optind], &argv[optind]);
err(EXIT_FAILURE, _("executing %s failed"), argv[optind]);
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }