Merged revisions 83589 via svnmerge from
[asterisk/asterisk.git] / res / res_limit.c
index f3aaceb..d8fe851 100644 (file)
  *
  */
 
+/*! \file
+ *
+ * \brief Resource limits
+ *
+ * \author Tilghman Lesher <res_limit_200607@the-tilghman.com>
+ */
+
+
 #include "asterisk.h"
 
 #include <stdio.h>
 #include "asterisk/module.h"
 #include "asterisk/cli.h"
 
+/* Find proper rlimit for virtual memory */
+#ifdef RLIMIT_AS
+#define VMEM_DEF RLIMIT_AS
+#else
+#ifdef RLIMIT_VMEM
+#define VMEM_DEF RLIMIT_VMEM
+#endif
+#endif
+
 static struct limits {
        int resource;
        char limit[3];
@@ -37,11 +54,15 @@ static struct limits {
        { RLIMIT_CORE, "-c", "core file size" },
 #ifdef RLIMIT_RSS
        { RLIMIT_RSS, "-m", "resident memory" },
-       { RLIMIT_NPROC, "-u", "number of processes" },
        { RLIMIT_MEMLOCK, "-l", "amount of memory locked into RAM" },
 #endif
+#ifdef RLIMIT_NPROC
+       { RLIMIT_NPROC, "-u", "number of processes" },
+#endif
        { RLIMIT_NOFILE, "-n", "number of file descriptors" },
-       { RLIMIT_AS, "-v", "virtual memory" },
+#ifdef VMEM_DEF
+       { VMEM_DEF, "-v", "virtual memory" },
+#endif
 };
 
 static int str2limit(const char *string)
@@ -87,13 +108,18 @@ static int my_ulimit(int fd, int argc, char **argv)
                }
 
                if (argc == 3) {
+                       int x;
+#ifdef RLIMIT_NPROC
                        if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_NPROC && resource != RLIMIT_FSIZE) {
+#else
+                         if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_FSIZE) {
+#endif
                                ast_cli(fd, "Resource not permitted to be set\n");
                                return RESULT_FAILURE;
                        }
 
-                       sscanf(argv[2], "%d", (int *)&rlimit.rlim_cur);
-                       rlimit.rlim_max = rlimit.rlim_cur;
+                       sscanf(argv[2], "%d", &x);
+                       rlimit.rlim_max = rlimit.rlim_cur = x;
                        setrlimit(resource, &rlimit);
                        return RESULT_SUCCESS;
                } else {
@@ -129,7 +155,7 @@ static char *complete_ulimit(const char *line, const char *word, int pos, int st
        return NULL;
 }
 
-static char ulimit_usage[] =
+static const char ulimit_usage[] =
 "Usage: ulimit {-d|-l|-f|-m|-s|-t|-u|-v|-c|-n} [<num>]\n"
 "       Shows or sets the corresponding resource limit.\n"
 "         -d  Process data segment [readonly]\n"
@@ -139,7 +165,9 @@ static char ulimit_usage[] =
 "         -s  Process stack size [readonly]\n"
 "         -t  CPU usage [readonly]\n"
 "         -u  Child processes\n"
+#ifdef VMEM_DEF
 "         -v  Process virtual memory [readonly]\n"
+#endif
 "         -c  Core dump file size\n"
 "         -n  Number of file descriptors\n";