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