Path: blob/main/tools/regression/sockets/unix_cmsg/t_cmsg_len.c
96317 views
/*-1* Copyright (c) 2005 Andrey Simonenko2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/types.h>27#include <sys/socket.h>28#include <sys/un.h>29#include <inttypes.h>30#include <errno.h>31#include <stdarg.h>32#include <stdbool.h>33#include <stdio.h>34#include <stdlib.h>35#include <string.h>3637#include "uc_common.h"38#include "t_generic.h"39#include "t_cmsg_len.h"4041#ifndef __LP64__42static int43t_cmsg_len_client(int fd)44{45struct msghdr msghdr;46struct iovec iov[1];47struct cmsghdr *cmsghdr;48void *cmsg_data;49size_t size, cmsg_size;50socklen_t socklen;51int rv;5253if (uc_sync_recv() < 0)54return (-2);5556rv = -2;5758cmsg_size = CMSG_SPACE(sizeof(struct cmsgcred));59cmsg_data = malloc(cmsg_size);60if (cmsg_data == NULL) {61uc_logmsg("malloc");62goto done;63}64uc_msghdr_init_client(&msghdr, iov, cmsg_data, cmsg_size,65SCM_CREDS, sizeof(struct cmsgcred));66cmsghdr = CMSG_FIRSTHDR(&msghdr);6768if (uc_socket_connect(fd) < 0)69goto done;7071size = msghdr.msg_iov != NULL ? msghdr.msg_iov->iov_len : 0;72rv = -1;73for (socklen = 0; socklen < CMSG_LEN(0); ++socklen) {74cmsghdr->cmsg_len = socklen;75uc_dbgmsg("send: data size %zu", size);76uc_dbgmsg("send: msghdr.msg_controllen %u",77(u_int)msghdr.msg_controllen);78uc_dbgmsg("send: cmsghdr.cmsg_len %u",79(u_int)cmsghdr->cmsg_len);80if (sendmsg(fd, &msghdr, 0) < 0) {81uc_dbgmsg("sendmsg(2) failed: %s; retrying",82strerror(errno));83continue;84}85uc_logmsgx("sent message with cmsghdr.cmsg_len %u < %u",86(u_int)cmsghdr->cmsg_len, (u_int)CMSG_LEN(0));87break;88}89if (socklen == CMSG_LEN(0))90rv = 0;9192if (uc_sync_send() < 0) {93rv = -2;94goto done;95}96done:97free(cmsg_data);98return (rv);99}100101static int102t_cmsg_len_server(int fd1)103{104int fd2, rv;105106if (uc_sync_send() < 0)107return (-2);108109rv = -2;110111if (uc_cfg.sock_type == SOCK_STREAM) {112fd2 = uc_socket_accept(fd1);113if (fd2 < 0)114goto done;115} else116fd2 = fd1;117118if (uc_sync_recv() < 0)119goto done;120121rv = 0;122done:123if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)124if (uc_socket_close(fd2) < 0)125rv = -2;126return (rv);127}128129int130t_cmsg_len(void)131{132return (t_generic(t_cmsg_len_client, t_cmsg_len_server));133}134#endif135136137