xref: /freebsd/tools/test/stress2/misc/pts3.sh (revision faf25f48d601ae39f5752602f3020e2e92605625)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2019 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# "panic: Most recently used by tty" seen.
31# Reported by syzbot+c9b6206303bf47bac87e@syzkaller.appspotmail.com
32# Fixed by r349733
33
34[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
35kldstat -v | grep -q pty || { kldload pty || exit 0; }
36
37. ../default.cfg
38
39here=`pwd`
40cd /tmp
41sed '1,/^EOF/d' < $here/$0 > pts3.c
42mycc -o pts3 -Wall -Wextra -O2 pts3.c || exit 1
43rm -f pts3.c
44
45/tmp/pts3; s=$?
46
47rm -f /tmp/pts3
48exit $s
49EOF
50#include <sys/param.h>
51#include <sys/mman.h>
52#include <sys/wait.h>
53
54#include <machine/atomic.h>
55
56#include <err.h>
57#include <errno.h>
58#include <fcntl.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <time.h>
62#include <unistd.h>
63
64static volatile u_int *share;
65#define SYNC 0
66
67int
68main(void)
69{
70	pid_t p, pid;
71	size_t len;
72	time_t start;
73        int fd;
74	char path[128];
75
76	len = PAGE_SIZE;
77	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
78	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
79		err(1, "mmap");
80
81	p = getpid();
82        sprintf(path, "/dev/ptmx");
83	start = time(NULL);
84	while (time(NULL) - start < 60) {
85		share[SYNC] = 0;
86		if ((fd = open(path, O_RDWR)) == -1)
87			err(1,"open()");
88		if ((pid = fork()) == 0) {
89			while (share[SYNC] == 0)
90				;
91			_exit(0);
92		}
93		share[SYNC] = 1;
94		if (fcntl(fd, F_SETOWN, p) == -1)
95			warn("fcntl()");
96		close(fd);
97		if (waitpid(pid, NULL, 0) != pid)
98			err(1, "waitpid()");
99	}
100
101        return (0);
102}
103