1*1031c584SApple OSS Distributions /* 2*1031c584SApple OSS Distributions * Copyright (c) 2021 Apple Inc. All rights reserved. 3*1031c584SApple OSS Distributions */ 4*1031c584SApple OSS Distributions 5*1031c584SApple OSS Distributions #include <stdio.h> 6*1031c584SApple OSS Distributions #include <sys/socket.h> 7*1031c584SApple OSS Distributions #include <sys/resource.h> 8*1031c584SApple OSS Distributions #include <errno.h> 9*1031c584SApple OSS Distributions 10*1031c584SApple OSS Distributions #include <darwintest.h> 11*1031c584SApple OSS Distributions #include <darwintest_utils.h> 12*1031c584SApple OSS Distributions 13*1031c584SApple OSS Distributions #define SCM_RIGHTS 0x01 14*1031c584SApple OSS Distributions 15*1031c584SApple OSS Distributions T_DECL(scm_rights_control_msg, "Test the fd alloc failure behavior with SCM_RIGHTS control msg") 16*1031c584SApple OSS Distributions { 17*1031c584SApple OSS Distributions T_SETUPBEGIN; 18*1031c584SApple OSS Distributions int res, sock[2]; 19*1031c584SApple OSS Distributions 20*1031c584SApple OSS Distributions int fd = open("/dev/null", O_RDWR); 21*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd, "open(/dev/null)"); 22*1031c584SApple OSS Distributions 23*1031c584SApple OSS Distributions struct rlimit rlim = { 6, 6 }; 24*1031c584SApple OSS Distributions setrlimit(RLIMIT_NOFILE, &rlim); 25*1031c584SApple OSS Distributions 26*1031c584SApple OSS Distributions res = socketpair(AF_UNIX, SOCK_STREAM, 0, sock); 27*1031c584SApple OSS Distributions T_QUIET; T_ASSERT_TRUE(sock[0] >= 0, "failed to create socket"); 28*1031c584SApple OSS Distributions 29*1031c584SApple OSS Distributions struct iovec iovec[1]; 30*1031c584SApple OSS Distributions struct msghdr msg = {0}; 31*1031c584SApple OSS Distributions struct cmsghdr *cmsghdr; 32*1031c584SApple OSS Distributions char buf[CMSG_SPACE(sizeof(int))]; 33*1031c584SApple OSS Distributions 34*1031c584SApple OSS Distributions iovec[0].iov_base = ""; 35*1031c584SApple OSS Distributions iovec[0].iov_len = 1; 36*1031c584SApple OSS Distributions 37*1031c584SApple OSS Distributions msg.msg_iov = iovec; 38*1031c584SApple OSS Distributions msg.msg_iovlen = 1; 39*1031c584SApple OSS Distributions msg.msg_control = buf; 40*1031c584SApple OSS Distributions msg.msg_controllen = CMSG_SPACE(sizeof(int)); 41*1031c584SApple OSS Distributions 42*1031c584SApple OSS Distributions cmsghdr = CMSG_FIRSTHDR(&msg); 43*1031c584SApple OSS Distributions cmsghdr->cmsg_len = CMSG_LEN(sizeof(int)); 44*1031c584SApple OSS Distributions cmsghdr->cmsg_level = SOL_SOCKET; 45*1031c584SApple OSS Distributions cmsghdr->cmsg_type = SCM_RIGHTS; 46*1031c584SApple OSS Distributions memcpy(CMSG_DATA(cmsghdr), &fd, sizeof(fd)); 47*1031c584SApple OSS Distributions 48*1031c584SApple OSS Distributions T_SETUPEND; 49*1031c584SApple OSS Distributions 50*1031c584SApple OSS Distributions sendmsg(sock[1], &msg, 0); 51*1031c584SApple OSS Distributions 52*1031c584SApple OSS Distributions u_char c; 53*1031c584SApple OSS Distributions struct iovec riovec[1]; 54*1031c584SApple OSS Distributions struct msghdr rmsg = { 0, }; 55*1031c584SApple OSS Distributions char rbuf[CMSG_SPACE(sizeof(int))]; 56*1031c584SApple OSS Distributions 57*1031c584SApple OSS Distributions riovec[0].iov_base = &c; 58*1031c584SApple OSS Distributions riovec[0].iov_len = 1; 59*1031c584SApple OSS Distributions 60*1031c584SApple OSS Distributions rmsg.msg_iov = riovec; 61*1031c584SApple OSS Distributions rmsg.msg_iovlen = 1; 62*1031c584SApple OSS Distributions rmsg.msg_control = rbuf; 63*1031c584SApple OSS Distributions rmsg.msg_controllen = CMSG_SPACE(sizeof(int)); 64*1031c584SApple OSS Distributions 65*1031c584SApple OSS Distributions ssize_t ret = recvmsg(sock[0], &rmsg, 0); 66*1031c584SApple OSS Distributions T_ASSERT_TRUE(ret == -1, "recvmsg should fail"); 67*1031c584SApple OSS Distributions T_ASSERT_TRUE(errno == 24, "the fail code is EMFILE"); 68*1031c584SApple OSS Distributions 69*1031c584SApple OSS Distributions close(fd); 70*1031c584SApple OSS Distributions close(sock[0]); 71*1031c584SApple OSS Distributions close(sock[1]); 72*1031c584SApple OSS Distributions } 73