1272343Sngie/*	$NetBSD: t_mountd.c,v 1.5 2012/02/24 13:53:46 joerg Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2010 The NetBSD Foundation, Inc.
5272343Sngie *
6272343Sngie * Redistribution and use in source and binary forms, with or without
7272343Sngie * modification, are permitted provided that the following conditions
8272343Sngie * are met:
9272343Sngie * 1. Redistributions of source code must retain the above copyright
10272343Sngie *    notice, this list of conditions and the following disclaimer.
11272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie *    notice, this list of conditions and the following disclaimer in the
13272343Sngie *    documentation and/or other materials provided with the distribution.
14272343Sngie *
15272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16272343Sngie * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17272343Sngie * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18272343Sngie * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21272343Sngie * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25272343Sngie * SUCH DAMAGE.
26272343Sngie */
27272343Sngie
28272343Sngie#include <sys/types.h>
29272343Sngie#include <sys/mount.h>
30272343Sngie
31272343Sngie#include <atf-c.h>
32272343Sngie#include <errno.h>
33272343Sngie#include <fcntl.h>
34272343Sngie#include <pthread.h>
35272343Sngie#include <stdio.h>
36272343Sngie#include <stdlib.h>
37272343Sngie#include <unistd.h>
38272343Sngie#include <string.h>
39272343Sngie#include <signal.h>
40272343Sngie
41272343Sngie#include <rump/rump.h>
42272343Sngie#include <rump/rump_syscalls.h>
43272343Sngie
44272343Sngie#include "../../h_macros.h"
45272343Sngie#include "../common/h_fsmacros.h"
46272343Sngie
47272343SngieATF_TC(mountdhup);
48272343SngieATF_TC_HEAD(mountdhup, tc)
49272343Sngie{
50272343Sngie
51272343Sngie	atf_tc_set_md_var(tc, "descr", "test for service interrupt while "
52272343Sngie	    "mountd handles SIGHUP");
53272343Sngie}
54272343Sngie
55272343Sngiestatic volatile int quit;
56272343Sngie
57272343Sngiestatic void *
58272343Sngiewrkwrkwrk(void *unused)
59272343Sngie{
60272343Sngie	int fd, fail;
61272343Sngie
62272343Sngie	fail = 0;
63272343Sngie
64272343Sngie	rump_sys_chdir(FSTEST_MNTNAME);
65272343Sngie	while (!quit) {
66272343Sngie		fd = rump_sys_open("file", O_RDWR | O_CREAT);
67272343Sngie		if (fd == -1) {
68272343Sngie			if (errno == EACCES) {
69272343Sngie				fail++;
70272343Sngie				break;
71272343Sngie			}
72272343Sngie		}
73272343Sngie		rump_sys_close(fd);
74272343Sngie		if (rump_sys_unlink("file") == -1) {
75272343Sngie			if (errno == EACCES) {
76272343Sngie				fail++;
77272343Sngie				break;
78272343Sngie			}
79272343Sngie		}
80272343Sngie	}
81272343Sngie	rump_sys_chdir("/");
82272343Sngie	quit = 1;
83272343Sngie
84272343Sngie	return fail ? wrkwrkwrk : NULL;
85272343Sngie}
86272343Sngie
87272343SngieATF_TC_BODY(mountdhup, tc)
88272343Sngie{
89272343Sngie	pthread_t pt;
90272343Sngie	struct nfstestargs *nfsargs;
91272343Sngie	void *voidargs;
92272343Sngie	int attempts;
93272343Sngie	void *fail;
94272343Sngie
95272343Sngie	FSTEST_CONSTRUCTOR(tc, nfs, voidargs);
96272343Sngie	nfsargs = voidargs;
97272343Sngie
98272343Sngie	pthread_create(&pt, NULL, wrkwrkwrk, NULL);
99272343Sngie	for (attempts = 100; attempts && !quit; attempts--) {
100272343Sngie		usleep(100000);
101272343Sngie		kill(nfsargs->ta_childpid, SIGHUP);
102272343Sngie	}
103272343Sngie	quit = 1;
104272343Sngie	pthread_join(pt, &fail);
105272343Sngie
106272343Sngie	FSTEST_DESTRUCTOR(tc, nfs, voidargs);
107272343Sngie
108272343Sngie	atf_tc_expect_fail("PR kern/5844");
109272343Sngie	if (fail)
110272343Sngie		atf_tc_fail("op failed with EACCES");
111272343Sngie	else
112272343Sngie		atf_tc_fail("race did not trigger this time");
113272343Sngie}
114272343Sngie
115272343SngieATF_TP_ADD_TCS(tp)
116272343Sngie{
117272343Sngie
118272343Sngie	ATF_TP_ADD_TC(tp, mountdhup);
119272343Sngie
120272343Sngie	return atf_no_error();
121272343Sngie}
122