forked from sassoftware/relic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify_unix.go
80 lines (74 loc) · 2.14 KB
/
notify_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright © SAS Institute Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// +build !windows
package activation
import (
"fmt"
"net"
"os"
"strconv"
"syscall"
)
// DaemonReady signals to a parent init system or daemon manager that the
// daemon is finished starting up. Use after all listening sockets have been
// opened.
func DaemonReady() (err error) {
if name := os.Getenv("NOTIFY_SOCKET"); name != "" {
// systemd
if err2 := writePath(name, "unixgram", "READY=1"); err2 != nil {
err = err2
}
}
if fdstr := os.Getenv("NOTIFY_FD"); fdstr != "" {
// github.com/pusher/crank
if err2 := writeFd(fdstr, "READY=1"); err2 != nil {
err = err2
}
}
if fdstr := os.Getenv("EINHORN_SOCK_FD"); fdstr != "" {
// einhorn -g
if err2 := writeFd(fdstr, einhornReadyStr()); err2 != nil {
err = err2
}
} else if name := os.Getenv("EINHORN_SOCK_PATH"); name != "" {
// github.com/stripe/einhorn
if err2 := writePath(name, "unix", einhornReadyStr()); err2 != nil {
err = err2
}
}
return
}
// write a string to the unix socket at the named path
func writePath(path, netType, message string) error {
sockAddr := &net.UnixAddr{Name: path, Net: netType}
conn, err := net.DialUnix(netType, nil, sockAddr)
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Write([]byte(message))
return err
}
// write a string to a numeric file descriptor
func writeFd(fdstr, message string) error {
fd, err := strconv.Atoi(fdstr)
if err != nil {
return err
}
_, err = syscall.Write(fd, []byte(message))
return err
}
func einhornReadyStr() string {
return fmt.Sprintf(`{"command":"worker:ack", "pid":%d}`+"\n", os.Getpid())
}