procfs_rlimit.c revision 77031
1234353Sdim/*
2234353Sdim * Copyright (c) 1999 Adrian Chadd
3194612Sed * Copyright (c) 1993
4194612Sed *	The Regents of the University of California.  All rights reserved.
5194612Sed *
6194612Sed * This code is derived from software contributed to Berkeley by
7234353Sdim * Jan-Simon Pendry.
8194612Sed *
9194612Sed * Redistribution and use in source and binary forms, with or without
10194612Sed * modification, are permitted provided that the following conditions
11194612Sed * are met:
12194612Sed * 1. Redistributions of source code must retain the above copyright
13194612Sed *    notice, this list of conditions and the following disclaimer.
14199511Srdivacky * 2. Redistributions in binary form must reproduce the above copyright
15199511Srdivacky *    notice, this list of conditions and the following disclaimer in the
16207618Srdivacky *    documentation and/or other materials provided with the distribution.
17207618Srdivacky * 3. All advertising materials mentioning features or use of this software
18207618Srdivacky *    must display the following acknowledgement:
19210299Sed *	This product includes software developed by the University of
20199511Srdivacky *	California, Berkeley and its contributors.
21207618Srdivacky * 4. Neither the name of the University nor the names of its contributors
22218893Sdim *    may be used to endorse or promote products derived from this software
23199511Srdivacky *    without specific prior written permission.
24199511Srdivacky *
25207618Srdivacky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26199511Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27199511Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28207618Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29207618Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30207618Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31207618Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32199511Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33218893Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34218893Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35218893Sdim * SUCH DAMAGE.
36218893Sdim *
37218893Sdim *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
38218893Sdim *
39199511Srdivacky * $FreeBSD: head/sys/fs/procfs/procfs_rlimit.c 77031 2001-05-23 09:42:29Z ru $
40207618Srdivacky */
41207618Srdivacky
42199511Srdivacky/*
43218893Sdim * To get resource.h to include our rlimit_ident[] array of rlimit identifiers
44218893Sdim */
45218893Sdim
46218893Sdim#define _RLIMIT_IDENT
47218893Sdim
48199511Srdivacky#include <sys/param.h>
49207618Srdivacky#include <sys/systm.h>
50207618Srdivacky#include <sys/proc.h>
51207618Srdivacky#include <sys/vnode.h>
52207618Srdivacky#include <sys/resourcevar.h>
53199511Srdivacky#include <sys/resource.h>
54218893Sdim#include <sys/types.h>
55218893Sdim#include <fs/procfs/procfs.h>
56218893Sdim
57218893Sdim
58218893Sdimint
59218893Sdimprocfs_dorlimit(curp, p, pfs, uio)
60199511Srdivacky	struct proc *curp;
61207618Srdivacky	struct proc *p;
62207618Srdivacky	struct pfsnode *pfs;
63207618Srdivacky	struct uio *uio;
64207618Srdivacky{
65218893Sdim	char *ps;
66218893Sdim	int i;
67218893Sdim	int xlen;
68218893Sdim	int error;
69218893Sdim	char psbuf[512];		/* XXX - conservative */
70218893Sdim
71218893Sdim	if (uio->uio_rw != UIO_READ)
72218893Sdim		return (EOPNOTSUPP);
73199511Srdivacky
74199511Srdivacky
75207618Srdivacky	ps = psbuf;
76207618Srdivacky
77207618Srdivacky	for (i = 0; i < RLIM_NLIMITS; i++) {
78207618Srdivacky
79218893Sdim		/*
80218893Sdim		 * Add the rlimit ident
81218893Sdim		 */
82218893Sdim
83218893Sdim		ps += sprintf(ps, "%s ", rlimit_ident[i]);
84218893Sdim
85218893Sdim		/*
86218893Sdim		 * Replace RLIM_INFINITY with -1 in the string
87199511Srdivacky		 */
88199511Srdivacky
89199511Srdivacky		/*
90207618Srdivacky		 * current limit
91207618Srdivacky		 */
92207618Srdivacky
93207618Srdivacky		if (p->p_rlimit[i].rlim_cur == RLIM_INFINITY) {
94207618Srdivacky			ps += sprintf(ps, "-1 ");
95207618Srdivacky		} else {
96199511Srdivacky			ps += sprintf(ps, "%llu ",
97199511Srdivacky				(unsigned long long)p->p_rlimit[i].rlim_cur);
98199511Srdivacky		}
99199511Srdivacky
100218893Sdim		/*
101218893Sdim		 * maximum limit
102218893Sdim		 */
103199511Srdivacky
104199511Srdivacky		if (p->p_rlimit[i].rlim_max == RLIM_INFINITY) {
105218893Sdim			ps += sprintf(ps, "-1\n");
106218893Sdim		} else {
107218893Sdim			ps += sprintf(ps, "%llu\n",
108199511Srdivacky				(unsigned long long)p->p_rlimit[i].rlim_max);
109199511Srdivacky		}
110218893Sdim	}
111218893Sdim
112199511Srdivacky	/*
113199511Srdivacky	 * This logic is rather tasty - but its from procfs_status.c, so
114218893Sdim	 * I guess I'll use it here.
115218893Sdim	 */
116199511Srdivacky
117199511Srdivacky	xlen = ps - psbuf;
118218893Sdim	xlen -= uio->uio_offset;
119218893Sdim	ps = psbuf + uio->uio_offset;
120218893Sdim	xlen = imin(xlen, uio->uio_resid);
121199511Srdivacky	if (xlen <= 0)
122199511Srdivacky		error = 0;
123218893Sdim	else
124218893Sdim		error = uiomove(ps, xlen, uio);
125199511Srdivacky
126199511Srdivacky	return (error);
127218893Sdim}
128218893Sdim
129218893Sdim