2017-02-03 21:17:34 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-07 18:17:41 +08:00
|
|
|
"bytes"
|
2017-02-03 21:17:34 +08:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-05-08 00:28:49 +08:00
|
|
|
"os"
|
2018-05-26 00:04:06 +08:00
|
|
|
"path"
|
2017-02-03 21:17:34 +08:00
|
|
|
"path/filepath"
|
2018-05-26 00:04:06 +08:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2017-02-03 21:17:34 +08:00
|
|
|
|
2018-05-26 00:04:06 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2017-02-03 21:17:34 +08:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
type notifySocket struct {
|
|
|
|
socket *net.UnixConn
|
|
|
|
host string
|
|
|
|
socketPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNotifySocket(context *cli.Context, notifySocketHost string, id string) *notifySocket {
|
|
|
|
if notifySocketHost == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
root := filepath.Join(context.GlobalString("root"), id)
|
2018-05-26 00:04:06 +08:00
|
|
|
socketPath := filepath.Join(root, "notify", "notify.sock")
|
2017-02-03 21:17:34 +08:00
|
|
|
|
|
|
|
notifySocket := ¬ifySocket{
|
|
|
|
socket: nil,
|
|
|
|
host: notifySocketHost,
|
2018-05-26 00:04:06 +08:00
|
|
|
socketPath: socketPath,
|
2017-02-03 21:17:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return notifySocket
|
|
|
|
}
|
|
|
|
|
2018-10-14 03:14:03 +08:00
|
|
|
func (s *notifySocket) Close() error {
|
|
|
|
return s.socket.Close()
|
2017-02-03 21:17:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If systemd is supporting sd_notify protocol, this function will add support
|
|
|
|
// for sd_notify protocol from within the container.
|
2018-05-26 00:04:06 +08:00
|
|
|
func (s *notifySocket) setupSpec(context *cli.Context, spec *specs.Spec) error {
|
|
|
|
pathInContainer := filepath.Join("/run/notify", path.Base(s.socketPath))
|
|
|
|
mount := specs.Mount{
|
|
|
|
Destination: path.Dir(pathInContainer),
|
|
|
|
Source: path.Dir(s.socketPath),
|
|
|
|
Options: []string{"bind", "nosuid", "noexec", "nodev", "ro"},
|
|
|
|
}
|
2017-02-03 21:17:34 +08:00
|
|
|
spec.Mounts = append(spec.Mounts, mount)
|
2018-05-26 00:04:06 +08:00
|
|
|
spec.Process.Env = append(spec.Process.Env, fmt.Sprintf("NOTIFY_SOCKET=%s", pathInContainer))
|
|
|
|
return nil
|
2017-02-03 21:17:34 +08:00
|
|
|
}
|
|
|
|
|
2018-05-26 00:04:06 +08:00
|
|
|
func (s *notifySocket) bindSocket() error {
|
2017-02-03 21:17:34 +08:00
|
|
|
addr := net.UnixAddr{
|
|
|
|
Name: s.socketPath,
|
|
|
|
Net: "unixgram",
|
|
|
|
}
|
|
|
|
|
|
|
|
socket, err := net.ListenUnixgram("unixgram", &addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-08 00:28:49 +08:00
|
|
|
err = os.Chmod(s.socketPath, 0777)
|
|
|
|
if err != nil {
|
|
|
|
socket.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:17:34 +08:00
|
|
|
s.socket = socket
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-26 00:04:06 +08:00
|
|
|
func (s *notifySocket) setupSocketDirectory() error {
|
|
|
|
return os.Mkdir(path.Dir(s.socketPath), 0755)
|
|
|
|
}
|
|
|
|
|
|
|
|
func notifySocketStart(context *cli.Context, notifySocketHost, id string) (*notifySocket, error) {
|
|
|
|
notifySocket := newNotifySocket(context, notifySocketHost, id)
|
|
|
|
if notifySocket == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := notifySocket.bindSocket(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return notifySocket, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifySocket) waitForContainer(container libcontainer.Container) error {
|
|
|
|
s, err := container.State()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return n.run(s.InitProcessPid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifySocket) run(pid1 int) error {
|
|
|
|
if n.socket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
notifySocketHostAddr := net.UnixAddr{Name: n.host, Net: "unixgram"}
|
2017-02-03 21:17:34 +08:00
|
|
|
client, err := net.DialUnix("unixgram", nil, ¬ifySocketHostAddr)
|
|
|
|
if err != nil {
|
2018-05-26 00:04:06 +08:00
|
|
|
return err
|
2017-02-03 21:17:34 +08:00
|
|
|
}
|
|
|
|
|
2018-05-26 00:04:06 +08:00
|
|
|
ticker := time.NewTicker(time.Millisecond * 100)
|
|
|
|
defer ticker.Stop()
|
2017-02-07 18:17:41 +08:00
|
|
|
|
2018-05-26 00:04:06 +08:00
|
|
|
fileChan := make(chan []byte)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
r, err := n.socket.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
got := buf[0:r]
|
|
|
|
// systemd-ready sends a single datagram with the state string as payload,
|
|
|
|
// so we don't need to worry about partial messages.
|
|
|
|
for _, line := range bytes.Split(got, []byte{'\n'}) {
|
|
|
|
if bytes.HasPrefix(got, []byte("READY=")) {
|
|
|
|
fileChan <- line
|
2017-02-07 18:17:41 +08:00
|
|
|
return
|
|
|
|
}
|
2018-05-26 00:04:06 +08:00
|
|
|
}
|
2017-02-09 19:27:12 +08:00
|
|
|
|
2018-05-26 00:04:06 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
_, err := os.Stat(filepath.Join("/proc", strconv.Itoa(pid1)))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2017-02-07 18:17:41 +08:00
|
|
|
}
|
2018-05-26 00:04:06 +08:00
|
|
|
case b := <-fileChan:
|
|
|
|
var out bytes.Buffer
|
|
|
|
_, err = out.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = out.Write([]byte{'\n'})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.Write(out.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we can inform systemd to use pid1 as the pid to monitor
|
|
|
|
newPid := fmt.Sprintf("MAINPID=%d\n", pid1)
|
|
|
|
client.Write([]byte(newPid))
|
|
|
|
return nil
|
2017-02-07 18:17:41 +08:00
|
|
|
}
|
2017-02-03 21:17:34 +08:00
|
|
|
}
|
|
|
|
}
|