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");
154 for (i = 0; i < argc; i++) {
155 ret1 = create_one_shm(argv[i], mode, idx);
156 if (ret1 != 0 && ret == 0)
163 delete_one_shm(const char *path)
167 error = shm_unlink(path);
169 warn("unlink of %s failed", path);
178 delete_shm(int argc, char **argv)
183 for (i = 1; i < argc; i++) {
184 ret1 = delete_one_shm(argv[i]);
185 if (ret1 != 0 && ret == 0)
191 static const char listmib[] = "kern.ipc.posix_shm_list";
194 shm_decode_mode(mode_t m, char *str)
199 str[i++] = (m & S_IRUSR) != 0 ? 'r' : '-';
200 str[i++] = (m & S_IWUSR) != 0 ? 'w' : '-';
201 str[i++] = (m & S_IXUSR) != 0 ? 'x' : '-';
202 str[i++] = (m & S_IRGRP) != 0 ? 'r' : '-';
203 str[i++] = (m & S_IWGRP) != 0 ? 'w' : '-';
204 str[i++] = (m & S_IXGRP) != 0 ? 'x' : '-';
205 str[i++] = (m & S_IROTH) != 0 ? 'r' : '-';
206 str[i++] = (m & S_IWOTH) != 0 ? 'w' : '-';
207 str[i++] = (m & S_IXOTH) != 0 ? 'x' : '-';
212 list_shm(int argc, char **argv)
214 char *buf, *bp, sizebuf[8], str[10];
215 const struct kinfo_file *kif;
217 int c, error, fd, mib[3], ret;
224 while ((c = getopt(argc, argv, "hn")) != -1) {
237 if (argc != optind) {
242 miblen = nitems(mib);
243 error = sysctlnametomib(listmib, mib, &miblen);
245 warn("cannot translate %s", listmib);
249 error = sysctl(mib, miblen, NULL, &len, NULL, 0);
251 warn("cannot get %s length", listmib);
260 error = sysctl(mib, miblen, buf, &len, NULL, 0);
262 warn("reading %s", listmib);
267 printf("MODE \tOWNER\tGROUP\tSIZE\tPATH\n");
268 for (bp = buf; bp < buf + len; bp += kif->kf_structsize) {
269 kif = (const struct kinfo_file *)(void *)bp;
270 if (kif->kf_structsize == 0)
272 fd = shm_open(kif->kf_path, O_RDONLY, 0);
274 warn("open %s", kif->kf_path);
278 error = fstat(fd, &st);
281 warn("stat %s", kif->kf_path);
285 shm_decode_mode(kif->kf_un.kf_file.kf_file_mode, str);
288 printf("%s\t%s\t", user_from_uid(st.st_uid, 0),
289 group_from_gid(st.st_gid, 0));
291 printf("%d\t%d\t", st.st_uid, st.st_gid);
294 humanize_number(sizebuf, sizeof(sizebuf),
295 kif->kf_un.kf_file.kf_file_size, "", HN_AUTOSCALE,
297 printf("%s\t", sizebuf);
300 (uintmax_t)kif->kf_un.kf_file.kf_file_size);
302 printf("%s\n", kif->kf_path);
310 read_one_shm(const char *path)
317 fd = shm_open(path, O_RDONLY, 0);
319 warn("open %s", path);
323 size = read(fd, buf, sizeof(buf));
325 se = fwrite(buf, 1, size, stdout);
327 warnx("short write to stdout");
331 if (size == (ssize_t)sizeof(buf))
333 if (size >= 0 && size < (ssize_t)sizeof(buf)) {
337 warn("read from %s", path);
346 read_shm(int argc, char **argv)
351 for (i = 1; i < argc; i++) {
352 ret1 = read_one_shm(argv[i]);
353 if (ret1 != 0 && ret == 0)
360 stat_one_shm(const char *path, bool hsize, bool uname)
366 fd = shm_open(path, O_RDONLY, 0);
368 warn("open %s", path);
372 error = fstat(fd, &st);
374 warn("stat %s", path);
377 printf("path\t%s\n", path);
378 printf("inode\t%jd\n", (uintmax_t)st.st_ino);
379 printf("mode\t%#o\n", st.st_mode);
380 printf("nlink\t%jd\n", (uintmax_t)st.st_nlink);
382 printf("owner\t%s\n", user_from_uid(st.st_uid, 0));
383 printf("group\t%s\n", group_from_gid(st.st_gid, 0));
385 printf("uid\t%d\n", st.st_uid);
386 printf("gid\t%d\n", st.st_gid);
389 humanize_number(sizebuf, sizeof(sizebuf),
390 st.st_size, "", HN_AUTOSCALE, HN_NOSPACE);
391 printf("size\t%s\n", sizebuf);
393 printf("size\t%jd\n", (uintmax_t)st.st_size);
395 printf("atime\t%ld.%09ld\n", (long)st.st_atime,
396 (long)st.st_atim.tv_nsec);
397 printf("mtime\t%ld.%09ld\n", (long)st.st_mtime,
398 (long)st.st_mtim.tv_nsec);
399 printf("ctime\t%ld.%09ld\n", (long)st.st_ctime,
400 (long)st.st_ctim.tv_nsec);
401 printf("birth\t%ld.%09ld\n", (long)st.st_birthtim.tv_sec,
402 (long)st.st_birthtim.tv_nsec);
403 if (st.st_blocks != 0)
404 printf("pagesz\t%jd\n", roundup((uintmax_t)st.st_size,
405 PAGE_SIZE) / st.st_blocks);
412 stat_shm(int argc, char **argv)
420 while ((c = getopt(argc, argv, "hn")) != -1) {
437 for (i = 0; i < argc; i++) {
438 ret1 = stat_one_shm(argv[i], hsize, uname);
439 if (ret1 != 0 && ret == 0)
446 truncate_one_shm(const char *path, uint64_t newsize)
451 fd = shm_open(path, O_RDWR, 0);
453 warn("open %s", path);
456 error = ftruncate(fd, newsize);
458 warn("truncate %s", path);
466 truncate_shm(int argc, char **argv)
472 while ((c = getopt(argc, argv, "s:")) != -1) {
475 if (expand_number(optarg, &newsize) == -1)
487 for (i = 0; i < argc; i++) {
488 ret1 = truncate_one_shm(argv[i], newsize);
489 if (ret1 != 0 && ret == 0)
497 int (*impl)(int argc, char **argv);
500 static const struct opmode opmodes[] = {
501 { .cmd = "create", .impl = create_shm},
502 { .cmd = "rm", .impl = delete_shm, },
503 { .cmd = "list", .impl = list_shm },
504 { .cmd = "ls", .impl = list_shm },
505 { .cmd = "dump", .impl = read_shm, },
506 { .cmd = "stat", .impl = stat_shm, },
507 { .cmd = "truncate", .impl = truncate_shm, },
511 main(int argc, char *argv[])
513 const struct opmode *opmode;
523 for (i = 0; i < (int)nitems(opmodes); i++) {
524 if (strcmp(argv[1], opmodes[i].cmd) == 0) {
525 opmode = &opmodes[i];
529 if (opmode == NULL) {
533 ret = opmode->impl(argc - 1, argv + 1);