Wed Jan 8 2020 09:50:19

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 = "ac1f6a56484a8820659555499174e588" , .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
 
static struct limits limits []
 

Detailed Description

Function Documentation

static void __reg_module ( void  )
static

Definition at line 217 of file res_limit.c.

static void __unreg_module ( void  )
static

Definition at line 217 of file res_limit.c.

static char* complete_ulimit ( struct ast_cli_args a)
static

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

89 {
90  int which = 0, i;
91  int wordlen = strlen(a->word);
92 
93  if (a->pos > 1)
94  return NULL;
95  for (i = 0; i < ARRAY_LEN(limits); i++) {
96  if (!strncasecmp(limits[i].clicmd, a->word, wordlen)) {
97  if (++which > a->n)
98  return ast_strdup(limits[i].clicmd);
99  }
100  }
101  return NULL;
102 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define ast_strdup(a)
Definition: astmm.h:109
const int n
Definition: cli.h:159
const char * word
Definition: cli.h:157
const int pos
Definition: cli.h:158
static char* handle_cli_ulimit ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

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

105 {
106  int resource;
107  struct rlimit rlimit = { 0, 0 };
108 
109  switch (cmd) {
110  case CLI_INIT:
111  e->command = "ulimit";
112  e->usage =
113  "Usage: ulimit {data|"
114 #ifdef RLIMIT_RSS
115  "limit|"
116 #endif
117  "file|"
118 #ifdef RLIMIT_RSS
119  "memory|"
120 #endif
121  "stack|time|"
122 #ifdef RLIMIT_NPROC
123  "processes|"
124 #endif
125 #ifdef VMEM_DEF
126  "virtual|"
127 #endif
128  "core|descriptors} [<num>]\n"
129  " Shows or sets the corresponding resource limit.\n"
130  " data Process data segment [readonly]\n"
131 #ifdef RLIMIT_RSS
132  " lock Memory lock size [readonly]\n"
133 #endif
134  " file File size\n"
135 #ifdef RLIMIT_RSS
136  " memory Process resident memory [readonly]\n"
137 #endif
138  " stack Process stack size [readonly]\n"
139  " time CPU usage [readonly]\n"
140 #ifdef RLIMIT_NPROC
141  " processes Child processes\n"
142 #endif
143 #ifdef VMEM_DEF
144  " virtual Process virtual memory [readonly]\n"
145 #endif
146  " core Core dump file size\n"
147  " descriptors Number of file descriptors\n";
148  return NULL;
149  case CLI_GENERATE:
150  return complete_ulimit(a);
151  }
152 
153  if (a->argc > 3)
154  return CLI_SHOWUSAGE;
155 
156  if (a->argc == 1) {
157  char arg2[15];
158  const char * const newargv[2] = { "ulimit", arg2 };
159  for (resource = 0; resource < ARRAY_LEN(limits); resource++) {
160  struct ast_cli_args newArgs = { .argv = newargv, .argc = 2 };
161  ast_copy_string(arg2, limits[resource].clicmd, sizeof(arg2));
162  handle_cli_ulimit(e, CLI_HANDLER, &newArgs);
163  }
164  return CLI_SUCCESS;
165  } else {
166  resource = str2limit(a->argv[1]);
167  if (resource == -1) {
168  ast_cli(a->fd, "Unknown resource\n");
169  return CLI_FAILURE;
170  }
171 
172  if (a->argc == 3) {
173  int x;
174 #ifdef RLIMIT_NPROC
175  if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_NPROC && resource != RLIMIT_FSIZE) {
176 #else
177  if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_FSIZE) {
178 #endif
179  ast_cli(a->fd, "Resource not permitted to be set\n");
180  return CLI_FAILURE;
181  }
182 
183  sscanf(a->argv[2], "%30d", &x);
184  rlimit.rlim_max = rlimit.rlim_cur = x;
185  setrlimit(resource, &rlimit);
186  return CLI_SUCCESS;
187  } else {
188  if (!getrlimit(resource, &rlimit)) {
189  char printlimit[32];
190  const char *desc;
191  if (rlimit.rlim_max == RLIM_INFINITY)
192  ast_copy_string(printlimit, "effectively unlimited", sizeof(printlimit));
193  else
194  snprintf(printlimit, sizeof(printlimit), "limited to %d", (int) rlimit.rlim_cur);
195  desc = str2desc(a->argv[1]);
196  ast_cli(a->fd, "%c%s (%s) is %s.\n", toupper(desc[0]), desc + 1, a->argv[1], printlimit);
197  } else
198  ast_cli(a->fd, "Could not retrieve resource limits for %s: %s\n", str2desc(a->argv[1]), strerror(errno));
199  return CLI_SUCCESS;
200  }
201  }
202 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
const int argc
Definition: cli.h:154
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
static const char * str2desc(const char *string)
Definition: res_limit.c:78
static int str2limit(const char *string)
Definition: res_limit.c:68
const int fd
Definition: cli.h:153
static char * handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: res_limit.c:104
const char *const * argv
Definition: cli.h:155
static const char desc[]
Definition: cdr_radius.c:85
#define CLI_SHOWUSAGE
Definition: cli.h:44
#define CLI_FAILURE
Definition: cli.h:45
int errno
char * command
Definition: cli.h:180
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
static char * complete_ulimit(struct ast_cli_args *a)
Definition: res_limit.c:88
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
static int load_module ( void  )
static

Definition at line 212 of file res_limit.c.

References ast_cli_register(), AST_MODULE_LOAD_FAILURE, and AST_MODULE_LOAD_SUCCESS.

213 {
215 }
int ast_cli_register(struct ast_cli_entry *e)
Registers a command or an array of commands.
Definition: cli.c:2159
static struct ast_cli_entry cli_ulimit
Definition: res_limit.c:204
static const char* str2desc ( const char *  string)
static

Definition at line 78 of file res_limit.c.

References ARRAY_LEN, and desc.

Referenced by handle_cli_ulimit().

79 {
80  size_t i;
81  for (i = 0; i < ARRAY_LEN(limits); i++) {
82  if (!strcmp(string, limits[i].clicmd))
83  return limits[i].desc;
84  }
85  return "<unknown>";
86 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static const char desc[]
Definition: cdr_radius.c:85
static int str2limit ( const char *  string)
static

Definition at line 68 of file res_limit.c.

References ARRAY_LEN.

Referenced by handle_cli_ulimit().

69 {
70  size_t i;
71  for (i = 0; i < ARRAY_LEN(limits); i++) {
72  if (!strcasecmp(string, limits[i].clicmd))
73  return limits[i].resource;
74  }
75  return -1;
76 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static int unload_module ( void  )
static

Definition at line 207 of file res_limit.c.

References ast_cli_unregister().

208 {
210 }
int ast_cli_unregister(struct ast_cli_entry *e)
Unregisters a command or an array of commands.
Definition: cli.c:2153
static struct ast_cli_entry cli_ulimit
Definition: res_limit.c:204

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

Definition at line 217 of file res_limit.c.

Definition at line 217 of file res_limit.c.

struct ast_cli_entry cli_ulimit
static
Initial value:
=
AST_CLI_DEFINE(handle_cli_ulimit, "Set or show process resource limits")
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
static char * handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: res_limit.c:104

Definition at line 204 of file res_limit.c.

struct limits limits[]
static