xref: /freebsd/tools/regression/sockets/pr_atomic/pr_atomic.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1a86ec338SBruce M Simpson /*-
2a86ec338SBruce M Simpson  * Copyright (c) 2006 Bruce M. Simpson
3a86ec338SBruce M Simpson  * All rights reserved.
4a86ec338SBruce M Simpson  *
5a86ec338SBruce M Simpson  * Redistribution and use in source and binary forms, with or without
6a86ec338SBruce M Simpson  * modification, are permitted provided that the following conditions
7a86ec338SBruce M Simpson  * are met:
8a86ec338SBruce M Simpson  * 1. Redistributions of source code must retain the above copyright
9a86ec338SBruce M Simpson  *    notice, this list of conditions and the following disclaimer.
10a86ec338SBruce M Simpson  * 2. Redistributions in binary form must reproduce the above copyright
11a86ec338SBruce M Simpson  *    notice, this list of conditions and the following disclaimer in the
12a86ec338SBruce M Simpson  *    documentation and/or other materials provided with the distribution.
13a86ec338SBruce M Simpson  *
14a86ec338SBruce M Simpson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a86ec338SBruce M Simpson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a86ec338SBruce M Simpson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a86ec338SBruce M Simpson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a86ec338SBruce M Simpson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a86ec338SBruce M Simpson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a86ec338SBruce M Simpson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a86ec338SBruce M Simpson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a86ec338SBruce M Simpson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a86ec338SBruce M Simpson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a86ec338SBruce M Simpson  * SUCH DAMAGE.
25a86ec338SBruce M Simpson  */
26a86ec338SBruce M Simpson 
27a86ec338SBruce M Simpson /*
28a86ec338SBruce M Simpson  * Regression test for uiomove in kernel; specifically for PR kern/38495.
29a86ec338SBruce M Simpson  */
30a86ec338SBruce M Simpson 
31a86ec338SBruce M Simpson #include <sys/types.h>
32a86ec338SBruce M Simpson #include <sys/socket.h>
33a86ec338SBruce M Simpson #include <sys/un.h>
34a86ec338SBruce M Simpson 
35a86ec338SBruce M Simpson #include <stdlib.h>
36a86ec338SBruce M Simpson #include <signal.h>
37a86ec338SBruce M Simpson #include <setjmp.h>
38a86ec338SBruce M Simpson #include <string.h>
39a86ec338SBruce M Simpson #include <err.h>
40a86ec338SBruce M Simpson #include <errno.h>
41a86ec338SBruce M Simpson #include <unistd.h>
42a86ec338SBruce M Simpson 
43*75171ec5SEnji Cooper static char socket_path[] = "tmp.XXXXXX";
44a86ec338SBruce M Simpson 
45a86ec338SBruce M Simpson static jmp_buf myjmpbuf;
46a86ec338SBruce M Simpson 
handle_sigalrm(int signo __unused)47*75171ec5SEnji Cooper static void handle_sigalrm(int signo __unused)
48a86ec338SBruce M Simpson {
49a86ec338SBruce M Simpson 	longjmp(myjmpbuf, 1);
50a86ec338SBruce M Simpson }
51a86ec338SBruce M Simpson 
52a86ec338SBruce M Simpson int
main(void)53*75171ec5SEnji Cooper main(void)
54a86ec338SBruce M Simpson {
55a86ec338SBruce M Simpson 	struct sockaddr_un un;
56a86ec338SBruce M Simpson 	pid_t pid;
57a86ec338SBruce M Simpson 	int s;
58a86ec338SBruce M Simpson 
59*75171ec5SEnji Cooper 	if (mkstemp(socket_path) == -1)
60*75171ec5SEnji Cooper 		err(1, "mkstemp");
61a86ec338SBruce M Simpson 	s = socket(PF_LOCAL, SOCK_DGRAM, 0);
62a86ec338SBruce M Simpson 	if (s == -1)
63a86ec338SBruce M Simpson 		errx(-1, "socket");
64a86ec338SBruce M Simpson 	memset(&un, 0, sizeof(un));
65a86ec338SBruce M Simpson 	un.sun_family = AF_LOCAL;
66*75171ec5SEnji Cooper 	unlink(socket_path);
67*75171ec5SEnji Cooper 	strcpy(un.sun_path, socket_path);
68a86ec338SBruce M Simpson 	if (bind(s, (struct sockaddr *)&un, sizeof(un)) == -1)
69a86ec338SBruce M Simpson 		errx(-1, "bind");
70a86ec338SBruce M Simpson 	pid = fork();
71a86ec338SBruce M Simpson 	if (pid == -1)
72a86ec338SBruce M Simpson 		errx(-1, "fork");
73a86ec338SBruce M Simpson 	if (pid == 0) {
74a86ec338SBruce M Simpson 		int conn;
75a86ec338SBruce M Simpson 		char buf[] = "AAAAAAAAA";
76a86ec338SBruce M Simpson 
77a86ec338SBruce M Simpson 		close(s);
78a86ec338SBruce M Simpson 		conn = socket(AF_LOCAL, SOCK_DGRAM, 0);
79a86ec338SBruce M Simpson 		if (conn == -1)
80a86ec338SBruce M Simpson 			errx(-1,"socket");
81a86ec338SBruce M Simpson 		if (sendto(conn, buf, sizeof(buf), 0, (struct sockaddr *)&un,
82a86ec338SBruce M Simpson 		    sizeof(un)) != sizeof(buf))
83a86ec338SBruce M Simpson 			errx(-1,"sendto");
84a86ec338SBruce M Simpson 		close(conn);
85a86ec338SBruce M Simpson 		_exit(0);
86a86ec338SBruce M Simpson 	}
87a86ec338SBruce M Simpson 
88a86ec338SBruce M Simpson 	sleep(5);
89a86ec338SBruce M Simpson 
90a86ec338SBruce M Simpson 	/* Make sure the data is there when we try to receive it. */
91a86ec338SBruce M Simpson 	if (recvfrom(s, (void *)-1, 1, 0, NULL, NULL) != -1)
92a86ec338SBruce M Simpson 		errx(-1,"recvfrom succeeded when failure expected");
93a86ec338SBruce M Simpson 
94a86ec338SBruce M Simpson 	(void)signal(SIGALRM, handle_sigalrm);
95a86ec338SBruce M Simpson 	if (setjmp(myjmpbuf) == 0) {
96a86ec338SBruce M Simpson 		/*
97a86ec338SBruce M Simpson 	 	 * This recvfrom will panic an unpatched system, and block
98a86ec338SBruce M Simpson 		 * a patched one.
99a86ec338SBruce M Simpson 		 */
100a86ec338SBruce M Simpson 		alarm(5);
101a86ec338SBruce M Simpson 		(void)recvfrom(s, (void *)-1, 1, 0, NULL, NULL);
102a86ec338SBruce M Simpson 	}
103a86ec338SBruce M Simpson 
104a86ec338SBruce M Simpson 	/* We should reach here via longjmp() and all should be well. */
105a86ec338SBruce M Simpson 
106a86ec338SBruce M Simpson 	return (0);
107a86ec338SBruce M Simpson }
108