Wed Apr 6 11:29:47 2011

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 
00022 #include "asterisk.h"
00023 
00024 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 217033 $")
00025 
00026 #include <ctype.h>
00027 #include <sys/time.h>
00028 #include <sys/resource.h>
00029 #include "asterisk/module.h"
00030 #include "asterisk/cli.h"
00031 
00032 /* Find proper rlimit for virtual memory */
00033 #ifdef RLIMIT_AS
00034 #define VMEM_DEF RLIMIT_AS
00035 #else
00036 #ifdef RLIMIT_VMEM
00037 #define VMEM_DEF RLIMIT_VMEM
00038 #endif
00039 #endif
00040 
00041 static const struct limits {
00042    int resource;
00043    char limit[3];
00044    char desc[40];
00045    char clicmd[15];
00046 } limits[] = {
00047    { RLIMIT_CPU,     "-t", "cpu time", "time" },
00048    { RLIMIT_FSIZE,   "-f", "file size" , "file" },
00049    { RLIMIT_DATA,    "-d", "program data segment", "data" },
00050    { RLIMIT_STACK,   "-s", "program stack size", "stack" },
00051    { RLIMIT_CORE,    "-c", "core file size", "core" },
00052 #ifdef RLIMIT_RSS
00053    { RLIMIT_RSS,     "-m", "resident memory", "memory" },
00054    { RLIMIT_MEMLOCK, "-l", "amount of memory locked into RAM", "locked" },
00055 #endif
00056 #ifdef RLIMIT_NPROC
00057    { RLIMIT_NPROC,   "-u", "number of processes", "processes" },
00058 #endif
00059    { RLIMIT_NOFILE,  "-n", "number of file descriptors", "descriptors" },
00060 #ifdef VMEM_DEF
00061    { VMEM_DEF,       "-v", "virtual memory", "virtual" },
00062 #endif
00063 };
00064 
00065 static int str2limit(const char *string)
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 }
00074 
00075 static const char *str2desc(const char *string)
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 }
00084 
00085 static char *complete_ulimit(struct ast_cli_args *a)
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 }
00100 
00101 static char *handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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 }
00200 
00201 static struct ast_cli_entry cli_ulimit =
00202    AST_CLI_DEFINE(handle_cli_ulimit, "Set or show process resource limits");
00203 
00204 static int unload_module(void)
00205 {
00206    return ast_cli_unregister(&cli_ulimit);
00207 }
00208 
00209 static int load_module(void)
00210 {
00211    return ast_cli_register(&cli_ulimit) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS;
00212 }
00213 
00214 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Resource limits");
00215 

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