10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*2256Sna195498 * Common Development and Distribution License (the "License").
6*2256Sna195498 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*2256Sna195498
221367Sakaplan /*
231367Sakaplan * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
241367Sakaplan * Use is subject to license terms.
251367Sakaplan */
26*2256Sna195498
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate
311367Sakaplan #pragma ident "%Z%%M% %I% %E% SMI"
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * ulimit builtin
350Sstevel@tonic-gate */
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include <sys/resource.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include "defs.h"
400Sstevel@tonic-gate
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate * order is important in this table! it is indexed by resource ID.
430Sstevel@tonic-gate */
440Sstevel@tonic-gate
450Sstevel@tonic-gate static struct rlimtab {
460Sstevel@tonic-gate char *name;
470Sstevel@tonic-gate char *scale;
480Sstevel@tonic-gate rlim_t divisor;
490Sstevel@tonic-gate } rlimtab[] = {
500Sstevel@tonic-gate /* RLIMIT_CPU */ "time", "seconds", 1,
510Sstevel@tonic-gate /* RLIMIT_FSIZE */ "file", "blocks", 512,
520Sstevel@tonic-gate /* RLIMIT_DATA */ "data", "kbytes", 1024,
530Sstevel@tonic-gate /* RLIMIT_STACK */ "stack", "kbytes", 1024,
540Sstevel@tonic-gate /* RLIMIT_CORE */ "coredump", "blocks", 512,
550Sstevel@tonic-gate /* RLIMIT_NOFILE */ "nofiles", "descriptors", 1,
560Sstevel@tonic-gate /* RLIMIT_VMEM */ "memory", "kbytes", 1024,
570Sstevel@tonic-gate };
580Sstevel@tonic-gate
590Sstevel@tonic-gate void
sysulimit(int argc,char ** argv)600Sstevel@tonic-gate sysulimit(int argc, char **argv)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate extern int opterr, optind;
630Sstevel@tonic-gate int savopterr, savoptind, savsp;
640Sstevel@tonic-gate char *savoptarg;
650Sstevel@tonic-gate char *args;
661367Sakaplan char errargs[PATH_MAX];
670Sstevel@tonic-gate int hard, soft, cnt, c, res;
680Sstevel@tonic-gate rlim_t limit, new_limit;
690Sstevel@tonic-gate struct rlimit rlimit;
700Sstevel@tonic-gate char resources[RLIM_NLIMITS];
710Sstevel@tonic-gate
720Sstevel@tonic-gate for (res = 0; res < RLIM_NLIMITS; res++) {
730Sstevel@tonic-gate resources[res] = 0;
740Sstevel@tonic-gate }
750Sstevel@tonic-gate
760Sstevel@tonic-gate savoptind = optind;
770Sstevel@tonic-gate savopterr = opterr;
780Sstevel@tonic-gate savsp = _sp;
790Sstevel@tonic-gate savoptarg = optarg;
800Sstevel@tonic-gate optind = 1;
810Sstevel@tonic-gate _sp = 1;
820Sstevel@tonic-gate opterr = 0;
830Sstevel@tonic-gate hard = 0;
840Sstevel@tonic-gate soft = 0;
850Sstevel@tonic-gate cnt = 0;
860Sstevel@tonic-gate
870Sstevel@tonic-gate while ((c = getopt(argc, argv, "HSacdfnstv")) != -1) {
880Sstevel@tonic-gate switch (c) {
890Sstevel@tonic-gate case 'S':
900Sstevel@tonic-gate soft++;
910Sstevel@tonic-gate continue;
920Sstevel@tonic-gate case 'H':
930Sstevel@tonic-gate hard++;
940Sstevel@tonic-gate continue;
950Sstevel@tonic-gate case 'a':
960Sstevel@tonic-gate for (res = 0; res < RLIM_NLIMITS; res++) {
970Sstevel@tonic-gate resources[res]++;
980Sstevel@tonic-gate }
990Sstevel@tonic-gate cnt = RLIM_NLIMITS;
1000Sstevel@tonic-gate continue;
1010Sstevel@tonic-gate case 'c':
1020Sstevel@tonic-gate res = RLIMIT_CORE;
1030Sstevel@tonic-gate break;
1040Sstevel@tonic-gate case 'd':
1050Sstevel@tonic-gate res = RLIMIT_DATA;
1060Sstevel@tonic-gate break;
1070Sstevel@tonic-gate case 'f':
1080Sstevel@tonic-gate res = RLIMIT_FSIZE;
1090Sstevel@tonic-gate break;
1100Sstevel@tonic-gate case 'n':
1110Sstevel@tonic-gate res = RLIMIT_NOFILE;
1120Sstevel@tonic-gate break;
1130Sstevel@tonic-gate case 's':
1140Sstevel@tonic-gate res = RLIMIT_STACK;
1150Sstevel@tonic-gate break;
1160Sstevel@tonic-gate case 't':
1170Sstevel@tonic-gate res = RLIMIT_CPU;
1180Sstevel@tonic-gate break;
1190Sstevel@tonic-gate case 'v':
1200Sstevel@tonic-gate res = RLIMIT_VMEM;
1210Sstevel@tonic-gate break;
1220Sstevel@tonic-gate case '?':
123*2256Sna195498 gfailure(usage, ulimuse);
1240Sstevel@tonic-gate goto err;
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate resources[res]++;
1270Sstevel@tonic-gate cnt++;
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (cnt == 0) {
1310Sstevel@tonic-gate resources[res = RLIMIT_FSIZE]++;
1320Sstevel@tonic-gate cnt++;
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate * if out of arguments, then print the specified resources
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if (optind == argc) {
1400Sstevel@tonic-gate if (!hard && !soft) {
1410Sstevel@tonic-gate soft++;
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate for (res = 0; res < RLIM_NLIMITS; res++) {
1440Sstevel@tonic-gate if (resources[res] == 0) {
1450Sstevel@tonic-gate continue;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate if (getrlimit(res, &rlimit) < 0) {
1480Sstevel@tonic-gate continue;
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate if (cnt > 1) {
151*2256Sna195498 prs_buff(_gettext(rlimtab[res].name));
1520Sstevel@tonic-gate prc_buff('(');
153*2256Sna195498 prs_buff(_gettext(rlimtab[res].scale));
1540Sstevel@tonic-gate prc_buff(')');
1550Sstevel@tonic-gate prc_buff(' ');
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate if (soft) {
1580Sstevel@tonic-gate if (rlimit.rlim_cur == RLIM_INFINITY) {
159*2256Sna195498 prs_buff(_gettext("unlimited"));
1600Sstevel@tonic-gate } else {
1610Sstevel@tonic-gate prull_buff(rlimit.rlim_cur /
1620Sstevel@tonic-gate rlimtab[res].divisor);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate if (hard && soft) {
1660Sstevel@tonic-gate prc_buff(':');
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate if (hard) {
1690Sstevel@tonic-gate if (rlimit.rlim_max == RLIM_INFINITY) {
170*2256Sna195498 prs_buff(_gettext("unlimited"));
1710Sstevel@tonic-gate } else {
1720Sstevel@tonic-gate prull_buff(rlimit.rlim_max /
1730Sstevel@tonic-gate rlimtab[res].divisor);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate prc_buff('\n');
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate goto err;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate if (cnt > 1 || optind + 1 != argc) {
182*2256Sna195498 gfailure(usage, ulimuse);
1830Sstevel@tonic-gate goto err;
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate if (eq(argv[optind], "unlimited")) {
1870Sstevel@tonic-gate limit = RLIM_INFINITY;
1880Sstevel@tonic-gate } else {
1890Sstevel@tonic-gate args = argv[optind];
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate new_limit = limit = 0;
1920Sstevel@tonic-gate do {
1930Sstevel@tonic-gate if (*args < '0' || *args > '9') {
1941367Sakaplan snprintf(errargs, PATH_MAX-1,
1951367Sakaplan "%s: %s", argv[0], args);
1961367Sakaplan failure(errargs, badnum);
1970Sstevel@tonic-gate goto err;
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate /* Check for overflow! */
2000Sstevel@tonic-gate new_limit = (limit * 10) + (*args - '0');
2010Sstevel@tonic-gate if (new_limit >= limit) {
2020Sstevel@tonic-gate limit = new_limit;
2030Sstevel@tonic-gate } else {
2041367Sakaplan snprintf(errargs, PATH_MAX-1,
2051367Sakaplan "%s: %s", argv[0], args);
2061367Sakaplan failure(errargs, badnum);
2070Sstevel@tonic-gate goto err;
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate } while (*++args);
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate /* Check for overflow! */
2120Sstevel@tonic-gate new_limit = limit * rlimtab[res].divisor;
2130Sstevel@tonic-gate if (new_limit >= limit) {
2140Sstevel@tonic-gate limit = new_limit;
2150Sstevel@tonic-gate } else {
2161367Sakaplan snprintf(errargs, PATH_MAX-1,
2171367Sakaplan "%s: %s", argv[0], args);
2181367Sakaplan failure(errargs, badnum);
2190Sstevel@tonic-gate goto err;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate if (getrlimit(res, &rlimit) < 0) {
2241367Sakaplan failure(argv[0], badnum);
2250Sstevel@tonic-gate goto err;
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate if (!hard && !soft) {
2290Sstevel@tonic-gate hard++;
2300Sstevel@tonic-gate soft++;
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate if (hard) {
2330Sstevel@tonic-gate rlimit.rlim_max = limit;
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate if (soft) {
2360Sstevel@tonic-gate rlimit.rlim_cur = limit;
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate if (setrlimit(res, &rlimit) < 0) {
2401367Sakaplan snprintf(errargs, PATH_MAX-1,
2411367Sakaplan "%s: %s", argv[0], argv[optind]);
2421367Sakaplan failure(errargs, badulimit);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate err:
2460Sstevel@tonic-gate optind = savoptind;
2470Sstevel@tonic-gate opterr = savopterr;
2480Sstevel@tonic-gate _sp = savsp;
2490Sstevel@tonic-gate optarg = savoptarg;
2500Sstevel@tonic-gate }
251