git: bda73e441f25 - main - posix shm: add shm_get_path(9)

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 08 Oct 2024 12:39:24 UTC
The branch main has been updated by kib:

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

commit bda73e441f2576de5ad00856d758354c299a3f75
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-10-07 01:44:49 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-10-08 12:38:03 +0000

    posix shm: add shm_get_path(9)
    
    to calculate the posix shm path from the vm_object backing shm segment.
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D46970
---
 sys/kern/uipc_shm.c | 31 +++++++++++++++++++++++++++++++
 sys/sys/mman.h      |  1 +
 2 files changed, 32 insertions(+)

diff --git a/sys/kern/uipc_shm.c b/sys/kern/uipc_shm.c
index cf98e1410074..f6861a3278ff 100644
--- a/sys/kern/uipc_shm.c
+++ b/sys/kern/uipc_shm.c
@@ -2208,3 +2208,34 @@ sys_shm_open2(struct thread *td, struct shm_open2_args *uap)
 	return (kern_shm_open2(td, uap->path, uap->flags, uap->mode,
 	    uap->shmflags, NULL, uap->name));
 }
+
+int
+shm_get_path(struct vm_object *obj, char *path, size_t sz)
+{
+	struct shmfd *shmfd;
+	int error;
+
+	error = 0;
+	shmfd = NULL;
+	sx_slock(&shm_dict_lock);
+	VM_OBJECT_RLOCK(obj);
+	if ((obj->flags & OBJ_POSIXSHM) == 0) {
+		error = EINVAL;
+	} else {
+		if (obj->type == shmfd_pager_type)
+			shmfd = obj->un_pager.swp.swp_priv;
+		else if (obj->type == OBJT_PHYS)
+			shmfd = obj->un_pager.phys.phys_priv;
+		if (shmfd == NULL) {
+			error = ENXIO;
+		} else {
+			strlcpy(path, shmfd->shm_path == NULL ? "anon" :
+			    shmfd->shm_path, sz);
+		}
+	}
+	if (error != 0)
+		path[0] = '\0';
+	VM_OBJECT_RUNLOCK(obj);
+	sx_sunlock(&shm_dict_lock);
+	return (error);
+}
diff --git a/sys/sys/mman.h b/sys/sys/mman.h
index 6ee2d5562db1..d2c7bdf1f022 100644
--- a/sys/sys/mman.h
+++ b/sys/sys/mman.h
@@ -310,6 +310,7 @@ void	shm_drop(struct shmfd *shmfd);
 int	shm_dotruncate(struct shmfd *shmfd, off_t length);
 bool	shm_largepage(struct shmfd *shmfd);
 void	shm_remove_prison(struct prison *pr);
+int	shm_get_path(struct vm_object *obj, char *path, size_t sz);
 
 extern struct fileops shm_ops;