xref: /freebsd/tools/test/stress2/misc/pts2.sh (revision 2774f206809b8fd3a4904fe945f029a414fbc642)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
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# pts leak seen.
30# Fixed in r313496.
31
32[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33kldstat -v | grep -q pty || { kldload pty || exit 0; }
34
35. ../default.cfg
36
37here=`pwd`
38cd /tmp
39sed '1,/^EOF/d' < $here/$0 > pts2.c
40mycc -o pts2 -Wall -Wextra -O2 pts2.c || exit 1
41rm -f pts2.c
42
43pts=`vmstat -m | grep pts | awk '{print $2}'`
44for i in `jot 10`; do
45	/tmp/pts2
46done
47new=`vmstat -m | grep pts | awk '{print $2}'`
48s=0
49[ $((new - pts)) -gt 1 ] && { s=1; echo "Leaked $((new - pts)) pts."; }
50
51rm -f /tmp/pts2
52exit $s
53EOF
54#include <sys/ioctl.h>
55
56#include <err.h>
57#include <fcntl.h>
58#include <stdio.h>
59
60const char *master = "/dev/ptmx";
61int
62main(void)
63{
64        int fd, fd2, slave;
65        char sl[80];
66
67        if ((fd = open(master, O_RDONLY)) == -1)
68                err(1, "open(%s)", master);
69
70        if (ioctl(fd, TIOCGPTN, &slave) == -1)
71                err(1, "ioctl");
72
73        snprintf(sl, sizeof(sl), "/dev/pts/%d", slave);
74
75        if ((fd2 = open(sl, O_RDONLY | O_EXLOCK)) == -1)
76                err(1, "open(%s)", sl);
77
78        return (0);
79}
80