Thu Apr 28 2011 16:57:17

Asterisk developer's documentation


res_limit.c File Reference

Resource limits. More...

#include "asterisk.h"
#include <ctype.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "asterisk/module.h"
#include "asterisk/cli.h"
Include dependency graph for res_limit.c:

Go to the source code of this file.

Data Structures

struct  limits

Functions

static void __reg_module (void)
static void __unreg_module (void)
static char * complete_ulimit (struct ast_cli_args *a)
static char * handle_cli_ulimit (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int load_module (void)
static const char * str2desc (const char *string)
static int str2limit (const char *string)
static int unload_module (void)

Variables

static struct ast_module_info
__MODULE_INFO_SECTION 
__mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Resource limits" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_cli_entry cli_ulimit
static struct limits limits []

Detailed Description

Resource limits.

Author:
Tilghman Lesher <res_limit_200607@the-tilghman.com>

Definition in file res_limit.c.


Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 214 of file res_limit.c.

static void __unreg_module ( void  ) [static]

Definition at line 214 of file res_limit.c.

static char* complete_ulimit ( struct ast_cli_args a) [static]

Definition at line 85 of file res_limit.c.

References ARRAY_LEN, ast_strdup, ast_cli_args::n, ast_cli_args::pos, and ast_cli_args::word.

Referenced by handle_cli_ulimit().

{
   int which = 0, i;
   int wordlen = strlen(a->word);

   if (a->pos > 1)
      return NULL;
   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].clicmd);
      }
   }
   return NULL;
}
static char* handle_cli_ulimit ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 101 of file res_limit.c.

References ast_cli_args::argc, ast_cli_args::argv, ARRAY_LEN, ast_cli(), ast_copy_string(), CLI_FAILURE, CLI_GENERATE, CLI_HANDLER, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, complete_ulimit(), desc, errno, ast_cli_args::fd, str2desc(), str2limit(), and ast_cli_entry::usage.

{
   int resource;
   struct rlimit rlimit = { 0, 0 };

   switch (cmd) {
   case CLI_INIT:
      e->command = "ulimit";
      e->usage =
         "Usage: ulimit {data|"
#ifdef RLIMIT_RSS
         "limit|"
#endif
         "file|"
#ifdef RLIMIT_RSS
         "memory|"
#endif
         "stack|time|"
#ifdef RLIMIT_NPROC
         "processes|"
#endif
#ifdef VMEM_DEF
         "virtual|"
#endif
         "core|descriptors} [<num>]\n"
         "       Shows or sets the corresponding resource limit.\n"
         "         data          Process data segment [readonly]\n"
#ifdef RLIMIT_RSS
         "         lock          Memory lock size [readonly]\n"
#endif
         "         file          File size\n"
#ifdef RLIMIT_RSS
         "         memory        Process resident memory [readonly]\n"
#endif
         "         stack         Process stack size [readonly]\n"
         "         time          CPU usage [readonly]\n"
#ifdef RLIMIT_NPROC
         "         processes     Child processes\n"
#endif
#ifdef VMEM_DEF
         "         virtual       Process virtual memory [readonly]\n"
#endif
         "         core          Core dump file size\n"
         "         descriptors   Number of file descriptors\n";
      return NULL;
   case CLI_GENERATE:
      return complete_ulimit(a);
   }

   if (a->argc > 3)
      return CLI_SHOWUSAGE;

   if (a->argc == 1) {
      char arg2[15];
      char *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].clicmd, sizeof(arg2));
         handle_cli_ulimit(e, CLI_HANDLER, &newArgs);
      }
      return CLI_SUCCESS;
   } else {
      resource = str2limit(a->argv[1]);
      if (resource == -1) {
         ast_cli(a->fd, "Unknown resource\n");
         return CLI_FAILURE;
      }

      if (a->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(a->fd, "Resource not permitted to be set\n");
            return CLI_FAILURE;
         }

         sscanf(a->argv[2], "%30d", &x);
         rlimit.rlim_max = rlimit.rlim_cur = x;
         setrlimit(resource, &rlimit);
         return CLI_SUCCESS;
      } else {
         if (!getrlimit(resource, &rlimit)) {
            char printlimit[32];
            const char *desc;
            if (rlimit.rlim_max == RLIM_INFINITY)
               ast_copy_string(printlimit, "effectively unlimited", sizeof(printlimit));
            else
               snprintf(printlimit, sizeof(printlimit), "limited to %d", (int) rlimit.rlim_cur);
            desc = str2desc(a->argv[1]);
            ast_cli(a->fd, "%c%s (%s) is %s.\n", toupper(desc[0]), desc + 1, a->argv[1], printlimit);
         } else
            ast_cli(a->fd, "Could not retrieve resource limits for %s: %s\n", str2desc(a->argv[1]), strerror(errno));
         return CLI_SUCCESS;
      }
   }
}
static int load_module ( void  ) [static]
static const char* str2desc ( const char *  string) [static]

Definition at line 75 of file res_limit.c.

References ARRAY_LEN, and desc.

Referenced by handle_cli_ulimit().

{
   size_t i;
   for (i = 0; i < ARRAY_LEN(limits); i++) {
      if (!strcmp(string, limits[i].clicmd))
         return limits[i].desc;
   }
   return "<unknown>";
}
static int str2limit ( const char *  string) [static]

Definition at line 65 of file res_limit.c.

References ARRAY_LEN.

Referenced by handle_cli_ulimit().

{
   size_t i;
   for (i = 0; i < ARRAY_LEN(limits); i++) {
      if (!strcasecmp(string, limits[i].clicmd))
         return limits[i].resource;
   }
   return -1;
}
static int unload_module ( void  ) [static]

Definition at line 204 of file res_limit.c.

References ast_cli_unregister().


Variable Documentation

struct ast_module_info __MODULE_INFO_SECTION __mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Resource limits" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 214 of file res_limit.c.

Definition at line 214 of file res_limit.c.

struct ast_cli_entry cli_ulimit [static]
Initial value:
   AST_CLI_DEFINE(handle_cli_ulimit, "Set or show process resource limits")

Definition at line 201 of file res_limit.c.

struct limits limits[] [static]