29 #include <sys/resource.h>
35 #define VMEM_DEF RLIMIT_AS
38 #define VMEM_DEF RLIMIT_VMEM
48 { RLIMIT_CPU,
"-t",
"cpu time",
"time" },
49 { RLIMIT_FSIZE,
"-f",
"file size" ,
"file" },
50 { RLIMIT_DATA,
"-d",
"program data segment",
"data" },
51 { RLIMIT_STACK,
"-s",
"program stack size",
"stack" },
52 { RLIMIT_CORE,
"-c",
"core file size",
"core" },
54 { RLIMIT_RSS,
"-m",
"resident memory",
"memory" },
55 { RLIMIT_MEMLOCK,
"-l",
"amount of memory locked into RAM",
"locked" },
58 { RLIMIT_NPROC,
"-u",
"number of processes",
"processes" },
60 { RLIMIT_NOFILE,
"-n",
"number of file descriptors",
"descriptors" },
62 { VMEM_DEF,
"-v",
"virtual memory",
"virtual" },
66 static int str2limit(
const char *
string)
69 for (i = 0; i < ARRAY_LEN(
limits); i++) {
70 if (!strcasecmp(
string,
limits[i].clicmd))
76 static const char *str2desc(
const char *
string)
79 for (i = 0; i < ARRAY_LEN(
limits); i++) {
80 if (!strcmp(
string,
limits[i].clicmd))
89 int wordlen = strlen(a->word);
93 for (i = 0; i < ARRAY_LEN(
limits); i++) {
94 if (!strncasecmp(
limits[i].clicmd, a->word, wordlen)) {
105 struct rlimit rlimit = { 0, 0 };
111 "Usage: ulimit {data|"
126 "core|descriptors} [<num>]\n"
127 " Shows or sets the corresponding resource limit.\n"
128 " data Process data segment [readonly]\n"
130 " lock Memory lock size [readonly]\n"
134 " memory Process resident memory [readonly]\n"
136 " stack Process stack size [readonly]\n"
137 " time CPU usage [readonly]\n"
139 " processes Child processes\n"
142 " virtual Process virtual memory [readonly]\n"
144 " core Core dump file size\n"
145 " descriptors Number of file descriptors\n";
148 return complete_ulimit(a);
152 return CLI_SHOWUSAGE;
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 };
160 handle_cli_ulimit(e, CLI_HANDLER, &newArgs);
164 resource = str2limit(a->argv[1]);
165 if (resource == -1) {
166 ast_cli(a->fd,
"Unknown resource\n");
173 if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_NPROC && resource != RLIMIT_FSIZE) {
175 if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_FSIZE) {
177 ast_cli(a->fd,
"Resource not permitted to be set\n");
181 sscanf(a->argv[2],
"%30d", &x);
182 rlimit.rlim_max = rlimit.rlim_cur = x;
183 setrlimit(resource, &rlimit);
186 if (!getrlimit(resource, &rlimit)) {
189 if (rlimit.rlim_max == RLIM_INFINITY)
190 ast_copy_string(printlimit,
"effectively unlimited",
sizeof(printlimit));
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);
196 ast_cli(a->fd,
"Could not retrieve resource limits for %s: %s\n", str2desc(a->argv[1]), strerror(errno));
203 AST_CLI_DEFINE(handle_cli_ulimit,
"Set or show process resource limits");
205 static int unload_module(
void)
210 static int load_module(
void)
Asterisk main include file. File version handling, generic pbx functions.
int ast_cli_unregister(struct ast_cli_entry *e)
Unregisters a command or an array of commands.
descriptor for a cli entry.
#define ast_strdup(str)
A wrapper for strdup()
#define ast_cli_register(e)
Registers a command or an array of commands.
Module has failed to load, may be in an inconsistent state.
Standard Command Line Interface.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.