xref: /onnv-gate/usr/src/cmd/svr4pkg/libinst/doulimit.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM 
28*9781SMoriah.Waterland@Sun.COM 
29*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
30*9781SMoriah.Waterland@Sun.COM #include <sys/types.h>
31*9781SMoriah.Waterland@Sun.COM #include <sys/resource.h>
32*9781SMoriah.Waterland@Sun.COM #include <string.h>
33*9781SMoriah.Waterland@Sun.COM #include <signal.h>
34*9781SMoriah.Waterland@Sun.COM #include <limits.h>
35*9781SMoriah.Waterland@Sun.COM #include <errno.h>
36*9781SMoriah.Waterland@Sun.COM #include <fcntl.h>
37*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
38*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
39*9781SMoriah.Waterland@Sun.COM #include <time.h>
40*9781SMoriah.Waterland@Sun.COM #include <locale.h>
41*9781SMoriah.Waterland@Sun.COM #include <libintl.h>
42*9781SMoriah.Waterland@Sun.COM #include <ctype.h>
43*9781SMoriah.Waterland@Sun.COM #include <pkglib.h>
44*9781SMoriah.Waterland@Sun.COM #include <libinst.h>
45*9781SMoriah.Waterland@Sun.COM 
46*9781SMoriah.Waterland@Sun.COM #define	ERR_SET_ULIMIT	"unable to set ulimit to <%ld> blocks"
47*9781SMoriah.Waterland@Sun.COM #define	ERR_DO_ULIMIT	"An attempt was made to create a file larger than " \
48*9781SMoriah.Waterland@Sun.COM 			    "ULIMIT. Source of fault is unknown."
49*9781SMoriah.Waterland@Sun.COM #define	ERR_SCRULIMIT	"Script <%s> attempted to create a file exceeding " \
50*9781SMoriah.Waterland@Sun.COM 			    "ULIMIT."
51*9781SMoriah.Waterland@Sun.COM 
52*9781SMoriah.Waterland@Sun.COM static char *script_name = NULL, *scr_error = NULL;
53*9781SMoriah.Waterland@Sun.COM static struct rlimit ulimit = {RLIM_INFINITY, RLIM_INFINITY};
54*9781SMoriah.Waterland@Sun.COM static struct rlimit dblimit = {RLIM_INFINITY, RLIM_INFINITY};
55*9781SMoriah.Waterland@Sun.COM static int limit_is_set = 0, fail_return = 0;
56*9781SMoriah.Waterland@Sun.COM 
57*9781SMoriah.Waterland@Sun.COM void ulimit_quit();	/* XFSZ controlled signal handler. */
58*9781SMoriah.Waterland@Sun.COM int clr_ulimit();	/* Clear the user supplied file size limit. */
59*9781SMoriah.Waterland@Sun.COM void set_limit();	/* Called from installf to undo ulimit */
60*9781SMoriah.Waterland@Sun.COM int set_ulimit(char *script, char *err_msg);
61*9781SMoriah.Waterland@Sun.COM int assign_ulimit(char *fslimit);
62*9781SMoriah.Waterland@Sun.COM 
63*9781SMoriah.Waterland@Sun.COM extern int	warnflag;
64*9781SMoriah.Waterland@Sun.COM 
65*9781SMoriah.Waterland@Sun.COM void
set_limit()66*9781SMoriah.Waterland@Sun.COM set_limit()
67*9781SMoriah.Waterland@Sun.COM {
68*9781SMoriah.Waterland@Sun.COM 	limit_is_set = 1;
69*9781SMoriah.Waterland@Sun.COM }
70*9781SMoriah.Waterland@Sun.COM 
71*9781SMoriah.Waterland@Sun.COM int
clr_ulimit()72*9781SMoriah.Waterland@Sun.COM clr_ulimit()
73*9781SMoriah.Waterland@Sun.COM {
74*9781SMoriah.Waterland@Sun.COM 	if (limit_is_set) {
75*9781SMoriah.Waterland@Sun.COM 		if (script_name)
76*9781SMoriah.Waterland@Sun.COM 			free(script_name);
77*9781SMoriah.Waterland@Sun.COM 		script_name = NULL;
78*9781SMoriah.Waterland@Sun.COM 		if (scr_error)
79*9781SMoriah.Waterland@Sun.COM 			free(scr_error);
80*9781SMoriah.Waterland@Sun.COM 		scr_error = NULL;
81*9781SMoriah.Waterland@Sun.COM 		fail_return = 99;
82*9781SMoriah.Waterland@Sun.COM 
83*9781SMoriah.Waterland@Sun.COM 		/* Clear out the limit to infinity. */
84*9781SMoriah.Waterland@Sun.COM 		return (setrlimit(RLIMIT_FSIZE, &dblimit));
85*9781SMoriah.Waterland@Sun.COM 	} else
86*9781SMoriah.Waterland@Sun.COM 		return (0);
87*9781SMoriah.Waterland@Sun.COM }
88*9781SMoriah.Waterland@Sun.COM 
89*9781SMoriah.Waterland@Sun.COM /*
90*9781SMoriah.Waterland@Sun.COM  * This sets up the ULIMIT facility for the signal retrieval. This sets up
91*9781SMoriah.Waterland@Sun.COM  * the static pointers to the message constants for indicating where the
92*9781SMoriah.Waterland@Sun.COM  * error occurred.
93*9781SMoriah.Waterland@Sun.COM  */
94*9781SMoriah.Waterland@Sun.COM int
set_ulimit(char * script,char * err_msg)95*9781SMoriah.Waterland@Sun.COM set_ulimit(char *script, char *err_msg)
96*9781SMoriah.Waterland@Sun.COM {
97*9781SMoriah.Waterland@Sun.COM 	int n;
98*9781SMoriah.Waterland@Sun.COM 
99*9781SMoriah.Waterland@Sun.COM 	if (limit_is_set) {
100*9781SMoriah.Waterland@Sun.COM 		(void) signal(SIGXFSZ, ulimit_quit);
101*9781SMoriah.Waterland@Sun.COM 		if (script_name)
102*9781SMoriah.Waterland@Sun.COM 			free(script_name);
103*9781SMoriah.Waterland@Sun.COM 		script_name = strdup(script);
104*9781SMoriah.Waterland@Sun.COM 		if (scr_error)
105*9781SMoriah.Waterland@Sun.COM 			free(scr_error);
106*9781SMoriah.Waterland@Sun.COM 		scr_error = strdup(err_msg);
107*9781SMoriah.Waterland@Sun.COM 		fail_return = 99;
108*9781SMoriah.Waterland@Sun.COM 
109*9781SMoriah.Waterland@Sun.COM 		n = setrlimit(RLIMIT_FSIZE, &ulimit);
110*9781SMoriah.Waterland@Sun.COM 
111*9781SMoriah.Waterland@Sun.COM 		return (n);
112*9781SMoriah.Waterland@Sun.COM 	} else
113*9781SMoriah.Waterland@Sun.COM 		return (0);
114*9781SMoriah.Waterland@Sun.COM 
115*9781SMoriah.Waterland@Sun.COM }
116*9781SMoriah.Waterland@Sun.COM 
117*9781SMoriah.Waterland@Sun.COM /* Validate ULIMIT and set accordingly. */
118*9781SMoriah.Waterland@Sun.COM int
assign_ulimit(char * fslimit)119*9781SMoriah.Waterland@Sun.COM assign_ulimit(char *fslimit)
120*9781SMoriah.Waterland@Sun.COM {
121*9781SMoriah.Waterland@Sun.COM 	rlim_t limit;
122*9781SMoriah.Waterland@Sun.COM 	int cnt = 0;
123*9781SMoriah.Waterland@Sun.COM 
124*9781SMoriah.Waterland@Sun.COM 	if (fslimit && *fslimit) {
125*9781SMoriah.Waterland@Sun.COM 		/* fslimit must be a simple unsigned integer. */
126*9781SMoriah.Waterland@Sun.COM 		do {
127*9781SMoriah.Waterland@Sun.COM 			if (!isdigit(fslimit[cnt]))
128*9781SMoriah.Waterland@Sun.COM 				return (-1);
129*9781SMoriah.Waterland@Sun.COM 		} while (fslimit[++cnt]);
130*9781SMoriah.Waterland@Sun.COM 
131*9781SMoriah.Waterland@Sun.COM 		limit = atol(fslimit);
132*9781SMoriah.Waterland@Sun.COM 
133*9781SMoriah.Waterland@Sun.COM 		ulimit.rlim_cur = (limit * 512); /* fslimit is in blocks */
134*9781SMoriah.Waterland@Sun.COM 
135*9781SMoriah.Waterland@Sun.COM 		limit_is_set = 1;
136*9781SMoriah.Waterland@Sun.COM 
137*9781SMoriah.Waterland@Sun.COM 		return (0);
138*9781SMoriah.Waterland@Sun.COM 	} else
139*9781SMoriah.Waterland@Sun.COM 		return (-1);
140*9781SMoriah.Waterland@Sun.COM }
141*9781SMoriah.Waterland@Sun.COM 
142*9781SMoriah.Waterland@Sun.COM /*
143*9781SMoriah.Waterland@Sun.COM  * This is the signal handler for ULIMIT.
144*9781SMoriah.Waterland@Sun.COM  */
145*9781SMoriah.Waterland@Sun.COM void
ulimit_quit(int n)146*9781SMoriah.Waterland@Sun.COM ulimit_quit(int n)
147*9781SMoriah.Waterland@Sun.COM {
148*9781SMoriah.Waterland@Sun.COM #ifdef lint
149*9781SMoriah.Waterland@Sun.COM 	int i = n;
150*9781SMoriah.Waterland@Sun.COM 	n = i;
151*9781SMoriah.Waterland@Sun.COM #endif	/* lint */
152*9781SMoriah.Waterland@Sun.COM 
153*9781SMoriah.Waterland@Sun.COM 	setrlimit(RLIMIT_FSIZE, &dblimit);
154*9781SMoriah.Waterland@Sun.COM 	signal(SIGXFSZ, SIG_IGN);
155*9781SMoriah.Waterland@Sun.COM 
156*9781SMoriah.Waterland@Sun.COM 	if (script_name) {
157*9781SMoriah.Waterland@Sun.COM 		progerr(gettext(ERR_SCRULIMIT), script_name);
158*9781SMoriah.Waterland@Sun.COM 		if (scr_error)
159*9781SMoriah.Waterland@Sun.COM 			progerr("%s", scr_error);
160*9781SMoriah.Waterland@Sun.COM 	} else
161*9781SMoriah.Waterland@Sun.COM 		progerr(gettext(ERR_DO_ULIMIT));
162*9781SMoriah.Waterland@Sun.COM 
163*9781SMoriah.Waterland@Sun.COM 	quit(fail_return);
164*9781SMoriah.Waterland@Sun.COM }
165