当前位置: 首页>>代码示例>>Golang>>正文


Golang net.DialUnix函数代码示例

本文整理汇总了Golang中net.DialUnix函数的典型用法代码示例。如果您正苦于以下问题:Golang DialUnix函数的具体用法?Golang DialUnix怎么用?Golang DialUnix使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了DialUnix函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: newUnixTransport

func newUnixTransport(keys string) (transport, error) {
	var err error

	t := new(unixTransport)
	abstract := getKey(keys, "abstract")
	path := getKey(keys, "path")
	switch {
	case abstract == "" && path == "":
		return nil, errors.New("dbus: invalid address (neither path nor abstract set)")
	case abstract != "" && path == "":
		t.UnixConn, err = net.DialUnix("unix", nil, &net.UnixAddr{Name: "@" + abstract, Net: "unix"})
		if err != nil {
			return nil, err
		}
		return t, nil
	case abstract == "" && path != "":
		t.UnixConn, err = net.DialUnix("unix", nil, &net.UnixAddr{Name: path, Net: "unix"})
		if err != nil {
			return nil, err
		}
		return t, nil
	default:
		return nil, errors.New("dbus: invalid address (both path and abstract set)")
	}
}
开发者ID:CadeLaRen,项目名称:docker-3,代码行数:25,代码来源:transport_unix.go

示例2: DialServer

// DialServer dials a Unix Domain Socket where a server is listening and
// returns a client to the server.
func DialServer(uid string) (*Client, error) {
	// Have the client start listening for responses from the server.
	clientUID := uuid.New()
	clientPath := path.Join(os.TempDir(), clientUID)
	outConn, err := net.ListenUnixgram(udsType, &net.UnixAddr{clientPath, udsType})
	if err != nil {
		return nil, err
	}

	// Dial the server.
	log.Infof("client: dialing the server")
	setupConn, err := net.DialUnix(
		udsType,
		nil,
		&net.UnixAddr{path.Join(os.TempDir(), uid), udsType},
	)
	if err != nil {
		return nil, err
	}

	log.Infof("client: sending uid to server")
	if err := SetupEncode(clientUID, setupConn); err != nil {
		return nil, err
	}
	setupConn.Close()

	// Get the socket the server is going to listen on.
	out := bufio.NewReader(outConn)
	inUUID, err := SetupDecode(out)
	if err != nil {
		return nil, err
	}
	log.Infof("client: received server uid for conn")

	// Dial the server.
	in, err := net.DialUnix(
		udsType,
		nil,
		&net.UnixAddr{path.Join(os.TempDir(), inUUID), udsType},
	)
	if err != nil {
		return nil, err
	}
	log.Infof("client: dialed server")

	c := &Client{
		inConn:    in,
		outConn:   out,
		reqCh:     make(chan request, 50),
		responses: make(map[uint64]chan response),
	}

	go c.send()
	go c.receive()
	return c, nil
}
开发者ID:johnsiilver,项目名称:golib,代码行数:58,代码来源:client.go

示例3: SdNotify

// SdNotify sends a message to the init daemon. It is common to ignore the error.
// If `unsetEnvironment` is true, the environment variable `NOTIFY_SOCKET`
// will be unconditionally unset.
//
// It returns one of the following:
// (false, nil) - notification not supported (i.e. NOTIFY_SOCKET is unset)
// (false, err) - notification supported, but failure happened (e.g. error connecting to NOTIFY_SOCKET or while sending data)
// (true, nil) - notification supported, data has been sent
func SdNotify(unsetEnvironment bool, state string) (sent bool, err error) {
	socketAddr := &net.UnixAddr{
		Name: os.Getenv("NOTIFY_SOCKET"),
		Net:  "unixgram",
	}

	// NOTIFY_SOCKET not set
	if socketAddr.Name == "" {
		return false, nil
	}

	if unsetEnvironment {
		err = os.Unsetenv("NOTIFY_SOCKET")
	}
	if err != nil {
		return false, err
	}

	conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
	// Error connecting to NOTIFY_SOCKET
	if err != nil {
		return false, err
	}
	defer conn.Close()

	_, err = conn.Write([]byte(state))
	// Error sending the message
	if err != nil {
		return false, err
	}
	return true, nil
}
开发者ID:intelsdi-x,项目名称:rkt,代码行数:40,代码来源:sdnotify.go

示例4: ConnectDisplay

func ConnectDisplay(addr string) (ret *Display, err error) {
	runtime_dir := os.Getenv("XDG_RUNTIME_DIR")
	if runtime_dir == "" {
		return nil, errors.New("XDG_RUNTIME_DIR not set in the environment.")
	}
	if addr == "" {
		addr = os.Getenv("WAYLAND_DISPLAY")
	}
	if addr == "" {
		addr = "wayland-0"
	}
	addr = runtime_dir + "/" + addr
	ctx := &Connection{}
	ctx.objects = make(map[ProxyId]Proxy)
	ctx.currentId = 0
	ctx.dispatchRequest = make(chan bool)
	ctx.exit = make(chan bool)
	ctx.conn, err = net.DialUnix("unix", nil, &net.UnixAddr{addr, "unix"})
	if err != nil {
		return nil, err
	}
	ret = NewDisplay(ctx)
	// dispatch events in separate gorutine
	go ctx.run()
	return ret, nil
}
开发者ID:stanluk,项目名称:wayland-client,代码行数:26,代码来源:connection.go

