ipc_unix.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 comms
  18. import (
  19. "net"
  20. "os"
  21. "github.com/ethereum/go-ethereum/fdtrack"
  22. "github.com/ethereum/go-ethereum/logger"
  23. "github.com/ethereum/go-ethereum/logger/glog"
  24. "github.com/ethereum/go-ethereum/rpc/codec"
  25. "github.com/ethereum/go-ethereum/rpc/shared"
  26. )
  27. func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) {
  28. c, err := net.DialUnix("unix", nil, &net.UnixAddr{cfg.Endpoint, "unix"})
  29. if err != nil {
  30. return nil, err
  31. }
  32. return &ipcClient{cfg.Endpoint, c, codec, codec.New(c)}, nil
  33. }
  34. func (self *ipcClient) reconnect() error {
  35. self.coder.Close()
  36. c, err := net.DialUnix("unix", nil, &net.UnixAddr{self.endpoint, "unix"})
  37. if err == nil {
  38. self.coder = self.codec.New(c)
  39. }
  40. return err
  41. }
  42. func startIpc(cfg IpcConfig, codec codec.Codec, api shared.EthereumApi) error {
  43. os.Remove(cfg.Endpoint) // in case it still exists from a previous run
  44. l, err := net.Listen("unix", cfg.Endpoint)
  45. if err != nil {
  46. return err
  47. }
  48. l = fdtrack.WrapListener("ipc", l)
  49. os.Chmod(cfg.Endpoint, 0600)
  50. go func() {
  51. for {
  52. conn, err := l.Accept()
  53. if err != nil {
  54. glog.V(logger.Error).Infof("Error accepting ipc connection - %v\n", err)
  55. continue
  56. }
  57. id := newIpcConnId()
  58. glog.V(logger.Debug).Infof("New IPC connection with id %06d started\n", id)
  59. go handle(id, conn, api, codec)
  60. }
  61. os.Remove(cfg.Endpoint)
  62. }()
  63. glog.V(logger.Info).Infof("IPC service started (%s)\n", cfg.Endpoint)
  64. return nil
  65. }