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