示例5: Notify

// Notify sends a message to the init daemon. It is common to ignore the error.
func Notify(state string) error {
	socketAddr := &net.UnixAddr{
		Name: os.Getenv("NOTIFY_SOCKET"),
		Net:  "unixgram",
	}

	if socketAddr.Name == "" {
		return ErrNotifyNoSocket
	}
	switch socketAddr.Name[0] {
	case '@', '/':
	default:
		return ErrNotifyNoSocket
	}

	conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
	if err != nil {
		return err
	}
	defer logx.LogReturnedErr(conn.Close, nil, "failed to close connection")

	_, err = conn.Write([]byte(state))
	if err != nil {
		return err
	}

	return nil
}
开发者ID:joshie,项目名称:lochness,代码行数:29,代码来源:sd_notify.go

示例6: BusConnect

// returns a new BusConnection.
func BusConnect(path string, bh busHandler, rh readHandler) (conn *BusConnection, err error) {

	// check if file exists. if not, bail.
	_, err = os.Lstat(path)
	if err != nil {
		return
	}

	// resolve the address.
	addr, err := net.ResolveUnixAddr("unix", path)
	if err != nil {
		return
	}

	// connect.
	unixConn, err := net.DialUnix("unix", nil, addr)
	if err != nil {
		return
	}

	// create the connection.
	conn = &BusConnection{
		path:        path,
		socket:      unixConn,
		incoming:    bufio.NewReader(unixConn),
		outgoing:    bufio.NewWriter(unixConn),
		busHandler:  bh,
		readHandler: rh,
		Connected:   true,
	}

	return
}
开发者ID:cooper,项目名称:system,代码行数:34,代码来源:bus-client.go

示例7: newProxy

func newProxy(src, dst string) (*unixProxy, error) {
	os.Remove(src)

	// start listening
	local, err := net.ListenUnixgram("unixgram", &net.UnixAddr{
		Name: src,
		Net:  "unixgram",
	})

	if err != nil {
		return nil, err
	}

	remote, err := net.DialUnix("unixgram", nil, &net.UnixAddr{
		Name: dst,
		Net:  "unixgram",
	})

	if err != nil {
		return nil, err
	}

	return &unixProxy{
		local:  local,
		remote: remote,
	}, nil
}
开发者ID:postfix,项目名称:sdnotify-proxy,代码行数:27,代码来源:main.go

示例8: TestListenUnix

func (_ *ListenerSuite) TestListenUnix(c *C) {
	addr, _ := net.ResolveUnixAddr("unix", "/tmp/go_test_socket")
	cfg := &conf.ListenerConf{
		Name: "local",
		Ports: []*conf.PortConf{
			&conf.PortConf{
				Type: "unix",
				Addr: addr,
			},
		},
	}

	ll := listener.NewListener(cfg)
	ll.SetHandler(echoHandler)
	ll.Start()
	time.Sleep(50 * time.Millisecond)

	cc, err := net.DialUnix("unix", nil, addr)
	c.Assert(err, IsNil)

	testEchoConnection(c, cc)

	cc.Close()

	ll.Stop()
}
开发者ID:netvl,项目名称:bridge-server,代码行数:26,代码来源:listener_unix_test.go

示例9: NewUnixgramServer

func NewUnixgramServer(t lotf.Tail, raddr *net.UnixAddr) (*DgramServer, error) {
	conn, err := net.DialUnix("unixgram", nil, raddr)
	if err != nil {
		return nil, err
	}
	return &DgramServer{t, io.WriteCloser(conn)}, nil
}
开发者ID:wxdublin,项目名称:lotf,代码行数:7,代码来源:dgram.go

示例10: Connect

func Connect(path string) (conn *Connection, err error) {

	// check if file exists. if not, bail.
	_, err = os.Lstat(path)
	if err != nil {
		return
	}

	// resolve the address
	addr, err := net.ResolveUnixAddr("unix", path)
	if err != nil {
		return
	}

	// connect
	unixConn, err := net.DialUnix("unix", nil, addr)
	if err != nil {
		return
	}

	conn = &Connection{
		path:     path,
		socket:   unixConn,
		incoming: bufio.NewReader(unixConn),
	}

	return
}
开发者ID:cooper,项目名称:libclient3,代码行数:28,代码来源:connection.go

示例11: HijackSession

// Attempt to hijack session previously running bot
func (irc *IrcCon) HijackSession() bool {
	unaddr, err := net.ResolveUnixAddr("unix", irc.unixastr)
	if err != nil {
		irc.Log(LWarning, "could not resolve unix socket")
		return false
	}

	con, err := net.DialUnix("unix", nil, unaddr)
	if err != nil {
		fmt.Println("Couldnt restablish connection, no prior bot.")
		fmt.Println(err)
		return false
	}

	ncon, err := sendfd.RecvFD(con)
	if err != nil {
		panic(err)
	}

	netcon, err := net.FileConn(ncon)
	if err != nil {
		panic(err)
	}

	irc.reconnect = true
	irc.con = netcon
	return true
}
开发者ID:necrose99,项目名称:hellabot,代码行数:29,代码来源:recon_linux.go

