Home | History | Annotate | Line # | Download | only in kernel
      1  1.2  gutterid /*	$NetBSD: t_open_pr_57260.c,v 1.2 2023/04/23 00:46:46 gutteridge Exp $	*/
      2  1.1  gutterid 
      3  1.1  gutterid /*-
      4  1.1  gutterid  * Copyright (c) 2023 The NetBSD Foundation, Inc.
      5  1.1  gutterid  * All rights reserved.
      6  1.1  gutterid  *
      7  1.1  gutterid  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  gutterid  * by Taylor R. Campbell and David H. Gutteridge.
      9  1.1  gutterid  *
     10  1.1  gutterid  * Redistribution and use in source and binary forms, with or without
     11  1.1  gutterid  * modification, are permitted provided that the following conditions
     12  1.1  gutterid  * are met:
     13  1.1  gutterid  * 1. Redistributions of source code must retain the above copyright
     14  1.1  gutterid  *    notice, this list of conditions and the following disclaimer.
     15  1.1  gutterid  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  gutterid  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  gutterid  *    documentation and/or other materials provided with the distribution.
     18  1.1  gutterid  *
     19  1.1  gutterid  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  gutterid  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  gutterid  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  gutterid  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  gutterid  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  gutterid  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  gutterid  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  gutterid  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  gutterid  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  gutterid  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  gutterid  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  gutterid  */
     31  1.1  gutterid 
     32  1.1  gutterid #include <sys/cdefs.h>
     33  1.2  gutterid __RCSID("$NetBSD: t_open_pr_57260.c,v 1.2 2023/04/23 00:46:46 gutteridge Exp $");
     34  1.1  gutterid 
     35  1.1  gutterid #include <sys/stat.h>
     36  1.1  gutterid 
     37  1.1  gutterid #include <fcntl.h>
     38  1.1  gutterid #include <setjmp.h>
     39  1.1  gutterid #include <signal.h>
     40  1.1  gutterid #include <unistd.h>
     41  1.1  gutterid 
     42  1.1  gutterid #include <atf-c.h>
     43  1.1  gutterid #include "h_macros.h"
     44  1.1  gutterid 
     45  1.1  gutterid static jmp_buf env;
     46  1.1  gutterid static sig_atomic_t alarmed = 0;
     47  1.1  gutterid 
     48  1.1  gutterid static void
     49  1.1  gutterid on_alarm(int sig)
     50  1.1  gutterid {
     51  1.1  gutterid 
     52  1.2  gutterid 	if (!alarmed) {
     53  1.2  gutterid 		alarmed = 1;
     54  1.2  gutterid 		alarm(1);
     55  1.2  gutterid 	} else {
     56  1.2  gutterid 		longjmp(env, 1);
     57  1.2  gutterid 	}
     58  1.1  gutterid }
     59  1.1  gutterid 
     60  1.1  gutterid ATF_TC(openrestartsignal);
     61  1.1  gutterid ATF_TC_HEAD(openrestartsignal, tc)
     62  1.1  gutterid {
     63  1.1  gutterid 
     64  1.1  gutterid 	atf_tc_set_md_var(tc, "descr", "open(2) should restart on signal "
     65  1.1  gutterid 	    "(PR kern/57260)");
     66  1.1  gutterid }
     67  1.1  gutterid 
     68  1.1  gutterid ATF_TC_BODY(openrestartsignal, tc)
     69  1.1  gutterid {
     70  1.1  gutterid 	struct sigaction sa;
     71  1.1  gutterid 	int fd;
     72  1.1  gutterid 
     73  1.1  gutterid 	memset(&sa, 0, sizeof(sa));
     74  1.1  gutterid 	sa.sa_handler = on_alarm;
     75  1.1  gutterid 	(void)sigfillset(&sa.sa_mask);
     76  1.1  gutterid 	sa.sa_flags = SA_RESTART;
     77  1.1  gutterid 	if (sigaction(SIGALRM, &sa, NULL) == -1)
     78  1.1  gutterid 		atf_tc_fail_errno("sigaction");
     79  1.1  gutterid 
     80  1.1  gutterid 	if (mkfifo("fifo", 0600) == -1)
     81  1.1  gutterid 		atf_tc_fail_errno("mkfifo");
     82  1.1  gutterid 	alarm(1);
     83  1.1  gutterid 	if (setjmp(env))
     84  1.1  gutterid 		/* second signal handler longjmped out */
     85  1.1  gutterid 		return;
     86  1.1  gutterid 	fd = open("fifo", O_RDONLY);
     87  1.1  gutterid 	if (fd == -1)
     88  1.1  gutterid 		atf_tc_fail_errno("open");
     89  1.1  gutterid 	atf_tc_fail("open returned %d", fd);
     90  1.1  gutterid }
     91  1.1  gutterid 
     92  1.1  gutterid ATF_TP_ADD_TCS(tp)
     93  1.1  gutterid {
     94  1.1  gutterid 
     95  1.1  gutterid 	ATF_TP_ADD_TC(tp, openrestartsignal);
     96  1.1  gutterid 
     97  1.1  gutterid 	return atf_no_error();
     98  1.1  gutterid }
     99