You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "casaroli (via GitHub)" <gi...@apache.org> on 2026/10/18 18:15:44 UTC

[PR] nshlib: Add the ulimit command. [nuttx-apps]

casaroli opened a new pull request, #3875:
URL: https://github.com/apache/nuttx-apps/pull/3875

   ## Summary
   
   NSH had no way to show or change the resource limits of the shell, which the programs that it starts inherit.
   
   This PR adds `ulimit [-H|-S] [-a|-c|-d|-f|-l|-n|-s|-t|-v] [<limit>|unlimited]`, as in other shells. It runs in the shell, so a limit applies to the shell and to the programs that it starts. Without a resource option it uses `-f`, as POSIX says. Sizes are in KiB, `-n` is a number of files and `-t` is in seconds. `-H` and `-S` select the hard or the soft limit; without them, a new value sets both. `-a` shows all limits. `CONFIG_NSH_DISABLE_ULIMIT` removes the command.
   
   The first commit only fixes an old nxstyle error in `nsh_syscmds.c`. apache/nuttx#NNNN documents the command.
   
   depends-on: apache/nuttx/pull/20784
   
   ## Impact
   
   A new NSH command. With apache/nuttx#20784, `-n` and `-s` change the limits of NSH and its programs; setting the other limits fails, because NuttX does not enforce them.
   
   ## Testing
   
   `qemu-armv8a:nsh` (QEMU, TCG) with apache/nuttx#20784:
   
   ```
   nsh> ulimit -a
   core file size (KiB)       (-c) unlimited
   data seg size (KiB)        (-d) unlimited
   file size (KiB)            (-f) unlimited
   max locked memory (KiB)    (-l) unlimited
   open files                 (-n) 256
   stack size (KiB)           (-s) unlimited
   cpu time (seconds)         (-t) unlimited
   virtual memory (KiB)       (-v) unlimited
   nsh> ulimit -n 8; ulimit -n
   8
   nsh> ulimit -s 64; ulimit -s
   64
   nsh> ulimit -S -s 32; ulimit -s; ulimit -H -s; ulimit -c 0
   32
   unlimited
   nsh: ulimit: setrlimit failed: 22
   ```
   
   `tools/checkpatch.sh -c -u -m -g` passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] nshlib: Add the ulimit command. [nuttx-apps]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #3875:
URL: https://github.com/apache/nuttx-apps/pull/3875#issuecomment-6273477688

   <!-- nuttx-depends-on-bot run-39210080746 -->
   ### 🔗 Cross-repo PR dependencies
   
   The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
   
   - https://github.com/apache/nuttx/pull/20784 @ 606a3d6e08
   
   CI run: https://github.com/apache/nuttx-apps/actions/runs/39210080746


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] nshlib: Add the ulimit command. [nuttx-apps]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #3875:
URL: https://github.com/apache/nuttx-apps/pull/3875#issuecomment-6255914216

   <!-- nuttx-depends-on-bot run-39161573999 -->
   ### 🔗 Cross-repo PR dependencies
   
   The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
   
   - https://github.com/apache/nuttx/pull/20784 @ 5e8e54ff5c
   
   CI run: https://github.com/apache/nuttx-apps/actions/runs/39161573999


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] nshlib: Add the ulimit command. [nuttx-apps]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #3875:
URL: https://github.com/apache/nuttx-apps/pull/3875#issuecomment-6258552152

   <!-- nuttx-depends-on-bot run-39177092665 -->
   ### 🔗 Cross-repo PR dependencies
   
   The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
   
   - https://github.com/apache/nuttx/pull/20784 @ 606a3d6e08
   
   CI run: https://github.com/apache/nuttx-apps/actions/runs/39177092665


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] nshlib: Add the ulimit command. [nuttx-apps]

Posted by "casaroli (via GitHub)" <gi...@apache.org>.
casaroli commented on code in PR #3875:
URL: https://github.com/apache/nuttx-apps/pull/3875#discussion_r4308524739


##########
nshlib/nsh_command.c:
##########
@@ -665,6 +665,11 @@ static const struct cmdmap_s g_cmdmap[] =
 #  endif
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_ULIMIT

Review Comment:
   Done.



