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