pr_atomic.c revision 165503
11303Swesolows/*- 21303Swesolows * Copyright (c) 2006 Bruce M. Simpson 31303Swesolows * All rights reserved. 41303Swesolows * 51303Swesolows * Redistribution and use in source and binary forms, with or without 61303Swesolows * modification, are permitted provided that the following conditions 71303Swesolows * are met: 81303Swesolows * 1. Redistributions of source code must retain the above copyright 91303Swesolows * notice, this list of conditions and the following disclaimer. 101303Swesolows * 2. Redistributions in binary form must reproduce the above copyright 111303Swesolows * notice, this list of conditions and the following disclaimer in the 121303Swesolows * documentation and/or other materials provided with the distribution. 131303Swesolows * 141303Swesolows * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 151303Swesolows * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 161303Swesolows * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 171303Swesolows * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 181303Swesolows * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 191303Swesolows * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 201303Swesolows * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211303Swesolows * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 221303Swesolows * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 237197Sstephh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 241303Swesolows * SUCH DAMAGE. 251303Swesolows * 261303Swesolows * $FreeBSD: head/tools/regression/sockets/pr_atomic/pr_atomic.c 165503 2006-12-23 21:07:07Z bms $ 271303Swesolows */ 281303Swesolows 291303Swesolows/* 301303Swesolows * Regression test for uiomove in kernel; specifically for PR kern/38495. 317197Sstephh */ 327197Sstephh 337197Sstephh#include <sys/types.h> 347197Sstephh#include <sys/socket.h> 357197Sstephh#include <sys/un.h> 367197Sstephh 371303Swesolows#include <stdlib.h> 381303Swesolows#include <signal.h> 391303Swesolows#include <setjmp.h> 40#include <string.h> 41#include <err.h> 42#include <errno.h> 43#include <unistd.h> 44 45#define TEST_SOCKET "/tmp/test_socket" 46 47static jmp_buf myjmpbuf; 48 49void handle_sigalrm(int signo); 50 51void handle_sigalrm(int signo) 52{ 53 longjmp(myjmpbuf, 1); 54} 55 56int 57main(int argc, char *argv[]) 58{ 59 struct sockaddr_un un; 60 pid_t pid; 61 int s; 62 63 s = socket(PF_LOCAL, SOCK_DGRAM, 0); 64 if (s == -1) 65 errx(-1, "socket"); 66 memset(&un, 0, sizeof(un)); 67 un.sun_family = AF_LOCAL; 68 unlink(TEST_SOCKET); 69 strcpy(un.sun_path, TEST_SOCKET); 70 if (bind(s, (struct sockaddr *)&un, sizeof(un)) == -1) 71 errx(-1, "bind"); 72 pid = fork(); 73 if (pid == -1) 74 errx(-1, "fork"); 75 if (pid == 0) { 76 int conn; 77 char buf[] = "AAAAAAAAA"; 78 79 close(s); 80 conn = socket(AF_LOCAL, SOCK_DGRAM, 0); 81 if (conn == -1) 82 errx(-1,"socket"); 83 if (sendto(conn, buf, sizeof(buf), 0, (struct sockaddr *)&un, 84 sizeof(un)) != sizeof(buf)) 85 errx(-1,"sendto"); 86 close(conn); 87 _exit(0); 88 } 89 90 sleep(5); 91 92 /* Make sure the data is there when we try to receive it. */ 93 if (recvfrom(s, (void *)-1, 1, 0, NULL, NULL) != -1) 94 errx(-1,"recvfrom succeeded when failure expected"); 95 96 (void)signal(SIGALRM, handle_sigalrm); 97 if (setjmp(myjmpbuf) == 0) { 98 /* 99 * This recvfrom will panic an unpatched system, and block 100 * a patched one. 101 */ 102 alarm(5); 103 (void)recvfrom(s, (void *)-1, 1, 0, NULL, NULL); 104 } 105 106 /* We should reach here via longjmp() and all should be well. */ 107 108 return (0); 109} 110