security_events: Fix error caused by DTD validation error
[asterisk/asterisk.git] / res / res_limit.c
index 9d6d269..e883ff3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
  * Resource limits
  * 
  * \author Tilghman Lesher <res_limit_200607@the-tilghman.com>
  */
 
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
 
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#define _XOPEN_SOURCE 600
-#include <string.h>
 #include <ctype.h>
 #include <sys/time.h>
 #include <sys/resource.h>
-#include <errno.h>
 #include "asterisk/module.h"
 #include "asterisk/cli.h"
 
@@ -44,34 +41,35 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #endif
 #endif
 
-static struct limits {
+static const struct limits {
        int resource;
        char limit[3];
        char desc[40];
+       char clicmd[15];
 } limits[] = {
-       { RLIMIT_CPU,     "-t", "cpu time" },
-       { RLIMIT_FSIZE,   "-f", "file size" },
-       { RLIMIT_DATA,    "-d", "program data segment" },
-       { RLIMIT_STACK,   "-s", "program stack size" },
-       { RLIMIT_CORE,    "-c", "core file size" },
+       { RLIMIT_CPU,     "-t", "cpu time", "time" },
+       { RLIMIT_FSIZE,   "-f", "file size" , "file" },
+       { RLIMIT_DATA,    "-d", "program data segment", "data" },
+       { RLIMIT_STACK,   "-s", "program stack size", "stack" },
+       { RLIMIT_CORE,    "-c", "core file size", "core" },
 #ifdef RLIMIT_RSS
-       { RLIMIT_RSS,     "-m", "resident memory" },
-       { RLIMIT_MEMLOCK, "-l", "amount of memory locked into RAM" },
+       { RLIMIT_RSS,     "-m", "resident memory", "memory" },
+       { RLIMIT_MEMLOCK, "-l", "amount of memory locked into RAM", "locked" },
 #endif
 #ifdef RLIMIT_NPROC
-       { RLIMIT_NPROC,   "-u", "number of processes" },
+       { RLIMIT_NPROC,   "-u", "number of processes", "processes" },
 #endif
-       { RLIMIT_NOFILE,  "-n", "number of file descriptors" },
+       { RLIMIT_NOFILE,  "-n", "number of file descriptors", "descriptors" },
 #ifdef VMEM_DEF
-       { VMEM_DEF,       "-v", "virtual memory" },
+       { VMEM_DEF,       "-v", "virtual memory", "virtual" },
 #endif
 };
 
 static int str2limit(const char *string)
 {
        size_t i;
-       for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
-               if (!strcasecmp(string, limits[i].limit))
+       for (i = 0; i < ARRAY_LEN(limits); i++) {
+               if (!strcasecmp(string, limits[i].clicmd))
                        return limits[i].resource;
        }
        return -1;
@@ -80,8 +78,8 @@ static int str2limit(const char *string)
 static const char *str2desc(const char *string)
 {
        size_t i;
-       for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
-               if (!strcmp(string, limits[i].limit))
+       for (i = 0; i < ARRAY_LEN(limits); i++) {
+               if (!strcmp(string, limits[i].clicmd))
                        return limits[i].desc;
        }
        return "<unknown>";
@@ -94,10 +92,10 @@ static char *complete_ulimit(struct ast_cli_args *a)
 
        if (a->pos > 1)
                return NULL;
-       for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
-               if (!strncasecmp(limits[i].limit, a->word, wordlen)) {
+       for (i = 0; i < ARRAY_LEN(limits); i++) {
+               if (!strncasecmp(limits[i].clicmd, a->word, wordlen)) {
                        if (++which > a->n)
-                               return ast_strdup(limits[i].limit);
+                               return ast_strdup(limits[i].clicmd);
                }
        }
        return NULL;
@@ -112,41 +110,41 @@ static char *handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_
        case CLI_INIT:
                e->command = "ulimit";
                e->usage =
-                       "Usage: ulimit {-d|"
+                       "Usage: ulimit {data|"
 #ifdef RLIMIT_RSS
-                       "-l|"
+                       "limit|"
 #endif
-                       "-f|"
+                       "file|"
 #ifdef RLIMIT_RSS
-                       "-m|"
+                       "memory|"
 #endif
-                       "-s|-t|"
+                       "stack|time|"
 #ifdef RLIMIT_NPROC
-                       "-u|"
+                       "processes|"
 #endif
 #ifdef VMEM_DEF
-                       "-v|"
+                       "virtual|"
 #endif
-                       "-c|-n} [<num>]\n"
+                       "core|descriptors} [<num>]\n"
                        "       Shows or sets the corresponding resource limit.\n"
-                       "         -d  Process data segment [readonly]\n"
+                       "         data          Process data segment [readonly]\n"
 #ifdef RLIMIT_RSS
-                       "         -l  Memory lock size [readonly]\n"
+                       "         lock          Memory lock size [readonly]\n"
 #endif
-                       "         -f  File size\n"
+                       "         file          File size\n"
 #ifdef RLIMIT_RSS
-                       "         -m  Process resident memory [readonly]\n"
+                       "         memory        Process resident memory [readonly]\n"
 #endif
-                       "         -s  Process stack size [readonly]\n"
-                       "         -t  CPU usage [readonly]\n"
+                       "         stack         Process stack size [readonly]\n"
+                       "         time          CPU usage [readonly]\n"
 #ifdef RLIMIT_NPROC
-                       "         -u  Child processes\n"
+                       "         processes     Child processes\n"
 #endif
 #ifdef VMEM_DEF
-                       "         -v  Process virtual memory [readonly]\n"
+                       "         virtual       Process virtual memory [readonly]\n"
 #endif
-                       "         -c  Core dump file size\n"
-                       "         -n  Number of file descriptors\n";
+                       "         core          Core dump file size\n"
+                       "         descriptors   Number of file descriptors\n";
                return NULL;
        case CLI_GENERATE:
                return complete_ulimit(a);
@@ -156,11 +154,11 @@ static char *handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_
                return CLI_SHOWUSAGE;
 
        if (a->argc == 1) {
-               char arg2[3];
-               char *newargv[2] = { "ulimit", arg2 };
-               for (resource = 0; resource < sizeof(limits) / sizeof(limits[0]); resource++) {
+               char arg2[15];
+               const char * const newargv[2] = { "ulimit", arg2 };
+               for (resource = 0; resource < ARRAY_LEN(limits); resource++) {
                        struct ast_cli_args newArgs = { .argv = newargv, .argc = 2 };
-                       ast_copy_string(arg2, limits[resource].limit, sizeof(arg2));
+                       ast_copy_string(arg2, limits[resource].clicmd, sizeof(arg2));
                        handle_cli_ulimit(e, CLI_HANDLER, &newArgs);
                }
                return CLI_SUCCESS;
@@ -182,7 +180,7 @@ static char *handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_
                                return CLI_FAILURE;
                        }
 
-                       sscanf(a->argv[2], "%d", &x);
+                       sscanf(a->argv[2], "%30d", &x);
                        rlimit.rlim_max = rlimit.rlim_cur = x;
                        setrlimit(resource, &rlimit);
                        return CLI_SUCCESS;
@@ -204,7 +202,7 @@ static char *handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_
 }
 
 static struct ast_cli_entry cli_ulimit =
-       AST_CLI(handle_cli_ulimit, "Set or show process resource limits");
+       AST_CLI_DEFINE(handle_cli_ulimit, "Set or show process resource limits");
 
 static int unload_module(void)
 {