ref: ed24d5e6d8896f4fa1517c9bc542a03334479363
author: kvik <kvik@a-b.xyz>
date: Fri Aug 14 11:35:07 EDT 2020
init
--- /dev/null
+++ b/LICENSE
@@ -1,0 +1,19 @@
+Copyright (c) 2020 kvik@a-b.xyz
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
--- /dev/null
+++ b/man/1/post
@@ -1,0 +1,72 @@
+.TH POST 1
+.SH NAME
+post \- publish a file descriptor
+.SH SYNOPSIS
+.B post
+[
+.B -q
+] [
+.B -m
+mode
+] file [ name ]
+.SH DESCRIPTION
+.I Post
+opens
+.I file
+with highest allowed mode then
+publishes the open channel to the
+.BR srv (3)
+bulletin board, using the given
+.I mode
+(default 0660)
+and
+.I name
+(default
+.IR post.$pid ).
+.PP
+Finally, it prints the created
+path; the flag
+.I -q
+silences it.
+.SH EXAMPLES
+.PP
+Post our standard output:
+.PP
+.EX
+ post /fd/1 output
+.EE
+.PP
+Post a file descriptor connected to
+.IR ramfs (4)
+using stdio as its 9p channel.
+.PP
+.EX
+ post <>{ramfs -i >[1=0]} ram
+.EE
+.PP
+Same as above but also mount:
+.PP
+.EX
+ mount -bc `{post <>{ramfs -i >[1=0]}} /n/ram
+.EE
+.PP
+Post a connection to a remote service:
+.PP
+.EX
+ post <>{aux/trampoline tcp!9front.org!http >[1=0]}
+.EE
+.SH SEE ALSO
+.IR srv (3)
+.SH SOURCE
+.EX
+git/clone git://src.a-b.xyz/post
+.EE
+.SH BUGS
+To determine the highest mode with
+which the file can be opened with
+.IR post (1)
+must attempt to open the
+.I file
+up to
+three times. This may be an issue
+with some file servers.
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,61 @@
+</$objtype/mkfile
+
+HFILES=`{test -d src && walk -f src | grep '\.h$'}
+CFILES=`{test -d src && walk -f src | grep '\.c$'}
+CMAIN=`{grep -l '^(thread)?main\(' $CFILES /dev/null}
+CCOM=`{grep -L '^(thread)?main\(' $CFILES /dev/null | sed '/^\/dev\/null/d'}
+OCOM=${CCOM:src/%.c=obj/$objtype/%.o}
+
+BINTARG=${CMAIN:src/%.c=bin/$objtype/%}
+RCFILES=`{test -d rc && walk -f rc}
+MANFILES=`{test -d man && walk -n 2,2 -f man}
+
+BIN=/$objtype/bin
+RC=/rc/bin
+MAN=/sys/man
+
+DIRS=bin obj bin/$objtype obj/$objtype
+
+BININST=${BINTARG:bin/$objtype/%=$BIN/%}
+RCINST=${RCFILES:rc/%=$RC/%}
+MANINST=${MANFILES:man/%=$MAN/%}
+INST=$BININST $RCINST $MANINST
+
+none:V: all
+
+$DIRS:
+ mkdir -p $target
+
+obj/$objtype/%.o: obj/$objtype $HFILES
+
+obj/$objtype/%.o: src/%.c
+ $CC $CFLAGS -o $target src/$stem.c
+
+bin/$objtype/%: bin/$objtype obj/$objtype/%.o $OCOM
+ $LD $LDFLAGS -o $target obj/$objtype/$stem.o $OCOM
+
+$BIN/%: bin/$objtype/%
+ cp $prereq $target
+
+$RC/%: rc/%
+ cp -x $prereq $target
+
+/sys/man/%: man/%
+ cp $prereq $target
+
+man:V: $MANINST
+
+%.cpus:V:
+ for(objtype in $CPUS) mk $MKFLAGS $stem
+
+all:V: $BINTARG
+
+install:V: $INST
+
+installall:V: install.cpus
+
+uninstall:V:
+ rm -f $INST
+
+clean:V:
+ rm -rf bin obj
--- /dev/null
+++ b/src/post.c
@@ -1,0 +1,55 @@
+#include <u.h>
+#include <libc.h>
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s [-q] [-m mode] file [name]\n", argv0);
+ exits("usage");
+}
+
+int
+openrw(char *n)
+{
+ int fd;
+
+ if((fd = open(n, ORDWR)) >= 0)
+ return fd;
+ if((fd = open(n, OREAD)) >= 0)
+ return fd;
+ if((fd = open(n, OWRITE)) >= 0)
+ return fd;
+ return -1;
+}
+
+void
+main(int argc, char *argv[])
+{
+ int mode = 0660, quiet = 0;
+
+ int fd, srvfd;
+ char name[256];
+
+ ARGBEGIN{
+ case 'm':
+ mode = strtoul(EARGF(usage()), nil, 8); break;
+ case 'q':
+ quiet = 1; break;
+ default: usage();
+ }ARGEND;
+ if(argc == 0 || argc > 2) usage();
+ if(argc == 2)
+ snprint(name, sizeof name, "/srv/%s", argv[1]);
+ else
+ snprint(name, sizeof name, "/srv/post.%d", getpid());
+
+ if((fd = openrw(argv[0])) == -1)
+ sysfatal("open: %r");
+ if((srvfd = create(name, OWRITE, mode)) == -1)
+ sysfatal("create: %r");
+ fprint(srvfd, "%d", fd);
+
+ if(!quiet) print("%s\n", name);
+
+ exits(nil);
+}