1/* $OpenBSD: unfdpassfail.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/event.h>
21#include <sys/socket.h>
22#include <sys/un.h>
23#include <stdio.h>
24#include <err.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <pthread.h>
28#include <string.h>
29#include <time.h>
30#include <unistd.h>
31
32union msg_control{
33	struct cmsghdr cmsgh;
34	char control[CMSG_SPACE(sizeof(int)*2)];
35};
36
37static void *thr_close(void *arg)
38{
39	close(*(int *)arg);
40	return NULL;
41}
42
43int main(int argc, char *argv[])
44{
45	struct timespec ts_start, ts_now, ts_time;
46	union msg_control msg_control;
47	int iov_buf;
48	struct iovec iov;
49	struct msghdr msgh;
50	struct cmsghdr *cmsgh;
51	pthread_t thr;
52	int s[2], fd, kqfd;
53	int infinite = 0, error;
54
55	if (argc > 1 && !strcmp(argv[1], "--infinite"))
56		infinite = 1;
57
58	if ((kqfd = kqueue()) < 0)
59		err(1, "kqueue");
60
61	if (!infinite)
62		if (clock_gettime(CLOCK_BOOTTIME, &ts_start) <0)
63			err(1, "clock_gettime");
64
65	while (1) {
66		if (socketpair(AF_UNIX, SOCK_STREAM|O_NONBLOCK, 0, s) < 0)
67			err(1, "socketpair");
68		if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
69			err(1, "open");
70
71		iov_buf = 0;
72		iov.iov_base = &iov_buf;
73		iov.iov_len = sizeof(iov_buf);
74		msgh.msg_control = msg_control.control;
75		msgh.msg_controllen = sizeof(msg_control.control);
76		msgh.msg_iov = &iov;
77		msgh.msg_iovlen = 1;
78		msgh.msg_name = NULL;
79		msgh.msg_namelen = 0;
80		cmsgh = CMSG_FIRSTHDR(&msgh);
81		cmsgh->cmsg_len = CMSG_LEN(sizeof(int) * 2);
82		cmsgh->cmsg_level = SOL_SOCKET;
83		cmsgh->cmsg_type = SCM_RIGHTS;
84		*((int *)CMSG_DATA(cmsgh) + 0) = fd;
85		*((int *)CMSG_DATA(cmsgh) + 1) = kqfd;
86
87		error = pthread_create(&thr, NULL, thr_close, &fd);
88		if (error)
89			errc(1, error, "pthread_create");
90
91		if (sendmsg(s[0], &msgh, 0) < 0) {
92			if (errno != EINVAL)
93				err(1, "sendmsg");
94		}
95
96		error = pthread_join(thr, NULL);
97		if (error)
98			errc(1, error, "pthread_join");
99
100		close(s[0]);
101		close(s[1]);
102		close(fd);
103
104		if (!infinite) {
105			if (clock_gettime(CLOCK_BOOTTIME, &ts_now) <0)
106				err(1, "clock_gettime");
107
108			timespecsub(&ts_now, &ts_start, &ts_time);
109			if (ts_time.tv_sec >= 20)
110				break;
111		}
112	}
113
114	close(kqfd);
115
116	return 0;
117}
118