Merge the cli_cleanup branch.
[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         char clicmd[15];
48 } limits[] = {
49         { RLIMIT_CPU,     "-t", "cpu time", "time" },
50         { RLIMIT_FSIZE,   "-f", "file size" , "file" },
51         { RLIMIT_DATA,    "-d", "program data segment", "data" },
52         { RLIMIT_STACK,   "-s", "program stack size", "stack" },
53         { RLIMIT_CORE,    "-c", "core file size", "core" },
54 #ifdef RLIMIT_RSS
55         { RLIMIT_RSS,     "-m", "resident memory", "memory" },
56         { RLIMIT_MEMLOCK, "-l", "amount of memory locked into RAM", "locked" },
57 #endif
58 #ifdef RLIMIT_NPROC
59         { RLIMIT_NPROC,   "-u", "number of processes", "processes" },
60 #endif
61         { RLIMIT_NOFILE,  "-n", "number of file descriptors", "descriptors" },
62 #ifdef VMEM_DEF
63         { VMEM_DEF,       "-v", "virtual memory", "virtual" },
64 #endif
65 };
66
67 static int str2limit(const char *string)
68 {
69         size_t i;
70         for (i = 0; i < ARRAY_LEN(limits); i++) {
71                 if (!strcasecmp(string, limits[i].clicmd))
72                         return limits[i].resource;
73         }
74         return -1;
75 }
76
77 static const char *str2desc(const char *string)
78 {
79         size_t i;
80         for (i = 0; i < ARRAY_LEN(limits); i++) {
81                 if (!strcmp(string, limits[i].clicmd))
82                         return limits[i].desc;
83         }
84         return "<unknown>";
85 }
86
87 static char *complete_ulimit(struct ast_cli_args *a)
88 {
89         int which = 0, i;
90         int wordlen = strlen(a->word);
91
92         if (a->pos > 1)
93                 return NULL;
94         for (i = 0; i < ARRAY_LEN(limits); i++) {
95                 if (!strncasecmp(limits[i].clicmd, a->word, wordlen)) {
96                         if (++which > a->n)
97                                 return ast_strdup(limits[i].clicmd);
98                 }
99         }
100         return NULL;
101 }
102
103 static char *handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
104 {
105         int resource;
106         struct rlimit rlimit = { 0, 0 };
107
108         switch (cmd) {
109         case CLI_INIT:
110                 e->command = "ulimit";
111                 e->usage =
112                         "Usage: ulimit {data|"
113 #ifdef RLIMIT_RSS
114                         "limit|"
115 #endif
116                         "file|"
117 #ifdef RLIMIT_RSS
118                         "memory|"
119 #endif
120                         "stack|time|"
121 #ifdef RLIMIT_NPROC
122                         "processes|"
123 #endif
124 #ifdef VMEM_DEF
125                         "virtual|"
126 #endif
127                         "core|descriptors} [<num>]\n"
128                         "       Shows or sets the corresponding resource limit.\n"
129                         "         data          Process data segment [readonly]\n"
130 #ifdef RLIMIT_RSS
131                         "         lock          Memory lock size [readonly]\n"
132 #endif
133                         "         file          File size\n"
134 #ifdef RLIMIT_RSS
135                         "         memory        Process resident memory [readonly]\n"
136 #endif
137                         "         stack         Process stack size [readonly]\n"
138                         "         time          CPU usage [readonly]\n"
139 #ifdef RLIMIT_NPROC
140                         "         processes     Child processes\n"
141 #endif
142 #ifdef VMEM_DEF
143                         "         virtual       Process virtual memory [readonly]\n"
144 #endif
145                         "         core          Core dump file size\n"
146                         "         descriptors   Number of file descriptors\n";
147                 return NULL;
148         case CLI_GENERATE:
149                 return complete_ulimit(a);
150         }
151
152         if (a->argc > 3)
153                 return CLI_SHOWUSAGE;
154
155         if (a->argc == 1) {
156                 char arg2[15];
157                 char *newargv[2] = { "ulimit", arg2 };
158                 for (resource = 0; resource < ARRAY_LEN(limits); resource++) {
159                         struct ast_cli_args newArgs = { .argv = newargv, .argc = 2 };
160                         ast_copy_string(arg2, limits[resource].clicmd, sizeof(arg2));
161                         handle_cli_ulimit(e, CLI_HANDLER, &newArgs);
162                 }
163                 return CLI_SUCCESS;
164         } else {
165                 resource = str2limit(a->argv[1]);
166                 if (resource == -1) {
167                         ast_cli(a->fd, "Unknown resource\n");
168                         return CLI_FAILURE;
169                 }
170
171                 if (a->argc == 3) {
172                         int x;
173 #ifdef RLIMIT_NPROC
174                         if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_NPROC && resource != RLIMIT_FSIZE) {
175 #else
176                         if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_FSIZE) {
177 #endif
178                                 ast_cli(a->fd, "Resource not permitted to be set\n");
179                                 return CLI_FAILURE;
180                         }
181
182                         sscanf(a->argv[2], "%d", &x);
183                         rlimit.rlim_max = rlimit.rlim_cur = x;
184                         setrlimit(resource, &rlimit);
185                         return CLI_SUCCESS;
186                 } else {
187                         if (!getrlimit(resource, &rlimit)) {
188                                 char printlimit[32];
189                                 const char *desc;
190                                 if (rlimit.rlim_max == RLIM_INFINITY)
191                                         ast_copy_string(printlimit, "effectively unlimited", sizeof(printlimit));
192                                 else
193                                         snprintf(printlimit, sizeof(printlimit), "limited to %d", (int) rlimit.rlim_cur);
194                                 desc = str2desc(a->argv[1]);
195                                 ast_cli(a->fd, "%c%s (%s) is %s.\n", toupper(desc[0]), desc + 1, a->argv[1], printlimit);
196                         } else
197                                 ast_cli(a->fd, "Could not retrieve resource limits for %s: %s\n", str2desc(a->argv[1]), strerror(errno));
198                         return CLI_SUCCESS;
199                 }
200         }
201 }
202
203 static struct ast_cli_entry cli_ulimit =
204         AST_CLI_DEFINE(handle_cli_ulimit, "Set or show process resource limits");
205
206 static int unload_module(void)
207 {
208         return ast_cli_unregister(&cli_ulimit);
209 }
210
211 static int load_module(void)
212 {
213         return ast_cli_register(&cli_ulimit) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS;
214 }
215
216 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Resource limits");
217