func (r *fake_conn) run_queue(conn *net.UnixConn) {
	defer conn.Close()

	for {
		select {
		case <-r.end:
			return

		default:
			buffer := make([]byte, 1<<16)

			conn.SetReadDeadline(time.Now().Add(1000 * time.Millisecond))
			s, _, err := conn.ReadFrom(buffer)
			switch t_err := err.(type) {
			case nil:
			case net.Error:
				if !t_err.Timeout() {
					panic(err)
				}
			default:
				panic(err)
			}

			if s > 0 {
				r.queue <- buffer[0:s]
			}

		}
	}

}
Beispiel #2
0
func ReadFile(c *net.UnixConn, timeout time.Duration) (*os.File, error) {
	oob := make([]byte, 64)

	if timeout > 0 {
		deadline := time.Now().Add(timeout)
		if err := c.SetReadDeadline(deadline); err != nil {
			return nil, err
		}
	}

	_, oobn, flags, _, err := c.ReadMsgUnix(nil, oob)
	if err != nil {
		return nil, err
	}
	if flags != 0 || oobn <= 0 {
		panic("ReadMsgUnix: flags != 0 || oobn <= 0")
	}

	// file descriptors are now open in this process

	scm, err := syscall.ParseSocketControlMessage(oob[:oobn])
	if err != nil {
		return nil, err
	}
	if len(scm) != 1 {
		panic("invalid scm message")
	}

	fds, err := syscall.ParseUnixRights(&scm[0])
	if err != nil {
		return nil, err
	}
	if len(fds) != 1 {
		panic("invalid scm message")
	}

	return os.NewFile(uintptr(fds[0]), ""), nil
}