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/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/filio.h>
36 #include <sys/syscall.h>
37 #include <sys/sysctl.h>
54 fprintf(stderr, "Usage:\n"
55 "posixshmcontrol create [-m <mode>] [-l <largepage>] <path> ...\n"
56 "posixshmcontrol rm <path> ...\n"
57 "posixshmcontrol ls [-h] [-n]\n"
58 "posixshmcontrol dump <path> ...\n"
59 "posixshmcontrol stat [-h] [-n] <path> ...\n"
60 "posixshmcontrol truncate [-s <newlen>] <path> ...\n");
64 create_one_shm(const char *path, long mode, int idx)
69 fd = shm_open(path, O_RDWR | O_CREAT, mode);
71 warn("create %s", path);
75 fd = shm_create_largepage(path, O_RDWR, idx,
76 SHM_LARGEPAGE_ALLOC_DEFAULT, mode);
78 warn("shm_create_largepage %s psind %d", path, idx);
87 create_shm(int argc, char **argv)
93 int c, i, idx, pn, ret, ret1;
98 while ((c = getopt(argc, argv, "l:m:")) != -1) {
102 mode = strtol(optarg, &end, 0);
103 if (mode == 0 && errno != 0)
106 errx(1, "non-integer mode");
109 if (expand_number(optarg, &pgsz) == -1)
111 pn = getpagesizes(NULL, 0);
113 err(1, "getpagesizes");
114 pagesizes = malloc(sizeof(size_t) * pn);
115 if (pagesizes == NULL)
117 if (getpagesizes(pagesizes, pn) == -1)
118 err(1, "gtpagesizes");
119 for (idx = 0; idx < pn; idx++) {
120 if (pagesizes[idx] == pgsz)
125 "pagesize should be superpagesize, supported sizes:");
127 for (i = 0; i < pn; i++) {
128 if (pagesizes[i] == 0 ||
129 pagesizes[i] == (size_t)
133 fprintf(stderr, " %zu", pagesizes[i]);
136 fprintf(stderr, " none");
137 fprintf(stderr, "\n");
140 if (pgsz == (uint64_t)getpagesize())
141 errx(1, "pagesize should be large");
159 for (i = 0; i < argc; i++) {
160 ret1 = create_one_shm(argv[i], mode, idx);
161 if (ret1 != 0 && ret == 0)
168 delete_one_shm(const char *path)
172 error = shm_unlink(path);
174 warn("unlink of %s failed", path);
183 delete_shm(int argc, char **argv)
193 for (i = 1; i < argc; i++) {
194 ret1 = delete_one_shm(argv[i]);
195 if (ret1 != 0 && ret == 0)
201 static const char listmib[] = "kern.ipc.posix_shm_list";
204 shm_decode_mode(mode_t m, char *str)
209 str[i++] = (m & S_IRUSR) != 0 ? 'r' : '-';
210 str[i++] = (m & S_IWUSR) != 0 ? 'w' : '-';
211 str[i++] = (m & S_IXUSR) != 0 ? 'x' : '-';
212 str[i++] = (m & S_IRGRP) != 0 ? 'r' : '-';
213 str[i++] = (m & S_IWGRP) != 0 ? 'w' : '-';
214 str[i++] = (m & S_IXGRP) != 0 ? 'x' : '-';
215 str[i++] = (m & S_IROTH) != 0 ? 'r' : '-';
216 str[i++] = (m & S_IWOTH) != 0 ? 'w' : '-';
217 str[i++] = (m & S_IXOTH) != 0 ? 'x' : '-';
222 list_shm(int argc, char **argv)
224 char *buf, *bp, sizebuf[8], str[10];
225 const struct kinfo_file *kif;
227 int c, error, fd, mib[3], ret;
234 while ((c = getopt(argc, argv, "hn")) != -1) {
247 if (argc != optind) {
252 miblen = nitems(mib);
253 error = sysctlnametomib(listmib, mib, &miblen);
255 warn("cannot translate %s", listmib);
259 error = sysctl(mib, miblen, NULL, &len, NULL, 0);
261 warn("cannot get %s length", listmib);
270 error = sysctl(mib, miblen, buf, &len, NULL, 0);
272 warn("reading %s", listmib);
277 printf("MODE \tOWNER\tGROUP\tSIZE\tPATH\n");
278 for (bp = buf; bp < buf + len; bp += kif->kf_structsize) {
279 kif = (const struct kinfo_file *)(void *)bp;
280 if (kif->kf_structsize == 0)
282 fd = shm_open(kif->kf_path, O_RDONLY, 0);
284 warn("open %s", kif->kf_path);
288 error = fstat(fd, &st);
291 warn("stat %s", kif->kf_path);
295 shm_decode_mode(kif->kf_un.kf_file.kf_file_mode, str);
298 printf("%s\t%s\t", user_from_uid(st.st_uid, 0),
299 group_from_gid(st.st_gid, 0));
301 printf("%d\t%d\t", st.st_uid, st.st_gid);
304 humanize_number(sizebuf, sizeof(sizebuf),
305 kif->kf_un.kf_file.kf_file_size, "", HN_AUTOSCALE,
307 printf("%s\t", sizebuf);
310 (uintmax_t)kif->kf_un.kf_file.kf_file_size);
312 printf("%s\n", kif->kf_path);
320 read_one_shm(const char *path)
327 fd = shm_open(path, O_RDONLY, 0);
329 warn("open %s", path);
333 size = read(fd, buf, sizeof(buf));
335 se = fwrite(buf, 1, size, stdout);
337 warnx("short write to stdout");
341 if (size == (ssize_t)sizeof(buf))
343 if (size >= 0 && size < (ssize_t)sizeof(buf)) {
347 warn("read from %s", path);
356 read_shm(int argc, char **argv)
366 for (i = 1; i < argc; i++) {
367 ret1 = read_one_shm(argv[i]);
368 if (ret1 != 0 && ret == 0)
375 stat_one_shm(const char *path, bool hsize, bool uname)
381 fd = shm_open(path, O_RDONLY, 0);
383 warn("open %s", path);
387 error = fstat(fd, &st);
389 warn("stat %s", path);
392 printf("path\t%s\n", path);
393 printf("inode\t%jd\n", (uintmax_t)st.st_ino);
394 printf("mode\t%#o\n", st.st_mode);
395 printf("nlink\t%jd\n", (uintmax_t)st.st_nlink);
397 printf("owner\t%s\n", user_from_uid(st.st_uid, 0));
398 printf("group\t%s\n", group_from_gid(st.st_gid, 0));
400 printf("uid\t%d\n", st.st_uid);
401 printf("gid\t%d\n", st.st_gid);
404 humanize_number(sizebuf, sizeof(sizebuf),
405 st.st_size, "", HN_AUTOSCALE, HN_NOSPACE);
406 printf("size\t%s\n", sizebuf);
408 printf("size\t%jd\n", (uintmax_t)st.st_size);
410 printf("atime\t%ld.%09ld\n", (long)st.st_atime,
411 (long)st.st_atim.tv_nsec);
412 printf("mtime\t%ld.%09ld\n", (long)st.st_mtime,
413 (long)st.st_mtim.tv_nsec);
414 printf("ctime\t%ld.%09ld\n", (long)st.st_ctime,
415 (long)st.st_ctim.tv_nsec);
416 printf("birth\t%ld.%09ld\n", (long)st.st_birthtim.tv_sec,
417 (long)st.st_birthtim.tv_nsec);
418 if (st.st_blocks != 0)
419 printf("pagesz\t%jd\n", roundup((uintmax_t)st.st_size,
420 PAGE_SIZE) / st.st_blocks);
427 stat_shm(int argc, char **argv)
435 while ((c = getopt(argc, argv, "hn")) != -1) {
457 for (i = 0; i < argc; i++) {
458 ret1 = stat_one_shm(argv[i], hsize, uname);
459 if (ret1 != 0 && ret == 0)
466 truncate_one_shm(const char *path, uint64_t newsize)
471 fd = shm_open(path, O_RDWR, 0);
473 warn("open %s", path);
476 error = ftruncate(fd, newsize);
478 warn("truncate %s", path);
486 truncate_shm(int argc, char **argv)
492 while ((c = getopt(argc, argv, "s:")) != -1) {
495 if (expand_number(optarg, &newsize) == -1)
512 for (i = 0; i < argc; i++) {
513 ret1 = truncate_one_shm(argv[i], newsize);
514 if (ret1 != 0 && ret == 0)
522 int (*impl)(int argc, char **argv);
525 static const struct opmode opmodes[] = {
526 { .cmd = "create", .impl = create_shm},
527 { .cmd = "rm", .impl = delete_shm, },
528 { .cmd = "list", .impl = list_shm },
529 { .cmd = "ls", .impl = list_shm },
530 { .cmd = "dump", .impl = read_shm, },
531 { .cmd = "stat", .impl = stat_shm, },
532 { .cmd = "truncate", .impl = truncate_shm, },
536 main(int argc, char *argv[])
538 const struct opmode *opmode;
548 for (i = 0; i < (int)nitems(opmodes); i++) {
549 if (strcmp(argv[1], opmodes[i].cmd) == 0) {
550 opmode = &opmodes[i];
554 if (opmode == NULL) {
558 ret = opmode->impl(argc - 1, argv + 1);