All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] syscalls/sendmmsg: add new test
@ 2019-03-20  2:16 Steve Muckle
  2019-03-20 22:22 ` Petr Vorel
  2019-03-20 23:04 ` Petr Vorel
  0 siblings, 2 replies; 8+ messages in thread
From: Steve Muckle @ 2019-03-20  2:16 UTC (permalink / raw)
  To: ltp

Test basic functionality of sendmmsg and recvmmsg.

Signed-off-by: Steve Muckle <smuckle@google.com>
---
 runtest/syscalls                              |   2 +
 testcases/kernel/syscalls/sendmmsg/.gitignore |   1 +
 testcases/kernel/syscalls/sendmmsg/Makefile   |   9 ++
 .../kernel/syscalls/sendmmsg/sendmmsg01.c     | 123 ++++++++++++++++++
 4 files changed, 135 insertions(+)
 create mode 100644 testcases/kernel/syscalls/sendmmsg/.gitignore
 create mode 100644 testcases/kernel/syscalls/sendmmsg/Makefile
 create mode 100644 testcases/kernel/syscalls/sendmmsg/sendmmsg01.c

diff --git a/runtest/syscalls b/runtest/syscalls
index b0904086d..742b5bef3 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1095,6 +1095,8 @@ sendfile09_64 sendfile09_64
 sendmsg01 sendmsg01
 sendmsg02 sendmsg02
 
+sendmmsg01 sendmmsg01
+
 sendto01 sendto01
 sendto02 sendto02
 
