Skip to contentSkip to conversation

syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

PATCH New Petr Vorel 4 participants 15 messages Sep 29, 2017 – Oct 3, 2017 Library code

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

instead of tst_brk{,m}(TBROK, ...)

Patch was made thanks to Coccinelle.

---
 lib/tst_net.c                                      | 12 ++-----
 testcases/kernel/syscalls/accept/accept01.c        | 12 ++-----
 testcases/kernel/syscalls/connect/connect01.c      | 17 +++------
 .../kernel/syscalls/getpeername/getpeername01.c    |  7 ++--
 .../kernel/syscalls/getsockname/getsockname01.c    | 13 +++----
 .../kernel/syscalls/getsockopt/getsockopt01.c      | 13 +++----
 testcases/kernel/syscalls/listen/listen01.c        |  8 ++---
 testcases/kernel/syscalls/recv/recv01.c            | 17 +++------
 testcases/kernel/syscalls/recvfrom/recvfrom01.c    | 17 +++------
 testcases/kernel/syscalls/recvmsg/recvmsg01.c      | 23 +++---------
 testcases/kernel/syscalls/send/send01.c            | 17 +++------
 testcases/kernel/syscalls/sendfile/sendfile02.c    | 14 ++------
 testcases/kernel/syscalls/sendfile/sendfile04.c    | 14 ++------
 testcases/kernel/syscalls/sendfile/sendfile05.c    | 14 ++------
 testcases/kernel/syscalls/sendfile/sendfile06.c    | 13 ++-----
 testcases/kernel/syscalls/sendmsg/sendmsg01.c      | 42 ++++++----------------
 testcases/kernel/syscalls/sendto/sendto01.c        | 17 +++------
 .../kernel/syscalls/setsockopt/setsockopt01.c      | 13 +++----
 testcases/kernel/syscalls/sockioctl/sockioctl01.c  | 20 ++++-------
 19 files changed, 75 insertions(+), 228 deletions(-)
lib/tst_net.c +3 -9
index f842e94a6..326b92e6f 100644
@@ -25,6 +25,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 unsigned short tst_get_unused_port(void (cleanup_fn)(void),
 	unsigned short family, int type)
@@ -57,16 +58,9 @@ unsigned short tst_get_unused_port(void (cleanup_fn)(void),
 		return -1;
 	}
 
-	sock = socket(addr->sa_family, type, 0);
-	if (sock < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "socket failed");
-		return -1;
-	}
+	sock = SAFE_SOCKET(cleanup_fn, addr->sa_family, type, 0);
 
-	if (bind(sock, addr, slen) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "bind failed");
-		return -1;
-	}
+	SAFE_BIND(cleanup_fn, sock, addr, slen);
 
 	if (getsockname(sock, addr, &slen) == -1) {
 		tst_brkm(TBROK | TERRNO, cleanup_fn, "getsockname failed");
testcases/kernel/syscalls/accept/accept01.c +3 -9
index 986415897..b50056520 100644
@@ -156,15 +156,9 @@ static void cleanup0(void)
 
 static void setup1(void)
 {
-	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
-	if (s < 0) {
-		tst_brkm(TBROK, cleanup, "socket setup failed for accept "
-			 "test %d: %s", testno, strerror(errno));
-	}
-	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
-		tst_brkm(TBROK, cleanup, "socket bind failed for accept "
-			 "test %d: %s", testno, strerror(errno));
-	}
+	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
+			tdat[testno].proto);
+	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
 	sinlen = sizeof(fsin1);
 }
 
testcases/kernel/syscalls/connect/connect01.c +4 -13
index af6035435..5880b0794 100644
@@ -53,6 +53,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "connect01";
 int testno;
@@ -230,19 +231,9 @@ pid_t start_server(struct sockaddr_in *sin0)
 	sin0->sin_port = 0; /* pick random free port */
 	sin0->sin_addr.s_addr = INADDR_ANY;
 
