-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmsg.go
96 lines (83 loc) · 2.85 KB
/
cmsg.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
package tableroll
// Taken from
// https://github.com/opencontainers/runc/blob/cf6c074115d00c932ef01dedb3e13ba8b8f964c3/libcontainer/utils/cmsg.go,
// and modified under the terms of the apache license, 2.0.
/*
* Copyright 2016, 2017 SUSE 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.
*/
import (
"fmt"
"os"
"golang.org/x/sys/unix"
)
// oobSpace is the size of the oob slice required to store a single FD. Note
// that unix.UnixRights appears to make the assumption that fd is always int32,
// so sizeof(fd) = 4.
var oobSpace = unix.CmsgSpace(4)
const maxNameLen = 4096
// recvFile receives a '*file' object from a socket that was send using
// 'sendFile'.
// It's identical to the RecvFd call taken from libcontainer, other than
// returning the '*file' type instead of an '*os.File' type.
// This is important because the RecvFd, in returning a '*os.File', requires us
// to call '.Fd()' to get back the underlying file descriptor, which has a
// side-effect of putting the fd into blocking mode.
// We'd rather keep a reference to the fd in our own '*file' struct so we can
// avoid that.
func recvFile(socket *os.File) (*file, error) {
name := make([]byte, maxNameLen)
oob := make([]byte, oobSpace)
sockfd := socket.Fd()
n, oobn, _, _, err := unix.Recvmsg(int(sockfd), name, oob, 0)
if err != nil {
return nil, err
}
if n >= maxNameLen || oobn != oobSpace {
return nil, fmt.Errorf("recvfd: incorrect number of bytes read (n=%d oobn=%d)", n, oobn)
}
// Truncate.
name = name[:n]
oob = oob[:oobn]
scms, err := unix.ParseSocketControlMessage(oob)
if err != nil {
return nil, err
}
if len(scms) != 1 {
return nil, fmt.Errorf("recvfd: number of SCMs is not 1: %d", len(scms))
}
scm := scms[0]
fds, err := unix.ParseUnixRights(&scm)
if err != nil {
return nil, err
}
if len(fds) != 1 {
return nil, fmt.Errorf("recvfd: number of fds is not 1: %d", len(fds))
}
fd := uintptr(fds[0])
fi := newFile(fd, string(name))
if fi == nil {
return nil, fmt.Errorf("could not construct a file")
}
return fi, nil
}
// sendFile sends a *file's file descriptor and name over the given socket.
func sendFile(socket *os.File, fi *file) error {
name := fi.Name()
if len(name) >= maxNameLen {
return fmt.Errorf("sendfd: filename too long: %s", fi.Name())
}
oob := unix.UnixRights(int(fi.fd))
return unix.Sendmsg(int(socket.Fd()), []byte(name), oob, nil, 0)
}