Path: blob/main/tools/regression/sockets/unix_cmsg/t_cmsgcred.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 <stdarg.h>31#include <stdbool.h>32#include <stdlib.h>3334#include "uc_common.h"35#include "t_generic.h"36#include "t_cmsgcred.h"3738int39t_cmsgcred_client(int fd)40{41struct msghdr msghdr;42struct iovec iov[1];43void *cmsg_data;44size_t cmsg_size;45int rv;4647if (uc_sync_recv() < 0)48return (-2);4950rv = -2;5152cmsg_size = CMSG_SPACE(sizeof(struct cmsgcred));53cmsg_data = malloc(cmsg_size);54if (cmsg_data == NULL) {55uc_logmsg("malloc");56goto done;57}58uc_msghdr_init_client(&msghdr, iov, cmsg_data, cmsg_size,59SCM_CREDS, sizeof(struct cmsgcred));6061if (uc_socket_connect(fd) < 0)62goto done;6364if (uc_message_sendn(fd, &msghdr) < 0)65goto done;6667rv = 0;68done:69free(cmsg_data);70return (rv);71}7273static int74t_cmsgcred_server(int fd1)75{76struct msghdr msghdr;77struct iovec iov[1];78struct cmsghdr *cmsghdr;79void *cmsg_data;80size_t cmsg_size;81u_int i;82int fd2, rv;8384if (uc_sync_send() < 0)85return (-2);8687fd2 = -1;88rv = -2;8990cmsg_size = CMSG_SPACE(sizeof(struct cmsgcred));91cmsg_data = malloc(cmsg_size);92if (cmsg_data == NULL) {93uc_logmsg("malloc");94goto done;95}9697if (uc_cfg.sock_type == SOCK_STREAM) {98fd2 = uc_socket_accept(fd1);99if (fd2 < 0)100goto done;101} else102fd2 = fd1;103104rv = -1;105for (i = 1; i <= uc_cfg.ipc_msg.msg_num; ++i) {106uc_dbgmsg("message #%u", i);107108uc_msghdr_init_server(&msghdr, iov, cmsg_data, cmsg_size);109if (uc_message_recv(fd2, &msghdr) < 0) {110rv = -2;111break;112}113114if (uc_check_msghdr(&msghdr, sizeof(*cmsghdr)) < 0)115break;116117cmsghdr = CMSG_FIRSTHDR(&msghdr);118if (uc_check_scm_creds_cmsgcred(cmsghdr) < 0)119break;120}121if (i > uc_cfg.ipc_msg.msg_num)122rv = 0;123done:124free(cmsg_data);125if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)126if (uc_socket_close(fd2) < 0)127rv = -2;128return (rv);129}130131int132t_cmsgcred(void)133{134return (t_generic(t_cmsgcred_client, t_cmsgcred_server));135}136137138