Path: blob/main/tools/regression/sockets/pr_atomic/pr_atomic.c
48255 views
/*-1* Copyright (c) 2006 Bruce M. Simpson2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Regression test for uiomove in kernel; specifically for PR kern/38495.28*/2930#include <sys/types.h>31#include <sys/socket.h>32#include <sys/un.h>3334#include <stdlib.h>35#include <signal.h>36#include <setjmp.h>37#include <string.h>38#include <err.h>39#include <errno.h>40#include <unistd.h>4142static char socket_path[] = "tmp.XXXXXX";4344static jmp_buf myjmpbuf;4546static void handle_sigalrm(int signo __unused)47{48longjmp(myjmpbuf, 1);49}5051int52main(void)53{54struct sockaddr_un un;55pid_t pid;56int s;5758if (mkstemp(socket_path) == -1)59err(1, "mkstemp");60s = socket(PF_LOCAL, SOCK_DGRAM, 0);61if (s == -1)62errx(-1, "socket");63memset(&un, 0, sizeof(un));64un.sun_family = AF_LOCAL;65unlink(socket_path);66strcpy(un.sun_path, socket_path);67if (bind(s, (struct sockaddr *)&un, sizeof(un)) == -1)68errx(-1, "bind");69pid = fork();70if (pid == -1)71errx(-1, "fork");72if (pid == 0) {73int conn;74char buf[] = "AAAAAAAAA";7576close(s);77conn = socket(AF_LOCAL, SOCK_DGRAM, 0);78if (conn == -1)79errx(-1,"socket");80if (sendto(conn, buf, sizeof(buf), 0, (struct sockaddr *)&un,81sizeof(un)) != sizeof(buf))82errx(-1,"sendto");83close(conn);84_exit(0);85}8687sleep(5);8889/* Make sure the data is there when we try to receive it. */90if (recvfrom(s, (void *)-1, 1, 0, NULL, NULL) != -1)91errx(-1,"recvfrom succeeded when failure expected");9293(void)signal(SIGALRM, handle_sigalrm);94if (setjmp(myjmpbuf) == 0) {95/*96* This recvfrom will panic an unpatched system, and block97* a patched one.98*/99alarm(5);100(void)recvfrom(s, (void *)-1, 1, 0, NULL, NULL);101}102103/* We should reach here via longjmp() and all should be well. */104105return (0);106}107108109