]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/posixshmcontrol/posixshmcontrol.c
Merge LUA 5.4.6
[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 <jail.h>
43 #include <libutil.h>
44 #include <pwd.h>
45 #include <stdbool.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 static void
52 usage(void)
53 {
54
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");
62 }
63
64 static int
65 create_one_shm(const char *path, long mode, int idx)
66 {
67         int fd;
68
69         if (idx == -1) {
70                 fd = shm_open(path, O_RDWR | O_CREAT, mode);
71                 if (fd == -1) {
72                         warn("create %s", path);
73                         return (1);
74                 }
75         } else {
76                 fd = shm_create_largepage(path, O_RDWR, idx,
77                     SHM_LARGEPAGE_ALLOC_DEFAULT, mode);
78                 if (fd == -1) {
79                         warn("shm_create_largepage %s psind %d", path, idx);
80                         return (1);
81                 }
82         }
83         close(fd);
84         return (0);
85 }
86
87 static int
88 create_shm(int argc, char **argv)
89 {
90         char *end;
91         size_t *pagesizes;
92         long mode;
93         uint64_t pgsz;
94         int c, i, idx, pn, ret, ret1;
95         bool printed;
96
97         mode = 0600;
98         idx = -1;
99         while ((c = getopt(argc, argv, "l:m:")) != -1) {
100                 switch (c) {
101                 case 'm':
102                         errno = 0;
103                         mode = strtol(optarg, &end, 0);
104                         if (mode == 0 && errno != 0)
105                                 err(1, "mode");
106                         if (*end != '\0')
107                                 errx(1, "non-integer mode");
108                         break;
109                 case 'l':
110                         if (expand_number(optarg, &pgsz) == -1)
111                                 err(1, "size");
112                         pn = getpagesizes(NULL, 0);
113                         if (pn == -1)
114                                 err(1, "getpagesizes");
115                         pagesizes = malloc(sizeof(size_t) * pn);
116                         if (pagesizes == NULL)
117                                 err(1, "malloc");
118                         if (getpagesizes(pagesizes, pn) == -1)
119                                 err(1, "gtpagesizes");
120                         for (idx = 0; idx < pn; idx++) {
121                                 if (pagesizes[idx] == pgsz)
122                                         break;
123                         }
124                         if (idx == pn) {
125                                 fprintf(stderr,
126     "pagesize should be superpagesize, supported sizes:");
127                                 printed = false;
128                                 for (i = 0; i < pn; i++) {
129                                         if (pagesizes[i] == 0 ||
130                                             pagesizes[i] == (size_t)
131                                             getpagesize())
132                                                 continue;
133                                         printed = true;
134                                         fprintf(stderr, " %zu", pagesizes[i]);
135                                 }
136                                 if (!printed)
137                                         fprintf(stderr, " none");
138                                 fprintf(stderr, "\n");
139                                 exit(1);
140                         }
141                         if (pgsz == (uint64_t)getpagesize())
142                                 errx(1, "pagesize should be large");
143                         free(pagesizes);
144                         break;
145                 case '?':
146                 default:
147                         usage();
148                         return (2);
149                 }
150         }
151         argc -= optind;
152         argv += optind;
153
154         if (argc == 0) {
155                 usage();
156                 return (2);
157         }
158
159         ret = 0;
160         for (i = 0; i < argc; i++) {
161                 ret1 = create_one_shm(argv[i], mode, idx);
162                 if (ret1 != 0 && ret == 0)
163                         ret = ret1;
164         }
165         return (ret);
166 }
167
168 static int
169 delete_one_shm(const char *path)
170 {
171         int error, ret;
172
173         error = shm_unlink(path);
174         if (error != 0) {
175                 warn("unlink of %s failed", path);
176                 ret = 1;
177         } else {
178                 ret = 0;
179         }
180         return (ret);
181 }
182
183 static int
184 delete_shm(int argc, char **argv)
185 {
186         int i, ret, ret1;
187
188         if (argc == 1) {
189                 usage();
190                 return (2);
191         }
192
193         ret = 0;
194         for (i = 1; i < argc; i++) {
195                 ret1 = delete_one_shm(argv[i]);
196                 if (ret1 != 0 && ret == 0)
197                         ret = ret1;
198         }
199         return (ret);
200 }
201
202 static const char listmib[] = "kern.ipc.posix_shm_list";
203
204 static void
205 shm_decode_mode(mode_t m, char *str)
206 {
207         int i;
208
209         i = 0;
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' : '-';
219         str[i] = '\0';
220 }
221
222 static int
223 list_shm(int argc, char **argv)
224 {
225         char *buf, *bp, *ep, jailpath[MAXPATHLEN], sizebuf[8], str[10];
226         const char *jailparam;
227         const struct kinfo_file *kif;
228         struct stat st;
229         int c, error, fd, jid, mib[3], ret;
230         size_t len, jailpathlen, miblen;
231         bool hsize, jailed, uname;
232
233         hsize = false;
234         jailed = false;
235         uname = true;
236
237         while ((c = getopt(argc, argv, "hj:n")) != -1) {
238                 switch (c) {
239                 case 'h':
240                         hsize = true;
241                         break;
242                 case 'n':
243                         uname = false;
244                         break;
245                 case 'j':
246                         jid = strtoul(optarg, &ep, 10);
247                         if (ep > optarg && !*ep) {
248                                 jailparam = "jid";
249                                 jailed = jid > 0;
250                         } else {
251                                 jailparam = "name";
252                                 jailed = true;
253                         }
254                         if (jailed) {
255                                 if (jail_getv(0, jailparam, optarg, "path",
256                                     jailpath, NULL) < 0) {
257                                         if (errno == ENOENT)
258                                                 warnx("no such jail: %s", optarg);
259                                         else
260                                                 warnx("%s", jail_errmsg);
261                                         return (1);
262                                 }
263                                 jailpathlen = strlen(jailpath);
264                                 jailpath[jailpathlen] = '/';
265                         }
266                         break;
267                 default:
268                         usage();
269                         return (2);
270                 }
271         }
272         if (argc != optind) {
273                 usage();
274                 return (2);
275         }
276
277         miblen = nitems(mib);
278         error = sysctlnametomib(listmib, mib, &miblen);
279         if (error == -1) {
280                 warn("cannot translate %s", listmib);
281                 return (1);
282         }
283         len = 0;
284         error = sysctl(mib, miblen, NULL, &len, NULL, 0);
285         if (error == -1) {
286                 warn("cannot get %s length", listmib);
287                 return (1);
288         }
289         len = len * 4 / 3;
290         buf = malloc(len);
291         if (buf == NULL) {
292                 warn("malloc");
293                 return (1);
294         }
295         error = sysctl(mib, miblen, buf, &len, NULL, 0);
296         if (error != 0) {
297                 warn("reading %s", listmib);
298                 ret = 1;
299                 goto out;
300         }
301         ret = 0;
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)
306                         break;
307                 if (jailed && strncmp(kif->kf_path, jailpath, jailpathlen + 1))
308                         continue;
309                 fd = shm_open(kif->kf_path, O_RDONLY, 0);
310                 if (fd == -1) {
311                         if (errno != EACCES) {
312                                 warn("open %s", kif->kf_path);
313                                 ret = 1;
314                         }
315                         continue;
316                 }
317                 error = fstat(fd, &st);
318                 close(fd);
319                 if (error != 0) {
320                         warn("stat %s", kif->kf_path);
321                         ret = 1;
322                         continue;
323                 }
324                 shm_decode_mode(kif->kf_un.kf_file.kf_file_mode, str);
325                 printf("%s\t", str);
326                 if (uname) {
327                         printf("%s\t%s\t", user_from_uid(st.st_uid, 0),
328                             group_from_gid(st.st_gid, 0));
329                 } else {
330                         printf("%d\t%d\t", st.st_uid, st.st_gid);
331                 }
332                 if (hsize) {
333                         humanize_number(sizebuf, sizeof(sizebuf),
334                             kif->kf_un.kf_file.kf_file_size, "", HN_AUTOSCALE,
335                             HN_NOSPACE);
336                         printf("%s\t", sizebuf);
337                 } else {
338                         printf("%jd\t",
339                             (uintmax_t)kif->kf_un.kf_file.kf_file_size);
340                 }
341                 printf("%s\n", kif->kf_path);
342         }
343 out:
344         free(buf);
345         return (ret);
346 }
347
348 static int
349 read_one_shm(const char *path)
350 {
351         char buf[4096];
352         ssize_t size, se;
353         int fd, ret;
354
355         ret = 1;
356         fd = shm_open(path, O_RDONLY, 0);
357         if (fd == -1) {
358                 warn("open %s", path);
359                 goto out;
360         }
361         for (;;) {
362                 size = read(fd, buf, sizeof(buf));
363                 if (size > 0) {
364                         se = fwrite(buf, 1, size, stdout);
365                         if (se < size) {
366                                 warnx("short write to stdout");
367                                 goto out;
368                         }
369                 }
370                 if (size == (ssize_t)sizeof(buf))
371                         continue;
372                 if (size >= 0 && size < (ssize_t)sizeof(buf)) {
373                         ret = 0;
374                         goto out;
375                 }
376                 warn("read from %s", path);
377                 goto out;
378         }
379 out:
380         close(fd);
381         return (ret);
382 }
383
384 static int
385 read_shm(int argc, char **argv)
386 {
387         int i, ret, ret1;
388
389         if (argc == 1) {
390                 usage();
391                 return (2);
392         }
393
394         ret = 0;
395         for (i = 1; i < argc; i++) {
396                 ret1 = read_one_shm(argv[i]);
397                 if (ret1 != 0 && ret == 0)
398                         ret = ret1;
399         }
400         return (ret);
401 }
402
403 static int
404 stat_one_shm(const char *path, bool hsize, bool uname)
405 {
406         char sizebuf[8];
407         struct stat st;
408         int error, fd, ret;
409         struct shm_largepage_conf conf_dummy;
410         bool largepage;
411
412         fd = shm_open(path, O_RDONLY, 0);
413         if (fd == -1) {
414                 warn("open %s", path);
415                 return (1);
416         }
417         ret = 0;
418         error = fstat(fd, &st);
419         if (error == -1) {
420                 warn("stat %s", path);
421                 ret = 1;
422         } else {
423                 printf("path\t%s\n", path);
424                 printf("inode\t%jd\n", (uintmax_t)st.st_ino);
425                 printf("mode\t%#o\n", st.st_mode);
426                 printf("nlink\t%jd\n", (uintmax_t)st.st_nlink);
427                 if (uname) {
428                         printf("owner\t%s\n", user_from_uid(st.st_uid, 0));
429                         printf("group\t%s\n", group_from_gid(st.st_gid, 0));
430                 } else {
431                         printf("uid\t%d\n", st.st_uid);
432                         printf("gid\t%d\n", st.st_gid);
433                 }
434                 if (hsize) {
435                         humanize_number(sizebuf, sizeof(sizebuf),
436                             st.st_size, "", HN_AUTOSCALE, HN_NOSPACE);
437                         printf("size\t%s\n", sizebuf);
438                 } else {
439                         printf("size\t%jd\n", (uintmax_t)st.st_size);
440                 }
441                 printf("atime\t%ld.%09ld\n", (long)st.st_atime,
442                     (long)st.st_atim.tv_nsec);
443                 printf("mtime\t%ld.%09ld\n", (long)st.st_mtime,
444                     (long)st.st_mtim.tv_nsec);
445                 printf("ctime\t%ld.%09ld\n", (long)st.st_ctime,
446                     (long)st.st_ctim.tv_nsec);
447                 printf("birth\t%ld.%09ld\n", (long)st.st_birthtim.tv_sec,
448                     (long)st.st_birthtim.tv_nsec);
449                 error = ioctl(fd, FIOGSHMLPGCNF, &conf_dummy);
450                 largepage = error == 0;
451                 if (st.st_blocks != 0 && largepage)
452                         printf("pagesz\t%jd\n", roundup((uintmax_t)st.st_size,
453                             PAGE_SIZE) / st.st_blocks);
454                 else
455                         printf("pages\t%jd\n", st.st_blocks);
456         }
457         close(fd);
458         return (ret);
459 }
460
461 static int
462 stat_shm(int argc, char **argv)
463 {
464         int c, i, ret, ret1;
465         bool hsize, uname;
466
467         hsize = false;
468         uname = true;
469
470         while ((c = getopt(argc, argv, "hn")) != -1) {
471                 switch (c) {
472                 case 'h':
473                         hsize = true;
474                         break;
475                 case 'n':
476                         uname = false;
477                         break;
478                 default:
479                         usage();
480                         return (2);
481                 }
482         }
483         argc -= optind;
484         argv += optind;
485
486         if (argc == 0) {
487                 usage();
488                 return (2);
489         }
490
491         ret = 0;
492         for (i = 0; i < argc; i++) {
493                 ret1 = stat_one_shm(argv[i], hsize, uname);
494                 if (ret1 != 0 && ret == 0)
495                         ret = ret1;
496         }
497         return (ret);
498 }
499
500 static int
501 truncate_one_shm(const char *path, uint64_t newsize)
502 {
503         int error, fd, ret;
504
505         ret = 0;
506         fd = shm_open(path, O_RDWR, 0);
507         if (fd == -1) {
508                 warn("open %s", path);
509                 return (1);
510         }
511         error = ftruncate(fd, newsize);
512         if (error == -1) {
513                 warn("truncate %s", path);
514                 ret = 1;
515         }
516         close(fd);
517         return (ret);
518 }
519
520 static int
521 truncate_shm(int argc, char **argv)
522 {
523         uint64_t newsize;
524         int c, i, ret, ret1;
525
526         newsize = 0;
527         while ((c = getopt(argc, argv, "s:")) != -1) {
528                 switch (c) {
529                 case 's':
530                         if (expand_number(optarg, &newsize) == -1)
531                                 err(1, "size");
532                         break;
533                 case '?':
534                 default:
535                         return (2);
536                 }
537         }
538         argc -= optind;
539         argv += optind;
540
541         if (argc == 0) {
542                 usage();
543                 return (2);
544         }
545
546         ret = 0;
547         for (i = 0; i < argc; i++) {
548                 ret1 = truncate_one_shm(argv[i], newsize);
549                 if (ret1 != 0 && ret == 0)
550                         ret = ret1;
551         }
552         return (ret);
553 }
554
555 struct opmode {
556         const char *cmd;
557         int (*impl)(int argc, char **argv);
558 };
559
560 static const struct opmode opmodes[] = {
561         { .cmd = "create",      .impl = create_shm},
562         { .cmd = "rm",          .impl = delete_shm, },
563         { .cmd = "list",        .impl = list_shm },
564         { .cmd = "ls",          .impl = list_shm },
565         { .cmd = "dump",        .impl = read_shm, },
566         { .cmd = "stat",        .impl = stat_shm, },
567         { .cmd = "truncate",    .impl = truncate_shm, },
568 };
569
570 int
571 main(int argc, char *argv[])
572 {
573         const struct opmode *opmode;
574         int i, ret;
575
576         ret = 0;
577         opmode = NULL;
578
579         if (argc < 2) {
580                 usage();
581                 exit(2);
582         }
583         for (i = 0; i < (int)nitems(opmodes); i++) {
584                 if (strcmp(argv[1], opmodes[i].cmd) == 0) {
585                         opmode = &opmodes[i];
586                         break;
587                 }
588         }
589         if (opmode == NULL) {
590                 usage();
591                 exit(2);
592         }
593         ret = opmode->impl(argc - 1, argv + 1);
594         exit(ret);
595 }