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>
55 fprintf(stderr, "Usage:\n"
56 "posixshmcontrol create [-m <mode>] [-l <largepage>] <path> ...\n"
57 "posixshmcontrol rm <path> ...\n"
58 "posixshmcontrol ls [-h] [-n] [-j jail]\n"
59 "posixshmcontrol dump <path> ...\n"
60 "posixshmcontrol stat [-h] [-n] <path> ...\n"
61 "posixshmcontrol truncate [-s <newlen>] <path> ...\n");
65 create_one_shm(const char *path, long mode, int idx)
70 fd = shm_open(path, O_RDWR | O_CREAT, mode);
72 warn("create %s", path);
76 fd = shm_create_largepage(path, O_RDWR, idx,
77 SHM_LARGEPAGE_ALLOC_DEFAULT, mode);
79 warn("shm_create_largepage %s psind %d", path, idx);
88 create_shm(int argc, char **argv)
94 int c, i, idx, pn, ret, ret1;
99 while ((c = getopt(argc, argv, "l:m:")) != -1) {
103 mode = strtol(optarg, &end, 0);
104 if (mode == 0 && errno != 0)
107 errx(1, "non-integer mode");
110 if (expand_number(optarg, &pgsz) == -1)
112 pn = getpagesizes(NULL, 0);
114 err(1, "getpagesizes");
115 pagesizes = malloc(sizeof(size_t) * pn);
116 if (pagesizes == NULL)
118 if (getpagesizes(pagesizes, pn) == -1)
119 err(1, "gtpagesizes");
120 for (idx = 0; idx < pn; idx++) {
121 if (pagesizes[idx] == pgsz)
126 "pagesize should be superpagesize, supported sizes:");
128 for (i = 0; i < pn; i++) {
129 if (pagesizes[i] == 0 ||
130 pagesizes[i] == (size_t)
134 fprintf(stderr, " %zu", pagesizes[i]);
137 fprintf(stderr, " none");
138 fprintf(stderr, "\n");
141 if (pgsz == (uint64_t)getpagesize())
142 errx(1, "pagesize should be large");
160 for (i = 0; i < argc; i++) {
161 ret1 = create_one_shm(argv[i], mode, idx);
162 if (ret1 != 0 && ret == 0)
169 delete_one_shm(const char *path)
173 error = shm_unlink(path);
175 warn("unlink of %s failed", path);
184 delete_shm(int argc, char **argv)
194 for (i = 1; i < argc; i++) {
195 ret1 = delete_one_shm(argv[i]);
196 if (ret1 != 0 && ret == 0)
202 static const char listmib[] = "kern.ipc.posix_shm_list";
205 shm_decode_mode(mode_t m, char *str)
210 str[i++] = (m & S_IRUSR) != 0 ? 'r' : '-';
211 str[i++] = (m & S_IWUSR) != 0 ? 'w' : '-';
212 str[i++] = (m & S_IXUSR) != 0 ? 'x' : '-';
213 str[i++] = (m & S_IRGRP) != 0 ? 'r' : '-';
214 str[i++] = (m & S_IWGRP) != 0 ? 'w' : '-';
215 str[i++] = (m & S_IXGRP) != 0 ? 'x' : '-';
216 str[i++] = (m & S_IROTH) != 0 ? 'r' : '-';
217 str[i++] = (m & S_IWOTH) != 0 ? 'w' : '-';
218 str[i++] = (m & S_IXOTH) != 0 ? 'x' : '-';
223 list_shm(int argc, char **argv)
225 char *buf, *bp, *ep, jailpath[MAXPATHLEN], sizebuf[8], str[10];
226 const char *jailparam;
227 const struct kinfo_file *kif;
229 int c, error, fd, jid, mib[3], ret;
230 size_t len, jailpathlen, miblen;
231 bool hsize, jailed, uname;
237 while ((c = getopt(argc, argv, "hj:n")) != -1) {
246 jid = strtoul(optarg, &ep, 10);
247 if (ep > optarg && !*ep) {
255 if (jail_getv(0, jailparam, optarg, "path",
256 jailpath, NULL) < 0) {
258 warnx("no such jail: %s", optarg);
260 warnx("%s", jail_errmsg);
263 jailpathlen = strlen(jailpath);
264 jailpath[jailpathlen] = '/';
272 if (argc != optind) {
277 miblen = nitems(mib);
278 error = sysctlnametomib(listmib, mib, &miblen);
280 warn("cannot translate %s", listmib);
284 error = sysctl(mib, miblen, NULL, &len, NULL, 0);
286 warn("cannot get %s length", listmib);
295 error = sysctl(mib, miblen, buf, &len, NULL, 0);
297 warn("reading %s", listmib);
302 printf("MODE \tOWNER\tGROUP\tSIZE\tPATH\n");
303 for (bp = buf; bp < buf + len; bp += kif->kf_structsize) {
304 kif = (const struct kinfo_file *)(void *)bp;
305 if (kif->kf_structsize == 0)
307 if (jailed && strncmp(kif->kf_path, jailpath, jailpathlen + 1))
309 fd = shm_open(kif->kf_path, O_RDONLY, 0);
311 warn("open %s", kif->kf_path);
315 error = fstat(fd, &st);
318 warn("stat %s", kif->kf_path);
322 shm_decode_mode(kif->kf_un.kf_file.kf_file_mode, str);
325 printf("%s\t%s\t", user_from_uid(st.st_uid, 0),
326 group_from_gid(st.st_gid, 0));
328 printf("%d\t%d\t", st.st_uid, st.st_gid);
331 humanize_number(sizebuf, sizeof(sizebuf),
332 kif->kf_un.kf_file.kf_file_size, "", HN_AUTOSCALE,
334 printf("%s\t", sizebuf);
337 (uintmax_t)kif->kf_un.kf_file.kf_file_size);
339 printf("%s\n", kif->kf_path);
347 read_one_shm(const char *path)
354 fd = shm_open(path, O_RDONLY, 0);
356 warn("open %s", path);
360 size = read(fd, buf, sizeof(buf));
362 se = fwrite(buf, 1, size, stdout);
364 warnx("short write to stdout");
368 if (size == (ssize_t)sizeof(buf))
370 if (size >= 0 && size < (ssize_t)sizeof(buf)) {
374 warn("read from %s", path);
383 read_shm(int argc, char **argv)
393 for (i = 1; i < argc; i++) {
394 ret1 = read_one_shm(argv[i]);
395 if (ret1 != 0 && ret == 0)
402 stat_one_shm(const char *path, bool hsize, bool uname)
407 struct shm_largepage_conf conf_dummy;
410 fd = shm_open(path, O_RDONLY, 0);
412 warn("open %s", path);
416 error = fstat(fd, &st);
418 warn("stat %s", path);
421 printf("path\t%s\n", path);
422 printf("inode\t%jd\n", (uintmax_t)st.st_ino);
423 printf("mode\t%#o\n", st.st_mode);
424 printf("nlink\t%jd\n", (uintmax_t)st.st_nlink);
426 printf("owner\t%s\n", user_from_uid(st.st_uid, 0));
427 printf("group\t%s\n", group_from_gid(st.st_gid, 0));
429 printf("uid\t%d\n", st.st_uid);
430 printf("gid\t%d\n", st.st_gid);
433 humanize_number(sizebuf, sizeof(sizebuf),
434 st.st_size, "", HN_AUTOSCALE, HN_NOSPACE);
435 printf("size\t%s\n", sizebuf);
437 printf("size\t%jd\n", (uintmax_t)st.st_size);
439 printf("atime\t%ld.%09ld\n", (long)st.st_atime,
440 (long)st.st_atim.tv_nsec);
441 printf("mtime\t%ld.%09ld\n", (long)st.st_mtime,
442 (long)st.st_mtim.tv_nsec);
443 printf("ctime\t%ld.%09ld\n", (long)st.st_ctime,
444 (long)st.st_ctim.tv_nsec);
445 printf("birth\t%ld.%09ld\n", (long)st.st_birthtim.tv_sec,
446 (long)st.st_birthtim.tv_nsec);
447 error = ioctl(fd, FIOGSHMLPGCNF, &conf_dummy);
448 largepage = error == 0;
449 if (st.st_blocks != 0 && largepage)
450 printf("pagesz\t%jd\n", roundup((uintmax_t)st.st_size,
451 PAGE_SIZE) / st.st_blocks);
453 printf("pages\t%jd\n", st.st_blocks);
460 stat_shm(int argc, char **argv)
468 while ((c = getopt(argc, argv, "hn")) != -1) {
490 for (i = 0; i < argc; i++) {
491 ret1 = stat_one_shm(argv[i], hsize, uname);
492 if (ret1 != 0 && ret == 0)
499 truncate_one_shm(const char *path, uint64_t newsize)
504 fd = shm_open(path, O_RDWR, 0);
506 warn("open %s", path);
509 error = ftruncate(fd, newsize);
511 warn("truncate %s", path);
519 truncate_shm(int argc, char **argv)
525 while ((c = getopt(argc, argv, "s:")) != -1) {
528 if (expand_number(optarg, &newsize) == -1)
545 for (i = 0; i < argc; i++) {
546 ret1 = truncate_one_shm(argv[i], newsize);
547 if (ret1 != 0 && ret == 0)
555 int (*impl)(int argc, char **argv);
558 static const struct opmode opmodes[] = {
559 { .cmd = "create", .impl = create_shm},
560 { .cmd = "rm", .impl = delete_shm, },
561 { .cmd = "list", .impl = list_shm },
562 { .cmd = "ls", .impl = list_shm },
563 { .cmd = "dump", .impl = read_shm, },
564 { .cmd = "stat", .impl = stat_shm, },
565 { .cmd = "truncate", .impl = truncate_shm, },
569 main(int argc, char *argv[])
571 const struct opmode *opmode;
581 for (i = 0; i < (int)nitems(opmodes); i++) {
582 if (strcmp(argv[1], opmodes[i].cmd) == 0) {
583 opmode = &opmodes[i];
587 if (opmode == NULL) {
591 ret = opmode->impl(argc - 1, argv + 1);