diff --git a/testcases/kernel/syscalls/sendmmsg/.gitignore b/testcases/kernel/syscalls/sendmmsg/.gitignore
new file mode 100644
index 000000000..b703ececd
--- /dev/null
+++ b/testcases/kernel/syscalls/sendmmsg/.gitignore
@@ -0,0 +1 @@
+sendmmsg01
diff --git a/testcases/kernel/syscalls/sendmmsg/Makefile b/testcases/kernel/syscalls/sendmmsg/Makefile
new file mode 100644
index 000000000..47e063722
--- /dev/null
+++ b/testcases/kernel/syscalls/sendmmsg/Makefile
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+sendmmsg01: CFLAGS+=-pthread
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c b/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c
new file mode 100644
index 000000000..d1bdf40a1
--- /dev/null
+++ b/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c
@@ -0,0 +1,123 @@
+/*
+ * This test is based on source contained in the man pages for sendmmsg and
+ * recvmmsg in release 4.15 of the Linux man-pages project.
+ */
+
+#define _GNU_SOURCE
+#include <netinet/ip.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "tst_safe_pthread.h"
+
+#define BUFSIZE 16
+#define VLEN 2
+
+static void *sender_thread(LTP_ATTRIBUTE_UNUSED void *arg)
+{
+	struct sockaddr_in addr;
+	struct mmsghdr msg[2];
+	struct iovec msg1[2], msg2;
+	int send_sockfd;
+	int retval;
+
+	send_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+
+	addr.sin_family = AF_INET;
+	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+	addr.sin_port = htons(1234);
+	SAFE_CONNECT(send_sockfd, (struct sockaddr *) &addr, sizeof(addr));
+
+	memset(msg1, 0, sizeof(msg1));
+	msg1[0].iov_base = "one";
+	msg1[0].iov_len = 3;
+	msg1[1].iov_base = "two";
+	msg1[1].iov_len = 3;
+
+	memset(&msg2, 0, sizeof(msg2));
+	msg2.iov_base = "three";
+	msg2.iov_len = 5;
+
+	memset(msg, 0, sizeof(msg));
+	msg[0].msg_hdr.msg_iov = msg1;
+	msg[0].msg_hdr.msg_iovlen = 2;
+
+	msg[1].msg_hdr.msg_iov = &msg2;
+	msg[1].msg_hdr.msg_iovlen = 1;
+
+	retval = sendmmsg(send_sockfd, msg, 2, 0);
+	if (retval < 0)
+		tst_brk(TFAIL|TTERRNO, "sendmmsg failed");
+
+	return NULL;
+}
+
+
+static void *receiver_thread(LTP_ATTRIBUTE_UNUSED void *arg)
+{
+	int receive_sockfd;
+	struct sockaddr_in addr;
+	struct mmsghdr msgs[VLEN];
+	struct iovec iovecs[VLEN];
+	char bufs[VLEN][BUFSIZE+1];
+	struct timespec timeout;
+	int i, retval;
+
+	receive_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+	addr.sin_family = AF_INET;
+	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+	addr.sin_port = htons(1234);
+	SAFE_BIND(receive_sockfd, (struct sockaddr *)&addr, sizeof(addr));
+
+	memset(msgs, 0, sizeof(msgs));
+	for (i = 0; i < VLEN; i++) {
+		iovecs[i].iov_base = bufs[i];
+		iovecs[i].iov_len = BUFSIZE;
+		msgs[i].msg_hdr.msg_iov = &iovecs[i];
+		msgs[i].msg_hdr.msg_iovlen = 1;
+	}
+
+	timeout.tv_sec = 1;
+	timeout.tv_nsec = 0;
+
+	retval = recvmmsg(receive_sockfd, msgs, VLEN, 0, &timeout);
+	if (retval == -1)
+		tst_brk(TFAIL | TTERRNO, "recvmmsg failed");
+	if (retval != 2)
+		tst_brk(TFAIL, "Received unexpected number of messages (%d)",
+			retval);
+
+	bufs[0][msgs[0].msg_len] = 0;
+	if (strcmp(bufs[0], "onetwo"))
+		tst_res(TFAIL, "Error in first received message.");
+	else
+		tst_res(TPASS, "First message received successfully.");
+
+	bufs[1][msgs[1].msg_len] = 0;
+	if (strcmp(bufs[1], "three"))
+		tst_res(TFAIL, "Error in second received message.");
+	else
+		tst_res(TPASS, "Second message received successfully.");
+
+	return NULL;
+}
+
+static void run(void)
+{
+	pthread_t sender;
+	pthread_t receiver;
+
+	SAFE_PTHREAD_CREATE(&sender, NULL, sender_thread, NULL);
+	SAFE_PTHREAD_CREATE(&receiver, NULL, receiver_thread, NULL);
+	SAFE_PTHREAD_JOIN(sender, NULL);
+	SAFE_PTHREAD_JOIN(receiver, NULL);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
-- 
2.21.0.225.g810b269d1ac-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [LTP] [PATCH] syscalls/sendmmsg: add new test
  2019-03-20  2:16 [LTP] [PATCH] syscalls/sendmmsg: add new test Steve Muckle
@ 2019-03-20 22:22 ` Petr Vorel
  2019-03-20 23:04 ` Petr Vorel
  1 sibling, 0 replies; 8+ messages in thread
From: Petr Vorel @ 2019-03-20 22:22 UTC (permalink / raw)
  To: ltp

Hi Steve,

> Test basic functionality of sendmmsg and recvmmsg.

Nice.
Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Petr

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [LTP] [PATCH] syscalls/sendmmsg: add new test
  2019-03-20  2:16 [LTP] [PATCH] syscalls/sendmmsg: add new test Steve Muckle
  2019-03-20 22:22 ` Petr Vorel
@ 2019-03-20 23:04 ` Petr Vorel
  2019-03-20 23:57   ` Steve Muckle
  1 sibling, 1 reply; 8+ messages in thread
From: Petr Vorel @ 2019-03-20 23:04 UTC (permalink / raw)
  To: ltp

Hi Steve,

> Test basic functionality of sendmmsg and recvmmsg.
...

> +	retval = sendmmsg(send_sockfd, msg, 2, 0);
Unfortunately we need to have autotools check for sendmmsg() [1]
to fix failure on old distros
/usr/src/ltp/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c   -lltp -o sendmmsg01
/usr/src/ltp/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c: In function 'sender_thread':
/usr/src/ltp/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c:53: warning: implicit declaration of function 'sendmmsg'
/tmp/ccqXXRS6.o: In function `sender_thread':
/usr/src/ltp/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c:53: undefined reference to `sendmmsg'

> +	if (retval < 0)
> +		tst_brk(TFAIL|TTERRNO, "sendmmsg failed");
> +
> +	return NULL;
> +}


Kind regards,
Petr

[1] https://api.travis-ci.org/v3/job/509170474/log.txt

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [LTP] [PATCH] syscalls/sendmmsg: add new test
  2019-03-20 23:04 ` Petr Vorel
@ 2019-03-20 23:57   ` Steve Muckle
  0 siblings, 0 replies; 8+ messages in thread
From: Steve Muckle @ 2019-03-20 23:57 UTC (permalink / raw)
  To: ltp

On 03/20/2019 04:04 PM, Petr Vorel wrote:
>> +	retval = sendmmsg(send_sockfd, msg, 2, 0);
> Unfortunately we need to have autotools check for sendmmsg() [1]
> to fix failure on old distros
> /usr/src/ltp/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c   -lltp -o sendmmsg01
> /usr/src/ltp/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c: In function 'sender_thread':
> /usr/src/ltp/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c:53: warning: implicit declaration of function 'sendmmsg'
> /tmp/ccqXXRS6.o: In function `sender_thread':
> /usr/src/ltp/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c:53: undefined reference to `sendmmsg'

Thanks Petr for your review and pointing me to the issue. I will add a 
config check for this.

thanks,
Steve

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [LTP] [PATCH] syscalls/sendmmsg: add new test
  2019-07-29  9:01   ` Cyril Hrubis
@ 2019-07-29  9:31     ` Petr Vorel
  0 siblings, 0 replies; 8+ messages in thread
From: Petr Vorel @ 2019-07-29  9:31 UTC (permalink / raw)
  To: ltp

Hi Cyril, Steve,

> > > Changes since v3:
> > >  - drop resending of messages on partially successful sendmmsg()
> > >  - drop use of pthreads, do message creation and port management in
> > >    setup/cleanup

> > The only thing left in v4 is duplicate LTP_CHECK_MMSGHDR call.
> > + I'd use lapi/socket.h also in cve-2016-7117.c (as Cyril pointed out)
> > in this commit (this change is simple enough).

> > So I'd be for merging v4 with diff below.

> There is also leftover -pthread in CFLAGS in Makefile since we dropped
> pthreads from the test. Other than that and the minor issues you have
> pointed it looks good to me as well.

Cyril, thanks for pointing this out. Merged with these fixes and your ack.

Kind regards,
Petr

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [LTP] [PATCH] syscalls/sendmmsg: add new test
  2019-07-29  8:56 ` Petr Vorel
@ 2019-07-29  9:01   ` Cyril Hrubis
  2019-07-29  9:31     ` Petr Vorel
  0 siblings, 1 reply; 8+ messages in thread
From: Cyril Hrubis @ 2019-07-29  9:01 UTC (permalink / raw)
  To: ltp

Hi!
> > Signed-off-by: Steve Muckle <smuckle@google.com>
> Acked-by: Petr Vorel <pvorel@suse.cz>
> 
> > ---
> 
> > Changes since v3:
> >  - drop resending of messages on partially successful sendmmsg()
> >  - drop use of pthreads, do message creation and port management in
> >    setup/cleanup
> 
> The only thing left in v4 is duplicate LTP_CHECK_MMSGHDR call.
> + I'd use lapi/socket.h also in cve-2016-7117.c (as Cyril pointed out)
> in this commit (this change is simple enough).
> 
> So I'd be for merging v4 with diff below.

There is also leftover -pthread in CFLAGS in Makefile since we dropped
pthreads from the test. Other than that and the minor issues you have
pointed it looks good to me as well.

> Kind regards,
> Petr
> 
> diff --git configure.ac configure.ac
> index 5e4e7f1f9..f7d1afc40 100644
> --- configure.ac
> +++ configure.ac
> @@ -255,7 +255,6 @@ LTP_CHECK_TIME
>  LTP_CHECK_TIMERFD
>  test "x$with_tirpc" = xyes && LTP_CHECK_TIRPC
>  LTP_CHECK_TPACKET_V3
> -LTP_CHECK_MMSGHDR
>  LTP_CHECK_UNAME_DOMAINNAME
>  LTP_CHECK_XFS_QUOTACTL
>  LTP_CHECK_X_TABLES
> diff --git testcases/cve/cve-2016-7117.c testcases/cve/cve-2016-7117.c
> index f0f6c22f1..140839712 100644
> --- testcases/cve/cve-2016-7117.c
> +++ testcases/cve/cve-2016-7117.c
> @@ -30,6 +30,8 @@
>   * https://blog.lizzie.io/notes-about-cve-2016-7117.html
>   */
>  
> +#include "config.h"
> +
>  #include <sys/wait.h>
>  #include <sys/types.h>
>  #include <sys/socket.h>
> @@ -44,20 +46,12 @@
>  
>  /* The bug was present in the kernel before recvmmsg was exposed by glibc */
>  #include "lapi/syscalls.h"
> -
> -#include "config.h"
> +#include "lapi/socket.h"
>  
>  #define MSG "abcdefghijklmnop"
>  #define RECV_TIMEOUT 1
>  #define ATTEMPTS 0x1FFFFF
>  
> -#ifndef HAVE_STRUCT_MMSGHDR
> -struct mmsghdr {
> -	struct msghdr msg_hdr;
> -	unsigned int msg_len;
> -};
> -#endif
> -
>  static volatile int socket_fds[2];
>  static struct mmsghdr msghdrs[2] = {
>  	{

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [LTP] [PATCH] syscalls/sendmmsg: add new test
  2019-07-25 21:11 Steve Muckle
@ 2019-07-29  8:56 ` Petr Vorel
  2019-07-29  9:01   ` Cyril Hrubis
  0 siblings, 1 reply; 8+ messages in thread
From: Petr Vorel @ 2019-07-29  8:56 UTC (permalink / raw)
  To: ltp

Hi Steve,

> Test basic functionality of sendmmsg and recvmmsg.

> Signed-off-by: Steve Muckle <smuckle@google.com>
Acked-by: Petr Vorel <pvorel@suse.cz>

> ---

> Changes since v3:
>  - drop resending of messages on partially successful sendmmsg()
>  - drop use of pthreads, do message creation and port management in
>    setup/cleanup

The only thing left in v4 is duplicate LTP_CHECK_MMSGHDR call.
+ I'd use lapi/socket.h also in cve-2016-7117.c (as Cyril pointed out)
in this commit (this change is simple enough).

So I'd be for merging v4 with diff below.

Kind regards,
Petr

diff --git configure.ac configure.ac
index 5e4e7f1f9..f7d1afc40 100644
--- configure.ac
+++ configure.ac
@@ -255,7 +255,6 @@ LTP_CHECK_TIME
 LTP_CHECK_TIMERFD
 test "x$with_tirpc" = xyes && LTP_CHECK_TIRPC
 LTP_CHECK_TPACKET_V3
-LTP_CHECK_MMSGHDR
 LTP_CHECK_UNAME_DOMAINNAME
 LTP_CHECK_XFS_QUOTACTL
 LTP_CHECK_X_TABLES
diff --git testcases/cve/cve-2016-7117.c testcases/cve/cve-2016-7117.c
index f0f6c22f1..140839712 100644
--- testcases/cve/cve-2016-7117.c
+++ testcases/cve/cve-2016-7117.c
@@ -30,6 +30,8 @@
  * https://blog.lizzie.io/notes-about-cve-2016-7117.html
  */
 
+#include "config.h"
+
 #include <sys/wait.h>
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -44,20 +46,12 @@
 
 /* The bug was present in the kernel before recvmmsg was exposed by glibc */
 #include "lapi/syscalls.h"
-
-#include "config.h"
+#include "lapi/socket.h"
 
 #define MSG "abcdefghijklmnop"
 #define RECV_TIMEOUT 1
 #define ATTEMPTS 0x1FFFFF
 
-#ifndef HAVE_STRUCT_MMSGHDR
-struct mmsghdr {
-	struct msghdr msg_hdr;
-	unsigned int msg_len;
-};
-#endif
-
 static volatile int socket_fds[2];
 static struct mmsghdr msghdrs[2] = {
 	{

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [LTP] [PATCH] syscalls/sendmmsg: add new test
@ 2019-07-25 21:11 Steve Muckle
  2019-07-29  8:56 ` Petr Vorel
  0 siblings, 1 reply; 8+ messages in thread
From: Steve Muckle @ 2019-07-25 21:11 UTC (permalink / raw)
  To: ltp

Test basic functionality of sendmmsg and recvmmsg.

Signed-off-by: Steve Muckle <smuckle@google.com>
---

Changes since v3:
 - drop resending of messages on partially successful sendmmsg()
 - drop use of pthreads, do message creation and port management in
   setup/cleanup

Changes since v2:
 - fix race between sender and receiver
 - use TST_GET_UNUSED_PORT
 - fix checkpatch error (it still complains about the // on the SPDX
   identifier in sendmmsg_var.h, but other headers in this project
   use that style)
 - add SPDX identifier in sendmmsg01.c

Changes since v1:
 - rebased on recent autotools cleanup for libc func checks
 - used test_variant to test direct syscalls and libc syscalls
 - use tst_syscall instead of ltp_syscall
 - check that both messages have been sent, retry sending messages if
   necessary
 - use tst_brk(TBROK... if sendmmsg fails so recv thread is not kept
   waiting

 configure.ac                                  |   3 +
 include/lapi/socket.h                         |   8 ++
 runtest/syscalls                              |   2 +
 testcases/kernel/syscalls/sendmmsg/.gitignore |   1 +
 testcases/kernel/syscalls/sendmmsg/Makefile   |   9 ++
 .../kernel/syscalls/sendmmsg/sendmmsg01.c     | 126 ++++++++++++++++++
 .../kernel/syscalls/sendmmsg/sendmmsg_var.h   |  60 +++++++++
 7 files changed, 209 insertions(+)
 create mode 100644 testcases/kernel/syscalls/sendmmsg/.gitignore
 create mode 100644 testcases/kernel/syscalls/sendmmsg/Makefile
 create mode 100644 testcases/kernel/syscalls/sendmmsg/sendmmsg01.c
 create mode 100644 testcases/kernel/syscalls/sendmmsg/sendmmsg_var.h

diff --git a/configure.ac b/configure.ac
index 3dcf282e8..5e4e7f1f9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -82,9 +82,11 @@ AC_CHECK_FUNCS([ \
     pwritev \
     pwritev2 \
     readlinkat \
+    recvmmsg \
     renameat \
     renameat2 \
     sched_getcpu \
+    sendmmsg \
     sigpending \
     splice \
     stime \
@@ -253,6 +255,7 @@ LTP_CHECK_TIME
 LTP_CHECK_TIMERFD
 test "x$with_tirpc" = xyes && LTP_CHECK_TIRPC
 LTP_CHECK_TPACKET_V3
+LTP_CHECK_MMSGHDR
 LTP_CHECK_UNAME_DOMAINNAME
 LTP_CHECK_XFS_QUOTACTL
 LTP_CHECK_X_TABLES
diff --git a/include/lapi/socket.h b/include/lapi/socket.h
index 2605443e8..6d9e9fe30 100644
--- a/include/lapi/socket.h
+++ b/include/lapi/socket.h
@@ -19,6 +19,7 @@
 #ifndef __LAPI_SOCKET_H__
 #define __LAPI_SOCKET_H__
 
+#include "config.h"
 #include <sys/socket.h>
 
 #ifndef MSG_ZEROCOPY
@@ -69,4 +70,11 @@
 # define SOL_ALG		279
 #endif
 
+#ifndef HAVE_STRUCT_MMSGHDR
+struct mmsghdr {
+	struct msghdr msg_hdr;
+	unsigned int msg_len;
+};
+#endif
+
 #endif /* __LAPI_SOCKET_H__ */
diff --git a/runtest/syscalls b/runtest/syscalls
index 67dfed661..1e79e41c3 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1117,6 +1117,8 @@ sendfile09_64 sendfile09_64
 sendmsg01 sendmsg01
 sendmsg02 sendmsg02
 
+sendmmsg01 sendmmsg01
+
 sendto01 sendto01
 sendto02 sendto02
 
diff --git a/testcases/kernel/syscalls/sendmmsg/.gitignore b/testcases/kernel/syscalls/sendmmsg/.gitignore
new file mode 100644
index 000000000..b703ececd
--- /dev/null
+++ b/testcases/kernel/syscalls/sendmmsg/.gitignore
@@ -0,0 +1 @@
+sendmmsg01
diff --git a/testcases/kernel/syscalls/sendmmsg/Makefile b/testcases/kernel/syscalls/sendmmsg/Makefile
new file mode 100644
index 000000000..47e063722
--- /dev/null
+++ b/testcases/kernel/syscalls/sendmmsg/Makefile
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir		?= ../../../..
+
+sendmmsg01: CFLAGS+=-pthread
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c b/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c
new file mode 100644
index 000000000..37a71f281
--- /dev/null
+++ b/testcases/kernel/syscalls/sendmmsg/sendmmsg01.c
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * This test is based on source contained in the man pages for sendmmsg and
+ * recvmmsg in release 4.15 of the Linux man-pages project.
+ */
+
+#define _GNU_SOURCE
+#include <netinet/ip.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include "tst_test.h"
+#include "lapi/socket.h"
+#include "tst_safe_macros.h"
+
+#include "sendmmsg_var.h"
+
+#define BUFSIZE 16
+#define VLEN 2
+
+static int send_sockfd;
+static int receive_sockfd;
+static struct mmsghdr msg[VLEN];
+static struct iovec msg1[2], msg2;
+
+static void run(void)
+{
+	struct mmsghdr msgs_in[VLEN];
+	struct iovec iovecs[VLEN];
+	char bufs[VLEN][BUFSIZE+1];
+	struct timespec timeout;
+	int i, retval;
+
+	retval = do_sendmmsg(send_sockfd, msg, VLEN, 0);
+	if (retval < 0 || msg[0].msg_len != 6 || msg[1].msg_len != 5) {
+		tst_res(TFAIL|TTERRNO, "sendmmsg failed");
+		return;
+	}
+
+	memset(msgs_in, 0, sizeof(msgs_in));
+	for (i = 0; i < VLEN; i++) {
+		iovecs[i].iov_base = bufs[i];
+		iovecs[i].iov_len = BUFSIZE;
+		msgs_in[i].msg_hdr.msg_iov = &iovecs[i];
+		msgs_in[i].msg_hdr.msg_iovlen = 1;
+	}
+
+	timeout.tv_sec = 1;
+	timeout.tv_nsec = 0;
+
+	retval = do_recvmmsg(receive_sockfd, msgs_in, VLEN, 0, &timeout);
+
+	if (retval == -1) {
+		tst_res(TFAIL | TTERRNO, "recvmmsg failed");
+		return;
+	}
+	if (retval != 2) {
+		tst_res(TFAIL, "Received unexpected number of messages (%d)",
+			retval);
+		return;
+	}
+
+	bufs[0][msgs_in[0].msg_len] = 0;
+	if (strcmp(bufs[0], "onetwo"))
+		tst_res(TFAIL, "Error in first received message.");
+	else
+		tst_res(TPASS, "First message received successfully.");
+
+	bufs[1][msgs_in[1].msg_len] = 0;
+	if (strcmp(bufs[1], "three"))
+		tst_res(TFAIL, "Error in second received message.");
+	else
+		tst_res(TPASS, "Second message received successfully.");
+}
+
+static void setup(void)
+{
+	struct sockaddr_in addr;
+	unsigned int port = TST_GET_UNUSED_PORT(AF_INET, SOCK_DGRAM);
+
+	send_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+	receive_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+
+	addr.sin_family = AF_INET;
+	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+	addr.sin_port = port;
+
+	SAFE_BIND(receive_sockfd, (struct sockaddr *)&addr, sizeof(addr));
+	SAFE_CONNECT(send_sockfd, (struct sockaddr *) &addr, sizeof(addr));
+
+	memset(msg1, 0, sizeof(msg1));
+	msg1[0].iov_base = "one";
+	msg1[0].iov_len = 3;
+	msg1[1].iov_base = "two";
+	msg1[1].iov_len = 3;
+
+	memset(&msg2, 0, sizeof(msg2));
+	msg2.iov_base = "three";
+	msg2.iov_len = 5;
+
+	memset(msg, 0, sizeof(msg));
+	msg[0].msg_hdr.msg_iov = msg1;
+	msg[0].msg_hdr.msg_iovlen = 2;
+
+	msg[1].msg_hdr.msg_iov = &msg2;
+	msg[1].msg_hdr.msg_iovlen = 1;
+
+	test_info();
+}
+
+static void cleanup(void)
+{
+	if (send_sockfd > 0)
+		SAFE_CLOSE(send_sockfd);
+	if (receive_sockfd > 0)
+		SAFE_CLOSE(receive_sockfd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_variants = TEST_VARIANTS,
+};
diff --git a/testcases/kernel/syscalls/sendmmsg/sendmmsg_var.h b/testcases/kernel/syscalls/sendmmsg/sendmmsg_var.h
new file mode 100644
index 000000000..f00cf056a
--- /dev/null
+++ b/testcases/kernel/syscalls/sendmmsg/sendmmsg_var.h
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Google, Inc.
+ */
+
+#ifndef SENDMMSG_VAR__
+#define SENDMMSG_VAR__
+
+#include "lapi/syscalls.h"
+
+static int do_sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
+		       int flags)
+{
+	switch (tst_variant) {
+	case 0:
+		return tst_syscall(__NR_sendmmsg, sockfd, msgvec, vlen, flags);
+	case 1:
+#ifdef HAVE_SENDMMSG
+		return sendmmsg(sockfd, msgvec, vlen, flags);
+#else
+		tst_brk(TCONF, "libc sendmmsg not present");
+#endif
+	}
+
+	return -1;
+}
+
+static int do_recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
+		       int flags, struct timespec *timeout)
+{
+	switch (tst_variant) {
+	case 0:
+		return tst_syscall(__NR_recvmmsg, sockfd, msgvec, vlen, flags,
+				   timeout);
+	case 1:
+#ifdef HAVE_RECVMMSG
+		return recvmmsg(sockfd, msgvec, vlen, flags, timeout);
+#else
+		tst_brk(TCONF, "libc recvmmsg not present");
+#endif
+	}
+
+	return -1;
+}
+
+static void test_info(void)
+{
+	switch (tst_variant) {
+	case 0:
+		tst_res(TINFO, "Testing direct sendmmsg and recvmmsg syscalls");
+		break;
+	case 1:
+		tst_res(TINFO, "Testing libc sendmmsg and recvmmsg syscalls");
+		break;
+	}
+}
+
+#define TEST_VARIANTS 2
+
+#endif /* SENDMMSG_VAR__ */
-- 
2.22.0.709.g102302147b-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-07-29  9:31 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-20  2:16 [LTP] [PATCH] syscalls/sendmmsg: add new test Steve Muckle
2019-03-20 22:22 ` Petr Vorel
2019-03-20 23:04 ` Petr Vorel
2019-03-20 23:57   ` Steve Muckle
2019-07-25 21:11 Steve Muckle
2019-07-29  8:56 ` Petr Vorel
2019-07-29  9:01   ` Cyril Hrubis
2019-07-29  9:31     ` Petr Vorel

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.