-
Notifications
You must be signed in to change notification settings - Fork 18
/
socket.go
185 lines (152 loc) · 3.89 KB
/
socket.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package endpoints
import (
"context"
"fmt"
"net"
"net/http"
"os"
"os/user"
"strconv"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/logger"
)
// Socket represents a unix socket with a given path.
type Socket struct {
Path string
Group string
listener *net.UnixListener
server *http.Server
ctx context.Context
cancel context.CancelFunc
}
// NewSocket returns a Socket struct with no listener attached yet.
func NewSocket(ctx context.Context, server *http.Server, path api.URL, group string) *Socket {
ctx, cancel := context.WithCancel(ctx)
return &Socket{
Path: path.Hostname(),
Group: group,
server: server,
ctx: ctx,
cancel: cancel,
}
}
// Type returns the type of the Endpoint.
func (s *Socket) Type() EndpointType {
return EndpointControl
}
// Listen on the unix socket path.
func (s *Socket) Listen() error {
_, err := net.Dial("unix", s.Path)
if err == nil {
return fmt.Errorf("Unix socket at %q is already running", s.Path)
}
err = s.removeStale()
if err != nil {
return err
}
addr, err := net.ResolveUnixAddr("unix", s.Path)
if err != nil {
return fmt.Errorf("Cannot resolve socket address: %w", err)
}
s.listener, err = net.ListenUnix("unix", addr)
if err != nil {
return fmt.Errorf("Cannot bind socket: %w", err)
}
err = localSetAccess(s.Path, s.Group)
if err != nil {
s.listener.Close()
return err
}
return nil
}
// Serve binds to the Socket's server.
func (s *Socket) Serve() {
if s.listener == nil {
return
}
ctx := logger.Ctx{"socket": s.listener.Addr()}
logger.Info(" - binding control socket", ctx)
go func() {
select {
case <-s.ctx.Done():
logger.Infof("Received shutdown signal - aborting unix socket server startup")
default:
err := s.server.Serve(s.listener)
if err != nil {
select {
case <-s.ctx.Done():
logger.Infof("Received shutdown signal - aborting unix socket server startup")
default:
logger.Error("Failed to start server", logger.Ctx{"err": err})
}
}
}
}()
}
// Close the Socket's listener.
func (s *Socket) Close() error {
if s.listener == nil {
return nil
}
logger.Info("Stopping REST API handler - closing socket", logger.Ctx{"socket": s.listener.Addr()})
s.cancel()
return s.listener.Close()
}
// Remove any stale socket file at the given path.
func (s *Socket) removeStale() error {
// If there's no socket file at all, there's nothing to do.
if !shared.PathExists(s.Path) {
return nil
}
logger.Debugf("Detected stale control socket, deleting")
err := os.Remove(s.Path)
if err != nil {
return fmt.Errorf("Could not delete stale local socket: %w", err)
}
return nil
}
// Change the file mode and ownership of the local endpoint control socket file,
// so access is granted only to the process user and to the given group (or the
// process group if group is empty).
func localSetAccess(path string, group string) error {
err := socketControlSetPermissions(path, 0660)
if err != nil {
return err
}
err = socketControlSetOwnership(path, group)
if err != nil {
return err
}
return nil
}
// Change the file mode of the given control socket file.
func socketControlSetPermissions(path string, mode os.FileMode) error {
err := os.Chmod(path, mode)
if err != nil {
return fmt.Errorf("Cannot set permissions on local socket: %w", err)
}
return nil
}
// Change the ownership of the given control socket file.
func socketControlSetOwnership(path string, groupName string) error {
var gid int
var err error
if groupName != "" {
g, err := user.LookupGroup(groupName)
if err != nil {
return fmt.Errorf("Cannot get group ID of '%s': %w", groupName, err)
}
gid, err = strconv.Atoi(g.Gid)
if err != nil {
return err
}
} else {
gid = os.Getgid()
}
err = os.Chown(path, os.Getuid(), gid)
if err != nil {
return fmt.Errorf("Cannot change ownership on local socket: %w", err)
}
return nil
}