aboutsummaryrefslogtreecommitdiffstats
path: root/main/uwsgi/0001-core-socket-common-socket-creation-function-for-bind.patch
blob: 45b861f500e9155d07dd1057b4ed7dce64c371fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
From 4d43a66581168674a9db52ac9bf690fa4f106dca Mon Sep 17 00:00:00 2001
From: Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>
Date: Wed, 1 Jan 2014 23:36:26 +0200
Subject: [PATCH 1/5] core/socket: common socket creation function for
 bind_to_*

---
 core/socket.c | 40 +++++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 23 deletions(-)

diff --git a/core/socket.c b/core/socket.c
index 0696eff..7e47dc0 100644
--- a/core/socket.c
+++ b/core/socket.c
@@ -76,18 +76,23 @@ char *uwsgi_getsockname(int fd) {
 	return NULL;
 }
 
+static int create_server_socket(int domain, int type) {
+	int serverfd = socket(domain, type, 0);
+	if (serverfd < 0) {
+		uwsgi_error("socket()");
+		uwsgi_nuclear_blast();
+	}
+	return serverfd;
+}
+
 int bind_to_unix_dgram(char *socket_name) {
 
 	int serverfd;
 	struct sockaddr_un *uws_addr;
 	socklen_t len;
 
-	serverfd = socket(AF_UNIX, SOCK_DGRAM, 0);
-	if (serverfd < 0) {
-		uwsgi_error("socket()");
-		uwsgi_nuclear_blast();
-		return -1;
-	}
+	serverfd = create_server_socket(AF_UNIX, SOCK_DGRAM);
+	if (serverfd < 0) return -1;
 
 	if (unlink(socket_name) != 0 && errno != ENOENT) {
 		uwsgi_error("error removing unix socket, unlink()");
@@ -140,12 +145,8 @@ int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abst
 	}
 
 	memset(uws_addr, 0, sizeof(struct sockaddr_un));
-	serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
-	if (serverfd < 0) {
-		uwsgi_error("socket()");
-		uwsgi_nuclear_blast();
-		return -1;
-	}
+	serverfd = create_server_socket(AF_UNIX, SOCK_STREAM);
+	if (serverfd < 0) return -1;
 	if (abstract_socket == 0) {
 		if (unlink(socket_name) != 0 && errno != ENOENT) {
 			uwsgi_error("unlink()");
@@ -288,11 +289,8 @@ int bind_to_udp(char *socket_name, int multicast, int broadcast) {
 	}
 
 
-	serverfd = socket(AF_INET, SOCK_DGRAM, 0);
-	if (serverfd < 0) {
-		uwsgi_error("socket()");
-		return -1;
-	}
+	serverfd = create_server_socket(AF_INET, SOCK_DGRAM);
+	if (serverfd < 0) return -1;
 
 	if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse, sizeof(int)) < 0) {
 		uwsgi_error("setsockopt()");
@@ -667,12 +665,8 @@ int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
 #endif
 
 
-	serverfd = socket(family, SOCK_STREAM, 0);
-	if (serverfd < 0) {
-		uwsgi_error("socket()");
-		uwsgi_nuclear_blast();
-		return -1;
-	}
+	serverfd = create_server_socket(family, SOCK_STREAM);
+	if (serverfd < 0) return -1;
 	
 	if (uwsgi.so_sndbuf) {
 		socklen_t sndbuf = (socklen_t) uwsgi.so_sndbuf;
-- 
1.8.4.2