ipc_unix.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
  17. package rpc
  18. import (
  19. "context"
  20. "fmt"
  21. "net"
  22. "os"
  23. "path/filepath"
  24. "github.com/ethereum/go-ethereum/log"
  25. )
  26. // ipcListen will create a Unix socket on the given endpoint.
  27. func ipcListen(endpoint string) (net.Listener, error) {
  28. if len(endpoint) > int(max_path_size) {
  29. log.Warn(fmt.Sprintf("The ipc endpoint is longer than %d characters. ", max_path_size),
  30. "endpoint", endpoint)
  31. }
  32. // Ensure the IPC path exists and remove any previous leftover
  33. if err := os.MkdirAll(filepath.Dir(endpoint), 0751); err != nil {
  34. return nil, err
  35. }
  36. os.Remove(endpoint)
  37. l, err := net.Listen("unix", endpoint)
  38. if err != nil {
  39. return nil, err
  40. }
  41. os.Chmod(endpoint, 0600)
  42. return l, nil
  43. }
  44. // newIPCConnection will connect to a Unix socket on the given endpoint.
  45. func newIPCConnection(ctx context.Context, endpoint string) (net.Conn, error) {
  46. return new(net.Dialer).DialContext(ctx, "unix", endpoint)
  47. }