2 * Copyright (c) 2019 The FreeBSD Foundation
4 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
5 * under sponsorship from the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/filio.h>
33 #include <sys/syscall.h>
34 #include <sys/sysctl.h>
52 fprintf(stderr, "Usage:\n"
53 "posixshmcontrol create [-m <mode>] [-l <largepage>] <path> ...\n"
54 "posixshmcontrol rm <path> ...\n"
55 "posixshmcontrol ls [-h] [-n] [-j jail]\n"
56 "posixshmcontrol dump <path> ...\n"
57 "posixshmcontrol stat [-h] [-n] <path> ...\n"
58 "posixshmcontrol truncate [-s <newlen>] <path> ...\n");
62 create_one_shm(const char *path, long mode, int idx)
67 fd = shm_open(path, O_RDWR | O_CREAT, mode);
69 warn("create %s", path);
73 fd = shm_create_largepage(path, O_RDWR, idx,
74 SHM_LARGEPAGE_ALLOC_DEFAULT, mode);
76 warn("shm_create_largepage %s psind %d", path, idx);
85 create_shm(int argc, char **argv)
91 int c, i, idx, pn, ret, ret1;
96 while ((c = getopt(argc, argv, "l:m:")) != -1) {
100 mode = strtol(optarg, &end, 0);
101 if (mode == 0 && errno != 0)
104 errx(1, "non-integer mode");
107 if (expand_number(optarg, &pgsz) == -1)
109 pn = getpagesizes(NULL, 0);
111 err(1, "getpagesizes");
112 pagesizes = malloc(sizeof(size_t) * pn);
113 if (pagesizes == NULL)
115 if (getpagesizes(pagesizes, pn) == -1)
116 err(1, "gtpagesizes");
117 for (idx = 0; idx < pn; idx++) {
118 if (pagesizes[idx] == pgsz)
123 "pagesize should be superpagesize, supported sizes:");
125 for (i = 0; i < pn; i++) {
126 if (pagesizes[i] == 0 ||
127 pagesizes[i] == (size_t)
131 fprintf(stderr, " %zu", pagesizes[i]);
134 fprintf(stderr, " none");
135 fprintf(stderr, "\n");
138 if (pgsz == (uint64_t)getpagesize())
139 errx(1, "pagesize should be large");
157 for (i = 0; i < argc; i++) {
158 ret1 = create_one_shm(argv[i], mode, idx);
159 if (ret1 != 0 && ret == 0)
166 delete_one_shm(const char *path)
170 error = shm_unlink(path);
172 warn("unlink of %s failed", path);
181 delete_shm(int argc, char **argv)
191 for (i = 1; i < argc; i++) {
192 ret1 = delete_one_shm(argv[i]);
193 if (ret1 != 0 && ret == 0)
199 static const char listmib[] = "kern.ipc.posix_shm_list";
202 shm_decode_mode(mode_t m, char *str)
207 str[i++] = (m & S_IRUSR) != 0 ? 'r' : '-';
208 str[i++] = (m & S_IWUSR) != 0 ? 'w' : '-';
209 str[i++] = (m & S_IXUSR) != 0 ? 'x' : '-';
210 str[i++] = (m & S_IRGRP) != 0 ? 'r' : '-';
211 str[i++] = (m & S_IWGRP) != 0 ? 'w' : '-';
212 str[i++] = (m & S_IXGRP) != 0 ? 'x' : '-';
213 str[i++] = (m & S_IROTH) != 0 ? 'r' : '-';
214 str[i++] = (m & S_IWOTH) != 0 ? 'w' : '-';
215 str[i++] = (m & S_IXOTH) != 0 ? 'x' : '-';
220 list_shm(int argc, char **argv)
222 char *buf, *bp, *ep, jailpath[MAXPATHLEN], sizebuf[8], str[10];
223 const char *jailparam;
224 const struct kinfo_file *kif;
226 int c, error, fd, jid, mib[3], ret;
227 size_t len, jailpathlen, miblen;
228 bool hsize, jailed, uname;
234 while ((c = getopt(argc, argv, "hj:n")) != -1) {
243 jid = strtoul(optarg, &ep, 10);
244 if (ep > optarg && !*ep) {
252 if (jail_getv(0, jailparam, optarg, "path",
253 jailpath, NULL) < 0) {
255 warnx("no such jail: %s", optarg);
257 warnx("%s", jail_errmsg);
260 jailpathlen = strlen(jailpath);
261 jailpath[jailpathlen] = '/';
269 if (argc != optind) {
274 miblen = nitems(mib);
275 error = sysctlnametomib(listmib, mib, &miblen);
277 warn("cannot translate %s", listmib);
281 error = sysctl(mib, miblen, NULL, &len, NULL, 0);
283 warn("cannot get %s length", listmib);
292 error = sysctl(mib, miblen, buf, &len, NULL, 0);
294 warn("reading %s", listmib);
299 printf("MODE \tOWNER\tGROUP\tSIZE\tPATH\n");
300 for (bp = buf; bp < buf + len; bp += kif->kf_structsize) {
301 kif = (const struct kinfo_file *)(void *)bp;
302 if (kif->kf_structsize == 0)
304 if (jailed && strncmp(kif->kf_path, jailpath, jailpathlen + 1))
306 fd = shm_open(kif->kf_path, O_RDONLY, 0);
308 if (errno != EACCES) {
309 warn("open %s", kif->kf_path);
314 error = fstat(fd, &st);
317 warn("stat %s", kif->kf_path);
321 shm_decode_mode(kif->kf_un.kf_file.kf_file_mode, str);
324 printf("%s\t%s\t", user_from_uid(st.st_uid, 0),
325 group_from_gid(st.st_gid, 0));
327 printf("%d\t%d\t", st.st_uid, st.st_gid);
330 humanize_number(sizebuf, sizeof(sizebuf),
331 kif->kf_un.kf_file.kf_file_size, "", HN_AUTOSCALE,
333 printf("%s\t", sizebuf);
336 (uintmax_t)kif->kf_un.kf_file.kf_file_size);
338 printf("%s\n", kif->kf_path);
346 read_one_shm(const char *path)
353 fd = shm_open(path, O_RDONLY, 0);
355 warn("open %s", path);
359 size = read(fd, buf, sizeof(buf));
361 se = fwrite(buf, 1, size, stdout);
363 warnx("short write to stdout");
367 if (size == (ssize_t)sizeof(buf))
369 if (size >= 0 && size < (ssize_t)sizeof(buf)) {
373 warn("read from %s", path);
382 read_shm(int argc, char **argv)
392 for (i = 1; i < argc; i++) {
393 ret1 = read_one_shm(argv[i]);
394 if (ret1 != 0 && ret == 0)
401 stat_one_shm(const char *path, bool hsize, bool uname)
406 struct shm_largepage_conf conf_dummy;
409 fd = shm_open(path, O_RDONLY, 0);
411 warn("open %s", path);
415 error = fstat(fd, &st);
417 warn("stat %s", path);
420 printf("path\t%s\n", path);
421 printf("inode\t%jd\n", (uintmax_t)st.st_ino);
422 printf("mode\t%#o\n", st.st_mode);
423 printf("nlink\t%jd\n", (uintmax_t)st.st_nlink);
425 printf("owner\t%s\n", user_from_uid(st.st_uid, 0));
426 printf("group\t%s\n", group_from_gid(st.st_gid, 0));
428 printf("uid\t%d\n", st.st_uid);
429 printf("gid\t%d\n", st.st_gid);
432 humanize_number(sizebuf, sizeof(sizebuf),
433 st.st_size, "", HN_AUTOSCALE, HN_NOSPACE);
434 printf("size\t%s\n", sizebuf);
436 printf("size\t%jd\n", (uintmax_t)st.st_size);
438 printf("atime\t%ld.%09ld\n", (long)st.st_atime,
439 (long)st.st_atim.tv_nsec);
440 printf("mtime\t%ld.%09ld\n", (long)st.st_mtime,
441 (long)st.st_mtim.tv_nsec);
442 printf("ctime\t%ld.%09ld\n", (long)st.st_ctime,
443 (long)st.st_ctim.tv_nsec);
444 printf("birth\t%ld.%09ld\n", (long)st.st_birthtim.tv_sec,
445 (long)st.st_birthtim.tv_nsec);
446 error = ioctl(fd, FIOGSHMLPGCNF, &conf_dummy);
447 largepage = error == 0;
448 if (st.st_blocks != 0 && largepage)
449 printf("pagesz\t%jd\n", roundup((uintmax_t)st.st_size,
450 PAGE_SIZE) / st.st_blocks);
452 printf("pages\t%jd\n", st.st_blocks);
459 stat_shm(int argc, char **argv)
467 while ((c = getopt(argc, argv, "hn")) != -1) {
489 for (i = 0; i < argc; i++) {
490 ret1 = stat_one_shm(argv[i], hsize, uname);
491 if (ret1 != 0 && ret == 0)
498 truncate_one_shm(const char *path, uint64_t newsize)
503 fd = shm_open(path, O_RDWR, 0);
505 warn("open %s", path);
508 error = ftruncate(fd, newsize);
510 warn("truncate %s", path);
518 truncate_shm(int argc, char **argv)
524 while ((c = getopt(argc, argv, "s:")) != -1) {
527 if (expand_number(optarg, &newsize) == -1)
544 for (i = 0; i < argc; i++) {
545 ret1 = truncate_one_shm(argv[i], newsize);
546 if (ret1 != 0 && ret == 0)
554 int (*impl)(int argc, char **argv);
557 static const struct opmode opmodes[] = {
558 { .cmd = "create", .impl = create_shm},
559 { .cmd = "rm", .impl = delete_shm, },
560 { .cmd = "list", .impl = list_shm },
561 { .cmd = "ls", .impl = list_shm },
562 { .cmd = "dump", .impl = read_shm, },
563 { .cmd = "stat", .impl = stat_shm, },
564 { .cmd = "truncate", .impl = truncate_shm, },
568 main(int argc, char *argv[])
570 const struct opmode *opmode;
580 for (i = 0; i < (int)nitems(opmodes); i++) {
581 if (strcmp(argv[1], opmodes[i].cmd) == 0) {
582 opmode = &opmodes[i];
586 if (opmode == NULL) {
590 ret = opmode->impl(argc - 1, argv + 1);