Fri Aug 24 02:28:06 2007

Asterisk developer's documentation


res_limit.c File Reference

#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.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

Defines

#define _XOPEN_SOURCE   600
#define RLIMIT_AS   RLIMIT_VMEM

Functions

 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Resource limits")
static char * complete_ulimit (const char *line, const char *word, int pos, int state)
static int load_module (void)
static int my_ulimit (int fd, int argc, char **argv)
static const char * str2desc (const char *string)
static int str2limit (const char *string)
static int unload_module (void)

Variables

static struct ast_cli_entry cli_ulimit
static char ulimit_usage []


Define Documentation

#define _XOPEN_SOURCE   600

Definition at line 19 of file res_limit.c.

#define RLIMIT_AS   RLIMIT_VMEM


Function Documentation

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Resource limits  
)

static char* complete_ulimit ( const char *  line,
const char *  word,
int  pos,
int  state 
) [static]

Definition at line 122 of file res_limit.c.

References ast_strdup.

00123 {
00124    int which = 0, i;
00125    int wordlen = strlen(word);
00126 
00127    if (pos > 2)
00128       return NULL;
00129    for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
00130       if (!strncasecmp(limits[i].limit, word, wordlen)) {
00131          if (++which > state)
00132             return ast_strdup(limits[i].limit);
00133       }
00134    }
00135    return NULL;
00136 }

static int load_module ( void   )  [static]

Definition at line 159 of file res_limit.c.

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

static int my_ulimit ( int  fd,
int  argc,
char **  argv 
) [static]

Definition at line 73 of file res_limit.c.

References ast_cli(), desc, RESULT_FAILURE, RESULT_SHOWUSAGE, RESULT_SUCCESS, str2desc(), and str2limit().

00074 {
00075    int resource;
00076    struct rlimit rlimit = { 0, 0 };
00077    if (argc > 3)
00078       return RESULT_SHOWUSAGE;
00079 
00080    if (argc == 1) {
00081       char arg2[3];
00082       char *newargv[2] = { "ulimit", arg2 };
00083       for (resource = 0; resource < sizeof(limits) / sizeof(limits[0]); resource++) {
00084          ast_copy_string(arg2, limits[resource].limit, sizeof(arg2));
00085          my_ulimit(fd, 2, newargv);
00086       }
00087       return RESULT_SUCCESS;
00088    } else {
00089       resource = str2limit(argv[1]);
00090       if (resource == -1) {
00091          ast_cli(fd, "Unknown resource\n");
00092          return RESULT_FAILURE;
00093       }
00094 
00095       if (argc == 3) {
00096          if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_NPROC && resource != RLIMIT_FSIZE) {
00097             ast_cli(fd, "Resource not permitted to be set\n");
00098             return RESULT_FAILURE;
00099          }
00100 
00101          sscanf(argv[2], "%d", (int *) &rlimit.rlim_cur);
00102          rlimit.rlim_max = rlimit.rlim_cur;
00103          setrlimit(resource, &rlimit);
00104          return RESULT_SUCCESS;
00105       } else {
00106          if (!getrlimit(resource, &rlimit)) {
00107             char printlimit[32];
00108             const char *desc;
00109             if (rlimit.rlim_max == RLIM_INFINITY)
00110                ast_copy_string(printlimit, "effectively unlimited", sizeof(printlimit));
00111             else
00112                snprintf(printlimit, sizeof(printlimit), "limited to %d", (int) rlimit.rlim_cur);
00113             desc = str2desc(argv[1]);
00114             ast_cli(fd, "%c%s (%s) is %s.\n", toupper(desc[0]), desc + 1, argv[1], printlimit);
00115          } else
00116             ast_cli(fd, "Could not retrieve resource limits for %s: %s\n", str2desc(argv[1]), strerror(errno));
00117          return RESULT_SUCCESS;
00118       }
00119    }
00120 }

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

Definition at line 63 of file res_limit.c.

References desc.

Referenced by my_ulimit().

00064 {
00065    size_t i;
00066    for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
00067       if (!strcmp(string, limits[i].limit))
00068          return limits[i].desc;
00069    }
00070    return "<unknown>";
00071 }

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

Definition at line 53 of file res_limit.c.

Referenced by my_ulimit().

00054 {
00055    size_t i;
00056    for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
00057       if (!strcasecmp(string, limits[i].limit))
00058          return limits[i].resource;
00059    }
00060    return -1;
00061 }

static int unload_module ( void   )  [static]

Definition at line 154 of file res_limit.c.

References ast_cli_unregister(), and cli_ulimit.

00155 {
00156    return ast_cli_unregister(&cli_ulimit);
00157 }


Variable Documentation

struct ast_cli_entry cli_ulimit [static]

Initial value:

 {
   {"ulimit", NULL}, my_ulimit,
   "Set or show process resource limits", ulimit_usage, complete_ulimit
}

Definition at line 149 of file res_limit.c.

Referenced by load_module(), and unload_module().

char ulimit_usage[] [static]

Definition at line 138 of file res_limit.c.


Generated on Fri Aug 24 02:28:07 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1