Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 1 | // Package unixfd provides provides support for Dialing and Listening |
| 2 | // on already connected file descriptors (like those returned by socketpair). |
| 3 | package unixfd |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "net" |
| 10 | "os" |
| 11 | "strconv" |
| 12 | "sync" |
| 13 | "syscall" |
Cosmos Nicolaou | 87c0a55 | 2014-12-02 23:05:49 -0800 | [diff] [blame] | 14 | "time" |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 15 | "unsafe" |
Cosmos Nicolaou | 87c0a55 | 2014-12-02 23:05:49 -0800 | [diff] [blame] | 16 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 17 | "veyron.io/veyron/veyron2/ipc/stream" |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | const Network string = "unixfd" |
| 21 | |
| 22 | // singleConnListener implements net.Listener for an already-connected socket. |
| 23 | // This is different from net.FileListener, which calls syscall.Listen |
| 24 | // on an unconnected socket. |
| 25 | type singleConnListener struct { |
| 26 | c chan net.Conn |
| 27 | addr net.Addr |
| 28 | sync.Mutex |
| 29 | } |
| 30 | |
| 31 | func (l *singleConnListener) getChan() chan net.Conn { |
| 32 | l.Lock() |
| 33 | defer l.Unlock() |
| 34 | return l.c |
| 35 | } |
| 36 | |
| 37 | func (l *singleConnListener) Accept() (net.Conn, error) { |
| 38 | c := l.getChan() |
| 39 | if c == nil { |
| 40 | return nil, errors.New("listener closed") |
| 41 | } |
| 42 | if conn, ok := <-c; ok { |
| 43 | return conn, nil |
| 44 | } |
| 45 | return nil, io.EOF |
| 46 | } |
| 47 | |
| 48 | func (l *singleConnListener) Close() error { |
| 49 | l.Lock() |
| 50 | defer l.Unlock() |
| 51 | lc := l.c |
| 52 | if lc == nil { |
| 53 | return errors.New("listener already closed") |
| 54 | } |
| 55 | close(l.c) |
| 56 | l.c = nil |
| 57 | // If the socket was never Accept'ed we need to close it. |
| 58 | if c, ok := <-lc; ok { |
| 59 | return c.Close() |
| 60 | } |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | func (l *singleConnListener) Addr() net.Addr { |
| 65 | return l.addr |
| 66 | } |
| 67 | |
Cosmos Nicolaou | 87c0a55 | 2014-12-02 23:05:49 -0800 | [diff] [blame] | 68 | func unixFDConn(protocol, address string, timeout time.Duration) (net.Conn, error) { |
| 69 | // TODO(cnicolaou): have this respect the timeout. Possibly have a helper |
| 70 | // function that can be generally used for this, but in practice, I think |
| 71 | // it'll be cleaner to use the underlying protocol's deadline support of it |
| 72 | // has it. |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 73 | fd, err := strconv.ParseInt(address, 10, 32) |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | file := os.NewFile(uintptr(fd), "tmp") |
| 78 | conn, err := net.FileConn(file) |
| 79 | // 'file' is not used after this point, but we keep it open |
| 80 | // so that 'address' remains valid. |
| 81 | if err != nil { |
| 82 | file.Close() |
| 83 | return nil, err |
| 84 | } |
| 85 | // We wrap 'conn' so we can customize the address, and also |
| 86 | // to close 'file'. |
Matt Rosencrantz | 0610a23 | 2014-12-04 10:26:39 -0800 | [diff] [blame] | 87 | return &fdConn{addr: addr(address), sock: file, Conn: conn}, nil |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | type fdConn struct { |
| 91 | addr net.Addr |
| 92 | sock *os.File |
| 93 | net.Conn |
Matt Rosencrantz | 0610a23 | 2014-12-04 10:26:39 -0800 | [diff] [blame] | 94 | |
| 95 | mu sync.Mutex |
| 96 | closed bool |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | func (c *fdConn) Close() (err error) { |
Matt Rosencrantz | 0610a23 | 2014-12-04 10:26:39 -0800 | [diff] [blame] | 100 | c.mu.Lock() |
| 101 | defer c.mu.Unlock() |
| 102 | |
| 103 | if c.closed { |
| 104 | return nil |
| 105 | } |
| 106 | |
| 107 | c.closed = true |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 108 | defer c.sock.Close() |
| 109 | return c.Conn.Close() |
| 110 | } |
| 111 | |
| 112 | func (c *fdConn) LocalAddr() net.Addr { |
| 113 | return c.addr |
| 114 | } |
| 115 | |
| 116 | func (c *fdConn) RemoteAddr() net.Addr { |
| 117 | return c.addr |
| 118 | } |
| 119 | |
Cosmos Nicolaou | 87c0a55 | 2014-12-02 23:05:49 -0800 | [diff] [blame] | 120 | func unixFDListen(protocol, address string) (net.Listener, error) { |
| 121 | conn, err := unixFDConn(protocol, address, 0) |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 122 | if err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | c := make(chan net.Conn, 1) |
| 126 | c <- conn |
| 127 | return &singleConnListener{c, conn.LocalAddr(), sync.Mutex{}}, nil |
| 128 | } |
| 129 | |
| 130 | type addr string |
| 131 | |
| 132 | func (a addr) Network() string { return Network } |
| 133 | func (a addr) String() string { return string(a) } |
| 134 | |
| 135 | // Addr returns a net.Addr for the unixfd network for the given file descriptor. |
| 136 | func Addr(fd uintptr) net.Addr { |
| 137 | return addr(fmt.Sprintf("%d", fd)) |
| 138 | } |
| 139 | |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 140 | type fileDescriptor struct { |
| 141 | fd chan int |
| 142 | name string |
| 143 | } |
| 144 | |
| 145 | func newFd(fd int, name string) *fileDescriptor { |
| 146 | ch := make(chan int, 1) |
| 147 | ch <- fd |
| 148 | close(ch) |
| 149 | d := &fileDescriptor{ch, name} |
| 150 | return d |
| 151 | } |
| 152 | |
| 153 | func (f *fileDescriptor) releaseAddr() net.Addr { |
| 154 | if fd, ok := <-f.fd; ok { |
| 155 | return Addr(uintptr(fd)) |
| 156 | } |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | func (f *fileDescriptor) releaseFile() *os.File { |
| 161 | if fd, ok := <-f.fd; ok { |
| 162 | return os.NewFile(uintptr(fd), f.name) |
| 163 | } |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | // maybeClose closes the file descriptor, if it hasn't been released. |
| 168 | func (f *fileDescriptor) maybeClose() { |
| 169 | if file := f.releaseFile(); file != nil { |
| 170 | file.Close() |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Socketpair returns a pair of connected sockets for communicating with a child process. |
| 175 | func Socketpair() (*net.UnixConn, *os.File, error) { |
| 176 | lfd, rfd, err := socketpair(false) |
| 177 | if err != nil { |
| 178 | return nil, nil, err |
| 179 | } |
| 180 | defer rfd.maybeClose() |
| 181 | file := lfd.releaseFile() |
| 182 | // FileConn dups the fd, so we still want to close the original one. |
| 183 | defer file.Close() |
| 184 | conn, err := net.FileConn(file) |
| 185 | if err != nil { |
| 186 | return nil, nil, err |
| 187 | } |
| 188 | return conn.(*net.UnixConn), rfd.releaseFile(), nil |
| 189 | } |
| 190 | |
| 191 | func socketpair(closeRemoteOnExec bool) (local, remote *fileDescriptor, err error) { |
| 192 | syscall.ForkLock.RLock() |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 193 | fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 194 | if err == nil { |
| 195 | syscall.CloseOnExec(fds[0]) |
| 196 | if closeRemoteOnExec { |
| 197 | syscall.CloseOnExec(fds[1]) |
| 198 | } |
| 199 | } |
| 200 | syscall.ForkLock.RUnlock() |
| 201 | if err != nil { |
| 202 | return nil, nil, err |
| 203 | } |
| 204 | return newFd(fds[0], "local"), newFd(fds[1], "remote"), nil |
| 205 | } |
| 206 | |
| 207 | // SendConnection creates a new connected socket and sends |
| 208 | // one end over 'conn', along with 'data'. It returns the address for |
| 209 | // the local end of the socketpair. |
Ryan Brown | 8178944 | 2014-10-30 13:23:53 -0700 | [diff] [blame] | 210 | // Note that the returned address is an open file descriptor, |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 211 | // which you must close if you do not Dial or Listen to the address. |
Ryan Brown | 8178944 | 2014-10-30 13:23:53 -0700 | [diff] [blame] | 212 | func SendConnection(conn *net.UnixConn, data []byte, closeOnExec bool) (addr net.Addr, err error) { |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 213 | if len(data) < 1 { |
| 214 | return nil, errors.New("cannot send a socket without data.") |
| 215 | } |
Ryan Brown | 8178944 | 2014-10-30 13:23:53 -0700 | [diff] [blame] | 216 | remote, local, err := socketpair(closeOnExec) |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 217 | if err != nil { |
| 218 | return nil, err |
| 219 | } |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 220 | defer local.maybeClose() |
| 221 | rfile := remote.releaseFile() |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 222 | |
| 223 | rights := syscall.UnixRights(int(rfile.Fd())) |
| 224 | n, oobn, err := conn.WriteMsgUnix(data, rights, nil) |
| 225 | if err != nil { |
Ryan Brown | fac212e | 2014-11-13 12:23:47 -0800 | [diff] [blame] | 226 | rfile.Close() |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 227 | return nil, err |
| 228 | } else if n != len(data) || oobn != len(rights) { |
Ryan Brown | fac212e | 2014-11-13 12:23:47 -0800 | [diff] [blame] | 229 | rfile.Close() |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 230 | return nil, fmt.Errorf("expected to send %d, %d bytes, sent %d, %d", len(data), len(rights), n, oobn) |
| 231 | } |
Ryan Brown | fac212e | 2014-11-13 12:23:47 -0800 | [diff] [blame] | 232 | // Wait for the other side to acknowledge. |
| 233 | // This is to work around a race on OS X where it appears we can close |
| 234 | // the file descriptor before it gets transfered over the socket. |
| 235 | f := local.releaseFile() |
| 236 | fd, err := syscall.Dup(int(f.Fd())) |
| 237 | if err != nil { |
| 238 | f.Close() |
| 239 | rfile.Close() |
| 240 | return nil, err |
| 241 | } |
| 242 | newConn, err := net.FileConn(f) |
| 243 | f.Close() |
| 244 | if err != nil { |
| 245 | rfile.Close() |
| 246 | return nil, err |
| 247 | } |
| 248 | newConn.Read(make([]byte, 1)) |
| 249 | newConn.Close() |
| 250 | rfile.Close() |
| 251 | |
| 252 | return Addr(uintptr(fd)), nil |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | const cmsgDataLength = int(unsafe.Sizeof(int(1))) |
| 256 | |
| 257 | // ReadConnection reads a connection and additional data sent on 'conn' via a call to SendConnection. |
| 258 | // 'buf' must be large enough to hold the data. |
| 259 | func ReadConnection(conn *net.UnixConn, buf []byte) (net.Addr, int, error) { |
| 260 | oob := make([]byte, syscall.CmsgLen(cmsgDataLength)) |
| 261 | n, oobn, _, _, err := conn.ReadMsgUnix(buf, oob) |
| 262 | if err != nil { |
| 263 | return nil, n, err |
| 264 | } |
| 265 | if oobn > len(oob) { |
| 266 | return nil, n, fmt.Errorf("received too large oob data (%d, max %d)", oobn, len(oob)) |
| 267 | } |
| 268 | scms, err := syscall.ParseSocketControlMessage(oob[:oobn]) |
| 269 | if err != nil { |
| 270 | return nil, n, err |
| 271 | } |
| 272 | fd := -1 |
Ryan Brown | fac212e | 2014-11-13 12:23:47 -0800 | [diff] [blame] | 273 | // Loop through any file descriptors we are sent, and close |
| 274 | // all extras. |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 275 | for _, scm := range scms { |
| 276 | fds, err := syscall.ParseUnixRights(&scm) |
| 277 | if err != nil { |
| 278 | return nil, n, err |
| 279 | } |
| 280 | for _, f := range fds { |
Ryan Brown | fac212e | 2014-11-13 12:23:47 -0800 | [diff] [blame] | 281 | if fd == -1 { |
| 282 | fd = f |
| 283 | } else if f != -1 { |
| 284 | syscall.Close(f) |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 285 | } |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | if fd == -1 { |
| 289 | return nil, n, nil |
| 290 | } |
Ryan Brown | fac212e | 2014-11-13 12:23:47 -0800 | [diff] [blame] | 291 | result := Addr(uintptr(fd)) |
| 292 | fd, err = syscall.Dup(fd) |
| 293 | if err != nil { |
| 294 | CloseUnixAddr(result) |
| 295 | return nil, n, err |
| 296 | } |
| 297 | file := os.NewFile(uintptr(fd), "newconn") |
| 298 | newconn, err := net.FileConn(file) |
| 299 | file.Close() |
| 300 | if err != nil { |
| 301 | CloseUnixAddr(result) |
| 302 | return nil, n, err |
| 303 | } |
| 304 | newconn.Write(make([]byte, 1)) |
| 305 | newconn.Close() |
| 306 | return result, n, nil |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Ryan Brown | 8178944 | 2014-10-30 13:23:53 -0700 | [diff] [blame] | 309 | func CloseUnixAddr(addr net.Addr) error { |
| 310 | if addr.Network() != Network { |
| 311 | return errors.New("invalid network") |
| 312 | } |
| 313 | fd, err := strconv.ParseInt(addr.String(), 10, 32) |
| 314 | if err != nil { |
| 315 | return err |
| 316 | } |
| 317 | return syscall.Close(int(fd)) |
| 318 | } |
| 319 | |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 320 | func init() { |
| 321 | stream.RegisterProtocol(Network, unixFDConn, unixFDListen) |
| 322 | } |