1 /* $OpenBSD: ungc.c,v 1.2 2021/12/15 21:25:55 bluhm Exp $ */ 2 3 /* 4 * Copyright (c) 2021 Vitaliy Makkoveev <mvs@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <sys/time.h> 22 #include <sys/un.h> 23 #include <stdio.h> 24 #include <err.h> 25 #include <errno.h> 26 #include <fcntl.h> 27 #include <string.h> 28 #include <time.h> 29 #include <unistd.h> 30 31 union msg_control{ 32 struct cmsghdr cmsgh; 33 char control[CMSG_SPACE(sizeof(int)*2)]; 34 }; 35 36 int main(int argc, char *argv[]) 37 { 38 struct timespec ts_start, ts_now, ts_time; 39 union msg_control msg_control; 40 int iov_buf; 41 struct iovec iov; 42 struct msghdr msgh; 43 struct cmsghdr *cmsgh; 44 int s[2]; 45 int infinite = 0; 46 47 if (argc > 1 && !strcmp(argv[1], "--infinite")) 48 infinite = 1; 49 50 if (!infinite) 51 if (clock_gettime(CLOCK_BOOTTIME, &ts_start) <0) 52 err(1, "clock_gettime"); 53 54 while (1) { 55 if (socketpair(AF_UNIX, SOCK_STREAM|O_NONBLOCK, 0, s) < 0) 56 err(1, "socketpair"); 57 58 iov_buf = 0; 59 iov.iov_base = &iov_buf; 60 iov.iov_len = sizeof(iov_buf); 61 msgh.msg_control = msg_control.control; 62 msgh.msg_controllen = sizeof(msg_control.control); 63 msgh.msg_iov = &iov; 64 msgh.msg_iovlen = 1; 65 msgh.msg_name = NULL; 66 msgh.msg_namelen = 0; 67 cmsgh = CMSG_FIRSTHDR(&msgh); 68 cmsgh->cmsg_len = CMSG_LEN(sizeof(int) * 2); 69 cmsgh->cmsg_level = SOL_SOCKET; 70 cmsgh->cmsg_type = SCM_RIGHTS; 71 *((int *)CMSG_DATA(cmsgh) + 0) = s[0]; 72 *((int *)CMSG_DATA(cmsgh) + 1) = s[1]; 73 74 if (sendmsg(s[0], &msgh, 0) < 0) { 75 if (errno != EMFILE) 76 err(1, "sendmsg"); 77 } 78 79 close(s[0]); 80 close(s[1]); 81 82 if (!infinite) { 83 if (clock_gettime(CLOCK_BOOTTIME, &ts_now) <0) 84 err(1, "clock_gettime"); 85 86 timespecsub(&ts_now, &ts_start, &ts_time); 87 if (ts_time.tv_sec >= 20) 88 break; 89 } 90 } 91 92 return 0; 93 } 94