xref: /titanic_41/usr/src/lib/libshell/common/bltins/ulimit.c (revision 4bb7efa72ed531c10f097919636e67724ec4c25a)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1982-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                  David Korn <dgk@research.att.com>                   *
18 *                                                                      *
19 ***********************************************************************/
20 #pragma prototyped
21 /*
22  * ulimit [-HSacdfmnstuv] [limit]
23  *
24  *   David Korn
25  *   AT&T Labs
26  *
27  */
28 
29 #include	<ast.h>
30 #include	<sfio.h>
31 #include	<error.h>
32 #include	<shell.h>
33 #include	"builtins.h"
34 #include	"name.h"
35 #include	"ulimit.h"
36 #ifndef SH_DICT
37 #   define SH_DICT	"libshell"
38 #endif
39 
40 #ifdef _no_ulimit
41 	int	b_ulimit(int argc,char *argv[],void *extra)
42 	{
43 		NOT_USED(argc);
44 		NOT_USED(argv);
45 		NOT_USED(extra);
46 		errormsg(SH_DICT,ERROR_exit(2),e_nosupport);
47 		return(0);
48 	}
49 #else
50 
51 static int infof(Opt_t* op, Sfio_t* sp, const char* s, Optdisc_t* dp)
52 {
53 	register const Limit_t*	tp;
54 
55 	for (tp = shtab_limits; tp->option; tp++)
56 	{
57 		sfprintf(sp, "[%c=%d:%s?The %s", tp->option, tp - shtab_limits + 1, tp->name, tp->description);
58 		if(tp->type != LIM_COUNT)
59 			sfprintf(sp, " in %ss", e_units[tp->type]);
60 		sfprintf(sp, ".]");
61 	}
62         return(1);
63 }
64 
65 #define HARD	2
66 #define SOFT	4
67 
68 int	b_ulimit(int argc,char *argv[],void *extra)
69 {
70 	register char *limit;
71 	register int mode=0, n;
72 	register unsigned long hit = 0;
73 	Shell_t *shp = (Shell_t*)extra;
74 #ifdef _lib_getrlimit
75 	struct rlimit rlp;
76 #endif /* _lib_getrlimit */
77 	const Limit_t* tp;
78 	char* conf;
79 	int label, unit, noargs, nosupport;
80 	rlim_t i;
81 	char tmp[32];
82         Optdisc_t disc;
83         memset(&disc, 0, sizeof(disc));
84         disc.version = OPT_VERSION;
85         disc.infof = infof;
86 	opt_info.disc = &disc;
87 	while((n = optget(argv,sh_optulimit))) switch(n)
88 	{
89 		case 'H':
90 			mode |= HARD;
91 			continue;
92 		case 'S':
93 			mode |= SOFT;
94 			continue;
95 		case 'a':
96 			hit = ~0;
97 			break;
98 		default:
99 			if(n < 0)
100 				hit |= (1L<<(-(n+1)));
101 			else
102 				errormsg(SH_DICT,2, e_notimp, opt_info.name);
103 			break;
104 		case ':':
105 			errormsg(SH_DICT,2, "%s", opt_info.arg);
106 			break;
107 		case '?':
108 			errormsg(SH_DICT,ERROR_usage(2), "%s", opt_info.arg);
109 			break;
110 	}
111 	opt_info.disc = 0;
112 	limit = argv[opt_info.index];
113 	/* default to -f */
114 	if(noargs=(hit==0))
115 		for(n=0; shtab_limits[n].option; n++)
116 			if(shtab_limits[n].index == RLIMIT_FSIZE)
117 			{
118 				hit |= (1L<<n);
119 				break;
120 			}
121 	/* only one option at a time for setting */
122 	label = (hit&(hit-1));
123 	if(error_info.errors || (limit && label) || argc>opt_info.index+1)
124 		errormsg(SH_DICT,ERROR_usage(2),optusage((char*)0));
125 	if(mode==0)
126 		mode = (HARD|SOFT);
127 	for(tp = shtab_limits; tp->option && hit; tp++,hit>>=1)
128 	{
129 		if(!(hit&1))
130 			continue;
131 		nosupport = (n = tp->index) == RLIMIT_UNKNOWN;
132 		unit = shtab_units[tp->type];
133 		if(limit)
134 		{
135 			if(shp->subshell)
136 				sh_subfork();
137 			if(strcmp(limit,e_unlimited)==0)
138 				i = INFINITY;
139 			else
140 			{
141 				char *last;
142 				if((i=sh_strnum(limit,&last,2))==INFINITY || *last)
143 					errormsg(SH_DICT,ERROR_system(1),e_number,limit);
144 				i *= unit;
145 			}
146 			if(nosupport)
147 				errormsg(SH_DICT,ERROR_system(1),e_readonly,tp->name);
148 			else
149 			{
150 #ifdef _lib_getrlimit
151 				if(getrlimit(n,&rlp) <0)
152 					errormsg(SH_DICT,ERROR_system(1),e_number,limit);
153 				if(mode&HARD)
154 					rlp.rlim_max = i;
155 				if(mode&SOFT)
156 					rlp.rlim_cur = i;
157 				if(setrlimit(n,&rlp) <0)
158 					errormsg(SH_DICT,ERROR_system(1),e_overlimit,limit);
159 #else
160 				if((i=vlimit(n,i)) < 0)
161 					errormsg(SH_DICT,ERROR_system(1),e_number,limit);
162 #endif /* _lib_getrlimit */
163 			}
164 		}
165 		else
166 		{
167 			if(!nosupport)
168 			{
169 #ifdef  _lib_getrlimit
170 				if(getrlimit(n,&rlp) <0)
171 					errormsg(SH_DICT,ERROR_system(1),e_number,limit);
172 				if(mode&HARD)
173 					i = rlp.rlim_max;
174 				if(mode&SOFT)
175 					i = rlp.rlim_cur;
176 #else
177 #   ifdef _lib_ulimit
178 				n--;
179 #   endif /* _lib_ulimit */
180 				i = -1;
181 				if((i=vlimit(n,i)) < 0)
182 					errormsg(SH_DICT,ERROR_system(1),e_number,limit);
183 #endif /* _lib_getrlimit */
184 			}
185 			if(label)
186 			{
187 				if(tp->type != LIM_COUNT)
188 					sfsprintf(tmp,sizeof(tmp),"%s (%ss)", tp->description, e_units[tp->type]);
189 				else
190 					sfsprintf(tmp,sizeof(tmp),"%s", tp->name);
191 				sfprintf(sfstdout,"%-30s (-%c)  ",tmp,tp->option);
192 			}
193 			if(nosupport)
194 			{
195 				if(!tp->conf || !*(conf = astconf(tp->conf, NiL, NiL)))
196 					conf = (char*)e_nosupport;
197 				sfputr(sfstdout,conf,'\n');
198 			}
199 			else if(i!=INFINITY || noargs)
200 			{
201 				if(!noargs)
202 					i += (unit-1);
203 				sfprintf(sfstdout,"%I*d\n",sizeof(i),i/unit);
204 			}
205 			else
206 				sfputr(sfstdout,e_unlimited,'\n');
207 		}
208 	}
209 	return(0);
210 }
211 #endif /* _no_ulimit */
212