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