-	sfd = socket(PF_INET, SOCK_STREAM, 0);
-	if (sfd < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
-		return -1;
-	}
-	if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
-		return -1;
-	}
-	if (listen(sfd, 10) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
-		return -1;
-	}
+	sfd = SAFE_SOCKET(cleanup, PF_INET, SOCK_STREAM, 0);
+	SAFE_BIND(cleanup, sfd, (struct sockaddr *)sin0, sizeof(*sin0));
+	SAFE_LISTEN(cleanup, sfd, 10);
 	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
 
testcases/kernel/syscalls/getpeername/getpeername01.c +2 -5
index fd5d58d57..817cd38ad 100644
@@ -153,11 +153,8 @@ static void setup3(int i)
 		tst_brkm(TBROK | TERRNO, cleanup,
 			 "socket setup failed for getpeername test %d", i);
 	}
-	if (bind(test_cases[i].sockfd, (struct sockaddr *)&server_addr,
-		 sizeof(server_addr)) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "socket bind failed for getpeername test %d", i);
-	}
+	SAFE_BIND(cleanup, test_cases[i].sockfd,
+		  (struct sockaddr *)&server_addr, sizeof(server_addr));
 }
 
 static void setup4(int i)
testcases/kernel/syscalls/getsockname/getsockname01.c +4 -9
index c4b6e9b1a..cce1543fd 100644
@@ -52,6 +52,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "getsockname01";
 int testno;
@@ -167,15 +168,9 @@ void cleanup0(void)
 
 void setup1(void)
 {
-	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
-	if (s < 0) {
-		tst_brkm(TBROK, cleanup, "socket setup failed for getsockname "
-			 "test %d: %s", testno, strerror(errno));
-	}
-	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
-		tst_brkm(TBROK, cleanup, "socket bind failed for getsockname "
-			 "test %d: %s", testno, strerror(errno));
-	}
+	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
+			tdat[testno].proto);
+	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
 	sinlen = sizeof(fsin1);
 }
 
testcases/kernel/syscalls/getsockopt/getsockopt01.c +4 -9
index 1195306a0..d1692fcd3 100644
@@ -52,6 +52,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "getsockopt01";
 int testno;
@@ -193,15 +194,9 @@ void cleanup0(void)
 
 void setup1(void)
 {
-	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
-	if (s < 0) {
-		tst_brkm(TBROK, cleanup, "socket setup failed for getsockopt: "
-			 "%s", strerror(errno));
-	}
-	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
-		tst_brkm(TBROK, cleanup, "socket bind failed for getsockopt: "
-			 "%s", strerror(errno));
-	}
+	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
+			tdat[testno].proto);
+	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
 	sinlen = sizeof(fsin1);
 	optlen = sizeof(optval);
 }
testcases/kernel/syscalls/listen/listen01.c +3 -5
index e421bb46d..b3dd7125d 100644
@@ -53,6 +53,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "listen01";
 int testno;
@@ -140,11 +141,8 @@ void cleanup0(void)
 
 void setup1(void)
 {
-	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
-	if (s < 0) {
-		tst_brkm(TBROK, cleanup, "socket setup failed for listen: "
-			 "%s", strerror(errno));
-	}
+	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
+			tdat[testno].proto);
 }
 
 void cleanup1(void)
testcases/kernel/syscalls/recv/recv01.c +4 -13
index 9c21dc206..470cf9a19 100644
@@ -53,6 +53,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "recv01";
 int testno;
@@ -225,19 +226,9 @@ pid_t start_server(struct sockaddr_in *sin0)
 	sin0->sin_port = 0; /* pick random free port */
 	sin0->sin_addr.s_addr = INADDR_ANY;
 
-	sfd = socket(PF_INET, SOCK_STREAM, 0);
-	if (sfd < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
-		return -1;
-	}
-	if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
-		return -1;
-	}
-	if (listen(sfd, 10) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
-		return -1;
-	}
+	sfd = SAFE_SOCKET(cleanup, PF_INET, SOCK_STREAM, 0);
+	SAFE_BIND(cleanup, sfd, (struct sockaddr *)sin0, sizeof(*sin0));
+	SAFE_LISTEN(cleanup, sfd, 10);
 	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
 
