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