]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/posixshmcontrol/posixshmcontrol.c
Add posixshmcontrol(1) support for largepage shm objects.
[FreeBSD/FreeBSD.git] / usr.bin / posixshmcontrol / posixshmcontrol.c
1 /*-
2  * Copyright (c) 2019 The FreeBSD Foundation
3  *
4  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
5  * under sponsorship from the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/filio.h>
34 #include <sys/mman.h>
35 #include <sys/stat.h>
36 #include <sys/syscall.h>
37 #include <sys/sysctl.h>
38 #include <sys/user.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <grp.h>
42 #include <libutil.h>
43 #include <pwd.h>
44 #include <stdbool.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 static void
51 usage(void)
52 {
53
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");
61 }
62
63 static int
64 create_one_shm(const char *path, long mode, int idx)
65 {
66         int fd;
67
68         if (idx == -1) {
69                 fd = shm_open(path, O_RDWR | O_CREAT, mode);
70                 if (fd == -1) {
71                         warn("create %s", path);
72                         return (1);
73                 }
74         } else {
75                 fd = shm_create_largepage(path, O_RDWR, idx,
76                     SHM_LARGEPAGE_ALLOC_DEFAULT, mode);
77                 if (fd == -1) {
78                         warn("shm_create_largepage %s psind %d", path, idx);
79                         return (1);
80                 }
81         }
82         close(fd);
83         return (0);
84 }
85
86 static int
87 create_shm(int argc, char **argv)
88 {
89         char *end;
90         size_t *pagesizes;
91         long mode;
92         uint64_t pgsz;
93         int c, i, idx, pn, ret, ret1;
94         bool printed;
95
96         mode = 0600;
97         idx = -1;
98         while ((c = getopt(argc, argv, "l:m:")) != -1) {
99                 switch (c) {
100                 case 'm':
101                         errno = 0;
102                         mode = strtol(optarg, &end, 0);
103                         if (mode == 0 && errno != 0)
104                                 err(1, "mode");
105                         if (*end != '\0')
106                                 errx(1, "non-integer mode");
107                         break;
108                 case 'l':
109                         if (expand_number(optarg, &pgsz) == -1)
110                                 err(1, "size");
111                         pn = getpagesizes(NULL, 0);
112                         if (pn == -1)
113                                 err(1, "getpagesizes");
114                         pagesizes = malloc(sizeof(size_t) * pn);
115                         if (pagesizes == NULL)
116                                 err(1, "malloc");
117                         if (getpagesizes(pagesizes, pn) == -1)
118                                 err(1, "gtpagesizes");
119                         for (idx = 0; idx < pn; idx++) {
120                                 if (pagesizes[idx] == pgsz)
121                                         break;
122                         }
123                         if (idx == pn) {
124                                 fprintf(stderr,
125     "pagesize should be superpagesize, supported sizes:");
126                                 printed = false;
127                                 for (i = 0; i < pn; i++) {
128                                         if (pagesizes[i] == 0 ||
129                                             pagesizes[i] == (size_t)
130                                             getpagesize())
131                                                 continue;
132                                         printed = true;
133                                         fprintf(stderr, " %zu", pagesizes[i]);
134                                 }
135                                 if (!printed)
136                                         fprintf(stderr, " none");
137                                 fprintf(stderr, "\n");
138                                 exit(1);
139                         }
140                         if (pgsz == (uint64_t)getpagesize())
141                                 errx(1, "pagesize should be large");
142                         free(pagesizes);
143                         break;
144                 case '?':
145                 default:
146                         usage();
147                         return (2);
148                 }
149         }
150
151         argc -= optind;
152         argv += optind;
153         ret = 0;
154         for (i = 0; i < argc; i++) {
155                 ret1 = create_one_shm(argv[i], mode, idx);
156                 if (ret1 != 0 && ret == 0)
157                         ret = ret1;
158         }
159         return (ret);
160 }
161
162 static int
163 delete_one_shm(const char *path)
164 {
165         int error, ret;
166
167         error = shm_unlink(path);
168         if (error != 0) {
169                 warn("unlink of %s failed", path);
170                 ret = 1;
171         } else {
172                 ret = 0;
173         }
174         return (ret);
175 }
176
177 static int
178 delete_shm(int argc, char **argv)
179 {
180         int i, ret, ret1;
181
182         ret = 0;
183         for (i = 1; i < argc; i++) {
184                 ret1 = delete_one_shm(argv[i]);
185                 if (ret1 != 0 && ret == 0)
186                         ret = ret1;
187         }
188         return (ret);
189 }
190
191 static const char listmib[] = "kern.ipc.posix_shm_list";
192
193 static void
194 shm_decode_mode(mode_t m, char *str)
195 {
196         int i;
197
198         i = 0;
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' : '-';
208         str[i] = '\0';
209 }
210
211 static int
212 list_shm(int argc, char **argv)
213 {
214         char *buf, *bp, sizebuf[8], str[10];
215         const struct kinfo_file *kif;
216         struct stat st;
217         int c, error, fd, mib[3], ret;
218         size_t len, miblen;
219         bool hsize, uname;
220
221         hsize = false;
222         uname = true;
223
224         while ((c = getopt(argc, argv, "hn")) != -1) {
225                 switch (c) {
226                 case 'h':
227                         hsize = true;
228                         break;
229                 case 'n':
230                         uname = false;
231                         break;
232                 default:
233                         usage();
234                         return (2);
235                 }
236         }
237         if (argc != optind) {
238                 usage();
239                 return (2);
240         }
241
242         miblen = nitems(mib);
243         error = sysctlnametomib(listmib, mib, &miblen);
244         if (error == -1) {
245                 warn("cannot translate %s", listmib);
246                 return (1);
247         }
248         len = 0;
249         error = sysctl(mib, miblen, NULL, &len, NULL, 0);
250         if (error == -1) {
251                 warn("cannot get %s length", listmib);
252                 return (1);
253         }
254         len = len * 4 / 3;
255         buf = malloc(len);
256         if (buf == NULL) {
257                 warn("malloc");
258                 return (1);
259         }
260         error = sysctl(mib, miblen, buf, &len, NULL, 0);
261         if (error != 0) {
262                 warn("reading %s", listmib);
263                 ret = 1;
264                 goto out;
265         }
266         ret = 0;
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)
271                         break;
272                 fd = shm_open(kif->kf_path, O_RDONLY, 0);
273                 if (fd == -1) {
274                         warn("open %s", kif->kf_path);
275                         ret = 1;
276                         continue;
277                 }
278                 error = fstat(fd, &st);
279                 close(fd);
280                 if (error != 0) {
281                         warn("stat %s", kif->kf_path);
282                         ret = 1;
283                         continue;
284                 }
285                 shm_decode_mode(kif->kf_un.kf_file.kf_file_mode, str);
286                 printf("%s\t", str);
287                 if (uname) {
288                         printf("%s\t%s\t", user_from_uid(st.st_uid, 0),
289                             group_from_gid(st.st_gid, 0));
290                 } else {
291                         printf("%d\t%d\t", st.st_uid, st.st_gid);
292                 }
293                 if (hsize) {
294                         humanize_number(sizebuf, sizeof(sizebuf),
295                             kif->kf_un.kf_file.kf_file_size, "", HN_AUTOSCALE,
296                             HN_NOSPACE);
297                         printf("%s\t", sizebuf);
298                 } else {
299                         printf("%jd\t",
300                             (uintmax_t)kif->kf_un.kf_file.kf_file_size);
301                 }
302                 printf("%s\n", kif->kf_path);
303         }
304 out:
305         free(buf);
306         return (ret);
307 }
308
309 static int
310 read_one_shm(const char *path)
311 {
312         char buf[4096];
313         ssize_t size, se;
314         int fd, ret;
315
316         ret = 1;
317         fd = shm_open(path, O_RDONLY, 0);
318         if (fd == -1) {
319                 warn("open %s", path);
320                 goto out;
321         }
322         for (;;) {
323                 size = read(fd, buf, sizeof(buf));
324                 if (size > 0) {
325                         se = fwrite(buf, 1, size, stdout);
326                         if (se < size) {
327                                 warnx("short write to stdout");
328                                 goto out;
329                         }
330                 }
331                 if (size == (ssize_t)sizeof(buf))
332                         continue;
333                 if (size >= 0 && size < (ssize_t)sizeof(buf)) {
334                         ret = 0;
335                         goto out;
336                 }
337                 warn("read from %s", path);
338                 goto out;
339         }
340 out:
341         close(fd);
342         return (ret);
343 }
344
345 static int
346 read_shm(int argc, char **argv)
347 {
348         int i, ret, ret1;
349
350         ret = 0;
351         for (i = 1; i < argc; i++) {
352                 ret1 = read_one_shm(argv[i]);
353                 if (ret1 != 0 && ret == 0)
354                         ret = ret1;
355         }
356         return (ret);
357 }
358
359 static int
360 stat_one_shm(const char *path, bool hsize, bool uname)
361 {
362         char sizebuf[8];
363         struct stat st;
364         int error, fd, ret;
365
366         fd = shm_open(path, O_RDONLY, 0);
367         if (fd == -1) {
368                 warn("open %s", path);
369                 return (1);
370         }
371         ret = 0;
372         error = fstat(fd, &st);
373         if (error == -1) {
374                 warn("stat %s", path);
375                 ret = 1;
376         } else {
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);
381                 if (uname) {
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));
384                 } else {
385                         printf("uid\t%d\n", st.st_uid);
386                         printf("gid\t%d\n", st.st_gid);
387                 }
388                 if (hsize) {
389                         humanize_number(sizebuf, sizeof(sizebuf),
390                             st.st_size, "", HN_AUTOSCALE, HN_NOSPACE);
391                         printf("size\t%s\n", sizebuf);
392                 } else {
393                         printf("size\t%jd\n", (uintmax_t)st.st_size);
394                 }
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);
406         }
407         close(fd);
408         return (ret);
409 }
410
411 static int
412 stat_shm(int argc, char **argv)
413 {
414         int c, i, ret, ret1;
415         bool hsize, uname;
416
417         hsize = false;
418         uname = true;
419
420         while ((c = getopt(argc, argv, "hn")) != -1) {
421                 switch (c) {
422                 case 'h':
423                         hsize = true;
424                         break;
425                 case 'n':
426                         uname = false;
427                         break;
428                 default:
429                         usage();
430                         return (2);
431                 }
432         }
433         argc -= optind;
434         argv += optind;
435
436         ret = 0;
437         for (i = 0; i < argc; i++) {
438                 ret1 = stat_one_shm(argv[i], hsize, uname);
439                 if (ret1 != 0 && ret == 0)
440                         ret = ret1;
441         }
442         return (ret);
443 }
444
445 static int
446 truncate_one_shm(const char *path, uint64_t newsize)
447 {
448         int error, fd, ret;
449
450         ret = 0;
451         fd = shm_open(path, O_RDWR, 0);
452         if (fd == -1) {
453                 warn("open %s", path);
454                 return (1);
455         }
456         error = ftruncate(fd, newsize);
457         if (error == -1) {
458                 warn("truncate %s", path);
459                 ret = 1;
460         }
461         close(fd);
462         return (ret);
463 }
464
465 static int
466 truncate_shm(int argc, char **argv)
467 {
468         uint64_t newsize;
469         int c, i, ret, ret1;
470
471         newsize = 0;
472         while ((c = getopt(argc, argv, "s:")) != -1) {
473                 switch (c) {
474                 case 's':
475                         if (expand_number(optarg, &newsize) == -1)
476                                 err(1, "size:");
477                         break;
478                 case '?':
479                 default:
480                         return (2);
481                 }
482         }
483
484         argc -= optind;
485         argv += optind;
486         ret = 0;
487         for (i = 0; i < argc; i++) {
488                 ret1 = truncate_one_shm(argv[i], newsize);
489                 if (ret1 != 0 && ret == 0)
490                         ret = ret1;
491         }
492         return (ret);
493 }
494
495 struct opmode {
496         const char *cmd;
497         int (*impl)(int argc, char **argv);
498 };
499
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, },
508 };
509
510 int
511 main(int argc, char *argv[])
512 {
513         const struct opmode *opmode;
514         int i, ret;
515
516         ret = 0;
517         opmode = NULL;
518
519         if (argc < 2) {
520                 usage();
521                 exit(2);
522         }
523         for (i = 0; i < (int)nitems(opmodes); i++) {
524                 if (strcmp(argv[1], opmodes[i].cmd) == 0) {
525                         opmode = &opmodes[i];
526                         break;
527                 }
528         }
529         if (opmode == NULL) {
530                 usage();
531                 exit(2);
532         }
533         ret = opmode->impl(argc - 1, argv + 1);
534         exit(ret);
535 }