Home | History | Annotate | Line # | Download | only in procfs
      1  1.5  christos /*	$NetBSD: procfs_limit.c,v 1.5 2024/05/12 17:22:29 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Christos Zoulas.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.1  christos #include <sys/cdefs.h>
     33  1.5  christos __KERNEL_RCSID(0, "$NetBSD: procfs_limit.c,v 1.5 2024/05/12 17:22:29 christos Exp $");
     34  1.5  christos 
     35  1.5  christos #if defined(_KERNEL_OPT)
     36  1.5  christos #include "opt_sysv.h"
     37  1.5  christos #endif
     38  1.1  christos 
     39  1.1  christos #include <sys/param.h>
     40  1.1  christos #include <sys/systm.h>
     41  1.1  christos #include <sys/proc.h>
     42  1.1  christos #include <sys/malloc.h>
     43  1.1  christos #include <sys/resource.h>
     44  1.1  christos #include <miscfs/procfs/procfs.h>
     45  1.5  christos #include <compat/linux/common/linux_misc.h>
     46  1.5  christos #ifdef SYSVMSG
     47  1.5  christos #include <sys/msg.h>
     48  1.5  christos #endif
     49  1.5  christos 
     50  1.5  christos /* Taken from FreeBSD sys/compat/linprocfs/linprocfs.c */
     51  1.5  christos static const struct linux_rlimit_ident {
     52  1.5  christos 	const char      *desc;
     53  1.5  christos 	const char      *unit;
     54  1.5  christos 	unsigned int    rlim_id;
     55  1.5  christos } linux_rlimits_ident[] = {
     56  1.5  christos 	{ "Max cpu time",	"seconds",	RLIMIT_CPU },
     57  1.5  christos 	{ "Max file size",	"bytes",	RLIMIT_FSIZE },
     58  1.5  christos 	{ "Max data size",	"bytes",	RLIMIT_DATA },
     59  1.5  christos 	{ "Max stack size",	"bytes",	RLIMIT_STACK },
     60  1.5  christos 	{ "Max core file size",	"bytes",	RLIMIT_CORE },
     61  1.5  christos 	{ "Max resident set",	"bytes",	RLIMIT_RSS },
     62  1.5  christos 	{ "Max processes",	"processes",	RLIMIT_NPROC },
     63  1.5  christos 	{ "Max open files",	"files",	RLIMIT_NOFILE },
     64  1.5  christos 	{ "Max locked memory",	"bytes",	RLIMIT_MEMLOCK },
     65  1.5  christos 	{ "Max address space",	"bytes",	RLIMIT_AS },
     66  1.5  christos 	{ "Max file locks",	"locks",	LINUX_RLIMIT_LOCKS },
     67  1.5  christos 	{ "Max pending signals", "signals",	LINUX_RLIMIT_SIGPENDING },
     68  1.5  christos 	{ "Max msgqueue size",	"bytes",	LINUX_RLIMIT_MSGQUEUE },
     69  1.5  christos 	{ "Max nice priority",	"",		LINUX_RLIMIT_NICE },
     70  1.5  christos 	{ "Max realtime priority", "",		LINUX_RLIMIT_RTPRIO },
     71  1.5  christos 	{ "Max realtime timeout", "us",		LINUX_RLIMIT_RTTIME },
     72  1.5  christos 	{ 0, 0, 0 }
     73  1.5  christos };
     74  1.1  christos 
     75  1.1  christos static size_t
     76  1.1  christos prl(char *buf, size_t len, intmax_t lim, char sep)
     77  1.1  christos {
     78  1.1  christos 	if (lim == RLIM_INFINITY)
     79  1.1  christos 		return snprintf(buf, len, "%#20jx%c", lim, sep);
     80  1.1  christos 	else
     81  1.1  christos 		return snprintf(buf, len, "%20jd%c", lim, sep);
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos int
     85  1.1  christos procfs_dolimit(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
     86  1.1  christos      struct uio *uio)
     87  1.1  christos {
     88  1.1  christos 	static const char *label[] = RLIM_STRINGS;
     89  1.1  christos 	int error;
     90  1.1  christos 	char *buffer;
     91  1.1  christos 	size_t bufsize, pos, i;
     92  1.1  christos 	struct rlimit rl[RLIM_NLIMITS];
     93  1.1  christos 
     94  1.1  christos 	if (uio->uio_rw != UIO_READ)
     95  1.1  christos 		return EOPNOTSUPP;
     96  1.1  christos 
     97  1.4        ad 	mutex_enter(&proc_lock);
     98  1.1  christos 	mutex_enter(p->p_lock);
     99  1.1  christos 	memcpy(rl, p->p_rlimit, sizeof(rl));
    100  1.1  christos 	mutex_exit(p->p_lock);
    101  1.4        ad 	mutex_exit(&proc_lock);
    102  1.1  christos 
    103  1.1  christos 	error = 0;
    104  1.1  christos 
    105  1.1  christos 	bufsize = (64 * 3) * __arraycount(rl);
    106  1.1  christos 	buffer = malloc(bufsize, M_TEMP, M_WAITOK);
    107  1.1  christos 	pos = 0;
    108  1.1  christos 	for (i = 0; i < __arraycount(rl); i++) {
    109  1.1  christos 		pos += snprintf(buffer + pos, bufsize - pos, "%20.20s ",
    110  1.1  christos 		    label[i]);
    111  1.1  christos 		pos += prl(buffer + pos, bufsize - pos, rl[i].rlim_cur, ' ');
    112  1.1  christos 		pos += prl(buffer + pos, bufsize - pos, rl[i].rlim_max, '\n');
    113  1.1  christos 	}
    114  1.1  christos 
    115  1.3  christos 	if ((uintmax_t)uio->uio_offset < pos)
    116  1.1  christos 		error = uiomove(buffer + uio->uio_offset,
    117  1.1  christos 		    pos - uio->uio_offset, uio);
    118  1.1  christos 	else
    119  1.1  christos 		error = 0;
    120  1.1  christos 
    121  1.1  christos 	if (buffer != NULL)
    122  1.1  christos 		free(buffer, M_TEMP);
    123  1.1  christos 
    124  1.1  christos 	return error;
    125  1.1  christos }
    126  1.5  christos 
    127  1.5  christos int
    128  1.5  christos procfs_dolimits(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
    129  1.5  christos      struct uio *uio)
    130  1.5  christos {
    131  1.5  christos 	const struct linux_rlimit_ident *li;
    132  1.5  christos 	int error;
    133  1.5  christos 	char *buffer;
    134  1.5  christos 	size_t bufsize, pos;
    135  1.5  christos 	struct rlimit rl, rlimits[RLIM_NLIMITS];
    136  1.5  christos 
    137  1.5  christos 	if (uio->uio_rw != UIO_READ)
    138  1.5  christos 		return EOPNOTSUPP;
    139  1.5  christos 
    140  1.5  christos 	mutex_enter(&proc_lock);
    141  1.5  christos 	mutex_enter(p->p_lock);
    142  1.5  christos 	memcpy(rlimits, p->p_rlimit, sizeof(rlimits));
    143  1.5  christos 	mutex_exit(p->p_lock);
    144  1.5  christos 	mutex_exit(&proc_lock);
    145  1.5  christos 
    146  1.5  christos 	error = 0;
    147  1.5  christos 
    148  1.5  christos 	bufsize = (64 * 3) * __arraycount(linux_rlimits_ident);
    149  1.5  christos 	buffer = malloc(bufsize, M_TEMP, M_WAITOK);
    150  1.5  christos 	pos = snprintf(buffer, bufsize, "%-26s%-21s%-21s%-21s\n",
    151  1.5  christos 	    "Limit", "Soft Limit", "Hard Limit", "Units");
    152  1.5  christos 	for (li = linux_rlimits_ident; li->desc != NULL; ++li) {
    153  1.5  christos 		switch (li->rlim_id)
    154  1.5  christos 		{
    155  1.5  christos 		case LINUX_RLIMIT_LOCKS:
    156  1.5  christos 		case LINUX_RLIMIT_RTTIME:
    157  1.5  christos 		case LINUX_RLIMIT_SIGPENDING:
    158  1.5  christos 			rl.rlim_cur = RLIM_INFINITY;
    159  1.5  christos 			break;
    160  1.5  christos 		case LINUX_RLIMIT_MSGQUEUE:
    161  1.5  christos #ifdef SYSVMSG
    162  1.5  christos 			rl.rlim_cur = rl.rlim_max = msginfo.msgmnb;
    163  1.5  christos 			break;
    164  1.5  christos #endif
    165  1.5  christos 		case LINUX_RLIMIT_NICE:
    166  1.5  christos 		case LINUX_RLIMIT_RTPRIO:
    167  1.5  christos 			rl.rlim_cur = rl.rlim_max = 0;
    168  1.5  christos 			break;
    169  1.5  christos 		default:
    170  1.5  christos 			rl = rlimits[li->rlim_id];
    171  1.5  christos 			break;
    172  1.5  christos 		}
    173  1.5  christos 		if (rl.rlim_cur == RLIM_INFINITY)
    174  1.5  christos 			pos += snprintf(buffer + pos, bufsize - pos,
    175  1.5  christos 			    "%-26s%-21s%-21s%-10s\n",
    176  1.5  christos 			    li->desc, "unlimited", "unlimited", li->unit);
    177  1.5  christos 		else
    178  1.5  christos 			pos += snprintf(buffer + pos, bufsize - pos,
    179  1.5  christos 			    "%-26s%-21llu%-21llu%-10s\n",
    180  1.5  christos 			    li->desc, (unsigned long long)rl.rlim_cur,
    181  1.5  christos 			    (unsigned long long)rl.rlim_max, li->unit);
    182  1.5  christos 	}
    183  1.5  christos 
    184  1.5  christos 	if ((uintmax_t)uio->uio_offset < pos)
    185  1.5  christos 		error = uiomove(buffer + uio->uio_offset,
    186  1.5  christos 		    pos - uio->uio_offset, uio);
    187  1.5  christos 	else
    188  1.5  christos 		error = 0;
    189  1.5  christos 
    190  1.5  christos 	if (buffer != NULL)
    191  1.5  christos 		free(buffer, M_TEMP);
    192  1.5  christos 
    193  1.5  christos 	return error;
    194  1.5  christos }
    195