testcases/kernel/syscalls/recvfrom/recvfrom01.c +4 -13
index 3bcf2a1fc..d95c28f9a 100644
@@ -53,6 +53,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "recvfrom01";
 int testno;
@@ -254,19 +255,9 @@ pid_t start_server(struct sockaddr_in *sin0)
 	sin0->sin_port = 0; /* pick random free port */
 	sin0->sin_addr.s_addr = INADDR_ANY;
 
-	sfd = socket(PF_INET, SOCK_STREAM, 0);
-	if (sfd < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
-		return -1;
-	}
-	if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
-		return -1;
-	}
-	if (listen(sfd, 10) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
-		return -1;
-	}
+	sfd = SAFE_SOCKET(cleanup, PF_INET, SOCK_STREAM, 0);
+	SAFE_BIND(cleanup, sfd, (struct sockaddr *)sin0, sizeof(*sin0));
+	SAFE_LISTEN(cleanup, sfd, 10);
 	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
 
testcases/kernel/syscalls/recvmsg/recvmsg01.c +5 -18
index 13bd58f4b..51b2a0061 100644
@@ -56,6 +56,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "recvmsg01";
 int testno;
@@ -374,28 +375,14 @@ pid_t start_server(struct sockaddr_in *ssin, struct sockaddr_un *ssun)
 	ssin->sin_addr.s_addr = INADDR_ANY;
 
 	/* set up inet socket */
