Wed Aug 18 22:34:31 2010

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"

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_DEFAULT , .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 = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_cli_entry cli_ulimit


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 213 of file res_limit.c.

static void __unreg_module ( void   )  [static]

Definition at line 213 of file res_limit.c.

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

Definition at line 84 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().

00085 {
00086    int which = 0, i;
00087    int wordlen = strlen(a->word);
00088 
00089    if (a->pos > 1)
00090       return NULL;
00091    for (i = 0; i < ARRAY_LEN(limits); i++) {
00092       if (!strncasecmp(limits[i].limit, a->word, wordlen)) {
00093          if (++which > a->n)
00094             return ast_strdup(limits[i].limit);
00095       }
00096    }
00097    return NULL;
00098 }

static char* handle_cli_ulimit ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 100 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.

00101 {
00102    int resource;
00103    struct rlimit rlimit = { 0, 0 };
00104 
00105    switch (cmd) {
00106    case CLI_INIT:
00107       e->command = "ulimit";
00108       e->usage =
00109          "Usage: ulimit {-d|"
00110 #ifdef RLIMIT_RSS
00111          "-l|"
00112 #endif
00113          "-f|"
00114 #ifdef RLIMIT_RSS
00115          "-m|"
00116 #endif
00117          "-s|-t|"
00118 #ifdef RLIMIT_NPROC
00119          "-u|"
00120 #endif
00121 #ifdef VMEM_DEF
00122          "-v|"
00123 #endif
00124          "-c|-n} [<num>]\n"
00125          "       Shows or sets the corresponding resource limit.\n"
00126          "         -d  Process data segment [readonly]\n"
00127 #ifdef RLIMIT_RSS
00128          "         -l  Memory lock size [readonly]\n"
00129 #endif
00130          "         -f  File size\n"
00131 #ifdef RLIMIT_RSS
00132          "         -m  Process resident memory [readonly]\n"
00133 #endif
00134          "         -s  Process stack size [readonly]\n"
00135          "         -t  CPU usage [readonly]\n"
00136 #ifdef RLIMIT_NPROC
00137          "         -u  Child processes\n"
00138 #endif
00139 #ifdef VMEM_DEF
00140          "         -v  Process virtual memory [readonly]\n"
00141 #endif
00142          "         -c  Core dump file size\n"
00143          "         -n  Number of file descriptors\n";
00144       return NULL;
00145    case CLI_GENERATE:
00146       return complete_ulimit(a);
00147    }
00148 
00149    if (a->argc > 3)
00150       return CLI_SHOWUSAGE;
00151 
00152    if (a->argc == 1) {
00153       char arg2[3];
00154       char *newargv[2] = { "ulimit", arg2 };
00155       for (resource = 0; resource < ARRAY_LEN(limits); resource++) {
00156          struct ast_cli_args newArgs = { .argv = newargv, .argc = 2 };
00157          ast_copy_string(arg2, limits[resource].limit, sizeof(arg2));
00158          handle_cli_ulimit(e, CLI_HANDLER, &newArgs);
00159       }
00160       return CLI_SUCCESS;
00161    } else {
00162       resource = str2limit(a->argv[1]);
00163       if (resource == -1) {
00164          ast_cli(a->fd, "Unknown resource\n");
00165          return CLI_FAILURE;
00166       }
00167 
00168       if (a->argc == 3) {
00169          int x;
00170 #ifdef RLIMIT_NPROC
00171          if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_NPROC && resource != RLIMIT_FSIZE) {
00172 #else
00173          if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_FSIZE) {
00174 #endif
00175             ast_cli(a->fd, "Resource not permitted to be set\n");
00176             return CLI_FAILURE;
00177          }
00178 
00179          sscanf(a->argv[2], "%30d", &x);
00180          rlimit.rlim_max = rlimit.rlim_cur = x;
00181          setrlimit(resource, &rlimit);
00182          return CLI_SUCCESS;
00183       } else {
00184          if (!getrlimit(resource, &rlimit)) {
00185             char printlimit[32];
00186             const char *desc;
00187             if (rlimit.rlim_max == RLIM_INFINITY)
00188                ast_copy_string(printlimit, "effectively unlimited", sizeof(printlimit));
00189             else
00190                snprintf(printlimit, sizeof(printlimit), "limited to %d", (int) rlimit.rlim_cur);
00191             desc = str2desc(a->argv[1]);
00192             ast_cli(a->fd, "%c%s (%s) is %s.\n", toupper(desc[0]), desc + 1, a->argv[1], printlimit);
00193          } else
00194             ast_cli(a->fd, "Could not retrieve resource limits for %s: %s\n", str2desc(a->argv[1]), strerror(errno));
00195          return CLI_SUCCESS;
00196       }
00197    }
00198 }

static int load_module ( void   )  [static]

Definition at line 208 of file res_limit.c.

References ast_cli_register(), AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, and cli_ulimit.

static const char* str2desc ( const char *  string  )  [static]

Definition at line 74 of file res_limit.c.

References ARRAY_LEN.

Referenced by handle_cli_ulimit().

00075 {
00076    size_t i;
00077    for (i = 0; i < ARRAY_LEN(limits); i++) {
00078       if (!strcmp(string, limits[i].limit))
00079          return limits[i].desc;
00080    }
00081    return "<unknown>";
00082 }

static int str2limit ( const char *  string  )  [static]

Definition at line 64 of file res_limit.c.

References ARRAY_LEN.

Referenced by handle_cli_ulimit().

00065 {
00066    size_t i;
00067    for (i = 0; i < ARRAY_LEN(limits); i++) {
00068       if (!strcasecmp(string, limits[i].limit))
00069          return limits[i].resource;
00070    }
00071    return -1;
00072 }

static int unload_module ( void   )  [static]

Definition at line 203 of file res_limit.c.

References ast_cli_unregister(), and cli_ulimit.

00204 {
00205    return ast_cli_unregister(&cli_ulimit);
00206 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .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 = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, } [static]

Definition at line 213 of file res_limit.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 213 of file res_limit.c.

struct ast_cli_entry cli_ulimit [static]

Initial value:

   { .handler =  handle_cli_ulimit , .summary =  "Set or show process resource limits" ,__VA_ARGS__ }

Definition at line 200 of file res_limit.c.

Referenced by load_module(), and unload_module().


Generated on Wed Aug 18 22:34:31 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7