github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/rpc/ipc_unix.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
    13  
    14  package rpc
    15  
    16  import (
    17  	"context"
    18  	"net"
    19  	"os"
    20  	"path/filepath"
    21  )
    22  
    23  // ipcListen will create a Unix socket on the given endpoint.
    24  func ipcListen(endpoint string) (net.Listener, error) {
    25  	// Ensure the IPC path exists and remove any previous leftover
    26  	if err := os.MkdirAll(filepath.Dir(endpoint), 0751); err != nil {
    27  		return nil, err
    28  	}
    29  	os.Remove(endpoint)
    30  	l, err := net.Listen("unix", endpoint)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	os.Chmod(endpoint, 0600)
    35  	return l, nil
    36  }
    37  
    38  // newIPCConnection will connect to a Unix socket on the given endpoint.
    39  func newIPCConnection(ctx context.Context, endpoint string) (net.Conn, error) {
    40  	return dialContext(ctx, "unix", endpoint)
    41  }