Index: head/usr.bin/Makefile =================================================================== --- head/usr.bin/Makefile (revision 348166) +++ head/usr.bin/Makefile (revision 348167) @@ -1,300 +1,301 @@ # From: @(#)Makefile 8.3 (Berkeley) 1/7/94 # $FreeBSD$ .include SUBDIR= alias \ apply \ asa \ awk \ banner \ basename \ brandelf \ bsdcat \ bsdiff \ bzip2 \ bzip2recover \ cap_mkdb \ chat \ chpass \ cksum \ cmp \ col \ colldef \ colrm \ column \ comm \ compress \ cpuset \ csplit \ ctlstat \ cut \ diff \ dirname \ du \ elf2aout \ elfdump \ enigma \ env \ etdump \ expand \ false \ fetch \ find \ fmt \ fold \ fstat \ fsync \ gcore \ gencat \ getaddrinfo \ getconf \ getent \ getopt \ grep \ gzip \ head \ hexdump \ id \ ident \ ipcrm \ ipcs \ join \ jot \ keylogin \ keylogout \ killall \ ktrace \ ktrdump \ lam \ lastcomm \ ldd \ leave \ less \ lessecho \ lesskey \ limits \ locale \ localedef \ lock \ lockf \ logger \ login \ logins \ logname \ look \ lorder \ lsvfs \ lzmainfo \ m4 \ mandoc \ mesg \ minigzip \ ministat \ mkdep \ mkfifo \ mkimg \ mklocale \ mktemp \ mkuzip \ mt \ ncal \ netstat \ newgrp \ nfsstat \ nice \ nl \ nohup \ opieinfo \ opiekey \ opiepasswd \ pagesize \ passwd \ paste \ patch \ pathchk \ perror \ + posixshmcontrol \ pr \ printenv \ printf \ proccontrol \ procstat \ protect \ rctl \ renice \ resizewin \ rev \ revoke \ rpcinfo \ rs \ rup \ ruptime \ rusers \ rwall \ rwho \ script \ sdiff \ sed \ seq \ shar \ showmount \ sockstat \ soelim \ sort \ split \ stat \ stdbuf \ strings \ su \ systat \ tabs \ tail \ tar \ tcopy \ tee \ time \ timeout \ tip \ top \ touch \ tput \ tr \ true \ truncate \ tset \ tsort \ tty \ uname \ unexpand \ uniq \ unzip \ units \ unvis \ uudecode \ uuencode \ vis \ vmstat \ w \ wall \ wc \ what \ whereis \ which \ whois \ write \ xargs \ xinstall \ xo \ xz \ xzdec \ yes \ zstd # NB: keep these sorted by MK_* knobs SUBDIR.${MK_AT}+= at SUBDIR.${MK_ATM}+= atm SUBDIR.${MK_BLUETOOTH}+= bluetooth SUBDIR.${MK_BSD_CPIO}+= cpio SUBDIR.${MK_CALENDAR}+= calendar SUBDIR.${MK_CLANG}+= clang SUBDIR.${MK_DIALOG}+= dpv SUBDIR.${MK_EE}+= ee SUBDIR.${MK_FILE}+= file SUBDIR.${MK_FINGER}+= finger SUBDIR.${MK_FTP}+= ftp SUBDIR.${MK_GAMES}+= caesar SUBDIR.${MK_GAMES}+= factor SUBDIR.${MK_GAMES}+= fortune SUBDIR.${MK_GAMES}+= grdc SUBDIR.${MK_GAMES}+= morse SUBDIR.${MK_GAMES}+= number SUBDIR.${MK_GAMES}+= pom SUBDIR.${MK_GAMES}+= primes SUBDIR.${MK_GAMES}+= random .if ${MK_GPL_DTC} != "yes" .if ${COMPILER_FEATURES:Mc++11} SUBDIR+= dtc .endif .endif SUBDIR.${MK_HESIOD}+= hesinfo SUBDIR.${MK_ICONV}+= iconv SUBDIR.${MK_ICONV}+= mkcsmapper SUBDIR.${MK_ICONV}+= mkesdb SUBDIR.${MK_ISCSI}+= iscsictl SUBDIR.${MK_KDUMP}+= kdump SUBDIR.${MK_KDUMP}+= truss SUBDIR.${MK_KERBEROS_SUPPORT}+= compile_et SUBDIR.${MK_LDNS_UTILS}+= drill SUBDIR.${MK_LDNS_UTILS}+= host SUBDIR.${MK_LOCATE}+= locate # XXX msgs? SUBDIR.${MK_MAIL}+= biff SUBDIR.${MK_MAIL}+= from SUBDIR.${MK_MAIL}+= mail SUBDIR.${MK_MAIL}+= msgs SUBDIR.${MK_MAKE}+= bmake SUBDIR.${MK_MAN_UTILS}+= man SUBDIR.${MK_NETCAT}+= nc SUBDIR.${MK_NIS}+= ypcat SUBDIR.${MK_NIS}+= ypmatch SUBDIR.${MK_NIS}+= ypwhich SUBDIR.${MK_OPENSSH}+= ssh-copy-id SUBDIR.${MK_OPENSSL}+= bc SUBDIR.${MK_OPENSSL}+= chkey SUBDIR.${MK_OPENSSL}+= dc SUBDIR.${MK_OPENSSL}+= newkey SUBDIR.${MK_QUOTAS}+= quota SUBDIR.${MK_SENDMAIL}+= vacation SUBDIR.${MK_TALK}+= talk SUBDIR.${MK_TELNET}+= telnet SUBDIR.${MK_TESTS}+= tests SUBDIR.${MK_TEXTPROC}+= ul SUBDIR.${MK_TFTP}+= tftp SUBDIR.${MK_TOOLCHAIN}+= addr2line SUBDIR.${MK_TOOLCHAIN}+= ar SUBDIR.${MK_TOOLCHAIN}+= c89 SUBDIR.${MK_TOOLCHAIN}+= c99 SUBDIR.${MK_TOOLCHAIN}+= ctags SUBDIR.${MK_TOOLCHAIN}+= cxxfilt SUBDIR.${MK_TOOLCHAIN}+= objcopy SUBDIR.${MK_TOOLCHAIN}+= file2c # ARM64TODO gprof does not build # RISCVTODO gprof does not build .if ${MACHINE_ARCH} != "aarch64" && ${MACHINE_CPUARCH} != "riscv" SUBDIR.${MK_TOOLCHAIN}+= gprof .endif SUBDIR.${MK_TOOLCHAIN}+= indent SUBDIR.${MK_TOOLCHAIN}+= lex SUBDIR.${MK_TOOLCHAIN}+= mkstr SUBDIR.${MK_TOOLCHAIN}+= nm SUBDIR.${MK_TOOLCHAIN}+= readelf SUBDIR.${MK_TOOLCHAIN}+= rpcgen SUBDIR.${MK_TOOLCHAIN}+= unifdef SUBDIR.${MK_TOOLCHAIN}+= size SUBDIR.${MK_TOOLCHAIN}+= xstr SUBDIR.${MK_TOOLCHAIN}+= yacc SUBDIR.${MK_VI}+= vi SUBDIR.${MK_VT}+= vtfontcvt SUBDIR.${MK_USB}+= usbhidaction SUBDIR.${MK_USB}+= usbhidctl SUBDIR.${MK_UTMPX}+= last .if ${MK_CXX} != "no" SUBDIR.${MK_UTMPX}+= users .endif SUBDIR.${MK_UTMPX}+= who SUBDIR.${MK_SVN}+= svn SUBDIR.${MK_SVNLITE}+= svn SUBDIR.${MK_OFED}+= ofed # These are normally only handled for build-tools. .if make(clean*) SUBDIR+= mkcsmapper_static SUBDIR+= mkesdb_static .endif .include SUBDIR_PARALLEL= .include Index: head/usr.bin/posixshmcontrol/Makefile =================================================================== --- head/usr.bin/posixshmcontrol/Makefile (nonexistent) +++ head/usr.bin/posixshmcontrol/Makefile (revision 348167) @@ -0,0 +1,8 @@ +# $FreeBSD$ + +PROG= posixshmcontrol +LIBADD= util +WARNS?= 6 +MAN= + +.include Property changes on: head/usr.bin/posixshmcontrol/Makefile ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/usr.bin/posixshmcontrol/posixshmcontrol.c =================================================================== --- head/usr.bin/posixshmcontrol/posixshmcontrol.c (nonexistent) +++ head/usr.bin/posixshmcontrol/posixshmcontrol.c (revision 348167) @@ -0,0 +1,480 @@ +/*- + * Copyright (c) 2019 The FreeBSD Foundation + * + * This software was developed by Konstantin Belousov + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void +usage(void) +{ + + fprintf(stderr, "Usage:\n" + "posixshmcontrol create [-m ] ...\n" + "posixshmcontrol rm ...\n" + "posixshmcontrol ls [-h] [-n]\n" + "posixshmcontrol dump ...\n" + "posixshmcontrol stat [-h] [-n] ...\n" + "posixshmcontrol truncate [-s ] ...\n"); +} + +static int +create_one_shm(const char *path, long mode) +{ + int fd; + + fd = shm_open(path, O_RDWR | O_CREAT, mode); + if (fd == -1) { + warn("create %s", path); + return (1); + } + close(fd); + return (0); +} + +static int +create_shm(int argc, char **argv) +{ + char *end; + long mode; + int c, i, ret, ret1; + + mode = 0600; + while ((c = getopt(argc, argv, "m:")) != -1) { + switch (c) { + case 'm': + errno = 0; + mode = strtol(optarg, &end, 0); + if (mode == 0 && errno != 0) + err(1, "mode:"); + if (*end != '\0') + errx(1, "non-integer mode"); + break; + case '?': + default: + usage(); + return (2); + } + } + + argc -= optind; + argv += optind; + ret = 0; + for (i = 0; i < argc; i++) { + ret1 = create_one_shm(argv[i], mode); + if (ret1 != 0 && ret == 0) + ret = ret1; + } + return (ret); +} + +static int +delete_one_shm(const char *path) +{ + int error, ret; + + error = shm_unlink(path); + if (error != 0) { + warn("unlink of %s failed", path); + ret = 1; + } else { + ret = 0; + } + return (ret); +} + +static int +delete_shm(int argc, char **argv) +{ + int i, ret, ret1; + + ret = 0; + for (i = 1; i < argc; i++) { + ret1 = delete_one_shm(argv[i]); + if (ret1 != 0 && ret == 0) + ret = ret1; + } + return (ret); +} + +static const char listmib[] = "kern.ipc.posix_shm_list"; + +static void +shm_decode_mode(mode_t m, char *str) +{ + int i; + + i = 0; + str[i++] = (m & S_IRUSR) != 0 ? 'r' : '-'; + str[i++] = (m & S_IWUSR) != 0 ? 'w' : '-'; + str[i++] = (m & S_IXUSR) != 0 ? 'x' : '-'; + str[i++] = (m & S_IRGRP) != 0 ? 'r' : '-'; + str[i++] = (m & S_IWGRP) != 0 ? 'w' : '-'; + str[i++] = (m & S_IXGRP) != 0 ? 'x' : '-'; + str[i++] = (m & S_IROTH) != 0 ? 'r' : '-'; + str[i++] = (m & S_IWOTH) != 0 ? 'w' : '-'; + str[i++] = (m & S_IXOTH) != 0 ? 'x' : '-'; + str[i] = '\0'; +} + +static int +list_shm(int argc, char **argv) +{ + char *buf, *bp, sizebuf[8], str[10], c; + const struct kinfo_file *kif; + struct stat st; + int error, fd, mib[3], ret; + size_t len, miblen; + bool hsize, uname; + + hsize = false; + uname = true; + + while ((c = getopt(argc, argv, "hn")) != -1) { + switch (c) { + case 'h': + hsize = true; + break; + case 'n': + uname = false; + break; + default: + usage(); + return (2); + } + } + if (argc != optind) { + usage(); + return (2); + } + + miblen = nitems(mib); + error = sysctlnametomib(listmib, mib, &miblen); + if (error == -1) { + warn("cannot translate %s", listmib); + return (1); + } + len = 0; + error = sysctl(mib, miblen, NULL, &len, NULL, 0); + if (error == -1) { + warn("cannot get %s length", listmib); + return (1); + } + len = len * 4 / 3; + buf = malloc(len); + if (buf == NULL) { + warn("malloc"); + return (1); + } + error = sysctl(mib, miblen, buf, &len, NULL, 0); + if (error != 0) { + warn("reading %s", listmib); + ret = 1; + goto out; + } + ret = 0; + printf("MODE \tOWNER\tGROUP\tSIZE\tPATH\n"); + for (bp = buf; bp < buf + len; bp += kif->kf_structsize) { + kif = (const struct kinfo_file *)(void *)bp; + if (kif->kf_structsize == 0) + break; + fd = shm_open(kif->kf_path, O_RDONLY, 0); + if (fd == -1) { + warn("open %s", kif->kf_path); + ret = 1; + continue; + } + error = fstat(fd, &st); + close(fd); + if (error != 0) { + warn("stat %s", kif->kf_path); + ret = 1; + continue; + } + shm_decode_mode(kif->kf_un.kf_file.kf_file_mode, str); + printf("%s\t", str); + if (uname) { + printf("%s\t%s\t", user_from_uid(st.st_uid, 0), + group_from_gid(st.st_gid, 0)); + } else { + printf("%d\t%d\t", st.st_uid, st.st_gid); + } + if (hsize) { + humanize_number(sizebuf, sizeof(sizebuf), + kif->kf_un.kf_file.kf_file_size, "", HN_AUTOSCALE, + HN_NOSPACE); + printf("%s\t", sizebuf); + } else { + printf("%jd\t", + (uintmax_t)kif->kf_un.kf_file.kf_file_size); + } + printf("%s\n", kif->kf_path); + } +out: + free(buf); + return (ret); +} + +static int +read_one_shm(const char *path) +{ + char buf[4096]; + ssize_t size, se; + int fd, ret; + + ret = 1; + fd = shm_open(path, O_RDONLY, 0); + if (fd == -1) { + warn("open %s", path); + goto out; + } + for (;;) { + size = read(fd, buf, sizeof(buf)); + if (size > 0) { + se = fwrite(buf, 1, size, stdout); + if (se < size) { + warnx("short write to stdout"); + goto out; + } + } + if (size == (ssize_t)sizeof(buf)) + continue; + if (size >= 0 && size < (ssize_t)sizeof(buf)) { + ret = 0; + goto out; + } + warn("read from %s", path); + goto out; + } +out: + close(fd); + return (ret); +} + +static int +read_shm(int argc, char **argv) +{ + int i, ret, ret1; + + ret = 0; + for (i = 1; i < argc; i++) { + ret1 = read_one_shm(argv[i]); + if (ret1 != 0 && ret == 0) + ret = ret1; + } + return (ret); +} + +static int +stat_one_shm(const char *path, bool hsize, bool uname) +{ + char sizebuf[8]; + struct stat st; + int error, fd, ret; + + fd = shm_open(path, O_RDONLY, 0); + if (fd == -1) { + warn("open %s", path); + return (1); + } + ret = 0; + error = fstat(fd, &st); + if (error == -1) { + warn("stat %s", path); + ret = 1; + } else { + printf("path\t%s\n", path); + printf("inode\t%jd\n", (uintmax_t)st.st_ino); + printf("mode\t%#o\n", st.st_mode); + printf("nlink\t%jd\n", (uintmax_t)st.st_nlink); + if (uname) { + printf("owner\t%s\n", user_from_uid(st.st_uid, 0)); + printf("group\t%s\n", group_from_gid(st.st_gid, 0)); + } else { + printf("uid\t%d\n", st.st_uid); + printf("gid\t%d\n", st.st_gid); + } + if (hsize) { + humanize_number(sizebuf, sizeof(sizebuf), + st.st_size, "", HN_AUTOSCALE, HN_NOSPACE); + printf("size\t%s\n", sizebuf); + } else { + printf("size\t%jd\n", (uintmax_t)st.st_size); + } + printf("atime\t%ld.%09ld\n", (long)st.st_atime, + (long)st.st_atim.tv_nsec); + printf("mtime\t%ld.%09ld\n", (long)st.st_mtime, + (long)st.st_mtim.tv_nsec); + printf("ctime\t%ld.%09ld\n", (long)st.st_ctime, + (long)st.st_ctim.tv_nsec); + printf("birth\t%ld.%09ld\n", (long)st.st_birthtim.tv_sec, + (long)st.st_birthtim.tv_nsec); + } + close(fd); + return (ret); +} + +static int +stat_shm(int argc, char **argv) +{ + int c, i, ret, ret1; + bool hsize, uname; + + hsize = false; + uname = true; + + while ((c = getopt(argc, argv, "hn")) != -1) { + switch (c) { + case 'h': + hsize = true; + break; + case 'n': + uname = false; + break; + default: + usage(); + return (2); + } + } + argc -= optind; + argv += optind; + + ret = 0; + for (i = 0; i < argc; i++) { + ret1 = stat_one_shm(argv[i], hsize, uname); + if (ret1 != 0 && ret == 0) + ret = ret1; + } + return (ret); +} + +static int +truncate_one_shm(const char *path, uint64_t newsize) +{ + int error, fd, ret; + + ret = 0; + fd = shm_open(path, O_RDWR, 0); + if (fd == -1) { + warn("open %s", path); + return (1); + } + error = ftruncate(fd, newsize); + if (error == -1) { + warn("truncate %s", path); + ret = 1; + } + close(fd); + return (ret); +} + +static int +truncate_shm(int argc, char **argv) +{ + uint64_t newsize; + int c, i, ret, ret1; + + newsize = 0; + while ((c = getopt(argc, argv, "s:")) != -1) { + switch (c) { + case 's': + if (expand_number(optarg, &newsize) == -1) + err(1, "size:"); + break; + case '?': + default: + return (2); + } + } + + argc -= optind; + argv += optind; + ret = 0; + for (i = 0; i < argc; i++) { + ret1 = truncate_one_shm(argv[i], newsize); + if (ret1 != 0 && ret == 0) + ret = ret1; + } + return (ret); +} + +struct opmode { + const char *cmd; + int (*impl)(int argc, char **argv); +}; + +static const struct opmode opmodes[] = { + { .cmd = "create", .impl = create_shm}, + { .cmd = "rm", .impl = delete_shm, }, + { .cmd = "ls", .impl = list_shm }, + { .cmd = "dump", .impl = read_shm, }, + { .cmd = "stat", .impl = stat_shm, }, + { .cmd = "truncate", .impl = truncate_shm, }, +}; + +int +main(int argc, char *argv[]) +{ + const struct opmode *opmode; + int i, ret; + + ret = 0; + opmode = NULL; + + if (argc < 2) { + usage(); + exit(2); + } + for (i = 0; i < (int)nitems(opmodes); i++) { + if (strcmp(argv[1], opmodes[i].cmd) == 0) { + opmode = &opmodes[i]; + break; + } + } + if (opmode == NULL) { + usage(); + exit(2); + } + ret = opmode->impl(argc - 1, argv + 1); + exit(ret); +} Property changes on: head/usr.bin/posixshmcontrol/posixshmcontrol.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property