1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2002
3f08c3bdfSopenharmony_ci *   Copyright (c) 2020 Petr Vorel <pvorel@suse.cz>
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/* 12/23/2002   Port to LTP     robbiew@us.ibm.com */
21f08c3bdfSopenharmony_ci/* 06/30/2001   Port to Linux   nsharoff@us.ibm.com */
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#define _GNU_SOURCE
24f08c3bdfSopenharmony_ci#include <errno.h>
25f08c3bdfSopenharmony_ci#include <stdio.h>
26f08c3bdfSopenharmony_ci#include <stdlib.h>
27f08c3bdfSopenharmony_ci#include <stdio.h>
28f08c3bdfSopenharmony_ci#include <termios.h>
29f08c3bdfSopenharmony_ci#include <fcntl.h>
30f08c3bdfSopenharmony_ci#include <sys/stat.h>
31f08c3bdfSopenharmony_ci#include <sys/poll.h>
32f08c3bdfSopenharmony_ci#include <sys/types.h>
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci#include "test.h"
35f08c3bdfSopenharmony_ci#include "safe_macros.h"
36f08c3bdfSopenharmony_ci#include "lapi/ioctl.h"
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cichar *TCID = "ptem01";		/* Test program identifier.    */
39f08c3bdfSopenharmony_ciint TST_TOTAL = 6;		/* Total number of test cases. */
40f08c3bdfSopenharmony_ci/**************/
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci/*
43f08c3bdfSopenharmony_ci * pty master clone device
44f08c3bdfSopenharmony_ci */
45f08c3bdfSopenharmony_ci#define MASTERCLONE "/dev/ptmx"
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci#define BUFSZ 4096
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci/*
50f08c3bdfSopenharmony_ci * test termio/termios ioctls
51f08c3bdfSopenharmony_ci */
52f08c3bdfSopenharmony_ciint test1(void)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	int masterfd, slavefd;
55f08c3bdfSopenharmony_ci	char *slavename;
56f08c3bdfSopenharmony_ci	struct termio termio;
57f08c3bdfSopenharmony_ci	struct termios termios;
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	masterfd = SAFE_OPEN(NULL, MASTERCLONE, O_RDWR);
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	slavename = ptsname(masterfd);
62f08c3bdfSopenharmony_ci	if (slavename == NULL) {
63f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "ptsname() call failed");
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	if (grantpt(masterfd) != 0) {
67f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "grantpt() call failed");
68f08c3bdfSopenharmony_ci	}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	if (unlockpt(masterfd) != 0) {
71f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "unlockpt() call failed");
72f08c3bdfSopenharmony_ci	}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	if ((slavefd = open(slavename, O_RDWR)) < 0) {
75f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "Could not open %s", slavename);
76f08c3bdfSopenharmony_ci	}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCGETS, &termios) != 0) {
79f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCGETS");
80f08c3bdfSopenharmony_ci	}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCSETS, &termios) != 0) {
83f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCSETS");
84f08c3bdfSopenharmony_ci	}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCSETSW, &termios) != 0) {
87f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCSETSW");
88f08c3bdfSopenharmony_ci	}
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCSETSF, &termios) != 0) {
91f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCSETSF");
92f08c3bdfSopenharmony_ci	}
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCSETS, &termios) != 0) {
95f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCSETS");
96f08c3bdfSopenharmony_ci	}
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCGETA, &termio) != 0) {
99f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCGETA");
100f08c3bdfSopenharmony_ci	}
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCSETA, &termio) != 0) {
103f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCSETA");
104f08c3bdfSopenharmony_ci	}
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCSETAW, &termio) != 0) {
107f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCSETAW");
108f08c3bdfSopenharmony_ci	}
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCSETAF, &termio) != 0) {
111f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCSETAF");
112f08c3bdfSopenharmony_ci	}
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	if (close(slavefd) != 0) {
115f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close slave");
116f08c3bdfSopenharmony_ci	}
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_ci	if (close(masterfd) != 0) {
119f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close master");
120f08c3bdfSopenharmony_ci	}
121f08c3bdfSopenharmony_ci	tst_resm(TPASS, "test1");
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_ci	/** NOT REACHED **/
124f08c3bdfSopenharmony_ci	return 0;
125f08c3bdfSopenharmony_ci}
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci/*
128f08c3bdfSopenharmony_ci * test window size setting and getting
129f08c3bdfSopenharmony_ci */
130f08c3bdfSopenharmony_ciint test2(void)
131f08c3bdfSopenharmony_ci{
132f08c3bdfSopenharmony_ci	int masterfd, slavefd;
133f08c3bdfSopenharmony_ci	char *slavename;
134f08c3bdfSopenharmony_ci	struct winsize wsz;
135f08c3bdfSopenharmony_ci	struct winsize wsz1 = { 24, 80, 5, 10 };
136f08c3bdfSopenharmony_ci	struct winsize wsz2 = { 60, 100, 11, 777 };
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci	masterfd = SAFE_OPEN(NULL, MASTERCLONE, O_RDWR);
139f08c3bdfSopenharmony_ci
140f08c3bdfSopenharmony_ci	slavename = ptsname(masterfd);
141f08c3bdfSopenharmony_ci	if (slavename == NULL) {
142f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "ptsname() call failed");
143f08c3bdfSopenharmony_ci	}
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci	if (grantpt(masterfd) != 0) {
146f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "grantpt() call failed");
147f08c3bdfSopenharmony_ci	}
148f08c3bdfSopenharmony_ci
149f08c3bdfSopenharmony_ci	if (unlockpt(masterfd) != 0) {
150f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "unlockpt() call failed");
151f08c3bdfSopenharmony_ci	}
152f08c3bdfSopenharmony_ci
153f08c3bdfSopenharmony_ci	if ((slavefd = open(slavename, O_RDWR)) < 0) {
154f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "Could not open %s", slavename);
155f08c3bdfSopenharmony_ci	}
156f08c3bdfSopenharmony_ci
157f08c3bdfSopenharmony_ci	if (ioctl(masterfd, TIOCSWINSZ, &wsz1) != 0) {
158f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TIOCSWINSZ");
159f08c3bdfSopenharmony_ci	}
160f08c3bdfSopenharmony_ci
161f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TIOCGWINSZ, &wsz) != 0) {
162f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TIOCGWINSZ");
163f08c3bdfSopenharmony_ci	}
164f08c3bdfSopenharmony_ci
165f08c3bdfSopenharmony_ci	if (wsz.ws_row != wsz1.ws_row || wsz.ws_col != wsz1.ws_col ||
166f08c3bdfSopenharmony_ci	    wsz.ws_xpixel != wsz1.ws_xpixel ||
167f08c3bdfSopenharmony_ci	    wsz.ws_ypixel != wsz1.ws_ypixel) {
168f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "unexpected window size returned");
169f08c3bdfSopenharmony_ci	}
170f08c3bdfSopenharmony_ci
171f08c3bdfSopenharmony_ci	if (ioctl(masterfd, TIOCGWINSZ, &wsz) != 0) {
172f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TIOCGWINSZ");
173f08c3bdfSopenharmony_ci	}
174f08c3bdfSopenharmony_ci
175f08c3bdfSopenharmony_ci	if (wsz.ws_row != wsz1.ws_row || wsz.ws_col != wsz1.ws_col ||
176f08c3bdfSopenharmony_ci	    wsz.ws_xpixel != wsz1.ws_xpixel ||
177f08c3bdfSopenharmony_ci	    wsz.ws_ypixel != wsz1.ws_ypixel) {
178f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "unexpected window size returned");
179f08c3bdfSopenharmony_ci	}
180f08c3bdfSopenharmony_ci
181f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TIOCSWINSZ, &wsz2) != 0) {
182f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TIOCSWINSZ");
183f08c3bdfSopenharmony_ci	}
184f08c3bdfSopenharmony_ci
185f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TIOCGWINSZ, &wsz) != 0) {
186f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TIOCGWINSZ");
187f08c3bdfSopenharmony_ci	}
188f08c3bdfSopenharmony_ci
189f08c3bdfSopenharmony_ci	if (wsz.ws_row != wsz2.ws_row || wsz.ws_col != wsz2.ws_col ||
190f08c3bdfSopenharmony_ci	    wsz.ws_xpixel != wsz2.ws_xpixel ||
191f08c3bdfSopenharmony_ci	    wsz.ws_ypixel != wsz2.ws_ypixel) {
192f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "unexpected window size returned");
193f08c3bdfSopenharmony_ci	}
194f08c3bdfSopenharmony_ci
195f08c3bdfSopenharmony_ci	if (close(slavefd) != 0) {
196f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close");
197f08c3bdfSopenharmony_ci	}
198f08c3bdfSopenharmony_ci
199f08c3bdfSopenharmony_ci	if (close(masterfd) != 0) {
200f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close");
201f08c3bdfSopenharmony_ci	}
202f08c3bdfSopenharmony_ci	tst_resm(TPASS, "test2");
203f08c3bdfSopenharmony_ci
204f08c3bdfSopenharmony_ci	/** NOT REACHED **/
205f08c3bdfSopenharmony_ci	return 0;
206f08c3bdfSopenharmony_ci}
207f08c3bdfSopenharmony_ci
208f08c3bdfSopenharmony_ci/*
209f08c3bdfSopenharmony_ci * test sending a break
210f08c3bdfSopenharmony_ci */
211f08c3bdfSopenharmony_ciint test3(void)
212f08c3bdfSopenharmony_ci{
213f08c3bdfSopenharmony_ci	int masterfd, slavefd;
214f08c3bdfSopenharmony_ci	char *slavename;
215f08c3bdfSopenharmony_ci
216f08c3bdfSopenharmony_ci	masterfd = SAFE_OPEN(NULL, MASTERCLONE, O_RDWR);
217f08c3bdfSopenharmony_ci
218f08c3bdfSopenharmony_ci	slavename = ptsname(masterfd);
219f08c3bdfSopenharmony_ci	if (slavename == NULL) {
220f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "ptsname() call failed");
221f08c3bdfSopenharmony_ci	}
222f08c3bdfSopenharmony_ci
223f08c3bdfSopenharmony_ci	if (grantpt(masterfd) != 0) {
224f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "grantpt() call failed");
225f08c3bdfSopenharmony_ci	}
226f08c3bdfSopenharmony_ci
227f08c3bdfSopenharmony_ci	if (unlockpt(masterfd) != 0) {
228f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "unlockpt() call failed");
229f08c3bdfSopenharmony_ci	}
230f08c3bdfSopenharmony_ci
231f08c3bdfSopenharmony_ci	if ((slavefd = open(slavename, O_RDWR)) < 0) {
232f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "Could not open %s", slavename);
233f08c3bdfSopenharmony_ci	}
234f08c3bdfSopenharmony_ci
235f08c3bdfSopenharmony_ci	if (tcsendbreak(masterfd, 10) != 0) {
236f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "tcsendbreak");
237f08c3bdfSopenharmony_ci	}
238f08c3bdfSopenharmony_ci
239f08c3bdfSopenharmony_ci	if (tcsendbreak(slavefd, 10) != 0) {
240f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "tcsendbreak");
241f08c3bdfSopenharmony_ci	}
242f08c3bdfSopenharmony_ci
243f08c3bdfSopenharmony_ci	if (close(slavefd) != 0) {
244f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close slave");
245f08c3bdfSopenharmony_ci	}
246f08c3bdfSopenharmony_ci
247f08c3bdfSopenharmony_ci	if (close(masterfd) != 0) {
248f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close master");
249f08c3bdfSopenharmony_ci	}
250f08c3bdfSopenharmony_ci	tst_resm(TPASS, "test3");
251f08c3bdfSopenharmony_ci
252f08c3bdfSopenharmony_ci	/** NOT REACHED **/
253f08c3bdfSopenharmony_ci	return 0;
254f08c3bdfSopenharmony_ci}
255f08c3bdfSopenharmony_ci
256f08c3bdfSopenharmony_ci/*
257f08c3bdfSopenharmony_ci * test multiple opens of slave side
258f08c3bdfSopenharmony_ci */
259f08c3bdfSopenharmony_ciint test4(void)
260f08c3bdfSopenharmony_ci{
261f08c3bdfSopenharmony_ci	int masterfd, slavefd, slavefd2, slavefd3;
262f08c3bdfSopenharmony_ci	char *slavename;
263f08c3bdfSopenharmony_ci
264f08c3bdfSopenharmony_ci	masterfd = SAFE_OPEN(NULL, MASTERCLONE, O_RDWR);
265f08c3bdfSopenharmony_ci
266f08c3bdfSopenharmony_ci	slavename = ptsname(masterfd);
267f08c3bdfSopenharmony_ci	if (slavename == NULL) {
268f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "ptsname() call failed");
269f08c3bdfSopenharmony_ci	}
270f08c3bdfSopenharmony_ci
271f08c3bdfSopenharmony_ci	if (grantpt(masterfd) != 0) {
272f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "grantpt() call failed");
273f08c3bdfSopenharmony_ci	}
274f08c3bdfSopenharmony_ci
275f08c3bdfSopenharmony_ci	if (unlockpt(masterfd) != 0) {
276f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "unlockpt() call failed");
277f08c3bdfSopenharmony_ci	}
278f08c3bdfSopenharmony_ci
279f08c3bdfSopenharmony_ci	if ((slavefd = open(slavename, O_RDWR)) < 0) {
280f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "Could not open %s", slavename);
281f08c3bdfSopenharmony_ci	}
282f08c3bdfSopenharmony_ci
283f08c3bdfSopenharmony_ci	if ((slavefd2 = open(slavename, O_RDWR)) < 0) {
284f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "Could not open %s (again)", slavename);
285f08c3bdfSopenharmony_ci	}
286f08c3bdfSopenharmony_ci
287f08c3bdfSopenharmony_ci	if ((slavefd3 = open(slavename, O_RDWR)) < 0) {
288f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "Could not open %s (once more)",
289f08c3bdfSopenharmony_ci			 slavename);
290f08c3bdfSopenharmony_ci	}
291f08c3bdfSopenharmony_ci
292f08c3bdfSopenharmony_ci	if (close(slavefd) != 0) {
293f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close slave");
294f08c3bdfSopenharmony_ci	}
295f08c3bdfSopenharmony_ci	if (close(slavefd2) != 0) {
296f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close slave again");
297f08c3bdfSopenharmony_ci	}
298f08c3bdfSopenharmony_ci	if (close(slavefd3) != 0) {
299f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close slave once more");
300f08c3bdfSopenharmony_ci	}
301f08c3bdfSopenharmony_ci	if (close(masterfd) != 0) {
302f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close master");
303f08c3bdfSopenharmony_ci	}
304f08c3bdfSopenharmony_ci	tst_resm(TPASS, "test4");
305f08c3bdfSopenharmony_ci
306f08c3bdfSopenharmony_ci	/** NOT REACHED **/
307f08c3bdfSopenharmony_ci	return 0;
308f08c3bdfSopenharmony_ci}
309f08c3bdfSopenharmony_ci
310f08c3bdfSopenharmony_ci#define NUMOPENS 6
311f08c3bdfSopenharmony_ci
312f08c3bdfSopenharmony_ci/*
313f08c3bdfSopenharmony_ci * test several simultaneous opens
314f08c3bdfSopenharmony_ci */
315f08c3bdfSopenharmony_ciint test5(void)
316f08c3bdfSopenharmony_ci{
317f08c3bdfSopenharmony_ci	static int masterfd[NUMOPENS];
318f08c3bdfSopenharmony_ci	static int slavefd[NUMOPENS];
319f08c3bdfSopenharmony_ci	char *slavename;
320f08c3bdfSopenharmony_ci	int i;
321f08c3bdfSopenharmony_ci
322f08c3bdfSopenharmony_ci	for (i = 0; i < NUMOPENS; ++i) {
323f08c3bdfSopenharmony_ci		masterfd[i] = open(MASTERCLONE, O_RDWR);
324f08c3bdfSopenharmony_ci		if (masterfd[i] < 0) {
325f08c3bdfSopenharmony_ci			tst_resm(TBROK, "%s", MASTERCLONE);
326f08c3bdfSopenharmony_ci			tst_resm(TBROK, "out of ptys");
327f08c3bdfSopenharmony_ci			for (i = 0; i < NUMOPENS; ++i) {
328f08c3bdfSopenharmony_ci				if (masterfd[i] != 0) {
329f08c3bdfSopenharmony_ci					(void)close(masterfd[i]);
330f08c3bdfSopenharmony_ci				}
331f08c3bdfSopenharmony_ci				if (slavefd[i] != 0) {
332f08c3bdfSopenharmony_ci					(void)close(slavefd[i]);
333f08c3bdfSopenharmony_ci				}
334f08c3bdfSopenharmony_ci			}
335f08c3bdfSopenharmony_ci			tst_exit();
336f08c3bdfSopenharmony_ci		}
337f08c3bdfSopenharmony_ci
338f08c3bdfSopenharmony_ci		slavename = ptsname(masterfd[i]);
339f08c3bdfSopenharmony_ci		if (slavename == NULL) {
340f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TERRNO, NULL,
341f08c3bdfSopenharmony_ci				 "ptsname() call failed");
342f08c3bdfSopenharmony_ci		}
343f08c3bdfSopenharmony_ci
344f08c3bdfSopenharmony_ci		if (grantpt(masterfd[i]) != 0) {
345f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TERRNO, NULL,
346f08c3bdfSopenharmony_ci				 "grantpt() call failed");
347f08c3bdfSopenharmony_ci		}
348f08c3bdfSopenharmony_ci
349f08c3bdfSopenharmony_ci		if (unlockpt(masterfd[i]) != 0) {
350f08c3bdfSopenharmony_ci			tst_brkm(TBROK, NULL, "unlockpt() call failed");
351f08c3bdfSopenharmony_ci		}
352f08c3bdfSopenharmony_ci
353f08c3bdfSopenharmony_ci		if ((slavefd[i] = open(slavename, O_RDWR)) < 0) {
354f08c3bdfSopenharmony_ci			tst_brkm(TFAIL, NULL,
355f08c3bdfSopenharmony_ci				 "Iteration %d: Could not open %s", i,
356f08c3bdfSopenharmony_ci				 slavename);
357f08c3bdfSopenharmony_ci		}
358f08c3bdfSopenharmony_ci
359f08c3bdfSopenharmony_ci	}
360f08c3bdfSopenharmony_ci
361f08c3bdfSopenharmony_ci	for (i = 0; i < NUMOPENS; ++i) {
362f08c3bdfSopenharmony_ci		if (close(slavefd[i]) != 0) {
363f08c3bdfSopenharmony_ci			tst_brkm(TBROK, NULL, "Iteration %d: close slave", i);
364f08c3bdfSopenharmony_ci		}
365f08c3bdfSopenharmony_ci		if (close(masterfd[i]) != 0) {
366f08c3bdfSopenharmony_ci			tst_brkm(TBROK, NULL, "close master");
367f08c3bdfSopenharmony_ci		}
368f08c3bdfSopenharmony_ci	}
369f08c3bdfSopenharmony_ci	tst_resm(TPASS, "test5");
370f08c3bdfSopenharmony_ci
371f08c3bdfSopenharmony_ci	/** NOT REACHED **/
372f08c3bdfSopenharmony_ci	return 0;
373f08c3bdfSopenharmony_ci}
374f08c3bdfSopenharmony_ci
375f08c3bdfSopenharmony_ci/*
376f08c3bdfSopenharmony_ci * test hangup semantics
377f08c3bdfSopenharmony_ci */
378f08c3bdfSopenharmony_ciint test6(void)
379f08c3bdfSopenharmony_ci{
380f08c3bdfSopenharmony_ci	static int masterfd;
381f08c3bdfSopenharmony_ci	static int slavefd;
382f08c3bdfSopenharmony_ci	char *slavename;
383f08c3bdfSopenharmony_ci	struct termios termios;
384f08c3bdfSopenharmony_ci
385f08c3bdfSopenharmony_ci	masterfd = SAFE_OPEN(NULL, MASTERCLONE, O_RDWR);
386f08c3bdfSopenharmony_ci
387f08c3bdfSopenharmony_ci	slavename = ptsname(masterfd);
388f08c3bdfSopenharmony_ci	if (slavename == NULL) {
389f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "ptsname() call failed");
390f08c3bdfSopenharmony_ci	}
391f08c3bdfSopenharmony_ci
392f08c3bdfSopenharmony_ci	if (grantpt(masterfd) != 0) {
393f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, NULL, "grantpt() call failed");
394f08c3bdfSopenharmony_ci	}
395f08c3bdfSopenharmony_ci
396f08c3bdfSopenharmony_ci	if (unlockpt(masterfd) != 0) {
397f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "unlockpt() call failed");
398f08c3bdfSopenharmony_ci	}
399f08c3bdfSopenharmony_ci
400f08c3bdfSopenharmony_ci	if ((slavefd = open(slavename, O_RDWR)) < 0) {
401f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "Could not open %s", slavename);
402f08c3bdfSopenharmony_ci	}
403f08c3bdfSopenharmony_ci
404f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCGETS, &termios) != 0) {
405f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCGETS");
406f08c3bdfSopenharmony_ci	}
407f08c3bdfSopenharmony_ci
408f08c3bdfSopenharmony_ci	termios.c_cflag &= ~CBAUD;
409f08c3bdfSopenharmony_ci	termios.c_cflag |= B0 & CBAUD;
410f08c3bdfSopenharmony_ci	if (ioctl(slavefd, TCSETS, &termios) != 0) {
411f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "TCGETS");
412f08c3bdfSopenharmony_ci	}
413f08c3bdfSopenharmony_ci
414f08c3bdfSopenharmony_ci	if (close(slavefd) != 0) {
415f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close");
416f08c3bdfSopenharmony_ci	}
417f08c3bdfSopenharmony_ci	if (close(masterfd) != 0) {
418f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "close");
419f08c3bdfSopenharmony_ci	}
420f08c3bdfSopenharmony_ci	tst_resm(TPASS, "test6");
421f08c3bdfSopenharmony_ci
422f08c3bdfSopenharmony_ci	/** NOT REACHED **/
423f08c3bdfSopenharmony_ci	return 0;
424f08c3bdfSopenharmony_ci}
425f08c3bdfSopenharmony_ci
426f08c3bdfSopenharmony_ci/*
427f08c3bdfSopenharmony_ci * main test driver
428f08c3bdfSopenharmony_ci */
429f08c3bdfSopenharmony_ciint main(void)
430f08c3bdfSopenharmony_ci{
431f08c3bdfSopenharmony_ci	test1();
432f08c3bdfSopenharmony_ci	test2();
433f08c3bdfSopenharmony_ci	test3();
434f08c3bdfSopenharmony_ci	test4();
435f08c3bdfSopenharmony_ci	test5();
436f08c3bdfSopenharmony_ci	test6();
437f08c3bdfSopenharmony_ci	/*
438f08c3bdfSopenharmony_ci	 * all done
439f08c3bdfSopenharmony_ci	 */
440f08c3bdfSopenharmony_ci	tst_exit();
441f08c3bdfSopenharmony_ci}
442