b1cf025b0c77e80dce4b095e2b8cad76a851859e
[asterisk/asterisk.git] / res / res_limit.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Resource limits
5  * 
6  * Copyright (c) 2006 Tilghman Lesher.  All rights reserved.
7  *
8  * Tilghman Lesher <res_limit_200607@the-tilghman.com>
9  *
10  * This code is released by the author with no restrictions on usage.
11  *
12  */
13
14 /*! \file
15  *
16  * \brief Resource limits
17  *
18  * \author Tilghman Lesher <res_limit_200607@the-tilghman.com>
19  */
20
21
22 #include "asterisk.h"
23
24 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
25
26 #define _XOPEN_SOURCE 600
27
28 #include <ctype.h>
29 #include <sys/time.h>
30 #include <sys/resource.h>
31 #include "asterisk/module.h"
32 #include "asterisk/cli.h"
33
34 /* Find proper rlimit for virtual memory */
35 #ifdef RLIMIT_AS
36 #define VMEM_DEF RLIMIT_AS
37 #else
38 #ifdef RLIMIT_VMEM
39 #define VMEM_DEF RLIMIT_VMEM
40 #endif
41 #endif
42
43 static struct limits {
44         int resource;
45         char limit[3];
46         char desc[40];
47 } limits[] = {
48         { RLIMIT_CPU,     "-t", "cpu time" },
49         { RLIMIT_FSIZE,   "-f", "file size" },
50         { RLIMIT_DATA,    "-d", "program data segment" },
51         { RLIMIT_STACK,   "-s", "program stack size" },
52         { RLIMIT_CORE,    "-c", "core file size" },
53 #ifdef RLIMIT_RSS
54         { RLIMIT_RSS,     "-m", "resident memory" },
55         { RLIMIT_MEMLOCK, "-l", "amount of memory locked into RAM" },
56 #endif
57 #ifdef RLIMIT_NPROC
58         { RLIMIT_NPROC,   "-u", "number of processes" },
59 #endif
60         { RLIMIT_NOFILE,  "-n", "number of file descriptors" },
61 #ifdef VMEM_DEF
62         { VMEM_DEF,       "-v", "virtual memory" },
63 #endif
64 };
65
66 static int str2limit(const char *string)
67 {
68         size_t i;
69         for (i = 0; i < ARRAY_LEN(limits); i++) {
70                 if (!strcasecmp(string, limits[i].limit))
71                         return limits[i].resource;
72         }
73         return -1;
74 }
75
76 static const char *str2desc(const char *string)
77 {
78         size_t i;
79         for (i = 0; i < ARRAY_LEN(limits); i++) {
80                 if (!strcmp(string, limits[i].limit))
81                         return limits[i].desc;
82         }
83         return "<unknown>";
84 }
85
86 static char *complete_ulimit(struct ast_cli_args *a)
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].limit, a->word, wordlen)) {
95                         if (++which > a->n)
96                                 return ast_strdup(limits[i].limit);
97                 }
98         }
99         return NULL;
100 }
101
102 static char *handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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 {-d|"
112 #ifdef RLIMIT_RSS
113                         "-l|"
114 #endif
115                         "-f|"
116 #ifdef RLIMIT_RSS
117                         "-m|"
118 #endif
119                         "-s|-t|"
120 #ifdef RLIMIT_NPROC
121                         "-u|"
122 #endif
123 #ifdef VMEM_DEF
124                         "-v|"
125 #endif
126                         "-c|-n} [<num>]\n"
127                         "       Shows or sets the corresponding resource limit.\n"
128                         "         -d  Process data segment [readonly]\n"
129 #ifdef RLIMIT_RSS
130                         "         -l  Memory lock size [readonly]\n"
131 #endif
132                         "         -f  File size\n"
133 #ifdef RLIMIT_RSS
134                         "         -m  Process resident memory [readonly]\n"
135 #endif
136                         "         -s  Process stack size [readonly]\n"
137                         "         -t  CPU usage [readonly]\n"
138 #ifdef RLIMIT_NPROC
139                         "         -u  Child processes\n"
140 #endif
141 #ifdef VMEM_DEF
142                         "         -v  Process virtual memory [readonly]\n"
143 #endif
144                         "         -c  Core dump file size\n"
145                         "         -n  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[3];
156                 char *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].limit, 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], "%d", &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 }
201
202 static struct ast_cli_entry cli_ulimit =
203         AST_CLI_DEFINE(handle_cli_ulimit, "Set or show process resource limits");
204
205 static int unload_module(void)
206 {
207         return ast_cli_unregister(&cli_ulimit);
208 }
209
210 static int load_module(void)
211 {
212         return ast_cli_register(&cli_ulimit) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS;
213 }
214
215 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Resource limits");
216