xref: /freebsd/tools/test/stress2/misc/pcatch.sh (revision 52fba9a943cca518d2978f7588e99a35da37e5f2)
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
55bsdlabel -w md$mdstart auto
56newfs $newfs_flags md${mdstart}$part > /dev/null
57
58mount /dev/md${mdstart}$part $mntpoint
59
60start=`date '+%s'`
61while [ $((`date '+%s'` - start)) -lt 120 ]; do
62	/tmp/pcatch $mntpoint
63done
64
65while mount | grep -q "on $mntpoint "; do
66	umount $mntpoint || sleep 1
67done
68mdconfig -d -u $mdstart
69rm -f /tmp/pcatch
70exit 0
71EOF
72#include <sys/types.h>
73#include <sys/ioctl.h>
74#include <sys/mount.h>
75#include <sys/wait.h>
76
77#include <ufs/ffs/fs.h>
78
79#include <err.h>
80#include <errno.h>
81#include <fcntl.h>
82#include <signal.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86#include <unistd.h>
87
88static void
89hand_sigaction(int signo __unused, siginfo_t *si __unused, void *c __unused)
90{
91}
92
93static void
94suspend(char *path)
95{
96        struct statfs s;
97        int fd, error;
98
99	if (fork() == 0) {
100		if ((error = statfs(path, &s)) != 0)
101			err(1, "statfs %s", path);
102		fd = open("/dev/ufssuspend", O_RDWR);
103		if ((error = ioctl(fd, UFSSUSPEND, &s.f_fsid)) != 0)
104			err(1, "UFSSUSPEND");
105		sleep(1);
106		if ((error = ioctl(fd, UFSRESUME, &s.f_fsid)) != 0)
107			err(1, "UFSRESUME");
108		_exit(0);
109	}
110}
111
112static void
113test(char *mp)
114{
115	pid_t pid;
116	struct sigaction sa;
117	int fd;
118	char buf[80], file[80];
119
120	memset(&sa, 0, sizeof(sa));
121	sa.sa_sigaction = hand_sigaction;
122	if (sigaction(SIGUSR1, &sa, NULL) == -1)
123		err(1, "sigaction");
124
125	snprintf(file, sizeof(file), "%s/file", mp);
126	if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
127		err(1, "open(%s). %s:%d", file, __FILE__, __LINE__);
128
129	suspend(mp);
130
131	if ((pid = fork()) == 0) {
132		if (write(fd, buf, sizeof(buf)) == -1)
133			warn("FAIL: write");
134		_exit(0);
135	}
136	usleep(10000);
137	if (kill(pid, SIGUSR1) == -1)
138		err(1, "kill");
139	wait(NULL);
140	wait(NULL);
141	close(fd);
142}
143
144int
145main(int argc, char *argv[])
146{
147
148	if (argc != 2)
149		errx(1, "Usage: %s <UFS mount point>", argv[0]);
150
151	test(argv[1]);
152
153	return (0);
154}
155