]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/prlimit.c
libsmartcols: clean up flags usage
[thirdparty/util-linux.git] / sys-utils / prlimit.c
1 /*
2 * prlimit - get/set process resource limits.
3 *
4 * Copyright (C) 2011 Davidlohr Bueso <dave@gnu.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <errno.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26 #include <assert.h>
27 #include <unistd.h>
28 #include <sys/resource.h>
29
30 #include <libsmartcols.h>
31
32 #include "c.h"
33 #include "nls.h"
34 #include "xalloc.h"
35 #include "strutils.h"
36 #include "list.h"
37 #include "closestream.h"
38
39 #ifndef RLIMIT_RTTIME
40 # define RLIMIT_RTTIME 15
41 #endif
42
43 enum {
44 AS,
45 CORE,
46 CPU,
47 DATA,
48 FSIZE,
49 LOCKS,
50 MEMLOCK,
51 MSGQUEUE,
52 NICE,
53 NOFILE,
54 NPROC,
55 RSS,
56 RTPRIO,
57 RTTIME,
58 SIGPENDING,
59 STACK
60 };
61
62 /* basic output flags */
63 static int no_headings;
64 static int raw;
65
66 struct prlimit_desc {
67 const char *name;
68 const char *help;
69 const char *unit;
70 int resource;
71 };
72
73 static struct prlimit_desc prlimit_desc[] =
74 {
75 [AS] = { "AS", N_("address space limit"), N_("bytes"), RLIMIT_AS },
76 [CORE] = { "CORE", N_("max core file size"), N_("blocks"), RLIMIT_CORE },
77 [CPU] = { "CPU", N_("CPU time"), N_("seconds"), RLIMIT_CPU },
78 [DATA] = { "DATA", N_("max data size"), N_("bytes"), RLIMIT_DATA },
79 [FSIZE] = { "FSIZE", N_("max file size"), N_("blocks"), RLIMIT_FSIZE },
80 [LOCKS] = { "LOCKS", N_("max number of file locks held"), NULL, RLIMIT_LOCKS },
81 [MEMLOCK] = { "MEMLOCK", N_("max locked-in-memory address space"), N_("bytes"), RLIMIT_MEMLOCK },
82 [MSGQUEUE] = { "MSGQUEUE", N_("max bytes in POSIX mqueues"), N_("bytes"), RLIMIT_MSGQUEUE },
83 [NICE] = { "NICE", N_("max nice prio allowed to raise"), NULL, RLIMIT_NICE },
84 [NOFILE] = { "NOFILE", N_("max number of open files"), NULL, RLIMIT_NOFILE },
85 [NPROC] = { "NPROC", N_("max number of processes"), NULL, RLIMIT_NPROC },
86 [RSS] = { "RSS", N_("max resident set size"), N_("pages"), RLIMIT_RSS },
87 [RTPRIO] = { "RTPRIO", N_("max real-time priority"), NULL, RLIMIT_RTPRIO },
88 [RTTIME] = { "RTTIME", N_("timeout for real-time tasks"), N_("microsecs"), RLIMIT_RTTIME },
89 [SIGPENDING] = { "SIGPENDING", N_("max number of pending signals"), NULL, RLIMIT_SIGPENDING },
90 [STACK] = { "STACK", N_("max stack size"), N_("bytes"), RLIMIT_STACK }
91 };
92
93 struct prlimit {
94 struct list_head lims;
95
96 struct rlimit rlim;
97 struct prlimit_desc *desc;
98 int modify; /* PRLIMIT_{SOFT,HARD} mask */
99 };
100
101 #define PRLIMIT_EMPTY_LIMIT {{ 0, 0, }, NULL, 0 }
102
103 enum {
104 COL_HELP,
105 COL_RES,
106 COL_SOFT,
107 COL_HARD,
108 COL_UNITS,
109 };
110
111 /* column names */
112 struct colinfo {
113 const char *name; /* header */
114 double whint; /* width hint (N < 1 is in percent of termwidth) */
115 int flags; /* SCOLS_FL_* */
116 const char *help;
117 };
118
119 /* columns descriptions */
120 struct colinfo infos[] = {
121 [COL_RES] = { "RESOURCE", 0.25, SCOLS_FL_TRUNC, N_("resource name") },
122 [COL_HELP] = { "DESCRIPTION", 0.1, SCOLS_FL_TRUNC, N_("resource description")},
123 [COL_SOFT] = { "SOFT", 0.1, SCOLS_FL_RIGHT, N_("soft limit")},
124 [COL_HARD] = { "HARD", 1, SCOLS_FL_RIGHT, N_("hard limit (ceiling)")},
125 [COL_UNITS] = { "UNITS", 0.1, SCOLS_FL_TRUNC, N_("units")},
126 };
127
128 #define NCOLS ARRAY_SIZE(infos)
129 #define MAX_RESOURCES ARRAY_SIZE(prlimit_desc)
130
131 #define INFINITY_STR "unlimited"
132 #define INFINITY_STRLEN (sizeof(INFINITY_STR) - 1)
133
134 #define PRLIMIT_SOFT (1 << 1)
135 #define PRLIMIT_HARD (1 << 2)
136
137 /* array with IDs of enabled columns */
138 static int columns[NCOLS], ncolumns;
139 static pid_t pid; /* calling process (default) */
140 static int verbose;
141
142 #ifndef HAVE_PRLIMIT
143 # include <sys/syscall.h>
144 static int prlimit(pid_t p, int resource,
145 const struct rlimit *new_limit,
146 struct rlimit *old_limit)
147 {
148 return syscall(SYS_prlimit64, p, resource, new_limit, old_limit);
149 }
150 #endif
151
152 static void __attribute__ ((__noreturn__)) usage(FILE * out)
153 {
154 size_t i;
155
156 fputs(USAGE_HEADER, out);
157
158 fprintf(out,
159 _(" %s [options] [-p PID]\n"), program_invocation_short_name);
160 fprintf(out,
161 _(" %s [options] COMMAND\n"), program_invocation_short_name);
162
163 fputs(_("\nGeneral Options:\n"), out);
164 fputs(_(" -p, --pid <pid> process id\n"
165 " -o, --output <list> define which output columns to use\n"
166 " --noheadings don't print headings\n"
167 " --raw use the raw output format\n"
168 " --verbose verbose output\n"
169 " -h, --help display this help and exit\n"
170 " -V, --version output version information and exit\n"), out);
171
172 fputs(_("\nResources Options:\n"), out);
173 fputs(_(" -c, --core maximum size of core files created\n"
174 " -d, --data maximum size of a process's data segment\n"
175 " -e, --nice maximum nice priority allowed to raise\n"
176 " -f, --fsize maximum size of files written by the process\n"
177 " -i, --sigpending maximum number of pending signals\n"
178 " -l, --memlock maximum size a process may lock into memory\n"
179 " -m, --rss maximum resident set size\n"
180 " -n, --nofile maximum number of open files\n"
181 " -q, --msgqueue maximum bytes in POSIX message queues\n"
182 " -r, --rtprio maximum real-time scheduling priority\n"
183 " -s, --stack maximum stack size\n"
184 " -t, --cpu maximum amount of CPU time in seconds\n"
185 " -u, --nproc maximum number of user processes\n"
186 " -v, --as size of virtual memory\n"
187 " -x, --locks maximum number of file locks\n"
188 " -y, --rttime CPU time in microseconds a process scheduled\n"
189 " under real-time scheduling\n"), out);
190
191 fputs(_("\nAvailable columns (for --output):\n"), out);
192
193 for (i = 0; i < NCOLS; i++)
194 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
195
196 fprintf(out, USAGE_MAN_TAIL("prlimit(1)"));
197
198 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
199 }
200
201 static inline int get_column_id(int num)
202 {
203 assert(ARRAY_SIZE(columns) == NCOLS);
204 assert(num < ncolumns);
205 assert(columns[num] < (int) NCOLS);
206
207 return columns[num];
208 }
209
210 static inline struct colinfo *get_column_info(unsigned num)
211 {
212 return &infos[ get_column_id(num) ];
213 }
214
215 static void add_scols_line(struct libscols_table *table, struct prlimit *l)
216 {
217 int i;
218 struct libscols_line *line;
219
220 assert(table);
221 assert(l);
222
223 line = scols_table_new_line(table, NULL);
224 if (!line)
225 err(EXIT_FAILURE, _("failed to initialize output line"));
226
227 for (i = 0; i < ncolumns; i++) {
228 char *str = NULL;
229
230 switch (get_column_id(i)) {
231 case COL_RES:
232 str = xstrdup(l->desc->name);
233 break;
234 case COL_HELP:
235 str = xstrdup(l->desc->help);
236 break;
237 case COL_SOFT:
238 if (l->rlim.rlim_cur == RLIM_INFINITY)
239 str = xstrdup(_("unlimited"));
240 else
241 xasprintf(&str, "%llu", (unsigned long long) l->rlim.rlim_cur);
242 break;
243 case COL_HARD:
244 if (l->rlim.rlim_max == RLIM_INFINITY)
245 str = xstrdup(_("unlimited"));
246 else
247 xasprintf(&str, "%llu", (unsigned long long) l->rlim.rlim_max);
248 break;
249 case COL_UNITS:
250 str = l->desc->unit ? xstrdup(_(l->desc->unit)) : NULL;
251 break;
252 default:
253 break;
254 }
255
256 if (str)
257 scols_line_refer_data(line, i, str);
258 }
259 }
260
261 static int column_name_to_id(const char *name, size_t namesz)
262 {
263 size_t i;
264
265 assert(name);
266
267 for (i = 0; i < NCOLS; i++) {
268 const char *cn = infos[i].name;
269
270 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
271 return i;
272 }
273 warnx(_("unknown column: %s"), name);
274 return -1;
275 }
276
277 static void rem_prlim(struct prlimit *lim)
278 {
279 if (!lim)
280 return;
281 list_del(&lim->lims);
282 free(lim);
283 }
284
285 static int show_limits(struct list_head *lims)
286 {
287 int i;
288 struct list_head *p, *pnext;
289 struct libscols_table *table;
290
291 table = scols_new_table();
292 if (!table)
293 err(EXIT_FAILURE, _("failed to initialize output table"));
294
295 scols_table_enable_raw(table, raw);
296 scols_table_enable_noheadings(table, no_headings);
297
298 for (i = 0; i < ncolumns; i++) {
299 struct colinfo *col = get_column_info(i);
300
301 if (!scols_table_new_column(table, col->name, col->whint, col->flags))
302 err(EXIT_FAILURE, _("failed to initialize output column"));
303 }
304
305
306 list_for_each_safe(p, pnext, lims) {
307 struct prlimit *lim = list_entry(p, struct prlimit, lims);
308
309 add_scols_line(table, lim);
310 rem_prlim(lim);
311 }
312
313 scols_print_table(table);
314 scols_unref_table(table);
315 return 0;
316 }
317
318 /*
319 * If one of the limits is unknown (default value for not being passed), we
320 * need to get the current limit and use it. I see no other way other than
321 * using prlimit(2).
322 */
323 static void get_unknown_hardsoft(struct prlimit *lim)
324 {
325 struct rlimit old;
326
327 if (prlimit(pid, lim->desc->resource, NULL, &old) == -1)
328 err(EXIT_FAILURE, _("failed to get old %s limit"),
329 lim->desc->name);
330
331 if (!(lim->modify & PRLIMIT_SOFT))
332 lim->rlim.rlim_cur = old.rlim_cur;
333 else if (!(lim->modify & PRLIMIT_HARD))
334 lim->rlim.rlim_max = old.rlim_max;
335 }
336
337 static void do_prlimit(struct list_head *lims)
338 {
339 struct list_head *p, *pnext;
340
341 list_for_each_safe(p, pnext, lims) {
342 struct rlimit *new = NULL, *old = NULL;
343 struct prlimit *lim = list_entry(p, struct prlimit, lims);
344
345 if (lim->modify) {
346 if (lim->modify != (PRLIMIT_HARD | PRLIMIT_SOFT))
347 get_unknown_hardsoft(lim);
348
349 if ((lim->rlim.rlim_cur > lim->rlim.rlim_max) &&
350 (lim->rlim.rlim_cur != RLIM_INFINITY ||
351 lim->rlim.rlim_max != RLIM_INFINITY))
352 errx(EXIT_FAILURE, _("the soft limit %s cannot exceed the hard limit"),
353 lim->desc->name);
354 new = &lim->rlim;
355 } else
356 old = &lim->rlim;
357
358 if (verbose && new) {
359 printf(_("New %s limit: "), lim->desc->name);
360 if (new->rlim_cur == RLIM_INFINITY)
361 printf("<%s", _("unlimited"));
362 else
363 printf("<%ju", new->rlim_cur);
364
365 if (new->rlim_max == RLIM_INFINITY)
366 printf(":%s>\n", _("unlimited"));
367 else
368 printf(":%ju>\n", new->rlim_max);
369 }
370
371 if (prlimit(pid, lim->desc->resource, new, old) == -1)
372 err(EXIT_FAILURE, lim->modify ?
373 _("failed to set the %s resource limit") :
374 _("failed to get the %s resource limit"),
375 lim->desc->name);
376
377 if (lim->modify)
378 rem_prlim(lim); /* modify only; don't show */
379 }
380 }
381
382 static int get_range(char *str, rlim_t *soft, rlim_t *hard, int *found)
383 {
384 char *end = NULL;
385
386 if (!str)
387 return 0;
388
389 *found = errno = 0;
390 *soft = *hard = RLIM_INFINITY;
391
392 if (!strcmp(str, INFINITY_STR)) { /* <unlimited> */
393 *found |= PRLIMIT_SOFT | PRLIMIT_HARD;
394 return 0;
395
396 } else if (*str == ':') { /* <:hard> */
397 str++;
398
399 if (strcmp(str, INFINITY_STR) != 0) {
400 *hard = strtoull(str, &end, 10);
401
402 if (errno || !end || *end || end == str)
403 return -1;
404 }
405 *found |= PRLIMIT_HARD;
406 return 0;
407
408 }
409
410 if (strncmp(str, INFINITY_STR, INFINITY_STRLEN) == 0) {
411 /* <unlimited> or <unlimited:> */
412 end = str + INFINITY_STRLEN;
413 } else {
414 /* <value> or <soft:> */
415 *hard = *soft = strtoull(str, &end, 10);
416 if (errno || !end || end == str)
417 return -1;
418 }
419
420 if (*end == ':' && !*(end + 1)) /* <soft:> */
421 *found |= PRLIMIT_SOFT;
422
423 else if (*end == ':') { /* <soft:hard> */
424 str = end + 1;
425
426 if (!strcmp(str, INFINITY_STR))
427 *hard = RLIM_INFINITY;
428 else {
429 end = NULL;
430 errno = 0;
431 *hard = strtoull(str, &end, 10);
432
433 if (errno || !end || *end || end == str)
434 return -1;
435 }
436 *found |= PRLIMIT_SOFT | PRLIMIT_HARD;
437
438 } else /* <value> */
439 *found |= PRLIMIT_SOFT | PRLIMIT_HARD;
440
441 return 0;
442 }
443
444
445 static int parse_prlim(struct rlimit *lim, char *ops, size_t id)
446 {
447 rlim_t soft, hard;
448 int found = 0;
449
450 if (get_range(ops, &soft, &hard, &found))
451 errx(EXIT_FAILURE, _("failed to parse %s limit"),
452 prlimit_desc[id].name);
453
454 lim->rlim_cur = soft;
455 lim->rlim_max = hard;
456
457 return found;
458 }
459
460 static int add_prlim(char *ops, struct list_head *lims, size_t id)
461 {
462 struct prlimit *lim = xcalloc(1, sizeof(*lim));
463
464 INIT_LIST_HEAD(&lim->lims);
465 lim->desc = &prlimit_desc[id];
466
467 if (ops)
468 lim->modify = parse_prlim(&lim->rlim, ops, id);
469
470 list_add_tail(&lim->lims, lims);
471 return 0;
472 }
473
474 int main(int argc, char **argv)
475 {
476 int opt;
477 struct list_head lims;
478
479 enum {
480 VERBOSE_OPTION = CHAR_MAX + 1,
481 RAW_OPTION,
482 NOHEADINGS_OPTION
483 };
484
485 static const struct option longopts[] = {
486 { "pid", required_argument, NULL, 'p' },
487 { "output", required_argument, NULL, 'o' },
488 { "as", optional_argument, NULL, 'v' },
489 { "core", optional_argument, NULL, 'c' },
490 { "cpu", optional_argument, NULL, 't' },
491 { "data", optional_argument, NULL, 'd' },
492 { "fsize", optional_argument, NULL, 'f' },
493 { "locks", optional_argument, NULL, 'x' },
494 { "memlock", optional_argument, NULL, 'l' },
495 { "msgqueue", optional_argument, NULL, 'q' },
496 { "nice", optional_argument, NULL, 'e' },
497 { "nofile", optional_argument, NULL, 'n' },
498 { "nproc", optional_argument, NULL, 'u' },
499 { "rss", optional_argument, NULL, 'm' },
500 { "rtprio", optional_argument, NULL, 'r' },
501 { "rttime", optional_argument, NULL, 'y' },
502 { "sigpending", optional_argument, NULL, 'i' },
503 { "stack", optional_argument, NULL, 's' },
504 { "version", no_argument, NULL, 'V' },
505 { "help", no_argument, NULL, 'h' },
506 { "noheadings", no_argument, NULL, NOHEADINGS_OPTION },
507 { "raw", no_argument, NULL, RAW_OPTION },
508 { "verbose", no_argument, NULL, VERBOSE_OPTION },
509 { NULL, 0, NULL, 0 }
510 };
511
512 setlocale(LC_ALL, "");
513 bindtextdomain(PACKAGE, LOCALEDIR);
514 textdomain(PACKAGE);
515 atexit(close_stdout);
516
517 INIT_LIST_HEAD(&lims);
518
519 /*
520 * Something is very wrong if this doesn't succeed,
521 * assuming STACK is the last resource, of course.
522 */
523 assert(MAX_RESOURCES == STACK + 1);
524
525 while((opt = getopt_long(argc, argv,
526 "+c::d::e::f::i::l::m::n::q::r::s::t::u::v::x::y::p:o:vVh",
527 longopts, NULL)) != -1) {
528 switch(opt) {
529 case 'c':
530 add_prlim(optarg, &lims, CORE);
531 break;
532 case 'd':
533 add_prlim(optarg, &lims, DATA);
534 break;
535 case 'e':
536 add_prlim(optarg, &lims, NICE);
537 break;
538 case 'f':
539 add_prlim(optarg, &lims, FSIZE);
540 break;
541 case 'i':
542 add_prlim(optarg, &lims, SIGPENDING);
543 break;
544 case 'l':
545 add_prlim(optarg, &lims, MEMLOCK);
546 break;
547 case 'm':
548 add_prlim(optarg, &lims, RSS);
549 break;
550 case 'n':
551 add_prlim(optarg, &lims, NOFILE);
552 break;
553 case 'q':
554 add_prlim(optarg, &lims, MSGQUEUE);
555 break;
556 case 'r':
557 add_prlim(optarg, &lims, RTPRIO);
558 break;
559 case 's':
560 add_prlim(optarg, &lims, STACK);
561 break;
562 case 't':
563 add_prlim(optarg, &lims, CPU);
564 break;
565 case 'u':
566 add_prlim(optarg, &lims, NPROC);
567 break;
568 case 'v':
569 add_prlim(optarg, &lims, AS);
570 break;
571 case 'x':
572 add_prlim(optarg, &lims, LOCKS);
573 break;
574 case 'y':
575 add_prlim(optarg, &lims, RTTIME);
576 break;
577
578 case 'p':
579 if (pid)
580 errx(EXIT_FAILURE, _("option --pid may be specified only once"));
581 pid = strtos32_or_err(optarg, _("invalid PID argument"));
582 break;
583 case 'h':
584 usage(stdout);
585 case 'o':
586 ncolumns = string_to_idarray(optarg,
587 columns, ARRAY_SIZE(columns),
588 column_name_to_id);
589 if (ncolumns < 0)
590 return EXIT_FAILURE;
591 break;
592 case 'V':
593 printf(UTIL_LINUX_VERSION);
594 return EXIT_SUCCESS;
595
596 case NOHEADINGS_OPTION:
597 no_headings = 1;
598 break;
599 case VERBOSE_OPTION:
600 verbose++;
601 break;
602 case RAW_OPTION:
603 raw = 1;
604 break;
605
606 default:
607 usage(stderr);
608 }
609 }
610 if (argc > optind && pid)
611 errx(EXIT_FAILURE, _("options --pid and COMMAND are mutually exclusive"));
612 if (!ncolumns) {
613 /* default columns */
614 columns[ncolumns++] = COL_RES;
615 columns[ncolumns++] = COL_HELP;
616 columns[ncolumns++] = COL_SOFT;
617 columns[ncolumns++] = COL_HARD;
618 columns[ncolumns++] = COL_UNITS;
619 }
620
621 if (list_empty(&lims)) {
622 /* default is to print all resources */
623 size_t n;
624
625 for (n = 0; n < MAX_RESOURCES; n++)
626 add_prlim(NULL, &lims, n);
627 }
628
629 do_prlimit(&lims);
630
631 if (!list_empty(&lims))
632 show_limits(&lims);
633
634 if (argc > optind) {
635 /* prlimit [options] COMMAND */
636 execvp(argv[optind], &argv[optind]);
637 err(EXIT_FAILURE, _("failed to execute %s"), argv[optind]);
638 }
639
640 return EXIT_SUCCESS;
641 }