| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package conn |
| 18 |
|
| 19 |
import ( |
| 20 |
"bufio" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"os" |
| 24 |
"strconv" |
| 25 |
"strings" |
| 26 |
"time" |
| 27 |
) |
| 28 |
|
| 29 |
const ( |
| 30 |
mesStart = "start" |
| 31 |
mesStarted = "started" |
| 32 |
mesAck = "ack" |
| 33 |
mesFdPrefix = "fd:" |
| 34 |
) |
| 35 |
|
| 36 |
// Client is the client to talk to the fanotifier. |
| 37 |
type Client struct { |
| 38 |
r io.Reader |
| 39 |
w io.Writer |
| 40 |
notification *bufio.Scanner |
| 41 |
servicePid int |
| 42 |
timeout time.Duration |
| 43 |
} |
| 44 |
|
| 45 |
// NewClient returns the client to talk to the fanotifier. |
| 46 |
func NewClient(r io.Reader, w io.Writer, servicePid int, timeout time.Duration) *Client { |
| 47 |
return &Client{r, w, bufio.NewScanner(r), servicePid, timeout} |
| 48 |
} |
| 49 |
|
| 50 |
// Start lets the notirier start to monitor the filesystem. |
| 51 |
func (nc *Client) Start() error { |
| 52 |
if err := writeMessage(nc.w, mesStart); err != nil { |
| 53 |
return err |
| 54 |
} |
| 55 |
if mes, err := scanWithTimeout(nc.notification, nc.timeout); err != nil { |
| 56 |
return err |
| 57 |
} else if mes != mesStarted { |
| 58 |
return fmt.Errorf("non-started message got from fanotifier service: %q", mes) |
| 59 |
} |
| 60 |
return nil |
| 61 |
} |
| 62 |
|
| 63 |
// GetPath returns notified path from the fanotifier. This blocks until new path is notified |
| 64 |
// from the service |
| 65 |
func (nc *Client) GetPath() (string, error) { |
| 66 |
if !nc.notification.Scan() { // NOTE: no timeout |
| 67 |
return "", io.EOF |
| 68 |
} |
| 69 |
mes := nc.notification.Text() |
| 70 |
if !strings.HasPrefix(mes, mesFdPrefix) { |
| 71 |
return "", fmt.Errorf("unexpected prefix for message %q", mes) |
| 72 |
} |
| 73 |
fd, err := strconv.ParseInt(mes[len(mesFdPrefix):], 10, 32) |
| 74 |
if err != nil { |
| 75 |
return "", fmt.Errorf("invalid fd %q: %w", mes, err) |
| 76 |
} |
| 77 |
path, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%d", nc.servicePid, fd)) |
| 78 |
if err != nil { |
| 79 |
return "", fmt.Errorf("failed to get link from fd %q: %w", mes, err) |
| 80 |
} |
| 81 |
return path, writeMessage(nc.w, mesAck) |
| 82 |
} |
| 83 |
|
| 84 |
// Service is the service end of the fanotifier. |
| 85 |
type Service struct { |
| 86 |
r io.Reader |
| 87 |
w io.Writer |
| 88 |
client *bufio.Scanner |
| 89 |
timeout time.Duration |
| 90 |
} |
| 91 |
|
| 92 |
// NewService is the service end of the fanotifier. |
| 93 |
func NewService(r io.Reader, w io.Writer, timeout time.Duration) *Service { |
| 94 |
return &Service{r, w, bufio.NewScanner(r), timeout} |
| 95 |
} |
| 96 |
|
| 97 |
// WaitStart waits "start" message from the client. |
| 98 |
func (ns *Service) WaitStart() error { |
| 99 |
if !ns.client.Scan() { // NOTE: no timeout |
| 100 |
return io.EOF |
| 101 |
} |
| 102 |
if mes := ns.client.Text(); mes != mesStart { |
| 103 |
return fmt.Errorf("non-start message got from fanotifier client: %q", mes) |
| 104 |
} |
| 105 |
return nil |
| 106 |
} |
| 107 |
|
| 108 |
// SendStarted tells client that the service has been started. |
| 109 |
func (ns *Service) SendStarted() error { |
| 110 |
return writeMessage(ns.w, mesStarted) |
| 111 |
} |
| 112 |
|
| 113 |
// SendFd send file descriptor to the client. |
| 114 |
func (ns *Service) SendFd(fd int) error { |
| 115 |
if err := writeMessage(ns.w, fmt.Sprintf("%s%d", mesFdPrefix, fd)); err != nil { |
| 116 |
return err |
| 117 |
} |
| 118 |
if mes, err := scanWithTimeout(ns.client, ns.timeout); err != nil { |
| 119 |
return err |
| 120 |
} else if mes != mesAck { |
| 121 |
return fmt.Errorf("non-ack message got from fanotifier client: %q", mes) |
| 122 |
} |
| 123 |
return nil |
| 124 |
} |
| 125 |
|
| 126 |
func scanWithTimeout(sc *bufio.Scanner, timeout time.Duration) (string, error) { |
| 127 |
notifyCh := make(chan string) |
| 128 |
errCh := make(chan error) |
| 129 |
go func() { |
| 130 |
if !sc.Scan() { |
| 131 |
errCh <- io.EOF |
| 132 |
} |
| 133 |
notifyCh <- sc.Text() |
| 134 |
}() |
| 135 |
select { |
| 136 |
case mes := <-notifyCh: |
| 137 |
return mes, nil |
| 138 |
case err := <-errCh: |
| 139 |
return "", err |
| 140 |
case <-time.After(timeout): |
| 141 |
return "", fmt.Errorf("timeout") |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func writeMessage(w io.Writer, mes string) error { |
| 146 |
_, err := io.WriteString(w, mes+"\n") |
| 147 |
return err |
| 148 |
} |