ipc_unix.go 1.8 KB
Newer Older
F
Felix Lange 已提交
1
// Copyright 2015 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
F
Felix Lange 已提交
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
F
Felix Lange 已提交
5 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
F
Felix Lange 已提交
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
F
Felix Lange 已提交
12 13 14
// GNU Lesser General Public License for more details.
//
// 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/>.
F
Felix Lange 已提交
16

17
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
B
Bas van Kervel 已提交
18

19
package rpc
B
Bas van Kervel 已提交
20

21
import (
22
	"context"
23
	"fmt"
24 25 26
	"net"
	"os"
	"path/filepath"
27 28

	"github.com/ethereum/go-ethereum/log"
B
Bas van Kervel 已提交
29 30
)

31 32 33 34 35 36 37 38 39 40
/*
#include <sys/un.h>

int max_socket_path_size() {
struct sockaddr_un s;
return sizeof(s.sun_path);
}
*/
import "C"

41 42
// ipcListen will create a Unix socket on the given endpoint.
func ipcListen(endpoint string) (net.Listener, error) {
43 44 45 46 47
	if len(endpoint) > int(C.max_socket_path_size()) {
		log.Warn(fmt.Sprintf("The ipc endpoint is longer than %d characters. ", C.max_socket_path_size()),
			"endpoint", endpoint)
	}

48 49 50
	// Ensure the IPC path exists and remove any previous leftover
	if err := os.MkdirAll(filepath.Dir(endpoint), 0751); err != nil {
		return nil, err
B
Bas van Kervel 已提交
51
	}
52 53 54 55
	os.Remove(endpoint)
	l, err := net.Listen("unix", endpoint)
	if err != nil {
		return nil, err
B
Bas van Kervel 已提交
56
	}
57 58 59
	os.Chmod(endpoint, 0600)
	return l, nil
}
B
Bas van Kervel 已提交
60

61
// newIPCConnection will connect to a Unix socket on the given endpoint.
62 63
func newIPCConnection(ctx context.Context, endpoint string) (net.Conn, error) {
	return dialContext(ctx, "unix", endpoint)
B
Bas van Kervel 已提交
64
}