github.com/theQRL/go-zond@v0.1.1/rpc/ipc_unix.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  //go:build darwin || dragonfly || freebsd || linux || nacl || netbsd || openbsd || solaris
    18  // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
    19  
    20  package rpc
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  	"net"
    26  	"os"
    27  	"path/filepath"
    28  
    29  	"github.com/theQRL/go-zond/log"
    30  )
    31  
    32  const (
    33  	// On Linux, sun_path is 108 bytes in size
    34  	// see http://man7.org/linux/man-pages/man7/unix.7.html
    35  	maxPathSize = int(108)
    36  )
    37  
    38  // ipcListen will create a Unix socket on the given endpoint.
    39  func ipcListen(endpoint string) (net.Listener, error) {
    40  	// account for null-terminator too
    41  	if len(endpoint)+1 > maxPathSize {
    42  		log.Warn(fmt.Sprintf("The ipc endpoint is longer than %d characters. ", maxPathSize-1),
    43  			"endpoint", endpoint)
    44  	}
    45  
    46  	// Ensure the IPC path exists and remove any previous leftover
    47  	if err := os.MkdirAll(filepath.Dir(endpoint), 0751); err != nil {
    48  		return nil, err
    49  	}
    50  	os.Remove(endpoint)
    51  	l, err := net.Listen("unix", endpoint)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	os.Chmod(endpoint, 0600)
    56  	return l, nil
    57  }
    58  
    59  // newIPCConnection will connect to a Unix socket on the given endpoint.
    60  func newIPCConnection(ctx context.Context, endpoint string) (net.Conn, error) {
    61  	return new(net.Dialer).DialContext(ctx, "unix", endpoint)
    62  }