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