contrib: recvtty: add --no-stdin flag

This is mostly just useful for testing with the "single" mode, since it
allows you to run recvtty in the background without the console being
closed.

Signed-off-by: Aleksa Sarai <asarai@suse.de>
This commit is contained in:
Aleksa Sarai 2020-05-10 02:40:25 +10:00
parent df3d7f673a
commit a79fa7caa0
No known key found for this signature in database
GPG Key ID: 9E18AA267DDB8DB4
1 changed files with 13 additions and 6 deletions

View File

@ -65,7 +65,7 @@ func bail(err error) {
os.Exit(1) os.Exit(1)
} }
func handleSingle(path string) error { func handleSingle(path string, noStdin bool) error {
// Open a socket. // Open a socket.
ln, err := net.Listen("unix", path) ln, err := net.Listen("unix", path)
if err != nil { if err != nil {
@ -113,10 +113,12 @@ func handleSingle(path string) error {
io.Copy(os.Stdout, c) io.Copy(os.Stdout, c)
quitChan <- struct{}{} quitChan <- struct{}{}
}() }()
go func() { if !noStdin {
io.Copy(c, os.Stdin) go func() {
quitChan <- struct{}{} io.Copy(c, os.Stdin)
}() quitChan <- struct{}{}
}()
}
// Only close the master fd once we've stopped copying. // Only close the master fd once we've stopped copying.
<-quitChan <-quitChan
@ -201,6 +203,10 @@ func main() {
Value: "", Value: "",
Usage: "Path to write daemon process ID to", Usage: "Path to write daemon process ID to",
}, },
cli.BoolFlag{
Name: "no-stdin",
Usage: "Disable stdin handling (no-op for null mode)",
},
} }
app.Action = func(ctx *cli.Context) error { app.Action = func(ctx *cli.Context) error {
@ -218,9 +224,10 @@ func main() {
} }
} }
noStdin := ctx.Bool("no-stdin")
switch ctx.String("mode") { switch ctx.String("mode") {
case "single": case "single":
if err := handleSingle(path); err != nil { if err := handleSingle(path, noStdin); err != nil {
return err return err
} }
case "null": case "null":