Path: blob/main/tools/test/stress2/testcases/pty/pty.c
39566 views
/*-1* Copyright (c) 2008 Peter Holm <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25*/2627/* Test PTYs */2829#include <sys/types.h>30#include <sys/ioctl.h>31#include <err.h>32#include <libutil.h>33#include <stdio.h>34#include <stdlib.h>35#include <termios.h>36#include <unistd.h>3738#include "stress.h"3940#define TXT "Hello, world!"4142int43setup(int nb __unused)44{45return (0);46}4748void49cleanup(void)50{51}5253int54test(void)55{56struct termios tios;57int i, master, slave;58int s[32], m[32];59char buf[512], slname[1025];6061for (i = 0; i < 32; i++) {62if (openpty(&m[i], &s[i], slname, NULL, NULL) == -1)63err(1, "openpty");64}65for (i = 0; i < 32; i++) {66close(m[i]);67close(s[i]);68}6970for (i = 0; i < 1024; i++) {71if (openpty(&m[0], &s[0], slname, NULL, NULL) == -1)72err(1, "openpty");73close(m[0]);74close(s[0]);75}7677for (i = 0; i < 10 && done_testing == 0; i++) {78if (openpty(&master, &slave, slname, NULL, NULL) == -1)79err(1, "openpty");80if ((i & 1) == 0) {81if (close(master) == -1)82err(1, "close(master)");83if (close(slave) == -1)84err(1, "close(%s)", slname);85} else {86if (close(slave) == -1)87err(1, "close(%s)", slname);88if (close(master) == -1)89err(1, "close(master)");90}91}9293if (openpty(&master, &slave, slname, NULL, NULL) == -1)94err(1, "openpty");95if (tcgetattr(slave, &tios) < 0)96err(1, "tcgetattr(%s)", slname);97cfmakeraw(&tios);98if (tcsetattr(slave, TCSAFLUSH, &tios) < 0)99err(1, "tcsetattr(%s)", slname);100101for (i = 0; i < 64 && done_testing == 0; i++) {102if (write(master, TXT, sizeof(TXT)) == -1)103err(1, "write");104if (read(slave, buf, sizeof(TXT)) == -1)105err(1, "read(%s)", slname);106}107close(master);108close(slave);109return (0);110}111112113