xref: /freebsd/tools/regression/sockets/unix_cmsg/t_xxxtime.c.in (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
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__FBSDID("$FreeBSD$");
29
30#include <sys/time.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/un.h>
34#include <inttypes.h>
35#include <stdarg.h>
36#include <stdbool.h>
37#include <stdlib.h>
38
39#include "t_%%TTYPE%%.h"
40#include "t_generic.h"
41#include "uc_common.h"
42#include "uc_check_time.h"
43
44#if defined(%%SCM_TTYPE%%)
45static int
46check_scm_%%TTYPE%%(struct cmsghdr *cmsghdr)
47{
48	const struct %%DTYPE%% *bt;
49
50	if (uc_check_cmsghdr(cmsghdr, %%SCM_TTYPE%%, sizeof(struct %%DTYPE%%)) < 0)
51		return (-1);
52
53	bt = (struct %%DTYPE%% *)CMSG_DATA(cmsghdr);
54
55	if (uc_check_%%TTYPE%%(bt) < 0)
56		return (-1);
57
58	uc_dbgmsg("%%DTYPE%%.%%MAJ_MEMB%% %"PRIdMAX", %%DTYPE%%.%%MIN_MEMB%% %"PRIuMAX,
59	    (intmax_t)bt->%%MAJ_MEMB%%, (uintmax_t)bt->%%MIN_MEMB%%);
60
61	return (0);
62}
63
64static int
65t_%%TTYPE%%_client(int fd)
66{
67	struct msghdr msghdr;
68	struct iovec iov[1];
69	void *cmsg_data;
70	size_t cmsg_size;
71	int rv;
72
73	if (uc_sync_recv() < 0)
74		return (-2);
75
76	rv = -2;
77
78	cmsg_size = CMSG_SPACE(sizeof(struct %%DTYPE%%));
79	cmsg_data = malloc(cmsg_size);
80	if (cmsg_data == NULL) {
81		uc_logmsg("malloc");
82		goto done;
83	}
84	uc_msghdr_init_client(&msghdr, iov, cmsg_data, cmsg_size,
85	    %%SCM_TTYPE%%, sizeof(struct %%DTYPE%%));
86
87	if (uc_socket_connect(fd) < 0)
88		goto done;
89
90	if (uc_message_sendn(fd, &msghdr) < 0)
91		goto done;
92
93	rv = 0;
94done:
95	free(cmsg_data);
96	return (rv);
97}
98
99static int
100t_%%TTYPE%%_server(int fd1)
101{
102	struct msghdr msghdr;
103	struct iovec iov[1];
104	struct cmsghdr *cmsghdr;
105	void *cmsg_data;
106	size_t cmsg_size;
107	u_int i;
108	int fd2, rv;
109
110	if (uc_sync_send() < 0)
111		return (-2);
112
113	fd2 = -1;
114	rv = -2;
115
116	cmsg_size = CMSG_SPACE(sizeof(struct %%DTYPE%%));
117	cmsg_data = malloc(cmsg_size);
118	if (cmsg_data == NULL) {
119		uc_logmsg("malloc");
120		goto done;
121	}
122
123	if (uc_cfg.sock_type == SOCK_STREAM) {
124		fd2 = uc_socket_accept(fd1);
125		if (fd2 < 0)
126			goto done;
127	} else
128		fd2 = fd1;
129
130	rv = -1;
131	for (i = 1; i <= uc_cfg.ipc_msg.msg_num; ++i) {
132		uc_dbgmsg("message #%u", i);
133
134		uc_msghdr_init_server(&msghdr, iov, cmsg_data, cmsg_size);
135		if (uc_message_recv(fd2, &msghdr) < 0) {
136			rv = -2;
137			break;
138		}
139
140		if (uc_check_msghdr(&msghdr, sizeof(*cmsghdr)) < 0)
141			break;
142
143		cmsghdr = CMSG_FIRSTHDR(&msghdr);
144		if (check_scm_%%TTYPE%%(cmsghdr) < 0)
145			break;
146	}
147	if (i > uc_cfg.ipc_msg.msg_num)
148		rv = 0;
149done:
150	free(cmsg_data);
151	if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)
152		if (uc_socket_close(fd2) < 0)
153			rv = -2;
154	return (rv);
155}
156
157int
158t_%%TTYPE%%(void)
159{
160	return (t_generic(t_%%TTYPE%%_client, t_%%TTYPE%%_server));
161}
162#endif /* %%SCM_TTYPE%% */
163