xref: /freebsd/tools/test/stress2/misc/pcatch.sh (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# The issue makes it possible for applications to get wrong EINTR
30# or ERESTART (the later is not directly visible) when doing write
31# to the file on suspended UFS volume. I.e. the thread is sleeping
32# when fs is suspended, and process is signaled.
33
34# Test scenario mostly by kib.
35# Fixed in r275744.
36
37# Deadlock seen:
38# https://people.freebsd.org/~pho/stress/log/pcatch.txt
39
40[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
41[ -z "$DEBUG" ] && exit 0 # Waiting for fix
42
43. ../default.cfg
44
45here=`pwd`
46cd /tmp
47sed '1,/^EOF/d' < $here/$0 > pcatch.c
48mycc -o pcatch -Wall -Wextra -O0 -g pcatch.c || exit 1
49rm -f pcatch.c
50cd $here
51
52mount | grep -q "$mntpoint" && umount $mntpoint
53mdconfig -l | grep -q $mdstart &&  mdconfig -d -u $mdstart
54mdconfig -a -t swap -s 1g -u $mdstart
55newfs $newfs_flags md$mdstart > /dev/null
56
57mount /dev/md$mdstart $mntpoint
58
59start=`date '+%s'`
60while [ $((`date '+%s'` - start)) -lt 120 ]; do
61	/tmp/pcatch $mntpoint
62done
63
64while mount | grep -q "on $mntpoint "; do
65	umount $mntpoint || sleep 1
66done
67mdconfig -d -u $mdstart
68rm -f /tmp/pcatch
69exit 0
70EOF
71#include <sys/types.h>
72#include <sys/ioctl.h>
73#include <sys/mount.h>
74#include <sys/wait.h>
75
76#include <ufs/ffs/fs.h>
77
78#include <err.h>
79#include <errno.h>
80#include <fcntl.h>
81#include <signal.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <string.h>
85#include <unistd.h>
86
87static void
88hand_sigaction(int signo __unused, siginfo_t *si __unused, void *c __unused)
89{
90}
91
92static void
93suspend(char *path)
94{
95        struct statfs s;
96        int fd, error;
97
98	if (fork() == 0) {
99		if ((error = statfs(path, &s)) != 0)
100			err(1, "statfs %s", path);
101		fd = open("/dev/ufssuspend", O_RDWR);
102		if ((error = ioctl(fd, UFSSUSPEND, &s.f_fsid)) != 0)
103			err(1, "UFSSUSPEND");
104		sleep(1);
105		if ((error = ioctl(fd, UFSRESUME, &s.f_fsid)) != 0)
106			err(1, "UFSRESUME");
107		_exit(0);
108	}
109}
110
111static void
112test(char *mp)
113{
114	pid_t pid;
115	struct sigaction sa;
116	int fd;
117	char buf[80], file[80];
118
119	memset(&sa, 0, sizeof(sa));
120	sa.sa_sigaction = hand_sigaction;
121	if (sigaction(SIGUSR1, &sa, NULL) == -1)
122		err(1, "sigaction");
123
124	snprintf(file, sizeof(file), "%s/file", mp);
125	if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
126		err(1, "open(%s). %s:%d", file, __FILE__, __LINE__);
127
128	suspend(mp);
129
130	if ((pid = fork()) == 0) {
131		if (write(fd, buf, sizeof(buf)) == -1)
132			warn("FAIL: write");
133		_exit(0);
134	}
135	usleep(10000);
136	if (kill(pid, SIGUSR1) == -1)
137		err(1, "kill");
138	wait(NULL);
139	wait(NULL);
140	close(fd);
141}
142
143int
144main(int argc, char *argv[])
145{
146
147	if (argc != 2)
148		errx(1, "Usage: %s <UFS mount point>", argv[0]);
149
150	test(argv[1]);
151
152	return (0);
153}
154