-	sfd = socket(PF_INET, SOCK_STREAM, 0);
-	if (sfd < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
-		return -1;
-	}
-	if (bind(sfd, (struct sockaddr *)ssin, sizeof(*ssin)) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
-		return -1;
-	}
-	if (listen(sfd, 10) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
-		return -1;
-	}
+	sfd = SAFE_SOCKET(cleanup, PF_INET, SOCK_STREAM, 0);
+	SAFE_BIND(cleanup, sfd, (struct sockaddr *)ssin, sizeof(*ssin));
+	SAFE_LISTEN(cleanup, sfd, 10);
 	if (getsockname(sfd, (struct sockaddr *)ssin, &slen) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
 
 	/* set up UNIX-domain socket */
-	ufd = socket(PF_UNIX, SOCK_STREAM, 0);
-	if (ufd < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server UD socket failed");
-		return -1;
-	}
+	ufd = SAFE_SOCKET(cleanup, PF_UNIX, SOCK_STREAM, 0);
 	if (bind(ufd, (struct sockaddr *)ssun, sizeof(*ssun))) {
 		tst_brkm(TBROK | TERRNO, cleanup, "server UD bind failed");
 		return -1;
testcases/kernel/syscalls/send/send01.c +4 -13
index 6f71300ce..cef0c90a0 100644
@@ -42,6 +42,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "send01";
 int testno;
@@ -169,19 +170,9 @@ static pid_t start_server(struct sockaddr_in *sin0)
 	sin0->sin_port = 0; /* pick random free port */
 	sin0->sin_addr.s_addr = INADDR_ANY;
 
-	sfd = socket(PF_INET, SOCK_STREAM, 0);
-	if (sfd < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
-		return -1;
-	}
-	if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
-		return -1;
-	}
-	if (listen(sfd, 10) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
-		return -1;
-	}
+	sfd = SAFE_SOCKET(cleanup, PF_INET, SOCK_STREAM, 0);
+	SAFE_BIND(cleanup, sfd, (struct sockaddr *)sin0, sizeof(*sin0));
+	SAFE_LISTEN(cleanup, sfd, 10);
 	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
 
testcases/kernel/syscalls/sendfile/sendfile02.c +3 -11
index ba12308b3..37680839f 100644
@@ -57,6 +57,7 @@
 #include <unistd.h>
 #include <inttypes.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #ifndef OFF_T
 #define OFF_T off_t
@@ -214,21 +215,12 @@ int create_server(void)
 	static int count = 0;
 	socklen_t slen = sizeof(sin1);
 
-	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
-	if (sockfd < 0) {
-		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
+	sockfd = SAFE_SOCKET(cleanup, PF_INET, SOCK_DGRAM, 0);
 	sin1.sin_family = AF_INET;
 	sin1.sin_port = 0; /* pick random free port */
 	sin1.sin_addr.s_addr = INADDR_ANY;
 	count++;
-	if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
-		tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
+	SAFE_BIND(cleanup, sockfd, (struct sockaddr *)&sin1, sizeof(sin1));
 	if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
 
testcases/kernel/syscalls/sendfile/sendfile04.c +3 -11
index 822c4688c..45c15df50 100644
@@ -57,6 +57,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #ifndef OFF_T
 #define OFF_T off_t
@@ -212,21 +213,12 @@ int create_server(void)
 	static int count = 0;
 	socklen_t slen = sizeof(sin1);
 
-	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
-	if (sockfd < 0) {
-		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
+	sockfd = SAFE_SOCKET(cleanup, PF_INET, SOCK_DGRAM, 0);
 	sin1.sin_family = AF_INET;
 	sin1.sin_port = 0; /* pick random free port */
 	sin1.sin_addr.s_addr = INADDR_ANY;
 	count++;
-	if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
-		tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
+	SAFE_BIND(cleanup, sockfd, (struct sockaddr *)&sin1, sizeof(sin1));
 	if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
 
testcases/kernel/syscalls/sendfile/sendfile05.c +3 -11
index 110194b84..d3222c19a 100644
@@ -52,6 +52,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #ifndef OFF_T
 #define OFF_T off_t
@@ -176,21 +177,12 @@ int create_server(void)
 	static int count = 0;
 	socklen_t slen = sizeof(sin1);
 
-	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
-	if (sockfd < 0) {
-		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
+	sockfd = SAFE_SOCKET(cleanup, PF_INET, SOCK_DGRAM, 0);
 	sin1.sin_family = AF_INET;
 	sin1.sin_port = 0; /* pick random free port */
 	sin1.sin_addr.s_addr = INADDR_ANY;
 	count++;
-	if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
-		tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
+	SAFE_BIND(cleanup, sockfd, (struct sockaddr *)&sin1, sizeof(sin1));
 	if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
 
testcases/kernel/syscalls/sendfile/sendfile06.c +2 -11
index 794519549..b77c193a4 100644
@@ -154,21 +154,12 @@ static int create_server(void)
 	int s;
 	socklen_t slen = sizeof(sin1);
 
-	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
-	if (sockfd < 0) {
-		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
+	sockfd = SAFE_SOCKET(cleanup, PF_INET, SOCK_DGRAM, 0);
 	sin1.sin_family = AF_INET;
 	sin1.sin_port = 0; /* pick random free port */
 	sin1.sin_addr.s_addr = INADDR_ANY;
 
-	if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
-		tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
+	SAFE_BIND(cleanup, sockfd, (struct sockaddr *)&sin1, sizeof(sin1));
 	if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
 
testcases/kernel/syscalls/sendmsg/sendmsg01.c +10 -32

78 lines not on this page · raw message

testcases/kernel/syscalls/sendto/sendto01.c +4 -13
index ec0766197..7753a4ffb 100644
@@ -41,6 +41,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "sendto01";
 int testno;
@@ -239,19 +240,9 @@ static pid_t start_server(struct sockaddr_in *sin0)
 	sin0->sin_port = 0; /* pick random free port */
 	sin0->sin_addr.s_addr = INADDR_ANY;
 
-	sfd = socket(PF_INET, SOCK_STREAM, 0);
-	if (sfd < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
-		return -1;
-	}
-	if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
-		return -1;
-	}
-	if (listen(sfd, 10) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
-		return -1;
-	}
+	sfd = SAFE_SOCKET(cleanup, PF_INET, SOCK_STREAM, 0);
+	SAFE_BIND(cleanup, sfd, (struct sockaddr *)sin0, sizeof(*sin0));
+	SAFE_LISTEN(cleanup, sfd, 10);
 	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
 
testcases/kernel/syscalls/setsockopt/setsockopt01.c +4 -9
index f25694a96..743c6be14 100644
@@ -54,6 +54,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "setsockopt01";
 int testno;
@@ -200,15 +201,9 @@ void cleanup0(void)
 
 void setup1(void)
 {
-	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
-	if (s < 0) {
-		tst_brkm(TBROK, cleanup, "socket setup failed for setsockopt:"
-			 " %s", strerror(errno));
-	}
-	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
-		tst_brkm(TBROK, cleanup, "socket bind failed for setsockopt:"
-			 " %s", strerror(errno));
-	}
+	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
+			tdat[testno].proto);
+	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
 }
 
 void cleanup1(void)
testcases/kernel/syscalls/sockioctl/sockioctl01.c +6 -14
index 50bdc935a..096f138a9 100644
@@ -40,6 +40,7 @@
 #include <net/if.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "sockioctl01";
 int testno;
@@ -204,15 +205,9 @@ static void cleanup0(void)
 
 static void setup1(void)
 {
-	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
-	if (s < 0) {
-		tst_brkm(TBROK, cleanup, "socket setup failed: %s",
-			 strerror(errno));
-	}
-	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
-		tst_brkm(TBROK, cleanup, "socket bind failed for: %s",
-			 strerror(errno));
-	}
+	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
+			tdat[testno].proto);
+	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
 	sinlen = sizeof(fsin1);
 
 	if (strncmp(tdat[testno].desc, "ATMARK on UDP", 14) == 0) {
@@ -223,11 +218,8 @@ static void setup1(void)
 
 static void setup2(void)
 {
-	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
-	if (s < 0) {
-		tst_brkm(TBROK, cleanup, "socket setup failed: %s",
-			 strerror(errno));
-	}
+	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
+			tdat[testno].proto);
 	ifc.ifc_len = sizeof(buf);
 	ifc.ifc_buf = buf;
 }
Signature · 1 line
2.14.1
author
#1 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Quoted 6 lines

instead of tst_brk{,m}(TBROK, ...)

Patch was made thanks to Coccinelle.

Please ignore second sign.

<snip>

Quoted 18 lines
lib/tst_net.c +1 -2
index f842e94a6..326b92e6f 100644
@@ -25,6 +25,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 unsigned short tst_get_unused_port(void (cleanup_fn)(void),
 	unsigned short family, int type)
@@ -57,16 +58,9 @@ unsigned short tst_get_unused_port(void (cleanup_fn)(void),
 		return -1;
 	}
 
-	sock = socket(addr->sa_family, type, 0);
-	if (sock < 0) {

lib/tst_net.c unsigned short tst_get_unused_port(void (cleanup_fn)(void), · line 63

Quoted 10 lines
lib/tst_net.c +2 -7
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "socket failed");
-		return -1;
-	}
+	sock = SAFE_SOCKET(cleanup_fn, addr->sa_family, type, 0);
 
-	if (bind(sock, addr, slen) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup_fn, "bind failed");
-		return -1;
-	}
+	SAFE_BIND(cleanup_fn, sock, addr, slen);

