I'm unable to get a pipe state in Go (1.5).
While writing on a mkfifo
created pipe, I try to get the state of this output pipe:
- using the
Write
return statusEPIPE
- using the
Write
return statusEPIPE
andsignal.Ignore
on SIGPIPE (just in case) - using
signal.Notify
on SIGPIPE
I can see that:
-
EPIPE
is never returned - when I use
kill -13
, the signal handler is called: "Got signal: broken pipe" - when I
ctrl-c
the reader, the signal handler is not called and my program exits with output: "signal: broken pipe"
Would you, please, indicate my error ?
// tee.go
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
sys "golang.org/x/sys/unix"
)
// wait for a signal and print it
func handleSignal(csig chan os.Signal) {
for {
fmt.Println("Wait signal")
s := <-csig
fmt.Println("Got signal:", s)
}
}
func main() {
csig := make(chan os.Signal, 1)
// `kill -13` outputs "Got signal: broken pipe" => ok
signal.Notify(csig, sys.SIGPIPE)
// OR disable the previous `Notify` just to be sure ?
// maybe it will help to get the EPIPE error status on `Write` ?
//signal.Ignore(sys.SIGPIPE)
go handleSignal(csig)
// open previously created named pipe (`mkfifo /tmp/test`)
pipe, _ := os.OpenFile("/tmp/test", os.O_WRONLY, 0)
for {
_, err := pipe.Write([]byte("foo
"))
if err == syscall.EPIPE {
// never called => ko
fmt.Println("EPIPE error")
}
}
}
Note: as a simple Go exercise, I try to implement a command which almost acts like tee -a <a_file>
(print stdin to stdout and <a_file>
) with the following specificity: non blocking write on a named pipe and optional reader.