Wed Apr 6 11:30:09 2011

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_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 = "8586c2a7d357cb591cc3a6607a8f62d1" , .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


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().

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

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.

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

static int load_module ( void   )  [static]

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

References ARRAY_LEN.

Referenced by handle_cli_ulimit().

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

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

Definition at line 65 of file res_limit.c.

References ARRAY_LEN.

Referenced by handle_cli_ulimit().

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

static int unload_module ( void   )  [static]

Definition at line 204 of file res_limit.c.

References ast_cli_unregister(), and cli_ulimit.

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


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 = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 214 of file res_limit.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

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

Referenced by load_module(), and unload_module().


Generated on Wed Apr 6 11:30:09 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7