##########
nshlib/nsh.h:
##########
@@ -1191,6 +1191,10 @@ int cmd_switchboot(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
   int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_ULIMIT

Review Comment:
   Done.



##########
nshlib/nsh_syscmds.c:
##########
@@ -932,3 +958,151 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_ulimit
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+int cmd_ulimit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  unsigned long long number;
+  FAR char *endptr;
+  struct rlimit rl;
+  rlim_t value;
+  rlim_t shown;
+  bool hard = false;
+  bool soft = false;
+  bool all = false;
+  bool badarg = false;
+  int sel = 2;       /* -f, as POSIX says */
+  int option;
+  int first;
+  int last;
+  int i;
+
+  while ((option = getopt(argc, argv, ":HSacdflnstv")) != ERROR)
+    {
+      switch (option)
+        {
+          case 'H':
+            hard = true;
+            break;
+
+          case 'S':
+            soft = true;
+            break;
+
+          case 'a':
+            all = true;
+            break;
+
+          default:
+            for (i = 0; i < nitems(g_ulimit_resources); i++)
+              {
+                if (g_ulimit_opts[i] == option)
+                  {
+                    sel = i;
+                    break;
+                  }
+              }
+
+            if (i == nitems(g_ulimit_resources))
+              {
+                nsh_error(vtbl, g_fmtarginvalid, argv[0]);
+                badarg = true;
+              }
+            break;
+        }
+    }
+
+  if (badarg || argc - optind > 1 || (all && argc - optind > 0))
+    {
+      if (!badarg)
+        {
+          nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
+        }
+
+      return ERROR;
+    }
+
+  /* Show the limits: the soft limit, or the hard limit with -H */
+
+  if (argc == optind)
+    {
+      first = all ? 0 : sel;
+      last  = all ? nitems(g_ulimit_resources) - 1 : sel;
+      for (i = first; i <= last; i++)
+        {
+          if (getrlimit(g_ulimit_resources[i], &rl) < 0)
+            {
+              nsh_error(vtbl, g_fmtcmdfailed, argv[0], "getrlimit",
+                        NSH_ERRNO);
+              return ERROR;
+            }
+
+          shown = hard ? rl.rlim_max : rl.rlim_cur;
+          if (all)
+            {
+              nsh_output(vtbl, "%-26s (-%c) ", g_ulimit_names[i],
+                         g_ulimit_opts[i]);
+            }
+
+          if (shown == RLIM_INFINITY)
+            {
+              nsh_output(vtbl, "unlimited\n");
+            }
+          else
+            {
+              nsh_output(vtbl, "%llu\n",
+                         (unsigned long long)(shown / g_ulimit_units[i]));

Review Comment:
   Done.



##########
nshlib/nsh_syscmds.c:
##########
@@ -932,3 +958,151 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_ulimit
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+int cmd_ulimit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  unsigned long long number;
+  FAR char *endptr;
+  struct rlimit rl;
+  rlim_t value;
+  rlim_t shown;
+  bool hard = false;
+  bool soft = false;
+  bool all = false;
+  bool badarg = false;
+  int sel = 2;       /* -f, as POSIX says */
+  int option;
+  int first;
+  int last;
+  int i;
+
+  while ((option = getopt(argc, argv, ":HSacdflnstv")) != ERROR)
+    {
+      switch (option)
+        {
+          case 'H':
+            hard = true;
+            break;
+
+          case 'S':
+            soft = true;
+            break;
+
+          case 'a':
+            all = true;
+            break;
+
+          default:
+            for (i = 0; i < nitems(g_ulimit_resources); i++)
+              {
+                if (g_ulimit_opts[i] == option)
+                  {
+                    sel = i;
+                    break;
+                  }
+              }
+
+            if (i == nitems(g_ulimit_resources))
+              {
+                nsh_error(vtbl, g_fmtarginvalid, argv[0]);
+                badarg = true;
+              }
+            break;
+        }
+    }
+
+  if (badarg || argc - optind > 1 || (all && argc - optind > 0))
+    {
+      if (!badarg)
+        {
+          nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
+        }
+
+      return ERROR;
+    }
+
+  /* Show the limits: the soft limit, or the hard limit with -H */
+
+  if (argc == optind)
+    {
+      first = all ? 0 : sel;
+      last  = all ? nitems(g_ulimit_resources) - 1 : sel;
+      for (i = first; i <= last; i++)
+        {
+          if (getrlimit(g_ulimit_resources[i], &rl) < 0)
+            {
+              nsh_error(vtbl, g_fmtcmdfailed, argv[0], "getrlimit",
+                        NSH_ERRNO);
+              return ERROR;
+            }
+
+          shown = hard ? rl.rlim_max : rl.rlim_cur;
+          if (all)
+            {
+              nsh_output(vtbl, "%-26s (-%c) ", g_ulimit_names[i],
+                         g_ulimit_opts[i]);
+            }
+
+          if (shown == RLIM_INFINITY)
+            {
+              nsh_output(vtbl, "unlimited\n");
+            }
+          else
+            {
+              nsh_output(vtbl, "%llu\n",
+                         (unsigned long long)(shown / g_ulimit_units[i]));
+            }
+        }
+
+      return OK;
+    }
+
+  /* Set a limit: both the soft and the hard limit without -H or -S */
+
+  if (strcmp(argv[optind], "unlimited") == 0)
+    {
+      value = RLIM_INFINITY;
+    }
+  else
+    {
+      number = strtoull(argv[optind], &endptr, 10);
+      if (*argv[optind] == '\0' || *endptr != '\0' ||
+          number >= RLIM_INFINITY / g_ulimit_units[sel])
+        {
+          nsh_error(vtbl, g_fmtarginvalid, argv[0]);
+          return ERROR;
+        }
+
+      value = (rlim_t)number * g_ulimit_units[sel];

Review Comment:
   Done.



##########
nshlib/nsh_syscmds.c:
##########
@@ -89,6 +91,30 @@
 static const char g_unknown[] = "unknown";
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+/* The resources of ulimit: option, resource, bytes per unit and name */
+
+static const char g_ulimit_opts[] = "cdflnstv";

Review Comment:
   Done.



##########
nshlib/nsh_syscmds.c:
##########
@@ -932,3 +958,151 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_ulimit
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+int cmd_ulimit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  unsigned long long number;
+  FAR char *endptr;
+  struct rlimit rl;
+  rlim_t value;
+  rlim_t shown;
+  bool hard = false;
+  bool soft = false;
+  bool all = false;
+  bool badarg = false;
+  int sel = 2;       /* -f, as POSIX says */
+  int option;
+  int first;
+  int last;
+  int i;
+
+  while ((option = getopt(argc, argv, ":HSacdflnstv")) != ERROR)

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] nshlib: Add the ulimit command. [nuttx-apps]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #3875:
URL: https://github.com/apache/nuttx-apps/pull/3875#discussion_r4308465214


##########
nshlib/nsh.h:
##########
@@ -1191,6 +1191,10 @@ int cmd_switchboot(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
   int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_ULIMIT

Review Comment:
   move before line 1190



##########
nshlib/nsh_command.c:
##########
@@ -665,6 +665,11 @@ static const struct cmdmap_s g_cmdmap[] =
 #  endif
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_ULIMIT

Review Comment:
   move before line 660



##########
nshlib/nsh_syscmds.c:
##########
@@ -932,3 +958,151 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_ulimit
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+int cmd_ulimit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  unsigned long long number;
+  FAR char *endptr;
+  struct rlimit rl;
+  rlim_t value;
+  rlim_t shown;
+  bool hard = false;
+  bool soft = false;
+  bool all = false;
+  bool badarg = false;
+  int sel = 2;       /* -f, as POSIX says */
+  int option;
+  int first;
+  int last;
+  int i;
+
+  while ((option = getopt(argc, argv, ":HSacdflnstv")) != ERROR)
+    {
+      switch (option)
+        {
+          case 'H':
+            hard = true;
+            break;
+
+          case 'S':
+            soft = true;
+            break;
+
+          case 'a':
+            all = true;
+            break;
+
+          default:
+            for (i = 0; i < nitems(g_ulimit_resources); i++)
+              {
+                if (g_ulimit_opts[i] == option)
+                  {
+                    sel = i;
+                    break;
+                  }
+              }
+
+            if (i == nitems(g_ulimit_resources))
+              {
+                nsh_error(vtbl, g_fmtarginvalid, argv[0]);
+                badarg = true;
+              }
+            break;
+        }
+    }
+
+  if (badarg || argc - optind > 1 || (all && argc - optind > 0))
+    {
+      if (!badarg)
+        {
+          nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
+        }
+
+      return ERROR;
+    }
+
+  /* Show the limits: the soft limit, or the hard limit with -H */
+
+  if (argc == optind)
+    {
+      first = all ? 0 : sel;
+      last  = all ? nitems(g_ulimit_resources) - 1 : sel;
+      for (i = first; i <= last; i++)
+        {
+          if (getrlimit(g_ulimit_resources[i], &rl) < 0)
+            {
+              nsh_error(vtbl, g_fmtcmdfailed, argv[0], "getrlimit",
+                        NSH_ERRNO);
+              return ERROR;
+            }
+
+          shown = hard ? rl.rlim_max : rl.rlim_cur;
+          if (all)
+            {
+              nsh_output(vtbl, "%-26s (-%c) ", g_ulimit_names[i],
+                         g_ulimit_opts[i]);
+            }
+
+          if (shown == RLIM_INFINITY)
+            {
+              nsh_output(vtbl, "unlimited\n");
+            }
+          else
+            {
+              nsh_output(vtbl, "%llu\n",
+                         (unsigned long long)(shown / g_ulimit_units[i]));

Review Comment:
   remove the cast



##########
nshlib/nsh_syscmds.c:
##########
@@ -89,6 +91,30 @@
 static const char g_unknown[] = "unknown";
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+/* The resources of ulimit: option, resource, bytes per unit and name */
+
+static const char g_ulimit_opts[] = "cdflnstv";

Review Comment:
   `static const char g_ulimit_opts[] = "cdflnstvHSa";`



##########
nshlib/nsh_syscmds.c:
##########
@@ -932,3 +958,151 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_ulimit
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+int cmd_ulimit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  unsigned long long number;
+  FAR char *endptr;
+  struct rlimit rl;
+  rlim_t value;
+  rlim_t shown;
+  bool hard = false;
+  bool soft = false;
+  bool all = false;
+  bool badarg = false;
+  int sel = 2;       /* -f, as POSIX says */
+  int option;
+  int first;
+  int last;
+  int i;
+
+  while ((option = getopt(argc, argv, ":HSacdflnstv")) != ERROR)
+    {
+      switch (option)
+        {
+          case 'H':
+            hard = true;
+            break;
+
+          case 'S':
+            soft = true;
+            break;
+
+          case 'a':
+            all = true;
+            break;
+
+          default:
+            for (i = 0; i < nitems(g_ulimit_resources); i++)
+              {
+                if (g_ulimit_opts[i] == option)
+                  {
+                    sel = i;
+                    break;
+                  }
+              }
+
+            if (i == nitems(g_ulimit_resources))
+              {
+                nsh_error(vtbl, g_fmtarginvalid, argv[0]);
+                badarg = true;
+              }
+            break;
+        }
+    }
+
+  if (badarg || argc - optind > 1 || (all && argc - optind > 0))
+    {
+      if (!badarg)
+        {
+          nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
+        }
+
+      return ERROR;
+    }
+
+  /* Show the limits: the soft limit, or the hard limit with -H */
+
+  if (argc == optind)
+    {
+      first = all ? 0 : sel;
+      last  = all ? nitems(g_ulimit_resources) - 1 : sel;
+      for (i = first; i <= last; i++)
+        {
+          if (getrlimit(g_ulimit_resources[i], &rl) < 0)
+            {
+              nsh_error(vtbl, g_fmtcmdfailed, argv[0], "getrlimit",
+                        NSH_ERRNO);
+              return ERROR;
+            }
+
+          shown = hard ? rl.rlim_max : rl.rlim_cur;
+          if (all)
+            {
+              nsh_output(vtbl, "%-26s (-%c) ", g_ulimit_names[i],
+                         g_ulimit_opts[i]);
+            }
+
+          if (shown == RLIM_INFINITY)
+            {
+              nsh_output(vtbl, "unlimited\n");
+            }
+          else
+            {
+              nsh_output(vtbl, "%llu\n",
+                         (unsigned long long)(shown / g_ulimit_units[i]));
+            }
+        }
+
+      return OK;
+    }
+
+  /* Set a limit: both the soft and the hard limit without -H or -S */
+
+  if (strcmp(argv[optind], "unlimited") == 0)
+    {
+      value = RLIM_INFINITY;
+    }
+  else
+    {
+      number = strtoull(argv[optind], &endptr, 10);
+      if (*argv[optind] == '\0' || *endptr != '\0' ||
+          number >= RLIM_INFINITY / g_ulimit_units[sel])
+        {
+          nsh_error(vtbl, g_fmtarginvalid, argv[0]);
+          return ERROR;
+        }
+
+      value = (rlim_t)number * g_ulimit_units[sel];

Review Comment:
   remove the cast too



##########
nshlib/nsh_syscmds.c:
##########
@@ -932,3 +958,151 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_ulimit
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+int cmd_ulimit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  unsigned long long number;
+  FAR char *endptr;
+  struct rlimit rl;
+  rlim_t value;
+  rlim_t shown;
+  bool hard = false;
+  bool soft = false;
+  bool all = false;
+  bool badarg = false;
+  int sel = 2;       /* -f, as POSIX says */
+  int option;
+  int first;
+  int last;
+  int i;
+
+  while ((option = getopt(argc, argv, ":HSacdflnstv")) != ERROR)

Review Comment:
   replace  ":HSacdflnstv" with g_ulimit_opts



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] nshlib: Add the ulimit command. [nuttx-apps]

Posted by "casaroli (via GitHub)" <gi...@apache.org>.
casaroli closed pull request #3875: nshlib: Add the ulimit command.
URL: https://github.com/apache/nuttx-apps/pull/3875


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] nshlib: Add the ulimit command. [nuttx-apps]

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis merged PR #3875:
URL: https://github.com/apache/nuttx-apps/pull/3875


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org