I suppose tst_brkm(TBROK, ...) always exit testing, so we can have SAFE_* macros even here in lib.

Kind regards,
Petr

#2 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

In reply to Petr Vorel

Hello Petr,

Quoted 4 lines · Petr Vorel

instead of tst_brk{,m}(TBROK, ...)

Patch was made thanks to Coccinelle.

Very good. Also how about including the Coccinelle patch in the tools or scripts folder?

Signature · 2 lines
Thank you,
Richard.
author
#3 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Hi Richard, Cyril,

Quoted 5 lines

Hello Petr,

Quoted 1 line

Patch was made thanks to Coccinelle.

Very good. Also how about including the Coccinelle patch in the tools or scripts folder?

Cyril, do you like this idea?

Well, I'm not much proud on that script, it's my first Coccinelle script :-)

@test_h@
@@
#include "test.h"

@tst_test_h@
@@
#include "tst_test.h"

// test.h does not include safe_macros.h
@safe_macros_h@
@@
#include "safe_macros.h"

@is_safe_socket@
@@
int safe_socket(...) {...}

// SAFE_BIND
@old_safe_bind depends on test_h@
expression sockfd, sockaddr, addrlen;
identifier cleanup_fn;
expression e, x;
constant TFAIL;
@@
-if (bind(sockfd, sockaddr, addrlen) < 0) {
- tst_brkm(e, cleanup_fn, ...);
(
- return -1;
|
)

	... when != x & TFAIL

-}
+SAFE_BIND(cleanup_fn, sockfd, sockaddr, addrlen);

