This repository has been archived by the owner on May 15, 2024. It is now read-only.
forked from coreos/sdnotify-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (111 loc) · 2.44 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"net"
"os"
"os/signal"
"syscall"
)
const maxPktSize = 1024
func readUnixConn(conn *net.UnixConn, msgs chan []byte) {
for {
msg := make([]byte, maxPktSize)
nread, err := conn.Read(msg)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read from unix socket: %v\n", err)
return
}
msgs <- msg[:nread]
}
}
type unixProxy struct {
local, remote *net.UnixConn
}
func newProxy(src, dst string) (*unixProxy, error) {
os.Remove(src)
// start listening
local, err := net.ListenUnixgram("unixgram", &net.UnixAddr{
Name: src,
Net: "unixgram",
})
if err != nil {
return nil, err
}
remote, err := net.DialUnix("unixgram", nil, &net.UnixAddr{
Name: dst,
Net: "unixgram",
})
if err != nil {
return nil, err
}
return &unixProxy{
local: local,
remote: remote,
}, nil
}
func (p *unixProxy) run(cancel chan struct{}) {
msgs := make(chan []byte)
go readUnixConn(p.local, msgs)
for {
select {
case msg := <-msgs:
p.remote.Write(msg)
case <-cancel:
p.local.Close()
p.remote.Close()
return
}
}
}
func forkExec(argv []string) (*os.Process, error) {
return os.StartProcess(argv[0], argv, &os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
})
}
func main() {
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "Usage: %s proxy-socket cmd ...\n", os.Args[0])
os.Exit(1)
}
sdSock := os.Getenv("NOTIFY_SOCKET")
if sdSock == "" {
fmt.Fprintf(os.Stderr, "NOTIFY_SOCKET environment variable not set\n")
os.Exit(1)
}
proxySock := os.Args[1]
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGCHLD)
// replace NOTIFY_SOCKET with the proxy socket
os.Setenv("NOTIFY_SOCKET", proxySock)
proxy, err := newProxy(proxySock, sdSock)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating proxy: %v\n", err)
os.Exit(1)
}
// fork/exec
proc, err := forkExec(os.Args[2:len(os.Args)])
if err != nil {
fmt.Fprintf(os.Stderr, "Error executing command: %v\n", err)
os.Exit(1)
}
// proxy the unixgram messages
cancel := make(chan struct{})
go proxy.run(cancel)
for {
sig := <-sigs
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
// propogate to child
proc.Signal(sig)
case syscall.SIGCHLD:
ps, err := proc.Wait()
if err != nil {
fmt.Fprintf(os.Stderr, "waitpid failed: %v\n", err)
os.Exit(1)
}
close(cancel)
ec := ps.Sys().(syscall.WaitStatus).ExitStatus()
os.Exit(ec)
}
}
}