procfs_rlimit.c revision 46201
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 *
3946201Sphk *	$Id: procfs_status.c,v 1.12 1999/01/05 03:53:06 peter Exp $
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
5946201Sphk/*
6046201Sphk * This converts a quad_t to a string, stored in fin
6146201Sphk * It is here because there doesn't exist a quad_t entry in the kernel
6246201Sphk * printf() library.
6346201Sphk */
6446201Sphk
6546201Sphkstatic void quadtostring (char *fin, quad_t num)
6646201Sphk{
6746201Sphk	char str[128];
6846201Sphk	char *cp;
6946201Sphk	int i, n;
7046201Sphk
7146201Sphk	cp = str;
7246201Sphk	i = 0;
7346201Sphk
7446201Sphk	/*
7546201Sphk	 * Create the number string.
7646201Sphk	 * The string will be in reverse from the original number.
7746201Sphk	 */
7846201Sphk
7946201Sphk	while (num) {
8046201Sphk		*(cp++) = (num % 10) + '0';
8146201Sphk		num /= 10;
8246201Sphk		i++;
8346201Sphk	}
8446201Sphk
8546201Sphk	/* Null terminate */
8646201Sphk	*cp = '\0';
8746201Sphk
8846201Sphk	/*
8946201Sphk	 * Now, swap the order
9046201Sphk	 */
9146201Sphk
9246201Sphk	for (n = 0; n < i; n++) {
9346201Sphk  		fin[n] = str[(i-1)-n];
9446201Sphk	}
9546201Sphk
9646201Sphk	/* Null terminate */
9746201Sphk	fin[n] = '\0';
9846201Sphk
9946201Sphk	return;
10046201Sphk}
10146201Sphk
10246201Sphk
10346201Sphk
10446201Sphkint
10546201Sphkprocfs_dorlimit(curp, p, pfs, uio)
10646201Sphk	struct proc *curp;
10746201Sphk	struct proc *p;
10846201Sphk	struct pfsnode *pfs;
10946201Sphk	struct uio *uio;
11046201Sphk{
11146201Sphk	char *ps;
11246201Sphk	int i;
11346201Sphk	int xlen;
11446201Sphk	int error;
11546201Sphk	char psbuf[512];		/* XXX - conservative */
11646201Sphk	char qstr[64];
11746201Sphk
11846201Sphk	if (uio->uio_rw != UIO_READ)
11946201Sphk		return (EOPNOTSUPP);
12046201Sphk
12146201Sphk
12246201Sphk	ps = psbuf;
12346201Sphk
12446201Sphk	for (i = 0; i < RLIM_NLIMITS; i++) {
12546201Sphk
12646201Sphk		/*
12746201Sphk		 * Add the rlimit ident
12846201Sphk		 */
12946201Sphk
13046201Sphk		ps += sprintf(ps, "%s ", rlimit_ident[i]);
13146201Sphk
13246201Sphk		/*
13346201Sphk		 * Replace RLIM_INFINITY with -1 in the string
13446201Sphk		 */
13546201Sphk
13646201Sphk		/*
13746201Sphk		 * current limit
13846201Sphk		 */
13946201Sphk
14046201Sphk		if (p->p_rlimit[i].rlim_cur == RLIM_INFINITY) {
14146201Sphk			ps += sprintf(ps, "-1 ");
14246201Sphk		} else {
14346201Sphk			quadtostring(qstr, p->p_rlimit[i].rlim_cur);
14446201Sphk			ps += sprintf(ps, "%s ", qstr);
14546201Sphk		}
14646201Sphk
14746201Sphk		/*
14846201Sphk		 * maximum limit
14946201Sphk		 */
15046201Sphk
15146201Sphk		if (p->p_rlimit[i].rlim_max == RLIM_INFINITY) {
15246201Sphk			ps += sprintf(ps, "-1\n");
15346201Sphk		} else {
15446201Sphk			quadtostring(qstr, p->p_rlimit[i].rlim_max);
15546201Sphk			ps += sprintf(ps, "%s\n", qstr);
15646201Sphk		}
15746201Sphk	}
15846201Sphk
15946201Sphk	/*
16046201Sphk	 * This logic is rather tasty - but its from procfs_status.c, so
16146201Sphk	 * I guess I'll use it here.
16246201Sphk	 */
16346201Sphk
16446201Sphk	xlen = ps - psbuf;
16546201Sphk	xlen -= uio->uio_offset;
16646201Sphk	ps = psbuf + uio->uio_offset;
16746201Sphk	xlen = imin(xlen, uio->uio_resid);
16846201Sphk	if (xlen <= 0)
16946201Sphk		error = 0;
17046201Sphk	else
17146201Sphk		error = uiomove(ps, xlen, uio);
17246201Sphk
17346201Sphk	return (error);
17446201Sphk}
17546201Sphk
176