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"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2017-07-19 22:28:59 +08:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2017-02-03 21:17:34 +08:00
|
|
|
"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)
|
|
|
|
path := filepath.Join(root, "notify.sock")
|
|
|
|
|
|
|
|
notifySocket := ¬ifySocket{
|
|
|
|
socket: nil,
|
|
|
|
host: notifySocketHost,
|
|
|
|
socketPath: path,
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
func (s *notifySocket) setupSpec(context *cli.Context, spec *specs.Spec) {
|
2018-03-07 07:36:48 +08:00
|
|
|
mount := specs.Mount{Destination: s.host, Source: s.socketPath, Options: []string{"bind"}}
|
2017-02-03 21:17:34 +08:00
|
|
|
spec.Mounts = append(spec.Mounts, mount)
|
|
|
|
spec.Process.Env = append(spec.Process.Env, fmt.Sprintf("NOTIFY_SOCKET=%s", s.host))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *notifySocket) setupSocket() error {
|
|
|
|
addr := net.UnixAddr{
|
|
|
|
Name: s.socketPath,
|
|
|
|
Net: "unixgram",
|
|
|
|
}
|
|
|
|
|
|
|
|
socket, err := net.ListenUnixgram("unixgram", &addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.socket = socket
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-09 19:27:12 +08:00
|
|
|
// pid1 must be set only with -d, as it is used to set the new process as the main process
|
|
|
|
// for the service in systemd
|
2018-10-14 03:14:03 +08:00
|
|
|
func (s *notifySocket) run(pid1 int) {
|
2017-02-03 21:17:34 +08:00
|
|
|
buf := make([]byte, 512)
|
2018-10-14 03:14:03 +08:00
|
|
|
notifySocketHostAddr := net.UnixAddr{Name: s.host, Net: "unixgram"}
|
2017-02-03 21:17:34 +08:00
|
|
|
client, err := net.DialUnix("unixgram", nil, ¬ifySocketHostAddr)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
2018-10-14 03:14:03 +08:00
|
|
|
r, err := s.socket.Read(buf)
|
2017-02-03 21:17:34 +08:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2017-02-07 18:17:41 +08:00
|
|
|
var out bytes.Buffer
|
|
|
|
for _, line := range bytes.Split(buf[0:r], []byte{'\n'}) {
|
|
|
|
if bytes.HasPrefix(line, []byte("READY=")) {
|
|
|
|
_, err = out.Write(line)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-02-03 21:17:34 +08:00
|
|
|
|
2017-02-07 18:17:41 +08:00
|
|
|
_, err = out.Write([]byte{'\n'})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.Write(out.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-02-09 19:27:12 +08:00
|
|
|
|
|
|
|
// now we can inform systemd to use pid1 as the pid to monitor
|
|
|
|
if pid1 > 0 {
|
|
|
|
newPid := fmt.Sprintf("MAINPID=%d\n", pid1)
|
|
|
|
client.Write([]byte(newPid))
|
|
|
|
}
|
2017-02-07 18:17:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-02-03 21:17:34 +08:00
|
|
|
}
|
|
|
|
}
|