Fix up tree so that it compiles when MTX Profiling is enabled.
[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 int my_ulimit(int fd, int argc, char **argv)
91 {
92         int resource;
93         struct rlimit rlimit = { 0, 0 };
94         if (argc > 3)
95                 return RESULT_SHOWUSAGE;
96
97         if (argc == 1) {
98                 char arg2[3];
99                 char *newargv[2] = { "ulimit", arg2 };
100                 for (resource = 0; resource < sizeof(limits) / sizeof(limits[0]); resource++) {
101                         ast_copy_string(arg2, limits[resource].limit, sizeof(arg2));
102                         my_ulimit(fd, 2, newargv);
103                 }
104                 return RESULT_SUCCESS;
105         } else {
106                 resource = str2limit(argv[1]);
107                 if (resource == -1) {
108                         ast_cli(fd, "Unknown resource\n");
109                         return RESULT_FAILURE;
110                 }
111
112                 if (argc == 3) {
113                         int x;
114 #ifdef RLIMIT_NPROC
115                         if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_NPROC && resource != RLIMIT_FSIZE) {
116 #else
117                           if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_FSIZE) {
118 #endif
119                                 ast_cli(fd, "Resource not permitted to be set\n");
120                                 return RESULT_FAILURE;
121                         }
122
123                         sscanf(argv[2], "%d", &x);
124                         rlimit.rlim_max = rlimit.rlim_cur = x;
125                         setrlimit(resource, &rlimit);
126                         return RESULT_SUCCESS;
127                 } else {
128                         if (!getrlimit(resource, &rlimit)) {
129                                 char printlimit[32];
130                                 const char *desc;
131                                 if (rlimit.rlim_max == RLIM_INFINITY)
132                                         ast_copy_string(printlimit, "effectively unlimited", sizeof(printlimit));
133                                 else
134                                         snprintf(printlimit, sizeof(printlimit), "limited to %d", (int)rlimit.rlim_cur);
135                                 desc = str2desc(argv[1]);
136                                 ast_cli(fd, "%c%s (%s) is %s.\n", toupper(desc[0]), desc + 1, argv[1], printlimit);
137                         } else
138                                 ast_cli(fd, "Could not retrieve resource limits for %s: %s\n", str2desc(argv[1]), strerror(errno));
139                         return RESULT_SUCCESS;
140                 }
141         }
142 }
143
144 static char *complete_ulimit(const char *line, const char *word, int pos, int state)
145 {
146         int which = 0, i;
147         int wordlen = strlen(word);
148
149         if (pos > 2)
150                 return NULL;
151         for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
152                 if (!strncasecmp(limits[i].limit, word, wordlen)) {
153                         if (++which > state)
154                                 return ast_strdup(limits[i].limit);
155                 }
156         }
157         return NULL;
158 }
159
160 static const char ulimit_usage[] =
161 "Usage: ulimit {-d|-l|-f|-m|-s|-t|-u|-v|-c|-n} [<num>]\n"
162 "       Shows or sets the corresponding resource limit.\n"
163 "         -d  Process data segment [readonly]\n"
164 "         -l  Memory lock size [readonly]\n"
165 "         -f  File size\n"
166 "         -m  Process resident memory [readonly]\n"
167 "         -s  Process stack size [readonly]\n"
168 "         -t  CPU usage [readonly]\n"
169 "         -u  Child processes\n"
170 #ifdef VMEM_DEF
171 "         -v  Process virtual memory [readonly]\n"
172 #endif
173 "         -c  Core dump file size\n"
174 "         -n  Number of file descriptors\n";
175
176 static struct ast_cli_entry cli_ulimit = {
177         { "ulimit", NULL }, my_ulimit,
178         "Set or show process resource limits", ulimit_usage, complete_ulimit };
179
180 static int unload_module(void)
181 {
182         return ast_cli_unregister(&cli_ulimit);
183 }
184
185 static int load_module(void)
186 {
187         return ast_cli_register(&cli_ulimit)? AST_MODULE_LOAD_FAILURE: AST_MODULE_LOAD_SUCCESS;
188 }
189
190 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Resource limits");
191