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