17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*39e7390aSna * Common Development and Distribution License (the "License").
6*39e7390aSna * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*39e7390aSna
223d9df56cSakaplan /*
233d9df56cSakaplan * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
243d9df56cSakaplan * Use is subject to license terms.
253d9df56cSakaplan */
26*39e7390aSna
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * ulimit builtin
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include <sys/resource.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include "defs.h"
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * order is important in this table! it is indexed by resource ID.
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate static struct rlimtab {
437c478bd9Sstevel@tonic-gate char *name;
447c478bd9Sstevel@tonic-gate char *scale;
457c478bd9Sstevel@tonic-gate rlim_t divisor;
467c478bd9Sstevel@tonic-gate } rlimtab[] = {
477c478bd9Sstevel@tonic-gate /* RLIMIT_CPU */ "time", "seconds", 1,
487c478bd9Sstevel@tonic-gate /* RLIMIT_FSIZE */ "file", "blocks", 512,
497c478bd9Sstevel@tonic-gate /* RLIMIT_DATA */ "data", "kbytes", 1024,
507c478bd9Sstevel@tonic-gate /* RLIMIT_STACK */ "stack", "kbytes", 1024,
517c478bd9Sstevel@tonic-gate /* RLIMIT_CORE */ "coredump", "blocks", 512,
527c478bd9Sstevel@tonic-gate /* RLIMIT_NOFILE */ "nofiles", "descriptors", 1,
537c478bd9Sstevel@tonic-gate /* RLIMIT_VMEM */ "memory", "kbytes", 1024,
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate void
sysulimit(int argc,char ** argv)577c478bd9Sstevel@tonic-gate sysulimit(int argc, char **argv)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate extern int opterr, optind;
607c478bd9Sstevel@tonic-gate int savopterr, savoptind, savsp;
617c478bd9Sstevel@tonic-gate char *savoptarg;
627c478bd9Sstevel@tonic-gate char *args;
633d9df56cSakaplan char errargs[PATH_MAX];
647c478bd9Sstevel@tonic-gate int hard, soft, cnt, c, res;
657c478bd9Sstevel@tonic-gate rlim_t limit, new_limit;
667c478bd9Sstevel@tonic-gate struct rlimit rlimit;
677c478bd9Sstevel@tonic-gate char resources[RLIM_NLIMITS];
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate for (res = 0; res < RLIM_NLIMITS; res++) {
707c478bd9Sstevel@tonic-gate resources[res] = 0;
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate savoptind = optind;
747c478bd9Sstevel@tonic-gate savopterr = opterr;
757c478bd9Sstevel@tonic-gate savsp = _sp;
767c478bd9Sstevel@tonic-gate savoptarg = optarg;
777c478bd9Sstevel@tonic-gate optind = 1;
787c478bd9Sstevel@tonic-gate _sp = 1;
797c478bd9Sstevel@tonic-gate opterr = 0;
807c478bd9Sstevel@tonic-gate hard = 0;
817c478bd9Sstevel@tonic-gate soft = 0;
827c478bd9Sstevel@tonic-gate cnt = 0;
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "HSacdfnstv")) != -1) {
857c478bd9Sstevel@tonic-gate switch (c) {
867c478bd9Sstevel@tonic-gate case 'S':
877c478bd9Sstevel@tonic-gate soft++;
887c478bd9Sstevel@tonic-gate continue;
897c478bd9Sstevel@tonic-gate case 'H':
907c478bd9Sstevel@tonic-gate hard++;
917c478bd9Sstevel@tonic-gate continue;
927c478bd9Sstevel@tonic-gate case 'a':
937c478bd9Sstevel@tonic-gate for (res = 0; res < RLIM_NLIMITS; res++) {
947c478bd9Sstevel@tonic-gate resources[res]++;
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate cnt = RLIM_NLIMITS;
977c478bd9Sstevel@tonic-gate continue;
987c478bd9Sstevel@tonic-gate case 'c':
997c478bd9Sstevel@tonic-gate res = RLIMIT_CORE;
1007c478bd9Sstevel@tonic-gate break;
1017c478bd9Sstevel@tonic-gate case 'd':
1027c478bd9Sstevel@tonic-gate res = RLIMIT_DATA;
1037c478bd9Sstevel@tonic-gate break;
1047c478bd9Sstevel@tonic-gate case 'f':
1057c478bd9Sstevel@tonic-gate res = RLIMIT_FSIZE;
1067c478bd9Sstevel@tonic-gate break;
1077c478bd9Sstevel@tonic-gate case 'n':
1087c478bd9Sstevel@tonic-gate res = RLIMIT_NOFILE;
1097c478bd9Sstevel@tonic-gate break;
1107c478bd9Sstevel@tonic-gate case 's':
1117c478bd9Sstevel@tonic-gate res = RLIMIT_STACK;
1127c478bd9Sstevel@tonic-gate break;
1137c478bd9Sstevel@tonic-gate case 't':
1147c478bd9Sstevel@tonic-gate res = RLIMIT_CPU;
1157c478bd9Sstevel@tonic-gate break;
1167c478bd9Sstevel@tonic-gate case 'v':
1177c478bd9Sstevel@tonic-gate res = RLIMIT_VMEM;
1187c478bd9Sstevel@tonic-gate break;
1197c478bd9Sstevel@tonic-gate case '?':
120*39e7390aSna gfailure(usage, ulimuse);
1217c478bd9Sstevel@tonic-gate goto err;
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate resources[res]++;
1247c478bd9Sstevel@tonic-gate cnt++;
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate if (cnt == 0) {
1287c478bd9Sstevel@tonic-gate resources[res = RLIMIT_FSIZE]++;
1297c478bd9Sstevel@tonic-gate cnt++;
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate * if out of arguments, then print the specified resources
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate if (optind == argc) {
1377c478bd9Sstevel@tonic-gate if (!hard && !soft) {
1387c478bd9Sstevel@tonic-gate soft++;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate for (res = 0; res < RLIM_NLIMITS; res++) {
1417c478bd9Sstevel@tonic-gate if (resources[res] == 0) {
1427c478bd9Sstevel@tonic-gate continue;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate if (getrlimit(res, &rlimit) < 0) {
1457c478bd9Sstevel@tonic-gate continue;
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate if (cnt > 1) {
148*39e7390aSna prs_buff(_gettext(rlimtab[res].name));
1497c478bd9Sstevel@tonic-gate prc_buff('(');
150*39e7390aSna prs_buff(_gettext(rlimtab[res].scale));
1517c478bd9Sstevel@tonic-gate prc_buff(')');
1527c478bd9Sstevel@tonic-gate prc_buff(' ');
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate if (soft) {
1557c478bd9Sstevel@tonic-gate if (rlimit.rlim_cur == RLIM_INFINITY) {
156*39e7390aSna prs_buff(_gettext("unlimited"));
1577c478bd9Sstevel@tonic-gate } else {
1587c478bd9Sstevel@tonic-gate prull_buff(rlimit.rlim_cur /
1597c478bd9Sstevel@tonic-gate rlimtab[res].divisor);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate if (hard && soft) {
1637c478bd9Sstevel@tonic-gate prc_buff(':');
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate if (hard) {
1667c478bd9Sstevel@tonic-gate if (rlimit.rlim_max == RLIM_INFINITY) {
167*39e7390aSna prs_buff(_gettext("unlimited"));
1687c478bd9Sstevel@tonic-gate } else {
1697c478bd9Sstevel@tonic-gate prull_buff(rlimit.rlim_max /
1707c478bd9Sstevel@tonic-gate rlimtab[res].divisor);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate prc_buff('\n');
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate goto err;
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate if (cnt > 1 || optind + 1 != argc) {
179*39e7390aSna gfailure(usage, ulimuse);
1807c478bd9Sstevel@tonic-gate goto err;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate if (eq(argv[optind], "unlimited")) {
1847c478bd9Sstevel@tonic-gate limit = RLIM_INFINITY;
1857c478bd9Sstevel@tonic-gate } else {
1867c478bd9Sstevel@tonic-gate args = argv[optind];
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate new_limit = limit = 0;
1897c478bd9Sstevel@tonic-gate do {
1907c478bd9Sstevel@tonic-gate if (*args < '0' || *args > '9') {
1913d9df56cSakaplan snprintf(errargs, PATH_MAX-1,
1923d9df56cSakaplan "%s: %s", argv[0], args);
1933d9df56cSakaplan failure(errargs, badnum);
1947c478bd9Sstevel@tonic-gate goto err;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate /* Check for overflow! */
1977c478bd9Sstevel@tonic-gate new_limit = (limit * 10) + (*args - '0');
1987c478bd9Sstevel@tonic-gate if (new_limit >= limit) {
1997c478bd9Sstevel@tonic-gate limit = new_limit;
2007c478bd9Sstevel@tonic-gate } else {
2013d9df56cSakaplan snprintf(errargs, PATH_MAX-1,
2023d9df56cSakaplan "%s: %s", argv[0], args);
2033d9df56cSakaplan failure(errargs, badnum);
2047c478bd9Sstevel@tonic-gate goto err;
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate } while (*++args);
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate /* Check for overflow! */
2097c478bd9Sstevel@tonic-gate new_limit = limit * rlimtab[res].divisor;
2107c478bd9Sstevel@tonic-gate if (new_limit >= limit) {
2117c478bd9Sstevel@tonic-gate limit = new_limit;
2127c478bd9Sstevel@tonic-gate } else {
2133d9df56cSakaplan snprintf(errargs, PATH_MAX-1,
2143d9df56cSakaplan "%s: %s", argv[0], args);
2153d9df56cSakaplan failure(errargs, badnum);
2167c478bd9Sstevel@tonic-gate goto err;
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate if (getrlimit(res, &rlimit) < 0) {
2213d9df56cSakaplan failure(argv[0], badnum);
2227c478bd9Sstevel@tonic-gate goto err;
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate if (!hard && !soft) {
2267c478bd9Sstevel@tonic-gate hard++;
2277c478bd9Sstevel@tonic-gate soft++;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate if (hard) {
2307c478bd9Sstevel@tonic-gate rlimit.rlim_max = limit;
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate if (soft) {
2337c478bd9Sstevel@tonic-gate rlimit.rlim_cur = limit;
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate if (setrlimit(res, &rlimit) < 0) {
2373d9df56cSakaplan snprintf(errargs, PATH_MAX-1,
2383d9df56cSakaplan "%s: %s", argv[0], argv[optind]);
2393d9df56cSakaplan failure(errargs, badulimit);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate err:
2437c478bd9Sstevel@tonic-gate optind = savoptind;
2447c478bd9Sstevel@tonic-gate opterr = savopterr;
2457c478bd9Sstevel@tonic-gate _sp = savsp;
2467c478bd9Sstevel@tonic-gate optarg = savoptarg;
2477c478bd9Sstevel@tonic-gate }
248