-
Notifications
You must be signed in to change notification settings - Fork 350
/
proxy_other.go
211 lines (189 loc) · 5.75 KB
/
proxy_other.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2022 Google LLC
//
// 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.
//go:build !windows && !openbsd && !freebsd
// +build !windows,!openbsd,!freebsd
package proxy
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"syscall"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
)
type socketSymlink struct {
socket *socketMount
symlink *symlink
}
func configureFUSE(c *Client, conf *Config) (*Client, error) {
if _, err := os.Stat(conf.FUSEDir); err != nil {
return nil, err
}
if err := os.MkdirAll(conf.FUSETempDir, 0777); err != nil {
return nil, err
}
c.fuseMount = fuseMount{
fuseDir: conf.FUSEDir,
fuseTempDir: conf.FUSETempDir,
fuseSockets: map[string]socketSymlink{},
// Use pointers for the following mutexes so fuseMount may be embedded
// as a value and support zero value lookups on fuseDir.
fuseMu: &sync.Mutex{},
fuseServerMu: &sync.Mutex{},
fuseWg: &sync.WaitGroup{},
}
return c, nil
}
type fuseMount struct {
// fuseDir specifies the directory where a FUSE server is mounted. The value
// is empty if FUSE is not enabled. The directory holds symlinks to Unix
// domain sockets in the fuseTmpDir.
fuseDir string
fuseTempDir string
// fuseMu protects access to fuseSockets.
fuseMu *sync.Mutex
// fuseSockets is a map of instance connection name to socketMount and
// symlink.
fuseSockets map[string]socketSymlink
fuseServerMu *sync.Mutex
fuseServer *fuse.Server
fuseWg *sync.WaitGroup
fuseExitCh chan error
// Inode adds support for FUSE operations.
fs.Inode
}
// Readdir returns a list of all active Unix sockets in addition to the README.
func (c *Client) Readdir(_ context.Context) (fs.DirStream, syscall.Errno) {
entries := []fuse.DirEntry{
{Name: "README", Mode: 0555 | fuse.S_IFREG},
}
var active []string
c.fuseMu.Lock()
for k := range c.fuseSockets {
active = append(active, k)
}
c.fuseMu.Unlock()
for _, a := range active {
entries = append(entries, fuse.DirEntry{
Name: a,
Mode: 0777 | syscall.S_IFSOCK,
})
}
return fs.NewListDirStream(entries), fs.OK
}
// Lookup implements the fs.NodeLookuper interface and returns an index node
// (inode) for a symlink that points to a Unix domain socket. The Unix domain
// socket is connected to the requested Cloud SQL instance. Lookup returns a
// symlink (instead of the socket itself) so that multiple callers all use the
// same Unix socket.
func (c *Client) Lookup(_ context.Context, instance string, _ *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
ctx := context.Background()
if instance == "README" {
return c.NewInode(ctx, &readme{}, fs.StableAttr{}), fs.OK
}
if _, err := parseConnName(instance); err != nil {
c.logger.Debugf("could not parse instance connection name for %q: %v", instance, err)
return nil, syscall.ENOENT
}
c.fuseMu.Lock()
defer c.fuseMu.Unlock()
if l, ok := c.fuseSockets[instance]; ok {
c.logger.Debugf("found existing socket for instance %q", instance)
return l.symlink.EmbeddedInode(), fs.OK
}
c.logger.Debugf("creating new socket for instance %q", instance)
s, err := c.newSocketMount(
ctx, withUnixSocket(*c.conf, c.fuseTempDir),
nil, InstanceConnConfig{Name: instance},
)
if err != nil {
c.logger.Errorf("could not create socket for %q: %v", instance, err)
return nil, syscall.ENOENT
}
c.fuseWg.Add(1)
go func() {
defer c.fuseWg.Done()
sErr := c.serveSocketMount(ctx, s)
if sErr != nil {
c.logger.Debugf("could not serve socket for instance %q: %v", instance, sErr)
c.fuseMu.Lock()
defer c.fuseMu.Unlock()
delete(c.fuseSockets, instance)
select {
// Best effort attempt to send error.
// If this send fails, it means the reading goroutine has
// already pulled a value out of the channel and is no longer
// reading any more values. In other words, we report only the
// first error.
case c.fuseExitCh <- sErr:
default:
return
}
}
}()
// Return a symlink that points to the actual Unix socket within the
// temporary directory. For Postgres, return a symlink that points to the
// directory which holds the ".s.PGSQL.5432" Unix socket.
sl := &symlink{path: filepath.Join(c.fuseTempDir, instance)}
c.fuseSockets[instance] = socketSymlink{
socket: s,
symlink: sl,
}
return c.NewInode(ctx, sl, fs.StableAttr{
Mode: 0777 | fuse.S_IFLNK},
), fs.OK
}
func withUnixSocket(c Config, tmpDir string) *Config {
c.UnixSocket = tmpDir
return &c
}
func (c *Client) serveFuse(ctx context.Context, notify func()) error {
srv, err := fs.Mount(c.fuseDir, c, &fs.Options{
MountOptions: fuse.MountOptions{AllowOther: true},
})
if err != nil {
return fmt.Errorf("FUSE mount failed: %q: %v", c.fuseDir, err)
}
c.fuseServerMu.Lock()
c.fuseServer = srv
c.fuseExitCh = make(chan error)
c.fuseServerMu.Unlock()
notify()
select {
case err = <-c.fuseExitCh:
return err
case <-ctx.Done():
return ctx.Err()
}
}
func (c *Client) fuseMounts() []*socketMount {
var mnts []*socketMount
c.fuseMu.Lock()
for _, m := range c.fuseSockets {
mnts = append(mnts, m.socket)
}
c.fuseMu.Unlock()
return mnts
}
func (c *Client) unmountFUSE() error {
c.fuseServerMu.Lock()
defer c.fuseServerMu.Unlock()
if c.fuseServer == nil {
return nil
}
return c.fuseServer.Unmount()
}
func (c *Client) waitForFUSEMounts() { c.fuseWg.Wait() }