git: 1e40fc6fa9c4 - stable/13 - MFC posixshm: Add a -j option to posixshmcontrol ls, to specify a jail

From: Jamie Gritton <jamie_at_FreeBSD.org>
Date: Wed, 02 Mar 2022 23:12:37 UTC
The branch stable/13 has been updated by jamie:

URL: https://cgit.FreeBSD.org/src/commit/?id=1e40fc6fa9c4e394195fd2972c8831526c684f5e

commit 1e40fc6fa9c4e394195fd2972c8831526c684f5e
Author:     Jamie Gritton <jamie@FreeBSD.org>
AuthorDate: 2022-02-27 01:45:28 +0000
Commit:     Jamie Gritton <jamie@FreeBSD.org>
CommitDate: 2022-03-02 23:10:33 +0000

    MFC posixshm: Add a -j option to posixshmcontrol ls, to specify a jail
    
    PR:             257556
    Reported by:    grembo@
    
    (cherry picked from commit be7cf3f4b8c2818155f5a4a83c64c9ef6a60a320)
---
 usr.bin/posixshmcontrol/Makefile          |  2 +-
 usr.bin/posixshmcontrol/posixshmcontrol.1 |  8 ++++++-
 usr.bin/posixshmcontrol/posixshmcontrol.c | 39 ++++++++++++++++++++++++++-----
 3 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/usr.bin/posixshmcontrol/Makefile b/usr.bin/posixshmcontrol/Makefile
index c6f847e18478..e5e9588df7bb 100644
--- a/usr.bin/posixshmcontrol/Makefile
+++ b/usr.bin/posixshmcontrol/Makefile
@@ -1,6 +1,6 @@
 # $FreeBSD$
 
 PROG=   posixshmcontrol
-LIBADD=	util
+LIBADD=	jail util
 
 .include <bsd.prog.mk>
diff --git a/usr.bin/posixshmcontrol/posixshmcontrol.1 b/usr.bin/posixshmcontrol/posixshmcontrol.1
index f6743b070b50..1d8c3438b165 100644
--- a/usr.bin/posixshmcontrol/posixshmcontrol.1
+++ b/usr.bin/posixshmcontrol/posixshmcontrol.1
@@ -27,7 +27,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd March 18, 2020
+.Dd February 26, 2022
 .Dt POSIXSHMCONTROL 1
 .Os
 .Sh NAME
@@ -45,6 +45,7 @@
 .Ar ls
 .Op Fl h
 .Op Fl n
+.Op Fl j Ar jail
 .Nm
 .Ar dump
 .Op Pa path \&...
@@ -87,6 +88,11 @@ Unlink the paths specified.
 .It Ic ls
 List all linked named shared memory segments visible to the caller.
 For each segment, the user and group owner, size, and path are displayed.
+The
+.Fl j
+option limits the output to segments within the specified
+.Ar jail
+name or id.
 .It Ic dump
 Output raw bytes values from the segment to standard output.
 .It Ic stat
diff --git a/usr.bin/posixshmcontrol/posixshmcontrol.c b/usr.bin/posixshmcontrol/posixshmcontrol.c
index 47a19b78a9d2..f64fcd3dd663 100644
--- a/usr.bin/posixshmcontrol/posixshmcontrol.c
+++ b/usr.bin/posixshmcontrol/posixshmcontrol.c
@@ -39,6 +39,7 @@ __FBSDID("$FreeBSD$");
 #include <err.h>
 #include <fcntl.h>
 #include <grp.h>
+#include <jail.h>
 #include <libutil.h>
 #include <pwd.h>
 #include <stdbool.h>
@@ -54,7 +55,7 @@ usage(void)
 	fprintf(stderr, "Usage:\n"
 	    "posixshmcontrol create [-m <mode>] [-l <largepage>] <path> ...\n"
 	    "posixshmcontrol rm <path> ...\n"
-	    "posixshmcontrol ls [-h] [-n]\n"
+	    "posixshmcontrol ls [-h] [-n] [-j jail]\n"
 	    "posixshmcontrol dump <path> ...\n"
 	    "posixshmcontrol stat [-h] [-n] <path> ...\n"
 	    "posixshmcontrol truncate [-s <newlen>] <path> ...\n");
@@ -221,17 +222,19 @@ shm_decode_mode(mode_t m, char *str)
 static int
 list_shm(int argc, char **argv)
 {
-	char *buf, *bp, sizebuf[8], str[10];
+	char *buf, *bp, *ep, jailpath[MAXPATHLEN], sizebuf[8], str[10];
+	const char *jailparam;
 	const struct kinfo_file *kif;
 	struct stat st;
-	int c, error, fd, mib[3], ret;
-	size_t len, miblen;
-	bool hsize, uname;
+	int c, error, fd, jid, mib[3], ret;
+	size_t len, jailpathlen, miblen;
+	bool hsize, jailed, uname;
 
 	hsize = false;
+	jailed = false;
 	uname = true;
 
-	while ((c = getopt(argc, argv, "hn")) != -1) {
+	while ((c = getopt(argc, argv, "hj:n")) != -1) {
 		switch (c) {
 		case 'h':
 			hsize = true;
@@ -239,6 +242,28 @@ list_shm(int argc, char **argv)
 		case 'n':
 			uname = false;
 			break;
+		case 'j':
+			jid = strtoul(optarg, &ep, 10);
+			if (ep > optarg && !*ep) {
+				jailparam = "jid";
+				jailed = jid > 0;
+			} else {
+				jailparam = "name";
+				jailed = true;
+			}
+			if (jailed) {
+				if (jail_getv(0, jailparam, optarg, "path",
+				    jailpath, NULL) < 0) {
+					if (errno == ENOENT)
+						warnx("no such jail: %s", optarg);
+					else
+						warnx("%s", jail_errmsg);
+					return (1);
+				}
+				jailpathlen = strlen(jailpath);
+				jailpath[jailpathlen] = '/';
+			}
+			break;
 		default:
 			usage();
 			return (2);
@@ -279,6 +304,8 @@ list_shm(int argc, char **argv)
 		kif = (const struct kinfo_file *)(void *)bp;
 		if (kif->kf_structsize == 0)
 			break;
+		if (jailed && strncmp(kif->kf_path, jailpath, jailpathlen + 1))
+			continue;
 		fd = shm_open(kif->kf_path, O_RDONLY, 0);
 		if (fd == -1) {
 			warn("open %s", kif->kf_path);