@safe_bind depends on tst_test_h@
expression sockfd, sockaddr, addrlen;
expression e, x;
constant TFAIL;
@@
-if (bind(sockfd, sockaddr, addrlen) < 0) {
- tst_brk(e, ...);
(
- return -1;
|
)

	... when != x & TFAIL

-}
+SAFE_BIND(sockfd, sockaddr, addrlen);

// SAFE_SOCKET
@old_safe_socket depends on test_h && !is_safe_socket@ expression family, type, protocol;
identifier fd;
identifier cleanup_fn;
expression e, x;
constant TFAIL;
@@
-fd = socket(family, type, protocol);
-if (fd < 0) {
- tst_brkm(e, cleanup_fn, ...);
(
- return -1;
|
)

	... when != x & TFAIL

-}
+fd = SAFE_SOCKET(cleanup_fn, family, type, protocol);

@safe_socket depends on tst_test_h && !is_safe_socket@ expression family, type, protocol;
identifier fd;
expression e, x;
constant TFAIL;
@@
-fd = socket(family, type, protocol);
-if (fd < 0) {
- tst_brk(e, ...);
(
- return -1;
|
)

	... when != x & TFAIL

-}
+fd = SAFE_SOCKET(family, type, protocol);

// SAFE_FSETXATTR
// not used so far, not in old
@safe_fsetxattr depends on tst_test_h@
expression fd, name, value, flags, size;
identifier r;
expression e, x;
constant TFAIL;
@@
-r = fsetxattr(fd, name, value, size, flags);
-if (r < 0) {
- tst_brk(e, ...);
(
- return -1;
|
)

	... when != x & TFAIL

-}
+SAFE_FSETXATTR(fd, name, value, size, flags);

// SAFE_LISTEN
@safe_listen depends on tst_test_h@
expression backlog;
identifier socket;
identifier r;
expression e, x;
constant TFAIL;
@@
-r = listen(socket, backlog); if (r < 0) {
- tst_brk(e, ...);
(
- return -1;
|
)

	... when != x & TFAIL

-}
+SAFE_LISTEN(socket, backlog);

@safe_listen2 depends on tst_test_h@
expression backlog;
identifier socket;
identifier r;
expression e, x;
constant TFAIL;
@@
-if (listen(socket, backlog) < 0) {
- tst_brk(e, ...);
(
- return -1;
|
)

	... when != x & TFAIL

-}
+SAFE_LISTEN(socket, backlog);

@old_safe_listen depends on test_h@
expression backlog;
identifier socket, cleanup_fn;
identifier r;
expression e, x;
constant TFAIL;
@@
-r = listen(socket, backlog); if (r < 0) {
- tst_brk(e, ...);
(
- return -1;
|
)

	... when != x & TFAIL

-}
+SAFE_LISTEN(cleanup_fn, socket, backlog);

@old_safe_listen2 depends on test_h@
expression backlog;
identifier socket, cleanup_fn;
identifier r;
expression e, x;
constant TFAIL;
@@
-if (listen(socket, backlog) < 0) {
- tst_brkm(TBROK | TERRNO, cleanup_fn, "server listen failed");
- return -1;
- }
+SAFE_LISTEN(cleanup_fn, socket, backlog);

// SAFE_SENDTO
// not used so far, not in old
@safe_sendto depends on tst_test_h@ expression fd, buf, len, flags, dest_addr, addrlen; identifier r;
expression e, x;
constant TFAIL;
@@
-r = sendto(fd, buf, len, flags, dest_addr, addrlen);

-if (r < 0) {
- tst_brk(e, ...);
(
- return -1;
|
)

	... when != x & TFAIL

-}
+SAFE_SENDTO(1, fd, buf, len, flags, dest_addr, addrlen);

// INCLUDE
@depends on !safe_macros_h && (old_safe_bind || old_safe_listen || old_safe_listen2 || old_safe_socket)@ @@
#include "test.h"
+#include "safe_macros.h"

