da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER START
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The contents of this file are subject to the terms of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Common Development and Distribution License (the "License").
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner * You may not use this file except in compliance with the License.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin * or http://www.opensolaris.org/os/licensing.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * See the License for the specific language governing permissions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * and limitations under the License.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * When distributing Covered Code, include this CDDL HEADER in each
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If applicable, add the following below this CDDL HEADER, with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * fields enclosed by brackets "[]" replaced with your own identifying
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * information: Portions Copyright [yyyy] [name of copyright owner]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER END
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Use is subject to license terms.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <stdio.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/types.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/resource.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <string.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <signal.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <limits.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <errno.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <fcntl.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <stdlib.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <unistd.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <time.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <locale.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <libintl.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <ctype.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <pkglib.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <libinst.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define ERR_SET_ULIMIT "unable to set ulimit to <%ld> blocks"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define ERR_DO_ULIMIT "An attempt was made to create a file larger than " \
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "ULIMIT. Source of fault is unknown."
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define ERR_SCRULIMIT "Script <%s> attempted to create a file exceeding " \
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "ULIMIT."
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic char *script_name = NULL, *scr_error = NULL;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic struct rlimit ulimit = {RLIM_INFINITY, RLIM_INFINITY};
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic struct rlimit dblimit = {RLIM_INFINITY, RLIM_INFINITY};
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int limit_is_set = 0, fail_return = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvoid ulimit_quit(); /* XFSZ controlled signal handler. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint clr_ulimit(); /* Clear the user supplied file size limit. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvoid set_limit(); /* Called from installf to undo ulimit */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint set_ulimit(char *script, char *err_msg);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint assign_ulimit(char *fslimit);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern int warnflag;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvoid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset_limit()
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin limit_is_set = 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
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);
}