shithub: mq

Download patch

ref: 72ef5609a3dd6d5d0d996a3d60f9aebb14717129
parent: f12dbfcc9869e1256f338f985ce53894fbe3dfe5
author: kvik <kvik@a-b.xyz>
date: Fri Sep 4 18:49:25 EDT 2020

add pin(1) program and the manual page

diff: cannot open b/man/1//null: file does not exist: 'b/man/1//null' diff: cannot open b/man//null: file does not exist: 'b/man//null'
--- /dev/null
+++ b/man/1/pin
@@ -1,0 +1,91 @@
+.TH PIN 1
+.SH NAME
+pin \- pinned I/O shell
+.SH SYNOPSIS
+pin
+[
+.B -ns
+] [
+.B -m
+.I mtpt
+] [
+.I name
+] [
+.I command
+]
+.SH DESCRIPTION
+.PP
+.B Pin
+manages persistent sessions for long-running commands.
+It is used to create, join, and control sessions for programs
+connected to a named
+.IR mq (4)
+stream group.
+.PP
+If a session
+.I name
+exists, then
+.B pin mysession
+will join the named session,
+otherwise a new session will be created.
+The command run in the session is
+.IR command ,
+or
+.B rc -i
+if command is not given.
+.PP
+The
+.B -n
+flag supresses joining of existing session.
+.PP
+The -w
+flag writes the
+.I command
+string, or standard input if not given, to
+the input stream of an existing session.
+.PP
+Under the hood,
+.I pin
+expects an
+.IR mq (4)
+instance mounted on
+.BR /n/pin ,
+or to
+.I mtpt
+if the
+.B -m
+option is given.
+If no such mount is present,
+.I pin
+mounts the channel posted on
+.BR /srv/pin-$user ,
+creating it if needed.
+.SH EXAMPLES
+.PP
+Pin an
+.BR rc (1)
+shell, or join an already existing session:
+.EX
+	pin myshell
+.EE
+.PP
+Pin some other program:
+.EX
+	pin remote rcpu -h remote
+.EE
+.PP
+Send input to a pinned program:
+.EX
+	pin -s myshell echo 'hi, shell'
+.EE
+or:
+.EX
+	echo 'echo hi, shell' | pin -s myshell
+.EE
+.SH SEE ALSO
+.IR mq (4)
+.SH SOURCE
+.EX
+git://src.a-b.xyz/mq
+.EE
+.SH BUGS
--- /dev/null
+++ b/rc/pin
@@ -1,0 +1,46 @@
+#!/bin/rc -e
+rfork ens
+
+flagfmt = 'n:noattach, w:write, m:mtpt mtpt'
+args = 'name [command]'
+eval `''{aux/getflags $*} || exec aux/usage
+
+srv = pin-$user
+name = $1
+if(~ $#name 0)
+	exec aux/usage
+cmd = $*(2-)
+if(~ $#cmd 0){
+	nocmd = 1
+	cmd = (rc -i)
+}
+if(~ $#mtpt 0)
+	mtpt = /n/pin
+
+# Check if already mounted
+if(! ~ `{walk -n0 -eq $mtpt} a000000000000000.0.80){
+	if(test -e /srv/$srv)
+		mount -c /srv/$srv $mtpt
+	if not
+		mq -s $srv -m $mtpt
+}
+
+mq = $mtpt/$name
+if(~ $#write 1){
+	if(~ $nocmd 1)
+		exec cat >>$mq/0
+	if not
+		exec echo $cmd >>$mq/0
+}
+if(! test -d $mq){
+	echo creating $mq >[1=2]
+	mkdir -p $mq
+	echo replay on >$mq/ctl
+	touch $mq/^(0 1 2)
+	$cmd <$mq/0 >>$mq/1 >>[2]$mq/2 &
+}
+if(~ $#noattach 1)
+	exit
+echo attaching $mq >[1=2]
+mq-cat $mq &
+exec cat >>$mq/0