2020-02-12 20:53:55 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2020-07-06 23:36:57 +00:00
|
|
|
|
|
|
|
"tailscale.com/types/logger"
|
2020-02-12 20:53:55 +00:00
|
|
|
)
|
|
|
|
|
2020-07-14 23:28:02 +00:00
|
|
|
// unspecifiedMessage is a minimal message implementation that should not
|
|
|
|
// be ignored. In general, OS-specific implementations should use better
|
|
|
|
// types and avoid this if they can.
|
|
|
|
type unspecifiedMessage struct{}
|
|
|
|
|
|
|
|
func (unspecifiedMessage) ignore() bool { return false }
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
// devdConn implements osMon using devd(8).
|
2020-02-12 20:53:55 +00:00
|
|
|
type devdConn struct {
|
|
|
|
conn net.Conn
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:26:38 +00:00
|
|
|
func newOSMon(logf logger.Logf, m *Mon) (osMon, error) {
|
2020-02-12 20:53:55 +00:00
|
|
|
conn, err := net.Dial("unixpacket", "/var/run/devd.seqpacket.pipe")
|
|
|
|
if err != nil {
|
2021-12-06 02:26:38 +00:00
|
|
|
logf("devd dial error: %v, falling back to polling method", err)
|
|
|
|
return newPollingMon(logf, m)
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
return &devdConn{conn}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 19:03:50 +00:00
|
|
|
func (c *devdConn) IsInterestingInterface(iface string) bool { return true }
|
|
|
|
|
2020-02-12 20:53:55 +00:00
|
|
|
func (c *devdConn) Close() error {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
func (c *devdConn) Receive() (message, error) {
|
2020-02-12 20:53:55 +00:00
|
|
|
for {
|
|
|
|
msg, err := bufio.NewReader(c.conn).ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading devd socket: %v", err)
|
|
|
|
}
|
|
|
|
// Only return messages related to the network subsystem.
|
|
|
|
if !strings.Contains(msg, "system=IFNET") {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-06 23:36:57 +00:00
|
|
|
// TODO: this is where the devd-specific message would
|
2020-02-12 20:53:55 +00:00
|
|
|
// get converted into a "standard" event message and returned.
|
2020-07-06 23:36:57 +00:00
|
|
|
return unspecifiedMessage{}, nil
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
}
|