summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svr4pkg/libinst/doulimit.c
blob: 7400a0c0528f328a0bdfd96fc85873dbc3ed84dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */



#include <stdio.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <string.h>
#include <signal.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <locale.h>
#include <libintl.h>
#include <ctype.h>
#include <pkglib.h>
#include <libinst.h>

#define	ERR_SET_ULIMIT	"unable to set ulimit to <%ld> blocks"
#define	ERR_DO_ULIMIT	"An attempt was made to create a file larger than " \
			    "ULIMIT. Source of fault is unknown."
#define	ERR_SCRULIMIT	"Script <%s> attempted to create a file exceeding " \
			    "ULIMIT."

static char *script_name = NULL, *scr_error = NULL;
static struct rlimit ulimit = {RLIM_INFINITY, RLIM_INFINITY};
static struct rlimit dblimit = {RLIM_INFINITY, RLIM_INFINITY};
static int limit_is_set = 0, fail_return = 0;

void ulimit_quit();	/* XFSZ controlled signal handler. */
int clr_ulimit();	/* Clear the user supplied file size limit. */
void set_limit();	/* Called from installf to undo ulimit */
int set_ulimit(char *script, char *err_msg);
int assign_ulimit(char *fslimit);

extern int	warnflag;

void
set_limit()
{
	limit_is_set = 1;
}

int
clr_ulimit()
{
	if (limit_is_set) {
		if (script_name)
			free(script_name);
		script_name = NULL;
		if (scr_error)
			free(scr_error);
		scr_error = NULL;
		fail_return = 99;

		/* Clear out the limit to infinity. */
		return (setrlimit(RLIMIT_FSIZE, &dblimit));
	} else
		return (0);
}

/*
 * This sets up the ULIMIT facility for the signal retrieval. This sets up
 * the static pointers to the message constants for indicating where the
 * error occurred.
 */
int
set_ulimit(char *script, char *err_msg)
{
	int n;

	if (limit_is_set) {
		(void) signal(SIGXFSZ, ulimit_quit);
		if (script_name)
			free(script_name);
		script_name = strdup(script);
		if (scr_error)
			free(scr_error);
		scr_error = strdup(err_msg);
		fail_return = 99;

		n = setrlimit(RLIMIT_FSIZE, &ulimit);

		return (n);
	} else
		return (0);

}

/* Validate ULIMIT and set accordingly. */
int
assign_ulimit(char *fslimit)
{
	rlim_t limit;
	int cnt = 0;

	if (fslimit && *fslimit) {
		/* fslimit must be a simple unsigned integer. */
		do {
			if (!isdigit(fslimit[cnt]))
				return (-1);
		} while (fslimit[++cnt]);

		limit = atol(fslimit);

		ulimit.rlim_cur = (limit * 512); /* fslimit is in blocks */

		limit_is_set = 1;

		return (0);
	} else
		return (-1);
}

/*
 * This is the signal handler for ULIMIT.
 */
void
ulimit_quit(int n)
{
#ifdef lint
	int i = n;
	n = i;
#endif	/* lint */

	setrlimit(RLIMIT_FSIZE, &dblimit);
	signal(SIGXFSZ, SIG_IGN);

	if (script_name) {
		progerr(gettext(ERR_SCRULIMIT), script_name);
		if (scr_error)
			progerr("%s", scr_error);
	} else
		progerr(gettext(ERR_DO_ULIMIT));

	quit(fail_return);
}