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");
103 for (i = 0; i < argc; i++) {
104 ret1 = create_one_shm(argv[i], mode);
105 if (ret1 != 0 && ret == 0)
112 delete_one_shm(const char *path)
116 error = shm_unlink(path);
118 warn("unlink of %s failed", path);
127 delete_shm(int argc, char **argv)
132 for (i = 1; i < argc; i++) {
133 ret1 = delete_one_shm(argv[i]);
134 if (ret1 != 0 && ret == 0)
140 static const char listmib[] = "kern.ipc.posix_shm_list";
143 shm_decode_mode(mode_t m, char *str)
148 str[i++] = (m & S_IRUSR) != 0 ? 'r' : '-';
149 str[i++] = (m & S_IWUSR) != 0 ? 'w' : '-';
150 str[i++] = (m & S_IXUSR) != 0 ? 'x' : '-';
151 str[i++] = (m & S_IRGRP) != 0 ? 'r' : '-';
152 str[i++] = (m & S_IWGRP) != 0 ? 'w' : '-';
153 str[i++] = (m & S_IXGRP) != 0 ? 'x' : '-';
154 str[i++] = (m & S_IROTH) != 0 ? 'r' : '-';
155 str[i++] = (m & S_IWOTH) != 0 ? 'w' : '-';
156 str[i++] = (m & S_IXOTH) != 0 ? 'x' : '-';
161 list_shm(int argc, char **argv)
163 char *buf, *bp, sizebuf[8], str[10];
164 const struct kinfo_file *kif;
166 int c, error, fd, mib[3], ret;
173 while ((c = getopt(argc, argv, "hn")) != -1) {
186 if (argc != optind) {
191 miblen = nitems(mib);
192 error = sysctlnametomib(listmib, mib, &miblen);
194 warn("cannot translate %s", listmib);
198 error = sysctl(mib, miblen, NULL, &len, NULL, 0);
200 warn("cannot get %s length", listmib);
209 error = sysctl(mib, miblen, buf, &len, NULL, 0);
211 warn("reading %s", listmib);
216 printf("MODE \tOWNER\tGROUP\tSIZE\tPATH\n");
217 for (bp = buf; bp < buf + len; bp += kif->kf_structsize) {
218 kif = (const struct kinfo_file *)(void *)bp;
219 if (kif->kf_structsize == 0)
221 fd = shm_open(kif->kf_path, O_RDONLY, 0);
223 warn("open %s", kif->kf_path);
227 error = fstat(fd, &st);
230 warn("stat %s", kif->kf_path);
234 shm_decode_mode(kif->kf_un.kf_file.kf_file_mode, str);
237 printf("%s\t%s\t", user_from_uid(st.st_uid, 0),
238 group_from_gid(st.st_gid, 0));
240 printf("%d\t%d\t", st.st_uid, st.st_gid);
243 humanize_number(sizebuf, sizeof(sizebuf),
244 kif->kf_un.kf_file.kf_file_size, "", HN_AUTOSCALE,
246 printf("%s\t", sizebuf);
249 (uintmax_t)kif->kf_un.kf_file.kf_file_size);
251 printf("%s\n", kif->kf_path);
259 read_one_shm(const char *path)
266 fd = shm_open(path, O_RDONLY, 0);
268 warn("open %s", path);
272 size = read(fd, buf, sizeof(buf));
274 se = fwrite(buf, 1, size, stdout);
276 warnx("short write to stdout");
280 if (size == (ssize_t)sizeof(buf))
282 if (size >= 0 && size < (ssize_t)sizeof(buf)) {
286 warn("read from %s", path);
295 read_shm(int argc, char **argv)
300 for (i = 1; i < argc; i++) {
301 ret1 = read_one_shm(argv[i]);
302 if (ret1 != 0 && ret == 0)
309 stat_one_shm(const char *path, bool hsize, bool uname)
315 fd = shm_open(path, O_RDONLY, 0);
317 warn("open %s", path);
321 error = fstat(fd, &st);
323 warn("stat %s", path);
326 printf("path\t%s\n", path);
327 printf("inode\t%jd\n", (uintmax_t)st.st_ino);
328 printf("mode\t%#o\n", st.st_mode);
329 printf("nlink\t%jd\n", (uintmax_t)st.st_nlink);
331 printf("owner\t%s\n", user_from_uid(st.st_uid, 0));
332 printf("group\t%s\n", group_from_gid(st.st_gid, 0));
334 printf("uid\t%d\n", st.st_uid);
335 printf("gid\t%d\n", st.st_gid);
338 humanize_number(sizebuf, sizeof(sizebuf),
339 st.st_size, "", HN_AUTOSCALE, HN_NOSPACE);
340 printf("size\t%s\n", sizebuf);
342 printf("size\t%jd\n", (uintmax_t)st.st_size);
344 printf("atime\t%ld.%09ld\n", (long)st.st_atime,
345 (long)st.st_atim.tv_nsec);
346 printf("mtime\t%ld.%09ld\n", (long)st.st_mtime,
347 (long)st.st_mtim.tv_nsec);
348 printf("ctime\t%ld.%09ld\n", (long)st.st_ctime,
349 (long)st.st_ctim.tv_nsec);
350 printf("birth\t%ld.%09ld\n", (long)st.st_birthtim.tv_sec,
351 (long)st.st_birthtim.tv_nsec);
358 stat_shm(int argc, char **argv)
366 while ((c = getopt(argc, argv, "hn")) != -1) {
383 for (i = 0; i < argc; i++) {
384 ret1 = stat_one_shm(argv[i], hsize, uname);
385 if (ret1 != 0 && ret == 0)
392 truncate_one_shm(const char *path, uint64_t newsize)
397 fd = shm_open(path, O_RDWR, 0);
399 warn("open %s", path);
402 error = ftruncate(fd, newsize);
404 warn("truncate %s", path);
412 truncate_shm(int argc, char **argv)
418 while ((c = getopt(argc, argv, "s:")) != -1) {
421 if (expand_number(optarg, &newsize) == -1)
433 for (i = 0; i < argc; i++) {
434 ret1 = truncate_one_shm(argv[i], newsize);
435 if (ret1 != 0 && ret == 0)
443 int (*impl)(int argc, char **argv);
446 static const struct opmode opmodes[] = {
447 { .cmd = "create", .impl = create_shm},
448 { .cmd = "rm", .impl = delete_shm, },
449 { .cmd = "ls", .impl = list_shm },
450 { .cmd = "dump", .impl = read_shm, },
451 { .cmd = "stat", .impl = stat_shm, },
452 { .cmd = "truncate", .impl = truncate_shm, },
456 main(int argc, char *argv[])
458 const struct opmode *opmode;
468 for (i = 0; i < (int)nitems(opmodes); i++) {
469 if (strcmp(argv[1], opmodes[i].cmd) == 0) {
470 opmode = &opmodes[i];
474 if (opmode == NULL) {
478 ret = opmode->impl(argc - 1, argv + 1);