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