Mon Mar 19 11:30:29 2012

Asterisk developer's documentation


res_limit.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- A telephony toolkit for Linux.
00003  *
00004  * Resource limits
00005  * 
00006  * Copyright (c) 2006 Tilghman Lesher.  All rights reserved.
00007  *
00008  * Tilghman Lesher <res_limit_200607@the-tilghman.com>
00009  *
00010  * This code is released by the author with no restrictions on usage.
00011  *
00012  */
00013 
00014 /*! \file
00015  *
00016  * \brief Resource limits
00017  *
00018  * \author Tilghman Lesher <res_limit_200607@the-tilghman.com>
00019  */
00020 
00021 /*** MODULEINFO
00022    <support_level>core</support_level>
00023  ***/
00024 
00025 #include "asterisk.h"
00026 
00027 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00028 
00029 #include <ctype.h>
00030 #include <sys/time.h>
00031 #include <sys/resource.h>
00032 #include "asterisk/module.h"
00033 #include "asterisk/cli.h"
00034 
00035 /* Find proper rlimit for virtual memory */
00036 #ifdef RLIMIT_AS
00037 #define VMEM_DEF RLIMIT_AS
00038 #else
00039 #ifdef RLIMIT_VMEM
00040 #define VMEM_DEF RLIMIT_VMEM
00041 #endif
00042 #endif
00043 
00044 static const struct limits {
00045    int resource;
00046    char limit[3];
00047    char desc[40];
00048    char clicmd[15];
00049 } limits[] = {
00050    { RLIMIT_CPU,     "-t", "cpu time", "time" },
00051    { RLIMIT_FSIZE,   "-f", "file size" , "file" },
00052    { RLIMIT_DATA,    "-d", "program data segment", "data" },
00053    { RLIMIT_STACK,   "-s", "program stack size", "stack" },
00054    { RLIMIT_CORE,    "-c", "core file size", "core" },
00055 #ifdef RLIMIT_RSS
00056    { RLIMIT_RSS,     "-m", "resident memory", "memory" },
00057    { RLIMIT_MEMLOCK, "-l", "amount of memory locked into RAM", "locked" },
00058 #endif
00059 #ifdef RLIMIT_NPROC
00060    { RLIMIT_NPROC,   "-u", "number of processes", "processes" },
00061 #endif
00062    { RLIMIT_NOFILE,  "-n", "number of file descriptors", "descriptors" },
00063 #ifdef VMEM_DEF
00064    { VMEM_DEF,       "-v", "virtual memory", "virtual" },
00065 #endif
00066 };
00067 
00068 static int str2limit(const char *string)
00069 {
00070    size_t i;
00071    for (i = 0; i < ARRAY_LEN(limits); i++) {
00072       if (!strcasecmp(string, limits[i].clicmd))
00073          return limits[i].resource;
00074    }
00075    return -1;
00076 }
00077 
00078 static const char *str2desc(const char *string)
00079 {
00080    size_t i;
00081    for (i = 0; i < ARRAY_LEN(limits); i++) {
00082       if (!strcmp(string, limits[i].clicmd))
00083          return limits[i].desc;
00084    }
00085    return "<unknown>";
00086 }
00087 
00088 static char *complete_ulimit(struct ast_cli_args *a)
00089 {
00090    int which = 0, i;
00091    int wordlen = strlen(a->word);
00092 
00093    if (a->pos > 1)
00094       return NULL;
00095    for (i = 0; i < ARRAY_LEN(limits); i++) {
00096       if (!strncasecmp(limits[i].clicmd, a->word, wordlen)) {
00097          if (++which > a->n)
00098             return ast_strdup(limits[i].clicmd);
00099       }
00100    }
00101    return NULL;
00102 }
00103 
00104 static char *handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00105 {
00106    int resource;
00107    struct rlimit rlimit = { 0, 0 };
00108 
00109    switch (cmd) {
00110    case CLI_INIT:
00111       e->command = "ulimit";
00112       e->usage =
00113          "Usage: ulimit {data|"
00114 #ifdef RLIMIT_RSS
00115          "limit|"
00116 #endif
00117          "file|"
00118 #ifdef RLIMIT_RSS
00119          "memory|"
00120 #endif
00121          "stack|time|"
00122 #ifdef RLIMIT_NPROC
00123          "processes|"
00124 #endif
00125 #ifdef VMEM_DEF
00126          "virtual|"
00127 #endif
00128          "core|descriptors} [<num>]\n"
00129          "       Shows or sets the corresponding resource limit.\n"
00130          "         data          Process data segment [readonly]\n"
00131 #ifdef RLIMIT_RSS
00132          "         lock          Memory lock size [readonly]\n"
00133 #endif
00134          "         file          File size\n"
00135 #ifdef RLIMIT_RSS
00136          "         memory        Process resident memory [readonly]\n"
00137 #endif
00138          "         stack         Process stack size [readonly]\n"
00139          "         time          CPU usage [readonly]\n"
00140 #ifdef RLIMIT_NPROC
00141          "         processes     Child processes\n"
00142 #endif
00143 #ifdef VMEM_DEF
00144          "         virtual       Process virtual memory [readonly]\n"
00145 #endif
00146          "         core          Core dump file size\n"
00147          "         descriptors   Number of file descriptors\n";
00148       return NULL;
00149    case CLI_GENERATE:
00150       return complete_ulimit(a);
00151    }
00152 
00153    if (a->argc > 3)
00154       return CLI_SHOWUSAGE;
00155 
00156    if (a->argc == 1) {
00157       char arg2[15];
00158       const char * const newargv[2] = { "ulimit", arg2 };
00159       for (resource = 0; resource < ARRAY_LEN(limits); resource++) {
00160          struct ast_cli_args newArgs = { .argv = newargv, .argc = 2 };
00161          ast_copy_string(arg2, limits[resource].clicmd, sizeof(arg2));
00162          handle_cli_ulimit(e, CLI_HANDLER, &newArgs);
00163       }
00164       return CLI_SUCCESS;
00165    } else {
00166       resource = str2limit(a->argv[1]);
00167       if (resource == -1) {
00168          ast_cli(a->fd, "Unknown resource\n");
00169          return CLI_FAILURE;
00170       }
00171 
00172       if (a->argc == 3) {
00173          int x;
00174 #ifdef RLIMIT_NPROC
00175          if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_NPROC && resource != RLIMIT_FSIZE) {
00176 #else
00177          if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_FSIZE) {
00178 #endif
00179             ast_cli(a->fd, "Resource not permitted to be set\n");
00180             return CLI_FAILURE;
00181          }
00182 
00183          sscanf(a->argv[2], "%30d", &x);
00184          rlimit.rlim_max = rlimit.rlim_cur = x;
00185          setrlimit(resource, &rlimit);
00186          return CLI_SUCCESS;
00187       } else {
00188          if (!getrlimit(resource, &rlimit)) {
00189             char printlimit[32];
00190             const char *desc;
00191             if (rlimit.rlim_max == RLIM_INFINITY)
00192                ast_copy_string(printlimit, "effectively unlimited", sizeof(printlimit));
00193             else
00194                snprintf(printlimit, sizeof(printlimit), "limited to %d", (int) rlimit.rlim_cur);
00195             desc = str2desc(a->argv[1]);
00196             ast_cli(a->fd, "%c%s (%s) is %s.\n", toupper(desc[0]), desc + 1, a->argv[1], printlimit);
00197          } else
00198             ast_cli(a->fd, "Could not retrieve resource limits for %s: %s\n", str2desc(a->argv[1]), strerror(errno));
00199          return CLI_SUCCESS;
00200       }
00201    }
00202 }
00203 
00204 static struct ast_cli_entry cli_ulimit =
00205    AST_CLI_DEFINE(handle_cli_ulimit, "Set or show process resource limits");
00206 
00207 static int unload_module(void)
00208 {
00209    return ast_cli_unregister(&cli_ulimit);
00210 }
00211 
00212 static int load_module(void)
00213 {
00214    return ast_cli_register(&cli_ulimit) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS;
00215 }
00216 
00217 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Resource limits");
00218 

Generated on Mon Mar 19 11:30:29 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7