procfs_rlimit.c revision 50477
146201Sphk/*
246201Sphk * Copyright (c) 1999 Adrian Chadd
346201Sphk * Copyright (c) 1993
446201Sphk *	The Regents of the University of California.  All rights reserved.
546201Sphk *
646201Sphk * This code is derived from software contributed to Berkeley by
746201Sphk * Jan-Simon Pendry.
846201Sphk *
946201Sphk * Redistribution and use in source and binary forms, with or without
1046201Sphk * modification, are permitted provided that the following conditions
1146201Sphk * are met:
1246201Sphk * 1. Redistributions of source code must retain the above copyright
1346201Sphk *    notice, this list of conditions and the following disclaimer.
1446201Sphk * 2. Redistributions in binary form must reproduce the above copyright
1546201Sphk *    notice, this list of conditions and the following disclaimer in the
1646201Sphk *    documentation and/or other materials provided with the distribution.
1746201Sphk * 3. All advertising materials mentioning features or use of this software
1846201Sphk *    must display the following acknowledgement:
1946201Sphk *	This product includes software developed by the University of
2046201Sphk *	California, Berkeley and its contributors.
2146201Sphk * 4. Neither the name of the University nor the names of its contributors
2246201Sphk *    may be used to endorse or promote products derived from this software
2346201Sphk *    without specific prior written permission.
2446201Sphk *
2546201Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2646201Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2746201Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2846201Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2946201Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3046201Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3146201Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3246201Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3346201Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3446201Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3546201Sphk * SUCH DAMAGE.
3646201Sphk *
3746201Sphk *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
3846201Sphk *
3950477Speter * $FreeBSD: head/sys/fs/procfs/procfs_rlimit.c 50477 1999-08-28 01:08:13Z peter $
4046201Sphk */
4146201Sphk
4246201Sphk/*
4346201Sphk * To get resource.h to include our rlimit_ident[] array of rlimit identifiers
4446201Sphk */
4546201Sphk
4646201Sphk#define _RLIMIT_IDENT
4746201Sphk
4846201Sphk#include <sys/param.h>
4946201Sphk#include <sys/systm.h>
5046201Sphk#include <sys/proc.h>
5146201Sphk#include <sys/vnode.h>
5246201Sphk#include <sys/tty.h>
5346201Sphk#include <sys/resourcevar.h>
5446201Sphk#include <sys/resource.h>
5546201Sphk#include <sys/types.h>
5646201Sphk#include <miscfs/procfs/procfs.h>
5746201Sphk
5846201Sphk
5946201Sphkint
6046201Sphkprocfs_dorlimit(curp, p, pfs, uio)
6146201Sphk	struct proc *curp;
6246201Sphk	struct proc *p;
6346201Sphk	struct pfsnode *pfs;
6446201Sphk	struct uio *uio;
6546201Sphk{
6646201Sphk	char *ps;
6746201Sphk	int i;
6846201Sphk	int xlen;
6946201Sphk	int error;
7046201Sphk	char psbuf[512];		/* XXX - conservative */
7146201Sphk
7246201Sphk	if (uio->uio_rw != UIO_READ)
7346201Sphk		return (EOPNOTSUPP);
7446201Sphk
7546201Sphk
7646201Sphk	ps = psbuf;
7746201Sphk
7846201Sphk	for (i = 0; i < RLIM_NLIMITS; i++) {
7946201Sphk
8046201Sphk		/*
8146201Sphk		 * Add the rlimit ident
8246201Sphk		 */
8346201Sphk
8446201Sphk		ps += sprintf(ps, "%s ", rlimit_ident[i]);
8546201Sphk
8646201Sphk		/*
8746201Sphk		 * Replace RLIM_INFINITY with -1 in the string
8846201Sphk		 */
8946201Sphk
9046201Sphk		/*
9146201Sphk		 * current limit
9246201Sphk		 */
9346201Sphk
9446201Sphk		if (p->p_rlimit[i].rlim_cur == RLIM_INFINITY) {
9546201Sphk			ps += sprintf(ps, "-1 ");
9646201Sphk		} else {
9749525Sbde			ps += sprintf(ps, "%llu ",
9848715Speter				(unsigned long long)p->p_rlimit[i].rlim_cur);
9946201Sphk		}
10046201Sphk
10146201Sphk		/*
10246201Sphk		 * maximum limit
10346201Sphk		 */
10446201Sphk
10546201Sphk		if (p->p_rlimit[i].rlim_max == RLIM_INFINITY) {
10646201Sphk			ps += sprintf(ps, "-1\n");
10746201Sphk		} else {
10849525Sbde			ps += sprintf(ps, "%llu\n",
10948715Speter				(unsigned long long)p->p_rlimit[i].rlim_max);
11046201Sphk		}
11146201Sphk	}
11246201Sphk
11346201Sphk	/*
11446201Sphk	 * This logic is rather tasty - but its from procfs_status.c, so
11546201Sphk	 * I guess I'll use it here.
11646201Sphk	 */
11746201Sphk
11846201Sphk	xlen = ps - psbuf;
11946201Sphk	xlen -= uio->uio_offset;
12046201Sphk	ps = psbuf + uio->uio_offset;
12146201Sphk	xlen = imin(xlen, uio->uio_resid);
12246201Sphk	if (xlen <= 0)
12346201Sphk		error = 0;
12446201Sphk	else
12546201Sphk		error = uiomove(ps, xlen, uio);
12646201Sphk
12746201Sphk	return (error);
12846201Sphk}
12946201Sphk
130