d76ad400331c1a27024b814d8eceb786d98423f2
[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 #include "asterisk.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #define _XOPEN_SOURCE 600
20 #include <string.h>
21 #include <ctype.h>
22 #include <sys/time.h>
23 #include <sys/resource.h>
24 #include <errno.h>
25 #include "asterisk/module.h"
26 #include "asterisk/cli.h"
27
28 /* Find proper rlimit for virtual memory */
29 #ifdef RLIMIT_AS
30 #define VMEM_DEF RLIMIT_AS
31 #else
32 #ifdef RLIMIT_VMEM
33 #define VMEM_DEF RLIMIT_VMEM
34 #endif
35 #endif
36
37 static struct limits {
38         int resource;
39         char limit[3];
40         char desc[40];
41 } limits[] = {
42         { RLIMIT_CPU, "-t", "cpu time" },
43         { RLIMIT_FSIZE, "-f", "file size" },
44         { RLIMIT_DATA, "-d", "program data segment" },
45         { RLIMIT_STACK, "-s", "program stack size" },
46         { RLIMIT_CORE, "-c", "core file size" },
47 #ifdef RLIMIT_RSS
48         { RLIMIT_RSS, "-m", "resident memory" },
49         { RLIMIT_NPROC, "-u", "number of processes" },
50         { RLIMIT_MEMLOCK, "-l", "amount of memory locked into RAM" },
51 #endif
52         { RLIMIT_NOFILE, "-n", "number of file descriptors" },
53 #ifdef VMEM_DEF
54         { VMEM_DEF, "-v", "virtual memory" },
55 #endif
56 };
57
58 static int str2limit(const char *string)
59 {
60         size_t i;
61         for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
62                 if (!strcasecmp(string, limits[i].limit))
63                         return limits[i].resource;
64         }
65         return -1;
66 }
67
68 static const char *str2desc(const char *string)
69 {
70         size_t i;
71         for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
72                 if (!strcmp(string, limits[i].limit))
73                         return limits[i].desc;
74         }
75         return "<unknown>";
76 }
77
78 static int my_ulimit(int fd, int argc, char **argv)
79 {
80         int resource;
81         struct rlimit rlimit = { 0, 0 };
82         if (argc > 3)
83                 return RESULT_SHOWUSAGE;
84
85         if (argc == 1) {
86                 char arg2[3];
87                 char *newargv[2] = { "ulimit", arg2 };
88                 for (resource = 0; resource < sizeof(limits) / sizeof(limits[0]); resource++) {
89                         ast_copy_string(arg2, limits[resource].limit, sizeof(arg2));
90                         my_ulimit(fd, 2, newargv);
91                 }
92                 return RESULT_SUCCESS;
93         } else {
94                 resource = str2limit(argv[1]);
95                 if (resource == -1) {
96                         ast_cli(fd, "Unknown resource\n");
97                         return RESULT_FAILURE;
98                 }
99
100                 if (argc == 3) {
101                         int x;
102                         if (resource != RLIMIT_NOFILE && resource != RLIMIT_CORE && resource != RLIMIT_NPROC && resource != RLIMIT_FSIZE) {
103                                 ast_cli(fd, "Resource not permitted to be set\n");
104                                 return RESULT_FAILURE;
105                         }
106
107                         sscanf(argv[2], "%d", &x);
108                         rlimit.rlim_max = rlimit.rlim_cur = x;
109                         setrlimit(resource, &rlimit);
110                         return RESULT_SUCCESS;
111                 } else {
112                         if (!getrlimit(resource, &rlimit)) {
113                                 char printlimit[32];
114                                 const char *desc;
115                                 if (rlimit.rlim_max == RLIM_INFINITY)
116                                         ast_copy_string(printlimit, "effectively unlimited", sizeof(printlimit));
117                                 else
118                                         snprintf(printlimit, sizeof(printlimit), "limited to %d", (int)rlimit.rlim_cur);
119                                 desc = str2desc(argv[1]);
120                                 ast_cli(fd, "%c%s (%s) is %s.\n", toupper(desc[0]), desc + 1, argv[1], printlimit);
121                         } else
122                                 ast_cli(fd, "Could not retrieve resource limits for %s: %s\n", str2desc(argv[1]), strerror(errno));
123                         return RESULT_SUCCESS;
124                 }
125         }
126 }
127
128 static char *complete_ulimit(const char *line, const char *word, int pos, int state)
129 {
130         int which = 0, i;
131         int wordlen = strlen(word);
132
133         if (pos > 2)
134                 return NULL;
135         for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
136                 if (!strncasecmp(limits[i].limit, word, wordlen)) {
137                         if (++which > state)
138                                 return ast_strdup(limits[i].limit);
139                 }
140         }
141         return NULL;
142 }
143
144 static const char ulimit_usage[] =
145 "Usage: ulimit {-d|-l|-f|-m|-s|-t|-u|-v|-c|-n} [<num>]\n"
146 "       Shows or sets the corresponding resource limit.\n"
147 "         -d  Process data segment [readonly]\n"
148 "         -l  Memory lock size [readonly]\n"
149 "         -f  File size\n"
150 "         -m  Process resident memory [readonly]\n"
151 "         -s  Process stack size [readonly]\n"
152 "         -t  CPU usage [readonly]\n"
153 "         -u  Child processes\n"
154 #ifdef VMEM_DEF
155 "         -v  Process virtual memory [readonly]\n"
156 #endif
157 "         -c  Core dump file size\n"
158 "         -n  Number of file descriptors\n";
159
160 static struct ast_cli_entry cli_ulimit = {
161         { "ulimit", NULL }, my_ulimit,
162         "Set or show process resource limits", ulimit_usage, complete_ulimit };
163
164 static int unload_module(void)
165 {
166         return ast_cli_unregister(&cli_ulimit);
167 }
168
169 static int load_module(void)
170 {
171         return ast_cli_register(&cli_ulimit)? AST_MODULE_LOAD_FAILURE: AST_MODULE_LOAD_SUCCESS;
172 }
173
174 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Resource limits");
175