1 /*
2  * Check decoding of timeout argument of recvmmsg syscall.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2017 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 #include <stdio.h>
33 
34 #include "msghdr.h"
35 
36 int
main(void)37 main(void)
38 {
39 	int fds[2];
40 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
41 		perror_msg_and_skip("socketpair");
42 
43 	if (send(fds[1], "A", 1, 0) != 1)
44 		perror_msg_and_skip("send");
45 
46 	char buf;
47 	struct iovec iov = { .iov_base = &buf, .iov_len = sizeof(buf) };
48 	struct mmsghdr mh = {
49 		.msg_hdr = {
50 			.msg_iov = &iov,
51 			.msg_iovlen = 1
52 		}
53 	};
54 	TAIL_ALLOC_OBJECT_CONST_PTR(struct timespec, ts);
55 	ts->tv_sec = 0;
56 	ts->tv_nsec = 12345678;
57 
58 	int rc = recv_mmsg(-1, &mh, 1, 0, ts);
59 	printf("recvmmsg(-1, %p, 1, 0, {tv_sec=0, tv_nsec=12345678})"
60 	       " = %s\n", &mh, sprintrc(rc));
61 
62 	rc = recv_mmsg(fds[0], &mh, 1, 0, ts);
63 	if (rc < 0)
64 		perror_msg_and_skip("recvmmsg");
65 	printf("recvmmsg(%d, [{msg_hdr={msg_name=NULL, msg_namelen=0"
66 	       ", msg_iov=[{iov_base=\"A\", iov_len=1}], msg_iovlen=1"
67 	       ", msg_controllen=0, msg_flags=0}, msg_len=1}], 1, 0"
68 	       ", {tv_sec=0, tv_nsec=12345678}) = "
69 	       "%d (left {tv_sec=0, tv_nsec=%d})\n",
70 	       fds[0], rc, (int) ts->tv_nsec);
71 
72 	ts->tv_sec = 0xdeadbeefU;
73 	ts->tv_nsec = 0xfacefeedU;
74 
75 	rc = recv_mmsg(fds[0], &mh, 1, 0, ts);
76 	printf("recvmmsg(%d, %p, 1, 0, {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
77 	       fds[0], &mh, (long long) ts->tv_sec,
78 	       zero_extend_signed_to_ull(ts->tv_nsec), sprintrc(rc));
79 
80 	ts->tv_sec = (time_t) 0xcafef00ddeadbeefLL;
81 	ts->tv_nsec = (long) 0xbadc0dedfacefeedLL;
82 
83 	rc = recv_mmsg(fds[0], &mh, 1, 0, ts);
84 	printf("recvmmsg(%d, %p, 1, 0, {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
85 	       fds[0], &mh, (long long) ts->tv_sec,
86 	       zero_extend_signed_to_ull(ts->tv_nsec), sprintrc(rc));
87 
88 	puts("+++ exited with 0 +++");
89 	return 0;
90 }
91