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