Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh Have the ulimit command watch for ulimit -n (alter nu...



details:   https://anonhg.NetBSD.org/src/rev/48b0d0390089
branches:  trunk
changeset: 986258:48b0d0390089
user:      kre <kre%NetBSD.org@localhost>
date:      Wed Sep 15 18:30:57 2021 +0000

description:
Have the ulimit command watch for ulimit -n (alter number of available fds)
and keep the rest of the shell aware of any changes.

While here, modify 'ulimit -aSH' to print both the soft and hard limits
for the resources, rather than just (in this case, as H comes last) the
hard limit.   In any other case when both S and H are present, and we're
examining a limit, use the soft limit (just as if neither were given).

No change for setting limits (both are set, unless exactly one of -H
or -S is given).   However, we now check for overflow when converting
the value to be assigned, rather than just truncating the value however
it happens to work out...

diffstat:

 bin/sh/miscbltin.c |  65 ++++++++++++++++++++++++++++++++++++-----------------
 bin/sh/sh.1        |  13 ++++++++--
 2 files changed, 54 insertions(+), 24 deletions(-)

diffs (164 lines):

diff -r bd1b44731eba -r 48b0d0390089 bin/sh/miscbltin.c
--- a/bin/sh/miscbltin.c        Wed Sep 15 18:29:45 2021 +0000
+++ b/bin/sh/miscbltin.c        Wed Sep 15 18:30:57 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: miscbltin.c,v 1.44 2017/05/13 15:03:34 gson Exp $      */
+/*     $NetBSD: miscbltin.c,v 1.45 2021/09/15 18:30:57 kre Exp $       */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)miscbltin.c        8.4 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: miscbltin.c,v 1.44 2017/05/13 15:03:34 gson Exp $");
+__RCSID("$NetBSD: miscbltin.c,v 1.45 2021/09/15 18:30:57 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -63,6 +63,7 @@
 #include "error.h"
 #include "builtins.h"
 #include "mystring.h"
+#include "redir.h"             /* for user_fd_limit */
 
 #undef rflag
 
@@ -357,7 +358,7 @@
        int     c;
        rlim_t val = 0;
        enum { SOFT = 0x1, HARD = 0x2 }
-                       how = SOFT | HARD;
+                       how = 0, which;
        const struct limits     *l;
        int             set, all = 0;
        int             optc, what;
@@ -367,10 +368,10 @@
        while ((optc = nextopt("HSabtfdscmlrpnv")) != '\0')
                switch (optc) {
                case 'H':
-                       how = HARD;
+                       how |= HARD;
                        break;
                case 'S':
-                       how = SOFT;
+                       how |= SOFT;
                        break;
                case 'a':
                        all = 1;
@@ -390,38 +391,58 @@
 
                if (all || argptr[1])
                        error("too many arguments");
+               if (how == 0)
+                       how = HARD | SOFT;
+
                if (strcmp(p, "unlimited") == 0)
                        val = RLIM_INFINITY;
                else {
                        val = (rlim_t) 0;
 
-                       while ((c = *p++) >= '0' && c <= '9')
-                               val = (val * 10) + (long)(c - '0');
+                       while ((c = *p++) >= '0' && c <= '9') {
+                               if (val >= RLIM_INFINITY/10)
+                                       error("value overflow");
+                               val = (val * 10);
+                               if (val >= RLIM_INFINITY - (long)(c - '0'))
+                                       error("value overflow");
+                               val += (long)(c - '0');
+                       }
                        if (c)
                                error("bad number");
+                       if (val > RLIM_INFINITY / l->factor)
+                               error("value overflow");
                        val *= l->factor;
                }
-       }
+       } else if (how == 0)
+               how = SOFT;
+
        if (all) {
                for (l = limits; l->name; l++) {
                        getrlimit(l->cmd, &limit);
-                       if (how & SOFT)
-                               val = limit.rlim_cur;
-                       else if (how & HARD)
-                               val = limit.rlim_max;
-
-                       out1fmt("%-13s (-%c %-11s) ", l->name, l->option,
+                       out1fmt("%-13s (-%c %-11s)    ", l->name, l->option,
                            l->unit);
-                       if (val == RLIM_INFINITY)
-                               out1fmt("unlimited\n");
-                       else
-                       {
-                               val /= l->factor;
+
+                       which = how;
+                       while (which != 0) {
+                               if (which & SOFT) {
+                                       val = limit.rlim_cur;
+                                       which &= ~SOFT;
+                               } else if (which & HARD) {
+                                       val = limit.rlim_max;
+                                       which &= ~HARD;
+                               }
+
+                               if (val == RLIM_INFINITY)
+                                       out1fmt("unlimited");
+                               else {
+                                       val /= l->factor;
 #ifdef BSD4_4
-                               out1fmt("%lld\n", (long long) val);
+                                       out1fmt("%9lld", (long long) val);
 #else
-                               out1fmt("%ld\n", (long) val);
+                                       out1fmt("%9ld", (long) val);
 #endif
+                               }
+                               out1fmt("%c", which ? '\t' : '\n');
                        }
                }
                return 0;
@@ -436,6 +457,8 @@
                        limit.rlim_cur = val;
                if (setrlimit(l->cmd, &limit) < 0)
                        error("error setting limit (%s)", strerror(errno));
+               if (l->cmd == RLIMIT_NOFILE)
+                       user_fd_limit = sysconf(_SC_OPEN_MAX);
        } else {
                if (how & SOFT)
                        val = limit.rlim_cur;
diff -r bd1b44731eba -r 48b0d0390089 bin/sh/sh.1
--- a/bin/sh/sh.1       Wed Sep 15 18:29:45 2021 +0000
+++ b/bin/sh/sh.1       Wed Sep 15 18:30:57 2021 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: sh.1,v 1.233 2021/09/12 06:53:08 wiz Exp $
+.\"    $NetBSD: sh.1,v 1.234 2021/09/15 18:30:57 kre Exp $
 .\" Copyright (c) 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
 .\"
@@ -3720,13 +3720,20 @@
 nor
 .Fl S
 is specified, the soft limit is displayed or both limits are set.
-If both are specified, the last one wins.
+If both are specified,
+then with
+.Fl a
+both are displayed, the soft followed by the hard limit,
+otherwise for setting, both limits are set,
+and for interrogating the soft limit is displayed.
 .Pp
 The limit to be interrogated or set, then, is chosen by specifying
 any one of these flags:
 .Bl -tag -width Fl
 .It Fl a
-show all the current limits
+show all the current limits (it is an error to
+attempt to set the limits by giving a
+.Ar value )
 .It Fl b
 the socket buffer size of a process (bytes)
 .It Fl c



Home | Main Index | Thread Index | Old Index