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
43 type ConnEventType int
46 _ ConnEventType = iota
47 ConnEventStderr // stderr from spawned process
48 ConnEventDebug // debug message
49 ConnEventUpload // remote helper upload in progress
52 type ConnEvent struct {
57 // NewConn creates a new connection. Events in the returned struct must be
58 // regularly read or the connection will hang. This must be done before
59 // DialSSH is called to open a connection.
60 func NewConn(debug bool) *Conn {
61 ch := make(chan ConnEvent)
69 func (c *Conn) debugf(format string, a ...interface{}) {
73 c.events <- ConnEvent{
75 Data: fmt.Sprintf(format, a...),
79 // Wrap safcm.GobConn's Send() and Recv() to provide debug output.
81 // Send sends a single message to the remote.
82 func (c *Conn) Send(m safcm.Msg) error {
83 // No checks for invalid Conn, a stacktrace is more helpful
85 c.debugf("Send: sending %#v", m)
89 // Recv waits for a single message from the remote.
90 func (c *Conn) Recv() (safcm.Msg, error) {
91 // No checks for invalid Conn, a stacktrace is more helpful
93 c.debugf("Recv: waiting for message")
94 m, err := c.conn.Recv()
95 c.debugf("Recv: received msg=%#v err=%#v", m, err)
99 // Wait waits for the connection to terminate. It's safe to call Wait (and
100 // Kill) multiple times.
101 func (c *Conn) Wait() error {
102 // But check here because Wait() can be called multiple times
104 return fmt.Errorf("Dial*() not called or already terminated")
107 c.debugf("Wait: waiting for connection to terminate")
110 func (c *Conn) wait() error {
114 // Wait until we've received all events from the program's stderr.
116 // Notify consumers that no more events will occur.
118 // We cannot reuse this channel.
120 // Don't set c.Events to nil because this creates a data race when
121 // another thread is still waiting.
126 // Kill forcefully terminates the connection. It's safe to call Kill (and
127 // Wait) multiple times.
128 func (c *Conn) Kill() error {
130 return fmt.Errorf("Dial*() not called or already terminated")
133 c.debugf("Kill: killing connection")
139 func (c *Conn) handleStderrAsEvents(cmd *exec.Cmd) error {
140 // cmd may differ from c.cmd here!
141 stderr, err := cmd.StderrPipe()
148 r := bufio.NewReader(stderr)
150 x, err := r.ReadString('\n')
154 x = strings.TrimRight(x, "\n")
156 c.events <- ConnEvent{
157 Type: ConnEventStderr,