xref: /netbsd-src/regress/sys/kern/dislodgefd/dislodgefd.c (revision 3f351f34c6d827cf017cdcff3543f6ec0c88b420)
1 /*	$NetBSD: dislodgefd.c,v 1.1 2023/10/13 18:46:22 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2023 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 
32 #include <pthread.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <err.h>
38 
39 pthread_barrier_t	barrier;
40 int			fds[2];
41 
42 static void *
43 reader(void *cookie)
44 {
45 	char buf[1];
46 
47 	(void)pthread_barrier_wait(&barrier);
48 	printf("reader(): commencing read, this should error out... after 1s\n");
49 	if (read(fds[0], buf, sizeof(buf)) == -1)
50 		err(1, "read");
51 
52 	printf("reader(): read terminated without error??\n");
53 	return NULL;
54 }
55 
56 int
57 main(int argc, char *argv[])
58 {
59 	pthread_t pt;
60 
61 	if (argc > 1 && strcmp(argv[1], "pipe") == 0) {
62 		if (pipe(fds))
63 			err(1, "pipe");
64 	} else {
65 		if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0)
66 			err(1, "socketpair");
67 	}
68 	pthread_barrier_init(&barrier, NULL, 2);
69 	if (pthread_create(&pt, NULL, reader, NULL)) {
70 		errx(1, "pthread_create failed");
71 	}
72 	(void)pthread_barrier_wait(&barrier);
73 	sleep(1);
74 	printf("main(): closing the reader side fd..\n");
75 	close(fds[0]);
76 	printf("main(): sleeping again for a bit..\n");
77 	sleep(1);
78 	printf("main(): exiting.\n");
79 	return 0;
80 }
81