示例12: OpenTPM

// OpenTPM opens a channel to the TPM at the given path. If the file is a
// device, then it treats it like a normal TPM device, and if the file is a
// Unix domain socket, then it opens a connection to the socket.
func OpenTPM(path string) (io.ReadWriteCloser, error) {
	// If it's a regular file, then open it
	var rwc io.ReadWriteCloser
	fi, err := os.Stat(path)
	if err != nil {
		return nil, err
	}

	if fi.Mode()&os.ModeDevice != 0 {
		var f *os.File
		f, err = os.OpenFile(path, os.O_RDWR, 0600)
		if err != nil {
			return nil, err
		}
		rwc = io.ReadWriteCloser(f)
	} else if fi.Mode()&os.ModeSocket != 0 {
		uc, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: path, Net: "unix"})
		if err != nil {
			return nil, err
		}
		rwc = io.ReadWriteCloser(uc)
	} else {
		return nil, fmt.Errorf("unsupported TPM file mode %s", fi.Mode().String())
	}

	return rwc, nil
}
开发者ID:alex1818,项目名称:go-tpm,代码行数:30,代码来源:tpm.go

示例13: setupSockets

// setupSockets sets up the sockets used to communicate between the client
// and the server.  If successful it spins up another goroutine to handle
// all communication from that client.
func (s *Server) setupSockets(remoteID string) {
	log.Infof("server: dialing client socket")
	out, err := net.DialUnix(
		udsType,
		nil,
		&net.UnixAddr{path.Join(os.TempDir(), remoteID), udsType},
	)
	if err != nil {
		log.Infof("problem dialing client's socket: %s", err)
		return
	}

	log.Infof("server: preparing to listen on new socket")
	uid := uuid.New()
	p := path.Join(os.TempDir(), uid)
	in, err := net.ListenUnixgram(udsType, &net.UnixAddr{p, udsType})
	if err != nil {
		out.Close()
		log.Infof("could not listen on domain socket %q: %s", p, err)
		return
	}

	log.Infof("server: sending a uid to the client")
	if err := SetupEncode(uid, out); err != nil {
		out.Close()
		log.Infof("problem encoding UUIDv4 for setup: %s", err)
		return
	}

	go s.serveConn(in, out)
}
开发者ID:johnsiilver,项目名称:golib,代码行数:34,代码来源:server.go

示例14: NotifySend

// Send sends a message to the init daemon. It is common to ignore the error.
//
// This function differs from that in github.com/coreos/go-systemd/daemon in
// that that code closes the socket after each call, and so won't work in a
// chroot. This function keeps the socket open; so long as it is called at
// least once before chrooting, it can continue to be used.
//
// May return ErrNoSocket.
func NotifySend(state string) error {
	sdNotifyMutex.Lock()
	defer sdNotifyMutex.Unlock()

	if !sdNotifyInited {
		sdNotifyInited = true

		socketAddr := &net.UnixAddr{
			Name: os.Getenv("NOTIFY_SOCKET"),
			Net:  "unixgram",
		}

		if socketAddr.Name == "" {
			return ErrNoSocket
		}

		conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
		if err != nil {
			return err
		}

		sdNotifySocket = conn
	}

	if sdNotifySocket == nil {
		return ErrNoSocket
	}

	_, err := sdNotifySocket.Write([]byte(state))
	return err
}
开发者ID:hlandau,项目名称:svcutils,代码行数:39,代码来源:sdnotify.go

示例15: connectViaUnix

func connectViaUnix(c *Client, remote *RemoteConfig) error {
	c.BaseURL = "http://unix.socket"
	c.BaseWSURL = "ws://unix.socket"
	c.Transport = "unix"
	uDial := func(network, addr string) (net.Conn, error) {
		// The arguments 'network' and 'addr' are ignored because
		// they are the wrong information.
		// addr is generated from BaseURL which becomes
		// 'unix.socket:80' which is certainly not what we want.
		// handle:
		//   unix:///path/to/socket
		//   unix:/path/to/socket
		//   unix:path/to/socket
		path := strings.TrimPrefix(remote.Addr, "unix:")
		if strings.HasPrefix(path, "///") {
			// translate unix:///path/to, to just "/path/to"
			path = path[2:]
		}
		raddr, err := net.ResolveUnixAddr("unix", path)
		if err != nil {
			return nil, err
		}
		return net.DialUnix("unix", nil, raddr)
	}
	c.Http.Transport = &http.Transport{Dial: uDial}
	c.websocketDialer.NetDial = uDial
	c.Remote = remote

	st, err := c.ServerStatus()
	if err != nil {
		return err
	}
	c.Certificate = st.Environment.Certificate
	return nil
}
开发者ID:jameinel,项目名称:lxd,代码行数:35,代码来源:client.go


注:本文中的net.DialUnix函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。