#4 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Hello,

Quoted 4 lines · Petr Vorel

Hi Richard, Cyril,

Quoted 1 line

Hello Petr,

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Patch was made thanks to Coccinelle.

Very good. Also how about including the Coccinelle patch in the tools or scripts folder?

Cyril, do you like this idea?

Well, I'm not much proud on that script, it's my first Coccinelle script :-)

It is more advanced than the one I created to do something similar.

Quoted 31 lines

@test_h@
@@
#include "test.h"

@tst_test_h@
@@
#include "tst_test.h"

// test.h does not include safe_macros.h
@safe_macros_h@
@@
#include "safe_macros.h"

@is_safe_socket@
@@
int safe_socket(...) {...}

// SAFE_BIND
@old_safe_bind depends on test_h@
expression sockfd, sockaddr, addrlen;
identifier cleanup_fn;
expression e, x;
constant TFAIL;
@@

(quoted diff) +0 -2
-if (bind(sockfd, sockaddr, addrlen) < 0) {
-	tst_brkm(e, cleanup_fn, ...);

(

(quoted diff) +0 -1
-	return -1;

|
)

Quoted 1 line
	... when != x & TFAIL

What does the last line do? I am guessing it prevents the substitution if it finds TFAIL anywhere in the above expression.

Signature · 2 lines
Thank you,
Richard.
author
#5 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Hi Richard,

Quoted 14 lines

It is more advanced than the one I created to do something similar.

Quoted 11 lines

// SAFE_BIND
@old_safe_bind depends on test_h@
expression sockfd, sockaddr, addrlen;
identifier cleanup_fn;
expression e, x;
constant TFAIL;
@@

(quoted diff) +0 -2
-if (bind(sockfd, sockaddr, addrlen) < 0) {
-	tst_brkm(e, cleanup_fn, ...);

(

(quoted diff) +0 -1
-	return -1;
Quoted 5 lines
Quoted 2 lines

)

	... when != x & TFAIL

What does the last line do? I am guessing it prevents the substitution if it finds TFAIL anywhere in the above expression.

Precisely :-).

Kind regards,
Petr

#6 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Hi!

Quoted 12 lines
Quoted 11 lines
Quoted 11 lines

// SAFE_BIND
@old_safe_bind depends on test_h@
expression sockfd, sockaddr, addrlen;
identifier cleanup_fn;
expression e, x;
constant TFAIL;
@@

(quoted diff) +0 -2
-if (bind(sockfd, sockaddr, addrlen) < 0) {
-	tst_brkm(e, cleanup_fn, ...);

(

(quoted diff) +0 -1
-	return -1;
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

)

	... when != x & TFAIL

What does the last line do? I am guessing it prevents the substitution if it finds TFAIL anywhere in the above expression.

Precisely :-).

Shouldn't that be when != e & TFAIL? As far as I can tell the x is not used in the matching part, or did I miss something?

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#7 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Quoted 9 lines

Hi!

Quoted 5 lines
Quoted 4 lines
Quoted 1 line
	... when != x & TFAIL

What does the last line do? I am guessing it prevents the substitution if it finds TFAIL anywhere in the above expression.

Precisely :-).

Shouldn't that be when != e & TFAIL? As far as I can tell the x is not used in the matching part, or did I miss something?

Sure, you're right, Cyril. Now I got your question Richard. I'm surprised that it work with x (result is the same).

Kind regards,
Petr

#8 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Hi!

Quoted 4 lines
Quoted 2 lines

Shouldn't that be when != e & TFAIL? As far as I can tell the x is not used in the matching part, or did I miss something?

Sure, you're right, Cyril. Now I got your question Richard. I'm surprised that it work with x (result is the same).

I guess that this condition simply was always true, and it does not work either way.

Anyway I got an idea yesterday and implemented a shell script that generates a spatch based on function name and made it iterate over all safe macros we have implemented making a git commit for each. The script is attached for a reference.

I will look at the patches and do some minor adjustements now, there are a few cases where the formatting ended up a bit confusing.

