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*39e7390aSna195498 * Common Development and Distribution License (the "License").
6*39e7390aSna195498 * 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*39e7390aSna195498
223d9df56cSakaplan /*
233d9df56cSakaplan * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
243d9df56cSakaplan * Use is subject to license terms.
253d9df56cSakaplan */
26*39e7390aSna195498
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate
313d9df56cSakaplan #pragma ident "%Z%%M% %I% %E% SMI"
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate * ulimit builtin
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #include <sys/resource.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include "defs.h"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate * order is important in this table! it is indexed by resource ID.
437c478bd9Sstevel@tonic-gate */
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate static struct rlimtab {
467c478bd9Sstevel@tonic-gate char *name;
477c478bd9Sstevel@tonic-gate char *scale;
487c478bd9Sstevel@tonic-gate rlim_t divisor;
497c478bd9Sstevel@tonic-gate } rlimtab[] = {
507c478bd9Sstevel@tonic-gate /* RLIMIT_CPU */ "time", "seconds", 1,
517c478bd9Sstevel@tonic-gate /* RLIMIT_FSIZE */ "file", "blocks", 512,
527c478bd9Sstevel@tonic-gate /* RLIMIT_DATA */ "data", "kbytes", 1024,
537c478bd9Sstevel@tonic-gate /* RLIMIT_STACK */ "stack", "kbytes", 1024,
547c478bd9Sstevel@tonic-gate /* RLIMIT_CORE */ "coredump", "blocks", 512,
557c478bd9Sstevel@tonic-gate /* RLIMIT_NOFILE */ "nofiles", "descriptors", 1,
567c478bd9Sstevel@tonic-gate /* RLIMIT_VMEM */ "memory", "kbytes", 1024,
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate void
sysulimit(int argc,char ** argv)607c478bd9Sstevel@tonic-gate sysulimit(int argc, char **argv)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate extern int opterr, optind;
637c478bd9Sstevel@tonic-gate int savopterr, savoptind, savsp;
647c478bd9Sstevel@tonic-gate char *savoptarg;
657c478bd9Sstevel@tonic-gate char *args;
663d9df56cSakaplan char errargs[PATH_MAX];
677c478bd9Sstevel@tonic-gate int hard, soft, cnt, c, res;
687c478bd9Sstevel@tonic-gate rlim_t limit, new_limit;
697c478bd9Sstevel@tonic-gate struct rlimit rlimit;
707c478bd9Sstevel@tonic-gate char resources[RLIM_NLIMITS];
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate for (res = 0; res < RLIM_NLIMITS; res++) {
737c478bd9Sstevel@tonic-gate resources[res] = 0;
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate savoptind = optind;
777c478bd9Sstevel@tonic-gate savopterr = opterr;
787c478bd9Sstevel@tonic-gate savsp = _sp;
797c478bd9Sstevel@tonic-gate savoptarg = optarg;
807c478bd9Sstevel@tonic-gate optind = 1;
817c478bd9Sstevel@tonic-gate _sp = 1;
827c478bd9Sstevel@tonic-gate opterr = 0;
837c478bd9Sstevel@tonic-gate hard = 0;
847c478bd9Sstevel@tonic-gate soft = 0;
857c478bd9Sstevel@tonic-gate cnt = 0;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "HSacdfnstv")) != -1) {
887c478bd9Sstevel@tonic-gate switch (c) {
897c478bd9Sstevel@tonic-gate case 'S':
907c478bd9Sstevel@tonic-gate soft++;
917c478bd9Sstevel@tonic-gate continue;
927c478bd9Sstevel@tonic-gate case 'H':
937c478bd9Sstevel@tonic-gate hard++;
947c478bd9Sstevel@tonic-gate continue;
957c478bd9Sstevel@tonic-gate case 'a':
967c478bd9Sstevel@tonic-gate for (res = 0; res < RLIM_NLIMITS; res++) {
977c478bd9Sstevel@tonic-gate resources[res]++;
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate cnt = RLIM_NLIMITS;
1007c478bd9Sstevel@tonic-gate continue;
1017c478bd9Sstevel@tonic-gate case 'c':
1027c478bd9Sstevel@tonic-gate res = RLIMIT_CORE;
1037c478bd9Sstevel@tonic-gate break;
1047c478bd9Sstevel@tonic-gate case 'd':
1057c478bd9Sstevel@tonic-gate res = RLIMIT_DATA;
1067c478bd9Sstevel@tonic-gate break;
1077c478bd9Sstevel@tonic-gate case 'f':
1087c478bd9Sstevel@tonic-gate res = RLIMIT_FSIZE;
1097c478bd9Sstevel@tonic-gate break;
1107c478bd9Sstevel@tonic-gate case 'n':
1117c478bd9Sstevel@tonic-gate res = RLIMIT_NOFILE;
1127c478bd9Sstevel@tonic-gate break;
1137c478bd9Sstevel@tonic-gate case 's':
1147c478bd9Sstevel@tonic-gate res = RLIMIT_STACK;
1157c478bd9Sstevel@tonic-gate break;
1167c478bd9Sstevel@tonic-gate case 't':
1177c478bd9Sstevel@tonic-gate res = RLIMIT_CPU;
1187c478bd9Sstevel@tonic-gate break;
1197c478bd9Sstevel@tonic-gate case 'v':
1207c478bd9Sstevel@tonic-gate res = RLIMIT_VMEM;
1217c478bd9Sstevel@tonic-gate break;
1227c478bd9Sstevel@tonic-gate case '?':
123*39e7390aSna195498 gfailure(usage, ulimuse);
1247c478bd9Sstevel@tonic-gate goto err;
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate resources[res]++;
1277c478bd9Sstevel@tonic-gate cnt++;
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate if (cnt == 0) {
1317c478bd9Sstevel@tonic-gate resources[res = RLIMIT_FSIZE]++;
1327c478bd9Sstevel@tonic-gate cnt++;
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate * if out of arguments, then print the specified resources
1377c478bd9Sstevel@tonic-gate */
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate if (optind == argc) {
1407c478bd9Sstevel@tonic-gate if (!hard && !soft) {
1417c478bd9Sstevel@tonic-gate soft++;
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate for (res = 0; res < RLIM_NLIMITS; res++) {
1447c478bd9Sstevel@tonic-gate if (resources[res] == 0) {
1457c478bd9Sstevel@tonic-gate continue;
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate if (getrlimit(res, &rlimit) < 0) {
1487c478bd9Sstevel@tonic-gate continue;
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate if (cnt > 1) {
151*39e7390aSna195498 prs_buff(_gettext(rlimtab[res].name));
1527c478bd9Sstevel@tonic-gate prc_buff('(');
153*39e7390aSna195498 prs_buff(_gettext(rlimtab[res].scale));
1547c478bd9Sstevel@tonic-gate prc_buff(')');
1557c478bd9Sstevel@tonic-gate prc_buff(' ');
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate if (soft) {
1587c478bd9Sstevel@tonic-gate if (rlimit.rlim_cur == RLIM_INFINITY) {
159*39e7390aSna195498 prs_buff(_gettext("unlimited"));
1607c478bd9Sstevel@tonic-gate } else {
1617c478bd9Sstevel@tonic-gate prull_buff(rlimit.rlim_cur /
1627c478bd9Sstevel@tonic-gate rlimtab[res].divisor);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate if (hard && soft) {
1667c478bd9Sstevel@tonic-gate prc_buff(':');
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate if (hard) {
1697c478bd9Sstevel@tonic-gate if (rlimit.rlim_max == RLIM_INFINITY) {
170*39e7390aSna195498 prs_buff(_gettext("unlimited"));
1717c478bd9Sstevel@tonic-gate } else {
1727c478bd9Sstevel@tonic-gate prull_buff(rlimit.rlim_max /
1737c478bd9Sstevel@tonic-gate rlimtab[res].divisor);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate prc_buff('\n');
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate goto err;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate if (cnt > 1 || optind + 1 != argc) {
182*39e7390aSna195498 gfailure(usage, ulimuse);
1837c478bd9Sstevel@tonic-gate goto err;
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate if (eq(argv[optind], "unlimited")) {
1877c478bd9Sstevel@tonic-gate limit = RLIM_INFINITY;
1887c478bd9Sstevel@tonic-gate } else {
1897c478bd9Sstevel@tonic-gate args = argv[optind];
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate new_limit = limit = 0;
1927c478bd9Sstevel@tonic-gate do {
1937c478bd9Sstevel@tonic-gate if (*args < '0' || *args > '9') {
1943d9df56cSakaplan snprintf(errargs, PATH_MAX-1,
1953d9df56cSakaplan "%s: %s", argv[0], args);
1963d9df56cSakaplan failure(errargs, badnum);
1977c478bd9Sstevel@tonic-gate goto err;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate /* Check for overflow! */
2007c478bd9Sstevel@tonic-gate new_limit = (limit * 10) + (*args - '0');
2017c478bd9Sstevel@tonic-gate if (new_limit >= limit) {
2027c478bd9Sstevel@tonic-gate limit = new_limit;
2037c478bd9Sstevel@tonic-gate } else {
2043d9df56cSakaplan snprintf(errargs, PATH_MAX-1,
2053d9df56cSakaplan "%s: %s", argv[0], args);
2063d9df56cSakaplan failure(errargs, badnum);
2077c478bd9Sstevel@tonic-gate goto err;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate } while (*++args);
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate /* Check for overflow! */
2127c478bd9Sstevel@tonic-gate new_limit = limit * rlimtab[res].divisor;
2137c478bd9Sstevel@tonic-gate if (new_limit >= limit) {
2147c478bd9Sstevel@tonic-gate limit = new_limit;
2157c478bd9Sstevel@tonic-gate } else {
2163d9df56cSakaplan snprintf(errargs, PATH_MAX-1,
2173d9df56cSakaplan "%s: %s", argv[0], args);
2183d9df56cSakaplan failure(errargs, badnum);
2197c478bd9Sstevel@tonic-gate goto err;
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate if (getrlimit(res, &rlimit) < 0) {
2243d9df56cSakaplan failure(argv[0], badnum);
2257c478bd9Sstevel@tonic-gate goto err;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate if (!hard && !soft) {
2297c478bd9Sstevel@tonic-gate hard++;
2307c478bd9Sstevel@tonic-gate soft++;
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate if (hard) {
2337c478bd9Sstevel@tonic-gate rlimit.rlim_max = limit;
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate if (soft) {
2367c478bd9Sstevel@tonic-gate rlimit.rlim_cur = limit;
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate if (setrlimit(res, &rlimit) < 0) {
2403d9df56cSakaplan snprintf(errargs, PATH_MAX-1,
2413d9df56cSakaplan "%s: %s", argv[0], argv[optind]);
2423d9df56cSakaplan failure(errargs, badulimit);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate err:
2467c478bd9Sstevel@tonic-gate optind = savoptind;
2477c478bd9Sstevel@tonic-gate opterr = savopterr;
2487c478bd9Sstevel@tonic-gate _sp = savsp;
2497c478bd9Sstevel@tonic-gate optarg = savoptarg;
2507c478bd9Sstevel@tonic-gate }
251