sdNotifyBarrier performs synchronization with systemd by means of the sd_notify_barrier protocol.
(client *net.UnixConn)
| 176 | |
| 177 | // sdNotifyBarrier performs synchronization with systemd by means of the sd_notify_barrier protocol. |
| 178 | func sdNotifyBarrier(client *net.UnixConn) (retErr error) { |
| 179 | // Create a pipe for communicating with systemd daemon. |
| 180 | pipeR, pipeW, err := os.Pipe() |
| 181 | if err != nil { |
| 182 | return err |
| 183 | } |
| 184 | defer func() { |
| 185 | if retErr != nil { |
| 186 | pipeW.Close() |
| 187 | pipeR.Close() |
| 188 | } |
| 189 | }() |
| 190 | |
| 191 | // Get the FD for the unix socket file to be able to use sendmsg. |
| 192 | clientFd, err := client.File() |
| 193 | if err != nil { |
| 194 | return err |
| 195 | } |
| 196 | |
| 197 | // Send the write end of the pipe along with a BARRIER=1 message. |
| 198 | fdRights := unix.UnixRights(int(pipeW.Fd())) |
| 199 | err = linux.Sendmsg(int(clientFd.Fd()), []byte("BARRIER=1"), fdRights, nil, 0) |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | |
| 204 | // Close our copy of pipeW. |
| 205 | err = pipeW.Close() |
| 206 | if err != nil { |
| 207 | return err |
| 208 | } |
| 209 | |
| 210 | // Expect the read end of the pipe to be closed after 30 seconds. |
| 211 | err = pipeR.SetReadDeadline(time.Now().Add(30 * time.Second)) |
| 212 | if err != nil { |
| 213 | return nil |
| 214 | } |
| 215 | |
| 216 | // Read a single byte expecting EOF. |
| 217 | var buf [1]byte |
| 218 | n, err := pipeR.Read(buf[:]) |
| 219 | if n != 0 || err == nil { |
| 220 | return errUnexpectedRead |
| 221 | } else if errors.Is(err, os.ErrDeadlineExceeded) { |
| 222 | // Probably the other end doesn't support the sd_notify_barrier protocol. |
| 223 | logrus.Warn("Timeout after waiting 30s for barrier. Ignored.") |
| 224 | return nil |
| 225 | } else if err == io.EOF { //nolint:errorlint // https://github.com/polyfloyd/go-errorlint/issues/49 |
| 226 | return nil |
| 227 | } else { |
| 228 | return err |
| 229 | } |
| 230 | } |
no test coverage detected
searching dependent graphs…