And I would consider these to be obvious enough to be commited without review, but I can send them to ML if anyone wants to double check the results.

Signature · 9 lines
Cyril Hrubis
[email protected]
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch_safe_macros.sh
Type: application/x-sh
Size: 2280 bytes
Desc: not available
URL: <http://lists.linux.it/pipermail/ltp/attachments/20171003/23122597/attachment.sh>
author
#9 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Hi Cyril,

Quoted 4 lines

Anyway I got an idea yesterday and implemented a shell script that generates a spatch based on function name and made it iterate over all safe macros we have implemented making a git commit for each. The script is attached for a reference.

Really nice solution :-).
This script is really worth of adding into LTP (even it requires to check formatting).

Kind regards,
Petr

#10 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Hi!

Quoted 6 lines
Quoted 4 lines

Anyway I got an idea yesterday and implemented a shell script that generates a spatch based on function name and made it iterate over all safe macros we have implemented making a git commit for each. The script is attached for a reference.

Really nice solution :-).
This script is really worth of adding into LTP (even it requires to check formatting).

I will strip the old library parts, then we can add it, maybe we can even use it as a commit hook or something to automatically check if we could have used a SAFE_MACRO() instead of custom error handling...

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#11 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Quoted 11 lines

Hi!

Quoted 6 lines
Quoted 4 lines

Anyway I got an idea yesterday and implemented a shell script that generates a spatch based on function name and made it iterate over all safe macros we have implemented making a git commit for each. The script is attached for a reference.

Really nice solution :-).
This script is really worth of adding into LTP (even it requires to check formatting).

I will strip the old library parts, then we can add it, maybe we can even use it as a commit hook or something to automatically check if we could have used a SAFE_MACRO() instead of custom error handling...

Good idea. What a shame that post-receive hook on GitHub (aka "WebHooks") do not allow to execute anything on server side...

Kind regards,
Petr

#12 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

In reply to Cyril Hrubis

----- Original Message -----

Quoted 5 lines

Hi!

Quoted 4 lines
Quoted 4 lines

Anyway I got an idea yesterday and implemented a shell script that generates a spatch based on function name and made it iterate over all safe macros we have implemented making a git commit for each. The script is attached for a reference.

Quoted 8 lines
Quoted 3 lines

Really nice solution :-).
This script is really worth of adding into LTP (even it requires to check formatting).

I will strip the old library parts, then we can add it, maybe we can even use it as a commit hook or something to automatically check if we could have used a SAFE_MACRO() instead of custom error handling...

As I was looking at changes, I came across:
tst_brkm(TFAIL | TERRNO, ...);

I guess most of these tst_brk TFAILs should be TBROKs, but that would be likely difficult to script.

Regards,
Jan

#13 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

Hi!

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

Really nice solution :-).
This script is really worth of adding into LTP (even it requires to check formatting).

I will strip the old library parts, then we can add it, maybe we can even use it as a commit hook or something to automatically check if we could have used a SAFE_MACRO() instead of custom error handling...

Quoted 5 lines

As I was looking at changes, I came across:
tst_brkm(TFAIL | TERRNO, ...);

I guess most of these tst_brk TFAILs should be TBROKs, but that would be likely difficult to script.

Yes, we would have to review which of these are real TFAILs manually, which could be done, but would be quite time consuming. So I stuck with changes I can formally prove are equal.

Signature · 2 lines
Cyril Hrubis
[email protected]
#14 lore raw

PATCH 1/1 syscalls, lib: Use SAFE_{BIND, LISTEN, SOCKET} macros

In reply to Petr Vorel

Hi!

Quoted 6 lines
Quoted 4 lines

Anyway I got an idea yesterday and implemented a shell script that generates a spatch based on function name and made it iterate over all safe macros we have implemented making a git commit for each. The script is attached for a reference.

Really nice solution :-).
This script is really worth of adding into LTP (even it requires to check formatting).

FYI: I've missed a pattern where the result is stored in a variable
first hence things like SAFE_OPEN() haven't had chance to be used at all, I will fix that one and rerun the script.

Signature · 2 lines
Cyril Hrubis
[email protected]