Path: blob/main/tools/regression/capsicum/syscalls/misc.c
48254 views
/*-1* Copyright (c) 2012 The FreeBSD Foundation2*3* This software was developed by Pawel Jakub Dawidek under sponsorship from4* the FreeBSD Foundation.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/types.h>29#include <sys/select.h>30#include <sys/socket.h>3132#include <assert.h>33#include <errno.h>34#include <stdlib.h>35#include <strings.h>3637#include "misc.h"3839int40pdwait(int pfd)41{42fd_set fdset;4344FD_ZERO(&fdset);45FD_SET(pfd, &fdset);4647return (select(pfd + 1, NULL, &fdset, NULL, NULL) == -1 ? -1 : 0);48}4950int51descriptor_send(int sock, int fd)52{53unsigned char ctrl[CMSG_SPACE(sizeof(fd))];54struct msghdr msg;55struct cmsghdr *cmsg;5657assert(sock >= 0);58assert(fd >= 0);5960bzero(&msg, sizeof(msg));61bzero(&ctrl, sizeof(ctrl));6263msg.msg_iov = NULL;64msg.msg_iovlen = 0;65msg.msg_control = ctrl;66msg.msg_controllen = sizeof(ctrl);6768cmsg = CMSG_FIRSTHDR(&msg);69cmsg->cmsg_level = SOL_SOCKET;70cmsg->cmsg_type = SCM_RIGHTS;71cmsg->cmsg_len = CMSG_LEN(sizeof(fd));72bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));7374if (sendmsg(sock, &msg, 0) == -1)75return (errno);7677return (0);78}7980int81descriptor_recv(int sock, int *fdp)82{83unsigned char ctrl[CMSG_SPACE(sizeof(*fdp))];84struct msghdr msg;85struct cmsghdr *cmsg;86struct iovec iov;87int val;8889assert(sock >= 0);90assert(fdp != NULL);9192bzero(&msg, sizeof(msg));93bzero(&ctrl, sizeof(ctrl));9495#if 196/*97* This doesn't really make sense, as we don't plan to receive any98* data, but if no buffer is provided and recv(2) returns 0 without99* control message. Must be kernel bug.100*/101iov.iov_base = &val;102iov.iov_len = sizeof(val);103msg.msg_iov = &iov;104msg.msg_iovlen = 1;105#else106msg.msg_iov = NULL;107msg.msg_iovlen = 0;108#endif109msg.msg_control = ctrl;110msg.msg_controllen = sizeof(ctrl);111112if (recvmsg(sock, &msg, 0) == -1)113return (errno);114115cmsg = CMSG_FIRSTHDR(&msg);116if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||117cmsg->cmsg_type != SCM_RIGHTS) {118return (EINVAL);119}120bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));121122return (0);123}124125126