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