2 * SPDX-License-Identifier: GPL-2.0-or-later
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * Copyright (C) 2011 Davidlohr Bueso <dave@gnu.org>
11 * prlimit - get/set process resource limits.
21 #include <sys/types.h>
22 #include <sys/resource.h>
24 #include <libsmartcols.h>
31 #include "closestream.h"
34 # define RLIMIT_RTTIME 15
56 /* basic output flags */
57 static int no_headings
;
67 static struct prlimit_desc prlimit_desc
[] =
69 [AS
] = { "AS", N_("address space limit"), N_("bytes"), RLIMIT_AS
},
70 [CORE
] = { "CORE", N_("max core file size"), N_("bytes"), RLIMIT_CORE
},
71 [CPU
] = { "CPU", N_("CPU time"), N_("seconds"), RLIMIT_CPU
},
72 [DATA
] = { "DATA", N_("max data size"), N_("bytes"), RLIMIT_DATA
},
73 [FSIZE
] = { "FSIZE", N_("max file size"), N_("bytes"), RLIMIT_FSIZE
},
74 [LOCKS
] = { "LOCKS", N_("max number of file locks held"), N_("locks"), RLIMIT_LOCKS
},
75 [MEMLOCK
] = { "MEMLOCK", N_("max locked-in-memory address space"), N_("bytes"), RLIMIT_MEMLOCK
},
76 [MSGQUEUE
] = { "MSGQUEUE", N_("max bytes in POSIX mqueues"), N_("bytes"), RLIMIT_MSGQUEUE
},
77 [NICE
] = { "NICE", N_("max nice prio allowed to raise"), NULL
, RLIMIT_NICE
},
78 [NOFILE
] = { "NOFILE", N_("max number of open files"), N_("files"), RLIMIT_NOFILE
},
79 [NPROC
] = { "NPROC", N_("max number of processes"), N_("processes"), RLIMIT_NPROC
},
80 [RSS
] = { "RSS", N_("max resident set size"), N_("bytes"), RLIMIT_RSS
},
81 [RTPRIO
] = { "RTPRIO", N_("max real-time priority"), NULL
, RLIMIT_RTPRIO
},
82 [RTTIME
] = { "RTTIME", N_("timeout for real-time tasks"), N_("microsecs"), RLIMIT_RTTIME
},
83 [SIGPENDING
] = { "SIGPENDING", N_("max number of pending signals"), N_("signals"), RLIMIT_SIGPENDING
},
84 [STACK
] = { "STACK", N_("max stack size"), N_("bytes"), RLIMIT_STACK
}
87 #define MAX_RESOURCES ARRAY_SIZE(prlimit_desc)
90 struct list_head lims
;
93 struct prlimit_desc
*desc
;
94 int modify
; /* PRLIMIT_{SOFT,HARD} mask */
97 #define PRLIMIT_EMPTY_LIMIT {{ 0, 0, }, NULL, 0 }
109 const char * const name
; /* header */
110 double whint
; /* width hint (N < 1 is in percent of termwidth) */
111 int flags
; /* SCOLS_FL_* */
115 /* columns descriptions */
116 static const struct colinfo infos
[] = {
117 [COL_RES
] = { "RESOURCE", 0.25, SCOLS_FL_TRUNC
, N_("resource name") },
118 [COL_HELP
] = { "DESCRIPTION", 0.1, SCOLS_FL_TRUNC
, N_("resource description")},
119 [COL_SOFT
] = { "SOFT", 0.1, SCOLS_FL_RIGHT
, N_("soft limit")},
120 [COL_HARD
] = { "HARD", 1, SCOLS_FL_RIGHT
, N_("hard limit (ceiling)")},
121 [COL_UNITS
] = { "UNITS", 0.1, SCOLS_FL_TRUNC
, N_("units")},
124 static int columns
[ARRAY_SIZE(infos
) * 2];
129 #define INFINITY_STR "unlimited"
130 #define INFINITY_STRLEN (sizeof(INFINITY_STR) - 1)
132 #define PRLIMIT_SOFT (1 << 1)
133 #define PRLIMIT_HARD (1 << 2)
135 static pid_t pid
; /* calling process (default) */
138 #ifdef HAVE_SYS_SYSCALL_H
139 # include <sys/syscall.h>
140 # if defined(SYS_prlimit64)
141 # ifndef HAVE_PRLIMIT
142 static int prlimit(pid_t p
, int resource
,
143 const struct rlimit
*new_limit
,
144 struct rlimit
*old_limit
)
146 return syscall(SYS_prlimit64
, p
, resource
, new_limit
, old_limit
);
148 # endif /* !HAVE_PRLIMIT */
149 # endif /* SYS_prlimit64 */
150 #endif /* HAVE_SYS_SYSCALL_H */
152 static void __attribute__((__noreturn__
)) usage(void)
157 fputs(USAGE_HEADER
, out
);
160 _(" %s [options] [--<resource>=<limit>] [-p PID]\n"), program_invocation_short_name
);
162 _(" %s [options] [--<resource>=<limit>] COMMAND\n"), program_invocation_short_name
);
164 fputs(USAGE_SEPARATOR
, out
);
165 fputs(_("Show or change the resource limits of a process.\n"), out
);
167 fputs(USAGE_OPTIONS
, out
);
168 fputs(_(" -p, --pid <pid> process id\n"
169 " -o, --output <list> define which output columns to use\n"
170 " --noheadings don't print headings\n"
171 " --raw use the raw output format\n"
172 " --verbose verbose output\n"
174 fprintf(out
, USAGE_HELP_OPTIONS(24));
176 fputs(_("\nResources:\n"), out
);
177 fputs(_(" -c, --core maximum size of core files created\n"
178 " -d, --data maximum size of a process's data segment\n"
179 " -e, --nice maximum nice priority allowed to raise\n"
180 " -f, --fsize maximum size of files written by the process\n"
181 " -i, --sigpending maximum number of pending signals\n"
182 " -l, --memlock maximum size a process may lock into memory\n"
183 " -m, --rss maximum resident set size\n"
184 " -n, --nofile maximum number of open files\n"
185 " -q, --msgqueue maximum bytes in POSIX message queues\n"
186 " -r, --rtprio maximum real-time scheduling priority\n"
187 " -s, --stack maximum stack size\n"
188 " -t, --cpu maximum amount of CPU time in seconds\n"
189 " -u, --nproc maximum number of user processes\n"
190 " -v, --as size of virtual memory\n"
191 " -x, --locks maximum number of file locks\n"
192 " -y, --rttime CPU time in microseconds a process scheduled\n"
193 " under real-time scheduling\n"), out
);
195 fputs(USAGE_ARGUMENTS
, out
);
197 " <limit> is defined as a range soft:hard, soft:, :hard or a value to\n"
198 " define both limits (e.g. -e=0:10 -r=:10).\n"), out
);
200 fputs(USAGE_COLUMNS
, out
);
201 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
202 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
204 fprintf(out
, USAGE_MAN_TAIL("prlimit(1)"));
209 static inline int get_column_id(int num
)
211 assert(num
< ncolumns
);
212 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
217 static inline const struct colinfo
*get_column_info(unsigned num
)
219 return &infos
[ get_column_id(num
) ];
222 static void add_scols_line(struct libscols_table
*table
, struct prlimit
*l
)
225 struct libscols_line
*line
;
230 line
= scols_table_new_line(table
, NULL
);
232 err(EXIT_FAILURE
, _("failed to allocate output line"));
234 for (i
= 0; i
< ncolumns
; i
++) {
237 switch (get_column_id(i
)) {
240 str
= xstrdup(l
->desc
->name
);
244 str
= xstrdup(_(l
->desc
->help
));
247 if (l
->rlim
.rlim_cur
== RLIM_INFINITY
)
248 str
= xstrdup(_("unlimited"));
250 xasprintf(&str
, "%llu", (unsigned long long) l
->rlim
.rlim_cur
);
253 if (l
->rlim
.rlim_max
== RLIM_INFINITY
)
254 str
= xstrdup(_("unlimited"));
256 xasprintf(&str
, "%llu", (unsigned long long) l
->rlim
.rlim_max
);
260 str
= xstrdup(_(l
->desc
->unit
));
266 if (str
&& scols_line_refer_data(line
, i
, str
))
267 err(EXIT_FAILURE
, _("failed to add output data"));
271 static int column_name_to_id(const char *name
, size_t namesz
)
277 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
278 const char *cn
= infos
[i
].name
;
280 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
283 warnx(_("unknown column: %s"), name
);
287 static void rem_prlim(struct prlimit
*lim
)
291 list_del(&lim
->lims
);
295 static int show_limits(struct list_head
*lims
)
298 struct list_head
*p
, *pnext
;
299 struct libscols_table
*table
;
301 table
= scols_new_table();
303 err(EXIT_FAILURE
, _("failed to allocate output table"));
305 scols_table_enable_raw(table
, raw
);
306 scols_table_enable_noheadings(table
, no_headings
);
308 for (i
= 0; i
< ncolumns
; i
++) {
309 const struct colinfo
*col
= get_column_info(i
);
311 if (!scols_table_new_column(table
, col
->name
, col
->whint
, col
->flags
))
312 err(EXIT_FAILURE
, _("failed to allocate output column"));
315 list_for_each_safe(p
, pnext
, lims
) {
316 struct prlimit
*lim
= list_entry(p
, struct prlimit
, lims
);
318 add_scols_line(table
, lim
);
322 scols_print_table(table
);
323 scols_unref_table(table
);
328 * If one of the limits is unknown (default value for not being passed), we
329 * need to get the current limit and use it. I see no other way other than
332 static void get_unknown_hardsoft(struct prlimit
*lim
)
336 if (prlimit(pid
, lim
->desc
->resource
, NULL
, &old
) == -1)
337 err(EXIT_FAILURE
, _("failed to get old %s limit"),
340 if (!(lim
->modify
& PRLIMIT_SOFT
))
341 lim
->rlim
.rlim_cur
= old
.rlim_cur
;
342 else if (!(lim
->modify
& PRLIMIT_HARD
))
343 lim
->rlim
.rlim_max
= old
.rlim_max
;
346 static void do_prlimit(struct list_head
*lims
)
348 struct list_head
*p
, *pnext
;
350 list_for_each_safe(p
, pnext
, lims
) {
351 struct rlimit
*new = NULL
, *old
= NULL
;
352 struct prlimit
*lim
= list_entry(p
, struct prlimit
, lims
);
355 if (lim
->modify
!= (PRLIMIT_HARD
| PRLIMIT_SOFT
))
356 get_unknown_hardsoft(lim
);
358 if ((lim
->rlim
.rlim_cur
> lim
->rlim
.rlim_max
) &&
359 (lim
->rlim
.rlim_cur
!= RLIM_INFINITY
||
360 lim
->rlim
.rlim_max
!= RLIM_INFINITY
))
361 errx(EXIT_FAILURE
, _("the soft limit %s cannot exceed the hard limit"),
367 if (verbose
&& new) {
368 printf(_("New %s limit for pid %d: "), lim
->desc
->name
,
369 pid
? pid
: getpid());
370 if (new->rlim_cur
== RLIM_INFINITY
)
371 printf("<%s", _("unlimited"));
373 printf("<%ju", (uintmax_t)new->rlim_cur
);
375 if (new->rlim_max
== RLIM_INFINITY
)
376 printf(":%s>\n", _("unlimited"));
378 printf(":%ju>\n", (uintmax_t)new->rlim_max
);
381 if (prlimit(pid
, lim
->desc
->resource
, new, old
) == -1)
382 err(EXIT_FAILURE
, lim
->modify
?
383 _("failed to set the %s resource limit") :
384 _("failed to get the %s resource limit"),
388 rem_prlim(lim
); /* modify only; don't show */
392 static int get_range(char *str
, rlim_t
*soft
, rlim_t
*hard
, int *found
)
400 *soft
= *hard
= RLIM_INFINITY
;
402 if (!strcmp(str
, INFINITY_STR
)) { /* <unlimited> */
403 *found
|= PRLIMIT_SOFT
| PRLIMIT_HARD
;
408 if (*str
== ':') { /* <:hard> */
411 if (strcmp(str
, INFINITY_STR
) != 0) {
412 *hard
= strtoull(str
, &end
, 10);
414 if (errno
|| !end
|| *end
|| end
== str
)
417 *found
|= PRLIMIT_HARD
;
422 if (strncmp(str
, INFINITY_STR
, INFINITY_STRLEN
) == 0) {
423 /* <unlimited> or <unlimited:> */
424 end
= str
+ INFINITY_STRLEN
;
426 /* <value> or <soft:> */
427 *hard
= *soft
= strtoull(str
, &end
, 10);
428 if (errno
|| !end
|| end
== str
)
432 if (*end
== ':' && !*(end
+ 1)) /* <soft:> */
433 *found
|= PRLIMIT_SOFT
;
435 else if (*end
== ':') { /* <soft:hard> */
438 if (!strcmp(str
, INFINITY_STR
))
439 *hard
= RLIM_INFINITY
;
443 *hard
= strtoull(str
, &end
, 10);
445 if (errno
|| !end
|| *end
|| end
== str
)
448 *found
|= PRLIMIT_SOFT
| PRLIMIT_HARD
;
450 } else if (!*end
) /* <value> */
451 *found
|= PRLIMIT_SOFT
| PRLIMIT_HARD
;
459 static int parse_prlim(struct rlimit
*lim
, char *ops
, size_t id
)
461 rlim_t soft
= 0, hard
= 0;
464 if (ops
&& *ops
== '=')
467 if (get_range(ops
, &soft
, &hard
, &found
))
468 errx(EXIT_FAILURE
, _("failed to parse %s limit"),
469 prlimit_desc
[id
].name
);
471 lim
->rlim_cur
= soft
;
472 lim
->rlim_max
= hard
;
477 static int add_prlim(char *ops
, struct list_head
*lims
, size_t id
)
479 struct prlimit
*lim
= xcalloc(1, sizeof(*lim
));
481 INIT_LIST_HEAD(&lim
->lims
);
482 lim
->desc
= &prlimit_desc
[id
];
485 lim
->modify
= parse_prlim(&lim
->rlim
, ops
, id
);
487 list_add_tail(&lim
->lims
, lims
);
491 int main(int argc
, char **argv
)
494 struct list_head lims
;
497 VERBOSE_OPTION
= CHAR_MAX
+ 1,
502 static const struct option longopts
[] = {
503 { "pid", required_argument
, NULL
, 'p' },
504 { "output", required_argument
, NULL
, 'o' },
505 { "as", optional_argument
, NULL
, 'v' },
506 { "core", optional_argument
, NULL
, 'c' },
507 { "cpu", optional_argument
, NULL
, 't' },
508 { "data", optional_argument
, NULL
, 'd' },
509 { "fsize", optional_argument
, NULL
, 'f' },
510 { "locks", optional_argument
, NULL
, 'x' },
511 { "memlock", optional_argument
, NULL
, 'l' },
512 { "msgqueue", optional_argument
, NULL
, 'q' },
513 { "nice", optional_argument
, NULL
, 'e' },
514 { "nofile", optional_argument
, NULL
, 'n' },
515 { "nproc", optional_argument
, NULL
, 'u' },
516 { "rss", optional_argument
, NULL
, 'm' },
517 { "rtprio", optional_argument
, NULL
, 'r' },
518 { "rttime", optional_argument
, NULL
, 'y' },
519 { "sigpending", optional_argument
, NULL
, 'i' },
520 { "stack", optional_argument
, NULL
, 's' },
521 { "version", no_argument
, NULL
, 'V' },
522 { "help", no_argument
, NULL
, 'h' },
523 { "noheadings", no_argument
, NULL
, NOHEADINGS_OPTION
},
524 { "raw", no_argument
, NULL
, RAW_OPTION
},
525 { "verbose", no_argument
, NULL
, VERBOSE_OPTION
},
529 setlocale(LC_ALL
, "");
530 bindtextdomain(PACKAGE
, LOCALEDIR
);
532 close_stdout_atexit();
534 INIT_LIST_HEAD(&lims
);
537 * Something is very wrong if this doesn't succeed,
538 * assuming STACK is the last resource, of course.
540 assert(MAX_RESOURCES
== STACK
+ 1);
542 while((opt
= getopt_long(argc
, argv
,
543 "+c::d::e::f::i::l::m::n::q::r::s::t::u::v::x::y::p:o:vVh",
544 longopts
, NULL
)) != -1) {
547 add_prlim(optarg
, &lims
, CORE
);
550 add_prlim(optarg
, &lims
, DATA
);
553 add_prlim(optarg
, &lims
, NICE
);
556 add_prlim(optarg
, &lims
, FSIZE
);
559 add_prlim(optarg
, &lims
, SIGPENDING
);
562 add_prlim(optarg
, &lims
, MEMLOCK
);
565 add_prlim(optarg
, &lims
, RSS
);
568 add_prlim(optarg
, &lims
, NOFILE
);
571 add_prlim(optarg
, &lims
, MSGQUEUE
);
574 add_prlim(optarg
, &lims
, RTPRIO
);
577 add_prlim(optarg
, &lims
, STACK
);
580 add_prlim(optarg
, &lims
, CPU
);
583 add_prlim(optarg
, &lims
, NPROC
);
586 add_prlim(optarg
, &lims
, AS
);
589 add_prlim(optarg
, &lims
, LOCKS
);
592 add_prlim(optarg
, &lims
, RTTIME
);
597 errx(EXIT_FAILURE
, _("option --pid may be specified only once"));
598 pid
= strtos32_or_err(optarg
, _("invalid PID argument"));
601 ncolumns
= string_to_idarray(optarg
,
602 columns
, ARRAY_SIZE(columns
),
607 case NOHEADINGS_OPTION
:
620 print_version(EXIT_SUCCESS
);
622 errtryhelp(EXIT_FAILURE
);
625 if (argc
> optind
&& pid
)
626 errx(EXIT_FAILURE
, _("options --pid and COMMAND are mutually exclusive"));
628 /* default columns */
629 columns
[ncolumns
++] = COL_RES
;
630 columns
[ncolumns
++] = COL_HELP
;
631 columns
[ncolumns
++] = COL_SOFT
;
632 columns
[ncolumns
++] = COL_HARD
;
633 columns
[ncolumns
++] = COL_UNITS
;
638 if (list_empty(&lims
)) {
639 /* default is to print all resources */
642 for (n
= 0; n
< MAX_RESOURCES
; n
++)
643 add_prlim(NULL
, &lims
, n
);
648 if (!list_empty(&lims
))
652 /* prlimit [options] COMMAND */
653 execvp(argv
[optind
], &argv
[optind
]);
654 errexec(argv
[optind
]);