Skip to content

Commit cbb13ba

Browse files
committed
fuse: parse socket control message using the standard library
make mount procedure cleaner by using standard library to parse socker control message Change-Id: Ic52f5a0e2ddda40e680e341e35fb9b4158ba3324
1 parent 3502146 commit cbb13ba

File tree

3 files changed

+48
-56
lines changed

3 files changed

+48
-56
lines changed

fuse/mount.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package fuse
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"os"
7+
"syscall"
8+
)
9+
10+
func getConnection(local *os.File) (int, error) {
11+
conn, err := net.FileConn(local)
12+
if err != nil {
13+
return 0, err
14+
}
15+
defer conn.Close()
16+
unixConn := conn.(*net.UnixConn)
17+
18+
var data [4]byte
19+
control := make([]byte, 4*256)
20+
21+
_, oobn, _, _, err := unixConn.ReadMsgUnix(data[:], control[:])
22+
if err != nil {
23+
return 0, err
24+
}
25+
26+
messages, err := syscall.ParseSocketControlMessage(control[:oobn])
27+
if err != nil {
28+
return 0, err
29+
}
30+
if len(messages) != 1 {
31+
return 0, fmt.Errorf("getConnection: expect 1 control message, got %#v", messages)
32+
}
33+
message := messages[0]
34+
35+
fds, err := syscall.ParseUnixRights(&message)
36+
if err != nil {
37+
return 0, err
38+
}
39+
if len(fds) != 1 {
40+
return 0, fmt.Errorf("getConnection: expect 1 fd, got %#v", fds)
41+
}
42+
fd := fds[0]
43+
44+
if fd < 0 {
45+
return 0, fmt.Errorf("getConnection: fd < 0: %d", fd)
46+
}
47+
return fd, nil
48+
}

fuse/mount_darwin.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"os/exec"
1212
"strings"
1313
"syscall"
14-
"unsafe"
1514
)
1615

1716
func unixgramSocketpair() (l, r *os.File, err error) {
@@ -90,33 +89,6 @@ func unmount(dir string, opts *MountOptions) error {
9089
return syscall.Unmount(dir, 0)
9190
}
9291

93-
func getConnection(local *os.File) (int, error) {
94-
var data [4]byte
95-
control := make([]byte, 4*256)
96-
97-
// n, oobn, recvflags, from, errno - todo: error checking.
98-
_, oobn, _, _,
99-
err := syscall.Recvmsg(
100-
int(local.Fd()), data[:], control[:], 0)
101-
if err != nil {
102-
return 0, err
103-
}
104-
105-
message := *(*syscall.Cmsghdr)(unsafe.Pointer(&control[0]))
106-
fd := *(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(&control[0])) + syscall.SizeofCmsghdr))
107-
108-
if message.Type != syscall.SCM_RIGHTS {
109-
return 0, fmt.Errorf("getConnection: recvmsg returned wrong control type: %d", message.Type)
110-
}
111-
if oobn <= syscall.SizeofCmsghdr {
112-
return 0, fmt.Errorf("getConnection: too short control message. Length: %d", oobn)
113-
}
114-
if fd < 0 {
115-
return 0, fmt.Errorf("getConnection: fd < 0: %d", fd)
116-
}
117-
return int(fd), nil
118-
}
119-
12092
func fusermountBinary() (string, error) {
12193
binPaths := []string{
12294
"/Library/Filesystems/macfuse.fs/Contents/Resources/mount_macfuse",

fuse/mount_linux.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"path"
1313
"strings"
1414
"syscall"
15-
"unsafe"
1615
)
1716

1817
func unixgramSocketpair() (l, r *os.File, err error) {
@@ -203,33 +202,6 @@ func unmount(mountPoint string, opts *MountOptions) (err error) {
203202
return err
204203
}
205204

206-
func getConnection(local *os.File) (int, error) {
207-
var data [4]byte
208-
control := make([]byte, 4*256)
209-
210-
// n, oobn, recvflags, from, errno - todo: error checking.
211-
_, oobn, _, _,
212-
err := syscall.Recvmsg(
213-
int(local.Fd()), data[:], control[:], 0)
214-
if err != nil {
215-
return 0, err
216-
}
217-
218-
message := *(*syscall.Cmsghdr)(unsafe.Pointer(&control[0]))
219-
fd := *(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(&control[0])) + syscall.SizeofCmsghdr))
220-
221-
if message.Type != 1 {
222-
return 0, fmt.Errorf("getConnection: recvmsg returned wrong control type: %d", message.Type)
223-
}
224-
if oobn <= syscall.SizeofCmsghdr {
225-
return 0, fmt.Errorf("getConnection: too short control message. Length: %d", oobn)
226-
}
227-
if fd < 0 {
228-
return 0, fmt.Errorf("getConnection: fd < 0: %d", fd)
229-
}
230-
return int(fd), nil
231-
}
232-
233205
// lookPathFallback - search binary in PATH and, if that fails,
234206
// in fallbackDir. This is useful if PATH is possible empty.
235207
func lookPathFallback(file string, fallbackDir string) (string, error) {

0 commit comments

Comments
 (0)