Mon Mar 12 2012 21:44:41

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 __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Resource limits" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
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 217 of file res_limit.c.

static void __unreg_module ( void  ) [static]

Definition at line 217 of file res_limit.c.

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

Definition at line 88 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 104 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];
      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].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 78 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 68 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 207 of file res_limit.c.

References ast_cli_unregister().


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Resource limits" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 217 of file res_limit.c.

Definition at line 217 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 204 of file res_limit.c.

struct limits limits[] [static]