Browse Source

bfg_socket wrapper to ensure sockets are close-on-exec

Luke Dashjr 11 years ago
parent
commit
9277d3c87c
4 changed files with 29 additions and 11 deletions
  1. 3 7
      api.c
  2. 1 1
      miner.c
  3. 3 3
      util.c
  4. 22 0
      util.h

+ 3 - 7
api.c

@@ -3859,9 +3859,7 @@ static void mcast()
 		quit(1, "Invalid Multicast Address");
 	grp.imr_interface.s_addr = INADDR_ANY;
 
-	mcast_sock = socket(AF_INET, SOCK_DGRAM, 0);
-
-	set_cloexec_socket(*mcastsock, true);
+	mcast_sock = bfg_socket(AF_INET, SOCK_DGRAM, 0);
 	
 	int optval = 1;
 	if (SOCKETFAIL(setsockopt(mcast_sock, SOL_SOCKET, SO_REUSEADDR, (void *)(&optval), sizeof(optval)))) {
@@ -3942,7 +3940,7 @@ static void mcast()
 							&buf[expect_code_len], reply_port);
 
 				came_from.sin_port = htons(reply_port);
-				reply_sock = socket(AF_INET, SOCK_DGRAM, 0);
+				reply_sock = bfg_socket(AF_INET, SOCK_DGRAM, 0);
 
 				snprintf(replybuf, sizeof(replybuf),
 							"cgm-%s-%d-%s",
@@ -4055,14 +4053,12 @@ void api(int api_thr_id)
 	 * to ensure curl has already called WSAStartup() in windows */
 	cgsleep_ms(opt_log_interval*1000);
 
-	*apisock = socket(AF_INET, SOCK_STREAM, 0);
+	*apisock = bfg_socket(AF_INET, SOCK_STREAM, 0);
 	if (*apisock == INVSOCK) {
 		applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
 		return;
 	}
 	
-	set_cloexec_socket(*apisock, true);
-
 	memset(&serv, 0, sizeof(serv));
 
 	serv.sin_family = AF_INET;

+ 1 - 1
miner.c

@@ -9026,7 +9026,7 @@ static void wait_lpcurrent(struct pool *pool)
 
 static curl_socket_t save_curl_socket(void *vpool, __maybe_unused curlsocktype purpose, struct curl_sockaddr *addr) {
 	struct pool *pool = vpool;
-	curl_socket_t sock = socket(addr->family, addr->socktype, addr->protocol);
+	curl_socket_t sock = bfg_socket(addr->family, addr->socktype, addr->protocol);
 	pool->lp_socket = sock;
 	return sock;
 }

+ 3 - 3
util.c

@@ -2086,7 +2086,7 @@ out:
 curl_socket_t grab_socket_opensocket_cb(void *clientp, __maybe_unused curlsocktype purpose, struct curl_sockaddr *addr)
 {
 	struct pool *pool = clientp;
-	curl_socket_t sck = socket(addr->family, addr->socktype, addr->protocol);
+	curl_socket_t sck = bfg_socket(addr->family, addr->socktype, addr->protocol);
 	pool->sock = sck;
 	return sck;
 }
@@ -2666,11 +2666,11 @@ void notifier_init(notifier_t pipefd)
 #ifdef WIN32
 #define WindowsErrorStr(e)  bfg_strerror(e, BST_SOCKET)
 	SOCKET listener, connecter, acceptor;
-	listener = socket(AF_INET, SOCK_STREAM, 0);
+	listener = bfg_socket(AF_INET, SOCK_STREAM, 0);
 	if (listener == INVALID_SOCKET)
 		quit(1, "Failed to create listener socket"IN_FMT_FFL": %s",
 		     __FILE__, __func__, __LINE__, WindowsErrorStr(WSAGetLastError()));
-	connecter = socket(AF_INET, SOCK_STREAM, 0);
+	connecter = bfg_socket(AF_INET, SOCK_STREAM, 0);
 	if (connecter == INVALID_SOCKET)
 		quit(1, "Failed to create connect socket"IN_FMT_FFL": %s",
 		     __FILE__, __func__, __LINE__, WindowsErrorStr(WSAGetLastError()));

+ 22 - 0
util.h

@@ -27,6 +27,7 @@
 
 #if defined(unix) || defined(__APPLE__)
 	#include <errno.h>
+	#include <sys/types.h>
 	#include <sys/socket.h>
 	#include <netinet/in.h>
 	#include <arpa/inet.h>
@@ -108,8 +109,29 @@ struct pool;
 enum dev_reason;
 struct cgpu_info;
 
+
 extern void set_cloexec_socket(SOCKETTYPE, bool cloexec);
 
+static inline
+SOCKETTYPE bfg_socket(const int domain, const int type, const int protocol)
+{
+	const bool cloexec = true;
+	SOCKETTYPE sock;
+#ifdef WIN32
+# ifndef WSA_FLAG_NO_HANDLE_INHERIT
+#  define WSA_FLAG_NO_HANDLE_INHERIT 0x80
+# endif
+	sock = WSASocket(domain, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED | ((cloexec) ? WSA_FLAG_NO_HANDLE_INHERIT : 0));
+	if (sock == INVSOCK)
+#endif
+	sock = socket(domain, type, protocol);
+	if (sock == INVSOCK)
+		return INVSOCK;
+	set_cloexec_socket(sock, cloexec);
+	return sock;
+}
+
+
 extern void json_rpc_call_async(CURL *, const char *url, const char *userpass, const char *rpc_req, bool longpoll, struct pool *pool, bool share, void *priv);
 extern json_t *json_rpc_call_completed(CURL *, int rc, bool probe, int *rolltime, void *out_priv);