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>
35 #include <sys/sysctl.h>
52 fprintf(stderr, "Usage:\n"
53 "posixshmcontrol create [-m <mode>] <path> ...\n"
54 "posixshmcontrol rm <path> ...\n"
55 "posixshmcontrol ls [-h] [-n]\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)
66 fd = shm_open(path, O_RDWR | O_CREAT, mode);
68 warn("create %s", path);
76 create_shm(int argc, char **argv)
83 while ((c = getopt(argc, argv, "m:")) != -1) {
87 mode = strtol(optarg, &end, 0);
88 if (mode == 0 && errno != 0)
91 errx(1, "non-integer mode");
108 for (i = 0; i < argc; i++) {
109 ret1 = create_one_shm(argv[i], mode);
110 if (ret1 != 0 && ret == 0)
117 delete_one_shm(const char *path)
121 error = shm_unlink(path);
123 warn("unlink of %s failed", path);
132 delete_shm(int argc, char **argv)
142 for (i = 1; i < argc; i++) {
143 ret1 = delete_one_shm(argv[i]);
144 if (ret1 != 0 && ret == 0)
150 static const char listmib[] = "kern.ipc.posix_shm_list";
153 shm_decode_mode(mode_t m, char *str)
158 str[i++] = (m & S_IRUSR) != 0 ? 'r' : '-';
159 str[i++] = (m & S_IWUSR) != 0 ? 'w' : '-';
160 str[i++] = (m & S_IXUSR) != 0 ? 'x' : '-';
161 str[i++] = (m & S_IRGRP) != 0 ? 'r' : '-';
162 str[i++] = (m & S_IWGRP) != 0 ? 'w' : '-';
163 str[i++] = (m & S_IXGRP) != 0 ? 'x' : '-';
164 str[i++] = (m & S_IROTH) != 0 ? 'r' : '-';
165 str[i++] = (m & S_IWOTH) != 0 ? 'w' : '-';
166 str[i++] = (m & S_IXOTH) != 0 ? 'x' : '-';
171 list_shm(int argc, char **argv)
173 char *buf, *bp, sizebuf[8], str[10];
174 const struct kinfo_file *kif;
176 int c, error, fd, mib[3], ret;
183 while ((c = getopt(argc, argv, "hn")) != -1) {
196 if (argc != optind) {
201 miblen = nitems(mib);
202 error = sysctlnametomib(listmib, mib, &miblen);
204 warn("cannot translate %s", listmib);
208 error = sysctl(mib, miblen, NULL, &len, NULL, 0);
210 warn("cannot get %s length", listmib);
219 error = sysctl(mib, miblen, buf, &len, NULL, 0);
221 warn("reading %s", listmib);
226 printf("MODE \tOWNER\tGROUP\tSIZE\tPATH\n");
227 for (bp = buf; bp < buf + len; bp += kif->kf_structsize) {
228 kif = (const struct kinfo_file *)(void *)bp;
229 if (kif->kf_structsize == 0)
231 fd = shm_open(kif->kf_path, O_RDONLY, 0);
233 warn("open %s", kif->kf_path);
237 error = fstat(fd, &st);
240 warn("stat %s", kif->kf_path);
244 shm_decode_mode(kif->kf_un.kf_file.kf_file_mode, str);
247 printf("%s\t%s\t", user_from_uid(st.st_uid, 0),
248 group_from_gid(st.st_gid, 0));
250 printf("%d\t%d\t", st.st_uid, st.st_gid);
253 humanize_number(sizebuf, sizeof(sizebuf),
254 kif->kf_un.kf_file.kf_file_size, "", HN_AUTOSCALE,
256 printf("%s\t", sizebuf);
259 (uintmax_t)kif->kf_un.kf_file.kf_file_size);
261 printf("%s\n", kif->kf_path);
269 read_one_shm(const char *path)
276 fd = shm_open(path, O_RDONLY, 0);
278 warn("open %s", path);
282 size = read(fd, buf, sizeof(buf));
284 se = fwrite(buf, 1, size, stdout);
286 warnx("short write to stdout");
290 if (size == (ssize_t)sizeof(buf))
292 if (size >= 0 && size < (ssize_t)sizeof(buf)) {
296 warn("read from %s", path);
305 read_shm(int argc, char **argv)
315 for (i = 1; i < argc; i++) {
316 ret1 = read_one_shm(argv[i]);
317 if (ret1 != 0 && ret == 0)
324 stat_one_shm(const char *path, bool hsize, bool uname)
330 fd = shm_open(path, O_RDONLY, 0);
332 warn("open %s", path);
336 error = fstat(fd, &st);
338 warn("stat %s", path);
341 printf("path\t%s\n", path);
342 printf("inode\t%jd\n", (uintmax_t)st.st_ino);
343 printf("mode\t%#o\n", st.st_mode);
344 printf("nlink\t%jd\n", (uintmax_t)st.st_nlink);
346 printf("owner\t%s\n", user_from_uid(st.st_uid, 0));
347 printf("group\t%s\n", group_from_gid(st.st_gid, 0));
349 printf("uid\t%d\n", st.st_uid);
350 printf("gid\t%d\n", st.st_gid);
353 humanize_number(sizebuf, sizeof(sizebuf),
354 st.st_size, "", HN_AUTOSCALE, HN_NOSPACE);
355 printf("size\t%s\n", sizebuf);
357 printf("size\t%jd\n", (uintmax_t)st.st_size);
359 printf("atime\t%ld.%09ld\n", (long)st.st_atime,
360 (long)st.st_atim.tv_nsec);
361 printf("mtime\t%ld.%09ld\n", (long)st.st_mtime,
362 (long)st.st_mtim.tv_nsec);
363 printf("ctime\t%ld.%09ld\n", (long)st.st_ctime,
364 (long)st.st_ctim.tv_nsec);
365 printf("birth\t%ld.%09ld\n", (long)st.st_birthtim.tv_sec,
366 (long)st.st_birthtim.tv_nsec);
373 stat_shm(int argc, char **argv)
381 while ((c = getopt(argc, argv, "hn")) != -1) {
403 for (i = 0; i < argc; i++) {
404 ret1 = stat_one_shm(argv[i], hsize, uname);
405 if (ret1 != 0 && ret == 0)
412 truncate_one_shm(const char *path, uint64_t newsize)
417 fd = shm_open(path, O_RDWR, 0);
419 warn("open %s", path);
422 error = ftruncate(fd, newsize);
424 warn("truncate %s", path);
432 truncate_shm(int argc, char **argv)
438 while ((c = getopt(argc, argv, "s:")) != -1) {
441 if (expand_number(optarg, &newsize) == -1)
458 for (i = 0; i < argc; i++) {
459 ret1 = truncate_one_shm(argv[i], newsize);
460 if (ret1 != 0 && ret == 0)
468 int (*impl)(int argc, char **argv);
471 static const struct opmode opmodes[] = {
472 { .cmd = "create", .impl = create_shm},
473 { .cmd = "rm", .impl = delete_shm, },
474 { .cmd = "list", .impl = list_shm },
475 { .cmd = "ls", .impl = list_shm },
476 { .cmd = "dump", .impl = read_shm, },
477 { .cmd = "stat", .impl = stat_shm, },
478 { .cmd = "truncate", .impl = truncate_shm, },
482 main(int argc, char *argv[])
484 const struct opmode *opmode;
494 for (i = 0; i < (int)nitems(opmodes); i++) {
495 if (strcmp(argv[1], opmodes[i].cmd) == 0) {
496 opmode = &opmodes[i];
500 if (opmode == NULL) {
504 ret = opmode->impl(argc - 1, argv + 1);