1101497Smike/*-
2101497Smike * Copyright (c) 2002 Kyle Martin <mkm@ieee.org>
3101497Smike * All rights reserved.
4101497Smike *
5101497Smike * Redistribution and use in source and binary forms, with or without
6101497Smike * modification, are permitted provided that the following conditions
7101497Smike * are met:
8101497Smike * 1. Redistributions of source code must retain the above copyright
9101497Smike *    notice, this list of conditions and the following disclaimer.
10101497Smike * 2. Redistributions in binary form must reproduce the above copyright
11101497Smike *    notice, this list of conditions and the following disclaimer in the
12101497Smike *    documentation and/or other materials provided with the distribution.
13101497Smike *
14101497Smike * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15101497Smike * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16101497Smike * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17101497Smike * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18101497Smike * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19101497Smike * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20101497Smike * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21101497Smike * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22101497Smike * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23101497Smike * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24101497Smike * SUCH DAMAGE.
25101497Smike *
26101497Smike * $FreeBSD$
27101497Smike */
28101497Smike
29101497Smike#include <sys/types.h>
30101497Smike#include <sys/time.h>
31101497Smike#include <sys/resource.h>
32101497Smike
33101497Smike#include <errno.h>
34101497Smike#include <limits.h>
35101497Smike#include <stdarg.h>
36101497Smike#include <ulimit.h>
37101497Smike
38101497Smikelong
39101497Smikeulimit(int cmd, ...)
40101497Smike{
41101497Smike	struct rlimit limit;
42101497Smike	va_list ap;
43108632Stjr	long arg;
44101497Smike
45108637Stjr	if (cmd == UL_GETFSIZE) {
46101497Smike		if (getrlimit(RLIMIT_FSIZE, &limit) == -1)
47101497Smike			return (-1);
48101497Smike		limit.rlim_cur /= 512;
49101497Smike		if (limit.rlim_cur > LONG_MAX)
50101497Smike			return (LONG_MAX);
51101497Smike		return ((long)limit.rlim_cur);
52108637Stjr	} else if (cmd == UL_SETFSIZE) {
53101497Smike		va_start(ap, cmd);
54101497Smike		arg = va_arg(ap, long);
55101497Smike		va_end(ap);
56101497Smike		limit.rlim_max = limit.rlim_cur = (rlim_t)arg * 512;
57101497Smike
58101497Smike		/* The setrlimit() function sets errno to EPERM if needed. */
59101497Smike		if (setrlimit(RLIMIT_FSIZE, &limit) == -1)
60101497Smike			return (-1);
61101497Smike		if (arg * 512 > LONG_MAX)
62101497Smike			return (LONG_MAX);
63101497Smike		return (arg);
64101497Smike	} else {
65101497Smike		errno = EINVAL;
66101497Smike		return (-1);
67101497Smike	}
68101497Smike}
69