1 // Simple RPC-like protocol: implementation of connection and basic actions
3 // Copyright (C) 2021 Simon Ruderich
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "ruderich.org/simon/safcm"
31 Events <-chan ConnEvent
32 events chan<- ConnEvent // same as Events, to publish events
33 eventsWg sync.WaitGroup
42 type ConnEventType int
45 _ ConnEventType = iota
51 type ConnEvent struct {
56 // NewConn creates a new connection. Events in the returned struct must be
57 // regularly read or the connection will stall. This must be done before
58 // DialSSH is called to open a connection.
59 func NewConn(debug bool) *Conn {
60 ch := make(chan ConnEvent)
68 func (c *Conn) debugf(format string, a ...interface{}) {
72 c.events <- ConnEvent{
74 Data: fmt.Sprintf(format, a...),
78 // Wrap safcm.GobConn's Send() and Recv() to provide debug output.
80 // Send sends a single message to the remote.
81 func (c *Conn) Send(m safcm.Msg) error {
82 // No checks for invalid Conn, a stacktrace is more helpful
84 c.debugf("Send: sending %#v", m)
88 // Recv waits for a single message from the remote.
89 func (c *Conn) Recv() (safcm.Msg, error) {
90 // No checks for invalid Conn, a stacktrace is more helpful
92 c.debugf("Recv: waiting for message")
93 m, err := c.conn.Recv()
94 c.debugf("Recv: received msg=%#v err=%#v", m, err)
98 // Wait waits for the connection to terminate. It's safe to call Wait (and
99 // Kill) multiple times.
100 func (c *Conn) Wait() error {
101 // But check here because Wait() can be called multiple times
103 return fmt.Errorf("Dial*() not called or already terminated")
106 c.debugf("Wait: waiting for connection to terminate")
109 func (c *Conn) wait() error {
113 // Wait until we've received all events from the program's stderr.
115 // Notify consumers that no more events will occur.
117 // We cannot reuse this channel.
119 // Don't set c.Events to nil because this creates a data race when
120 // another thread is still waiting.
125 // Kill forcefully terminates the connection. It's safe to call Kill (and
126 // Wait) multiple times.
127 func (c *Conn) Kill() error {
129 return fmt.Errorf("Dial*() not called or already terminated")
132 c.debugf("Kill: killing connection")
138 func (c *Conn) handleStderrAsEvents(cmd *exec.Cmd) error {
139 // cmd may differ from c.cmd here!
140 stderr, err := cmd.StderrPipe()
147 r := bufio.NewReader(stderr)
149 x, err := r.ReadString('\n')
153 x = strings.TrimRight(x, "\n")
155 c.events <- ConnEvent{
156 Type: ConnEventStderr,