Properly use .Nm, .Em, .Pa etc. Make usage() a simplier function.

This commit is contained in:
Philippe Charnier 2000-03-26 14:37:47 +00:00
parent eb257e6ec5
commit c097456602
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=58609
2 changed files with 60 additions and 45 deletions

View File

@ -57,7 +57,7 @@ environment variables like
.Xr env 1
and run a program with the selected resources.
Three uses of the
.Nm limits
.Nm
command are possible:
.Pp
.Bl -hang -width indent
@ -99,9 +99,9 @@ format, suitable for the calling shell.
The calling shell is determined by examining the entries in the
.Pa /proc
filesystem for the parent process.
If the shell is known (ie. it is one of sh, csh, bash, tcsh, ksh,
If the shell is known (i.e. it is one of sh, csh, bash, tcsh, ksh,
pdksh or rc),
.Nm limits
.Nm
emits 'limit' or 'ulimit' commands in the format understood by
that shell.
If the name of the shell cannot be determined, then the 'ulimit'
@ -116,13 +116,13 @@ global configuration of maximum resource usage by maintaining a
central database of settings in the login class database.
.Pp
Within a shell script,
.Nm limits
.Nm
will normally be used with eval within backticks as follows:
.Pp
.Dl eval `limits -e -C daemon`
.Pp
which causes the output of
.Nm limits
.Nm
to be evaluated and set by the current shell.
.El
.Pp
@ -140,7 +140,7 @@ If the user does not belong to a class, then the resource capabilities
for the "default" class are used, if it exists, or the "root" class if
the user is a superuser account.
.It Fl S
Selects display or setting of "soft" (or current) resource limits.
Select display or setting of "soft" (or current) resource limits.
If specific limits settings follow this switch, only soft limits are
affected unless overridden later with either the
.Fl H
@ -148,7 +148,7 @@ or
.Fl B
flags.
.It Fl H
Selects display or setting of "hard" (or maximum) resource limits.
Select display or setting of "hard" (or maximum) resource limits.
If specific limits settings follow this switch, only hard limits are
affected until overridden later with either the
.Fl S
@ -156,7 +156,7 @@ or
.Fl B
flags.
.It Fl B
Selects display or setting of both "soft" (current) or "hard" (maximum)
Select display or setting of both "soft" (current) or "hard" (maximum)
resource limits.
If specific limits settings follow this switch, both soft and hard
limits are affected until overridden later with either the
@ -165,40 +165,40 @@ or
.Fl H
flags.
.Fl e
Selects "eval mode" formatting for output.
Select "eval mode" formatting for output.
This is valid only on display mode and cannot be used when running a
command.
The exact syntax used for output depends upon the type of shell from
which
.Nm limits
.Nm
is invoked.
.It Fl b Op Ar limit
Selects or sets the
.Em sbsize
resource limit.
.It Fl c Op Ar limit
Selects or sets (if 'limit' is specified) the
Select or set (if 'limit' is specified) the
.Em coredumpsize
resource limit.
A value of 0 disables core dumps.
.It Fl d Op Ar limit
Selects or sets (if 'limit' is specified) the
Select or set (if 'limit' is specified) the
.Em datasize
resource limit.
.It Fl f Op Ar limit
Selects or sets the
Select or set the
.Em filesize
resource limit.
.It Fl l Op Ar limit
Selects or sets the
Select or set the
.Em memorylocked
resource limit.
.It Fl m Op Ar limit
Selects or sets the
Select or set the
.Em memoryuse
size limit.
.It Fl n Op Ar limit
Selects or sets the
Select or set the
.Em openfiles
resource limit. The system-wide limit on the maximum number of
open files per process can be viewed using the 'sysctl kern.maxfilesperproc'
@ -206,15 +206,15 @@ command. The total number of simultaneously open files in the entire
system is limited to the value displayed by the 'sysctl kern.maxfiles'
command.
.It Fl s Op Ar limit
Selects or sets the
Select or set the
.Em stacksize
resource limit.
.It Fl t Op Ar limit
Selects or sets the
Select or set the
.Em cputime
resource limit.
.It Fl u Op Ar limit
Selects or sets the
Select or set the
.Em maxproc
resource limit. The system-wide limit on the maximum number of processes
allowed per UID can be viewed using the 'sysctl kern.maxprocperuid' command.
@ -222,8 +222,16 @@ The maximum number of processes that can be running simultaneously
in the entire system is limited to the value given by
the 'sysctl kern.maxproc' command.
.Pp
Valid values for 'limit' in the above set of flags consist of either the
string 'infinity' or 'inf' for an infinite (or kernel-defined maximum)
Valid values for
.Ar limit
in the above set of flags consist of either the
string
.Em infinity ,
.Em inf ,
.Em unlimited
or
.Em unlimit
for an infinite (or kernel-defined maximum)
limit, or a numeric value maybe followed by a suffix.
Values which relate to size default to a value in bytes, or one of the
following suffixes may be used as a multiplier:
@ -265,7 +273,7 @@ weeks.
The option
.Sq Fl E
causes
.Nm limits
.Nm
to completely ignore the environment it inherits.
.It Fl a
This option forces all resource settings to be displayed even if
@ -282,17 +290,17 @@ call, only the superuser may raise process "hard" resource limits.
Non-root users may, however, lower them or change "soft" resource limits
within to any value below the hard limit.
When invoked to execute a program, the failure of
.Nm limits
.Nm
to raise a hard limit is considered a fatal error.
.El
.Sh DIAGNOSTICS
.Nm Limits
exits with EXIT_FAILURE if usage is incorrect in any way; ie. an invalid
exits with EXIT_FAILURE if usage is incorrect in any way; i.e. an invalid
option, or set/display options are selected in the same invocation,
.Fl e
is used when running a program, etc.
When run in display or eval mode,
.Nm limits
.Nm
exits with a status of EXIT_SUCCESS.
When run in command mode and execution of the command succeeds, the exit status
will be whatever the executed program returns.
@ -311,18 +319,26 @@ will be whatever the executed program returns.
does not handle commands with equal (``='') signs in their
names, for obvious reasons.
.Pp
When eval output is selected, the /proc filesystem must be installed
When eval output is selected, the
.Pa /proc
filesystem must be installed
and mounted for the shell to be correctly determined, and therefore
output syntax correct for the running shell.
The default output is valid for /bin/sh, so this means that any
The default output is valid for
.Pa /bin/sh ,
so this means that any
usage of
.Nm limits
in eval mode prior mounting /proc may only occur in standard bourne
.Nm
in eval mode prior mounting
.Pa /proc
may only occur in standard bourne
shell scripts.
.Pp
.Nm Limits
makes no effort to ensure that resource settings emitted or displayed
are valid and settable by the current user.
Only a superuser account may raise hard limits, and when doing so
the FreeBSD kernel will silently lower limits to values less than
the
.Fx
kernel will silently lower limits to values less than
specified if the values given are too high.

View File

@ -214,7 +214,7 @@ static struct {
#define RCS_STRING "tfdscmlunb"
static rlim_t resource_num(int which, int ch, const char *str);
static void usage(const char *msg, ...);
static void usage(void);
static int getshelltype(void);
static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
const char *pfx, const char *sfx, const char *which);
@ -266,7 +266,8 @@ main(int argc, char *argv[])
if ((pwd = getpwnam(optarg)) == NULL) {
if (!isdigit(*optarg) ||
(pwd = getpwuid(atoi(optarg))) == NULL) {
usage("Invalid user `%s'\n", optarg);
warnx("invalid user `%s'", optarg);
usage();
}
}
break;
@ -297,7 +298,7 @@ main(int argc, char *argv[])
}
/* FALLTHRU */
case '?':
usage(NULL);
usage();
}
optarg = NULL;
}
@ -377,8 +378,10 @@ main(int argc, char *argv[])
* (perhaps) set environment variables and run a program
*/
if (*argv) {
if (doeval)
usage("-e cannot be used with `cmd' option\n");
if (doeval) {
warnx("-e cannot be used with `cmd' option");
usage();
}
login_close(lc);
@ -394,7 +397,7 @@ main(int argc, char *argv[])
}
if (*argv == NULL)
usage(NULL);
usage();
execvp(*argv, argv);
err(1, "%s", *argv);
@ -450,14 +453,8 @@ main(int argc, char *argv[])
static void
usage(char const *msg, ...)
usage(void)
{
if (msg) {
va_list argp;
va_start(argp, msg);
vfprintf(stderr, msg, argp);
va_end(argp);
}
(void)fprintf(stderr,
"usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstu [val]] [[name=val ...] cmd]\n");
exit(EXIT_FAILURE);
@ -567,8 +564,10 @@ resource_num(int which, int ch, const char *str)
s = e;
break;
}
if (*s)
usage("invalid value -%c `%s'\n", ch, str);
if (*s) {
warnx("invalid value -%c `%s'", ch, str);
usage();
}
}
return res;
}