Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
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"
Include dependency graph for res_limit.c:

Go to the source code of this file.

Data Structures

struct  limits
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_cli_entry cli_ulimit
 
static const struct limits limits []
 

Detailed Description

Resource limits.

Author
Tilghman Lesher res_l.nosp@m.imit.nosp@m._2006.nosp@m.07@t.nosp@m.he-ti.nosp@m.lghm.nosp@m.an.co.nosp@m.m

Definition in file res_limit.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 215 of file res_limit.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 215 of file res_limit.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 215 of file res_limit.c.

◆ complete_ulimit()

static char* complete_ulimit ( struct ast_cli_args a)
static

Definition at line 86 of file res_limit.c.

References ARRAY_LEN, ast_strdup, limits::clicmd, ast_cli_args::n, NULL, ast_cli_args::pos, and ast_cli_args::word.

Referenced by handle_cli_ulimit().

87 {
88  int which = 0, i;
89  int wordlen = strlen(a->word);
90 
91  if (a->pos > 1)
92  return NULL;
93  for (i = 0; i < ARRAY_LEN(limits); i++) {
94  if (!strncasecmp(limits[i].clicmd, a->word, wordlen)) {
95  if (++which > a->n)
96  return ast_strdup(limits[i].clicmd);
97  }
98  }
99  return NULL;
100 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
#define NULL
Definition: resample.c:96
const int n
Definition: cli.h:165
const char * word
Definition: cli.h:163
const int pos
Definition: cli.h:164

◆ handle_cli_ulimit()

static char* handle_cli_ulimit ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 102 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, limits::clicmd, ast_cli_entry::command, complete_ulimit(), limits::desc, errno, ast_cli_args::fd, NULL, limits::resource, str2desc(), str2limit(), and ast_cli_entry::usage.

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

◆ load_module()

static int load_module ( void  )
static

Definition at line 210 of file res_limit.c.

References ast_cli_register, AST_MODULE_LOAD_DECLINE, and AST_MODULE_LOAD_SUCCESS.

211 {
213 }
static struct ast_cli_entry cli_ulimit
Definition: res_limit.c:202
#define ast_cli_register(e)
Registers a command or an array of commands.
Definition: cli.h:256
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78

◆ str2desc()

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

Definition at line 76 of file res_limit.c.

References ARRAY_LEN, limits::clicmd, and limits::desc.

Referenced by handle_cli_ulimit().

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

◆ str2limit()

static int str2limit ( const char *  string)
static

Definition at line 66 of file res_limit.c.

References ARRAY_LEN, limits::clicmd, and limits::resource.

Referenced by handle_cli_ulimit().

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

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 205 of file res_limit.c.

References ast_cli_unregister().

206 {
208 }
int ast_cli_unregister(struct ast_cli_entry *e)
Unregisters a command or an array of commands.
Definition: main/cli.c:2397
static struct ast_cli_entry cli_ulimit
Definition: res_limit.c:202

Variable Documentation

◆ __mod_info

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

Definition at line 215 of file res_limit.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 215 of file res_limit.c.

◆ cli_ulimit

struct ast_cli_entry cli_ulimit
static
Initial value:
=
{ .handler = handle_cli_ulimit , .summary = "Set or show process resource limits" ,}
static char * handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: res_limit.c:102

Definition at line 202 of file res_limit.c.

◆ limits

const struct limits limits[]
static