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