xref: /freebsd/tools/regression/sockets/unix_cmsg/t_xxxtime.c.in (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1/*-
2 * Copyright (c) 2005 Andrey Simonenko
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28#include <sys/time.h>
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/un.h>
32#include <inttypes.h>
33#include <stdarg.h>
34#include <stdbool.h>
35#include <stdlib.h>
36
37#include "t_%%TTYPE%%.h"
38#include "t_generic.h"
39#include "uc_common.h"
40#include "uc_check_time.h"
41
42#if defined(%%SCM_TTYPE%%)
43static int
44check_scm_%%TTYPE%%(struct cmsghdr *cmsghdr)
45{
46	const struct %%DTYPE%% *bt;
47
48	if (uc_check_cmsghdr(cmsghdr, %%SCM_TTYPE%%, sizeof(struct %%DTYPE%%)) < 0)
49		return (-1);
50
51	bt = (struct %%DTYPE%% *)CMSG_DATA(cmsghdr);
52
53	if (uc_check_%%TTYPE%%(bt) < 0)
54		return (-1);
55
56	uc_dbgmsg("%%DTYPE%%.%%MAJ_MEMB%% %"PRIdMAX", %%DTYPE%%.%%MIN_MEMB%% %"PRIuMAX,
57	    (intmax_t)bt->%%MAJ_MEMB%%, (uintmax_t)bt->%%MIN_MEMB%%);
58
59	return (0);
60}
61
62static int
63t_%%TTYPE%%_client(int fd)
64{
65	struct msghdr msghdr;
66	struct iovec iov[1];
67	void *cmsg_data;
68	size_t cmsg_size;
69	int rv;
70
71	if (uc_sync_recv() < 0)
72		return (-2);
73
74	rv = -2;
75
76	cmsg_size = CMSG_SPACE(sizeof(struct %%DTYPE%%));
77	cmsg_data = malloc(cmsg_size);
78	if (cmsg_data == NULL) {
79		uc_logmsg("malloc");
80		goto done;
81	}
82	uc_msghdr_init_client(&msghdr, iov, cmsg_data, cmsg_size,
83	    %%SCM_TTYPE%%, sizeof(struct %%DTYPE%%));
84
85	if (uc_socket_connect(fd) < 0)
86		goto done;
87
88	if (uc_message_sendn(fd, &msghdr) < 0)
89		goto done;
90
91	rv = 0;
92done:
93	free(cmsg_data);
94	return (rv);
95}
96
97static int
98t_%%TTYPE%%_server(int fd1)
99{
100	struct msghdr msghdr;
101	struct iovec iov[1];
102	struct cmsghdr *cmsghdr;
103	void *cmsg_data;
104	size_t cmsg_size;
105	u_int i;
106	int fd2, rv;
107
108	if (uc_sync_send() < 0)
109		return (-2);
110
111	fd2 = -1;
112	rv = -2;
113
114	cmsg_size = CMSG_SPACE(sizeof(struct %%DTYPE%%));
115	cmsg_data = malloc(cmsg_size);
116	if (cmsg_data == NULL) {
117		uc_logmsg("malloc");
118		goto done;
119	}
120
121	if (uc_cfg.sock_type == SOCK_STREAM) {
122		fd2 = uc_socket_accept(fd1);
123		if (fd2 < 0)
124			goto done;
125	} else
126		fd2 = fd1;
127
128	rv = -1;
129	for (i = 1; i <= uc_cfg.ipc_msg.msg_num; ++i) {
130		uc_dbgmsg("message #%u", i);
131
132		uc_msghdr_init_server(&msghdr, iov, cmsg_data, cmsg_size);
133		if (uc_message_recv(fd2, &msghdr) < 0) {
134			rv = -2;
135			break;
136		}
137
138		if (uc_check_msghdr(&msghdr, sizeof(*cmsghdr)) < 0)
139			break;
140
141		cmsghdr = CMSG_FIRSTHDR(&msghdr);
142		if (check_scm_%%TTYPE%%(cmsghdr) < 0)
143			break;
144	}
145	if (i > uc_cfg.ipc_msg.msg_num)
146		rv = 0;
147done:
148	free(cmsg_data);
149	if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)
150		if (uc_socket_close(fd2) < 0)
151			rv = -2;
152	return (rv);
153}
154
155int
156t_%%TTYPE%%(void)
157{
158	return (t_generic(t_%%TTYPE%%_client, t_%%TTYPE%%_server));
159}
160#endif /* %%SCM_TTYPE%% */
161