Page MenuHomeFreeBSD

D20258.id57388.diff
No OneTemporary

D20258.id57388.diff

Index: sys/kern/kern_descrip.c
===================================================================
--- sys/kern/kern_descrip.c
+++ sys/kern/kern_descrip.c
@@ -3409,7 +3409,7 @@
}
/* Trim unused data from kf_path by truncating the structure size. */
-static void
+void
pack_kinfo(struct kinfo_file *kif)
{
Index: sys/kern/uipc_shm.c
===================================================================
--- sys/kern/uipc_shm.c
+++ sys/kern/uipc_shm.c
@@ -76,6 +76,7 @@
#include <sys/refcount.h>
#include <sys/resourcevar.h>
#include <sys/rwlock.h>
+#include <sys/sbuf.h>
#include <sys/stat.h>
#include <sys/syscallsubr.h>
#include <sys/sysctl.h>
@@ -1100,34 +1101,89 @@
}
static int
-shm_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
+shmfd_fill_kinfo(struct shmfd *shmfd, struct kinfo_file *kif, bool locked)
{
const char *path, *pr_path;
- struct shmfd *shmfd;
size_t pr_pathlen;
+ bool visible;
kif->kf_type = KF_TYPE_SHM;
- shmfd = fp->f_data;
-
mtx_lock(&shm_timestamp_lock);
kif->kf_un.kf_file.kf_file_mode = S_IFREG | shmfd->shm_mode; /* XXX */
mtx_unlock(&shm_timestamp_lock);
kif->kf_un.kf_file.kf_file_size = shmfd->shm_size;
if (shmfd->shm_path != NULL) {
- sx_slock(&shm_dict_lock);
+ if (!locked)
+ sx_slock(&shm_dict_lock);
if (shmfd->shm_path != NULL) {
path = shmfd->shm_path;
pr_path = curthread->td_ucred->cr_prison->pr_path;
if (strcmp(pr_path, "/") != 0) {
/* Return the jail-rooted pathname. */
pr_pathlen = strlen(pr_path);
- if (strncmp(path, pr_path, pr_pathlen) == 0 &&
- path[pr_pathlen] == '/')
+ visible = strncmp(path, pr_path, pr_pathlen)
+ == 0 && path[pr_pathlen] == '/';
+ if (locked && !visible)
+ return (EPERM);
+ if (visible)
path += pr_pathlen;
}
strlcpy(kif->kf_path, path, sizeof(kif->kf_path));
}
- sx_sunlock(&shm_dict_lock);
+ if (!locked)
+ sx_sunlock(&shm_dict_lock);
}
return (0);
}
+
+static int
+shm_fill_kinfo(struct file *fp, struct kinfo_file *kif,
+ struct filedesc *fdp __unused)
+{
+
+ return (shmfd_fill_kinfo(fp->f_data, kif, false));
+}
+
+static int
+sysctl_posix_shm_list(SYSCTL_HANDLER_ARGS)
+{
+ struct shm_mapping *shmm;
+ struct sbuf sb;
+ struct kinfo_file kif;
+ u_long i;
+ ssize_t curlen;
+ int error, error2;
+
+ sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_file) * 5, req);
+ sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
+ curlen = 0;
+ error = 0;
+ sx_slock(&shm_dict_lock);
+ for (i = 0; i < shm_hash + 1; i++) {
+ LIST_FOREACH(shmm, &shm_dictionary[i], sm_link) {
+ error = shmfd_fill_kinfo(shmm->sm_shmfd, &kif, true);
+ if (error == EPERM)
+ continue;
+ if (error != 0)
+ break;
+ pack_kinfo(&kif);
+ if (req->oldptr != NULL &&
+ kif.kf_structsize + curlen > req->oldlen)
+ break;
+ error = sbuf_bcat(&sb, &kif, kif.kf_structsize) == 0 ?
+ 0 : ENOMEM;
+ if (error != 0)
+ break;
+ curlen += kif.kf_structsize;
+ }
+ }
+ sx_sunlock(&shm_dict_lock);
+ error2 = sbuf_finish(&sb);
+ sbuf_delete(&sb);
+ return (error != 0 ? error : error2);
+}
+
+SYSCTL_PROC(_kern_ipc, OID_AUTO, posix_shm_list,
+ CTLFLAG_RD | CTLFLAG_MPSAFE | CTLTYPE_OPAQUE,
+ NULL, 0, sysctl_posix_shm_list, "",
+ "POSIX SHM list");
Index: sys/sys/user.h
===================================================================
--- sys/sys/user.h
+++ sys/sys/user.h
@@ -608,6 +608,7 @@
int flags);
int vntype_to_kinfo(int vtype);
+void pack_kinfo(struct kinfo_file *kif);
#endif /* !_KERNEL */
#endif
Index: usr.bin/posixshmcontrol/Makefile
===================================================================
--- /dev/null
+++ usr.bin/posixshmcontrol/Makefile
@@ -0,0 +1,7 @@
+# $FreeBSD$
+
+PROG= posixshmcontrol
+WARNS?= 6
+MAN=
+
+.include <bsd.prog.mk>
Index: usr.bin/posixshmcontrol/posixshmcontrol.c
===================================================================
--- /dev/null
+++ usr.bin/posixshmcontrol/posixshmcontrol.c
@@ -0,0 +1,295 @@
+/*-
+ * Copyright (c) 2019 The FreeBSD Foundation
+ *
+ * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static void
+usage(void)
+{
+
+ fprintf(stderr, "Usage:\n"
+ "posixshmcontrol -c -p <path> -- create named shm\n"
+ "posixshmcontrol -d -p <path> -- delete\n"
+ "posixshmcontrol -l -- list all non-anonymous shms\n"
+ "posixshmcontrol -r -p <path> -- dump content to stdout\n"
+ "posixshmcontrol -s -p <path> -- stat\n"
+ "posixshmcontrol -t -z newlen -- truncate\n");
+}
+
+static const char *path;
+static off_t newsize;
+static bool has_size = false;
+
+static int
+create_shm(void)
+{
+ int fd;
+
+ fd = shm_open(path, O_RDWR | O_CREAT, 0660);
+ if (fd == -1) {
+ warn("create %s", path);
+ return (1);
+ }
+ close(fd);
+ return (0);
+}
+
+static int
+delete_shm(void)
+{
+ int error;
+
+ error = shm_unlink(path);
+ if (error != 0)
+ warn("unlink of %s failed", path);
+ return (0);
+}
+
+static const char listmib[] = "kern.ipc.posix_shm_list";
+
+static int
+list_shm(void)
+{
+ char *buf, *bp;
+ const struct kinfo_file *kif;
+ int error, mib[3], ret;
+ size_t len, miblen;
+
+ miblen = nitems(mib);
+ error = sysctlnametomib(listmib, mib, &miblen);
+ if (error == -1) {
+ warn("cannot translate %s", listmib);
+ return (1);
+ }
+ len = 0;
+ error = sysctl(mib, miblen, NULL, &len, NULL, 0);
+ if (error == -1) {
+ warn("cannot get %s length", listmib);
+ return (1);
+ }
+ len = len * 4 / 3;
+ buf = malloc(len);
+ if (buf == NULL) {
+ warn("malloc");
+ return (1);
+ }
+ ret = 1;
+ error = sysctl(mib, miblen, buf, &len, NULL, 0);
+ if (error != 0) {
+ warn("reading %s", listmib);
+ goto out;
+ }
+ printf("MODE\tSIZE\tPATH\n");
+ for (bp = buf; bp < buf + len; bp += kif->kf_structsize) {
+ kif = (const struct kinfo_file *)(void *)bp;
+ if (kif->kf_structsize == 0)
+ break;
+ printf("%#o\t%zd\t%s\n", kif->kf_un.kf_file.kf_file_mode,
+ kif->kf_un.kf_file.kf_file_size, kif->kf_path);
+ }
+ ret = 0;
+out:
+ free(buf);
+ return (ret);
+}
+
+static int
+read_shm(void)
+{
+ char buf[4096];
+ ssize_t size, se;
+ int fd, ret;
+
+ ret = 1;
+ fd = shm_open(path, O_RDONLY, 0);
+ if (fd == -1) {
+ warn("open %s", path);
+ goto out;
+ }
+ for (;;) {
+ size = read(fd, buf, sizeof(buf));
+ if (size > 0) {
+ se = fwrite(buf, 1, size, stdout);
+ if (se < size) {
+ warnx("short write to stdout");
+ goto out;
+ }
+ }
+ if (size >= 0 && size < (ssize_t)sizeof(buf)) {
+ ret = 0;
+ goto out;
+ }
+ warn("read from %s", path);
+ goto out;
+ }
+out:
+ close(fd);
+ return (ret);
+}
+
+static int
+stat_shm(void)
+{
+ struct stat st;
+ int error, fd, ret;
+
+ fd = shm_open(path, O_RDONLY, 0);
+ if (fd == -1) {
+ warn("open %s", path);
+ return (1);
+ }
+ ret = 0;
+ error = fstat(fd, &st);
+ if (error == -1) {
+ warn("stat %s", path);
+ ret = 1;
+ } else {
+ printf("path\t%s\n", path);
+ printf("inode\t%jd\n", (uintmax_t)st.st_ino);
+ printf("mode\t%o\n", st.st_mode);
+ printf("uid\t%d\n", st.st_uid);
+ printf("gid\t%d\n", st.st_gid);
+ printf("size\t%zd\n", st.st_size);
+ printf("atime\t%ld\n", (long)st.st_atime);
+ printf("mtime\t%ld\n", (long)st.st_mtime);
+ printf("ctime\t%ld\n", (long)st.st_ctime);
+ printf("birth\t%ld\n", (long)st.st_birthtim.tv_sec);
+ }
+ close(fd);
+ return (ret);
+}
+
+static int
+truncate_shm(void)
+{
+ int error, fd, ret;
+
+ ret = 0;
+ fd = shm_open(path, O_RDWR, 0);
+ if (fd == -1) {
+ warn("open %s", path);
+ return (ret);
+ }
+ error = ftruncate(fd, newsize);
+ if (error == -1) {
+ warn("truncate %s", path);
+ ret = 1;
+ }
+ close(fd);
+ return (ret);
+}
+
+struct opmode {
+ char opt;
+ bool needs_path;
+ bool needs_size;
+ int (*impl)(void);
+};
+
+static const struct opmode opmodes[] = {
+ { .opt = 'c', .impl = create_shm, .needs_path = true, },
+ { .opt = 'd', .impl = delete_shm, .needs_path = true },
+ { .opt = 'l', .impl = list_shm },
+ { .opt = 'r', .impl = read_shm, .needs_path = true },
+ { .opt = 's', .impl = stat_shm, .needs_path = true },
+ { .opt = 't', .impl = truncate_shm, .needs_path = true,
+ .needs_size = true },
+};
+
+int
+main(int argc, char *argv[])
+{
+ const struct opmode *opmode;
+ int c, i, ret;
+
+ ret = 0;
+ opmode = NULL;
+ path = NULL;
+ while ((c = getopt(argc, argv, "cdlrstp:z:")) != -1) {
+ for (i = 0; i < (int)nitems(opmodes); i++) {
+ if (c == opmodes[i].opt) {
+ if (opmode != NULL) {
+ ret = 1;
+ goto out;
+ }
+ opmode = &opmodes[i];
+ break;
+ }
+ }
+ if (i != nitems(opmodes))
+ continue;
+ switch (c) {
+ case 'p':
+ if (path != NULL) {
+ ret = 1;
+ goto out;
+ }
+ path = optarg;
+ break;
+ case 'z':
+ if (has_size) {
+ ret = 1;
+ goto out;
+ }
+ newsize = atol(optarg); /* XXX */
+ has_size = true;
+ break;
+ case 'h':
+ usage();
+ exit(0);
+ case '?':
+ default:
+ ret = 1;
+ goto out;
+ }
+ }
+ if (opmode == NULL || (opmode->needs_path && path == NULL) ||
+ (opmode->needs_size && !has_size))
+ ret = 1;
+out:
+ if (ret != 0) {
+ usage();
+ exit(ret);
+ }
+
+ ret = opmode->impl();
+ exit(ret);
+}

File Metadata

Mime Type
text/plain
Expires
Fri, Aug 21, 9:52 AM (8 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
37027800
Default Alt Text
D20258.id57388.diff (10 KB)

Event Timeline