3 * Copyright (c) International Business Machines Corp., 2002
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* 12/24/2002 Port to LTP robbiew@us.ibm.com */
21 /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
32 #include <sys/fcntl.h>
41 char *TCID
="hangup01"; /* Test program identifier. */
42 int TST_TOTAL
=5; /* Total number of test cases. */
43 extern int Tst_count
; /* Test Case counter for tst_* routines */
47 * pty master clone device
49 #define MASTERCLONE "/dev/ptmx"
51 #define MESSAGE1 "I love Linux!"
52 #define MESSAGE2 "Use the LTP for all your Linux testing needs."
53 #define MESSAGE3 "For the latest version of the LTP tests, visit http://ltp.sourceforge.net"
60 * parent process for hangup test
63 parent(int masterfd
, int childpid
)
66 struct pollfd pollfds
[1];
71 int len
= strlen(MESSAGE1
);
73 pollfds
[0].fd
= masterfd
;
74 pollfds
[0].events
= POLLIN
;
78 while ((i
= poll(pollfds
, 1, -1)) == 1) {
79 if (read(masterfd
, buf
, len
) == -1) {
82 tst_resm(TINFO
,"hangup %d", hangupcount
);
84 if (hangupcount
== NUMMESSAGES
) {
91 if (strncmp(buf
, MESSAGE1
,
92 strlen(MESSAGE1
)) != 0) {
93 tst_resm(TFAIL
, "unexpected message 1");
96 len
= strlen(MESSAGE2
);
99 if (strncmp(buf
, MESSAGE2
,
100 strlen(MESSAGE2
)) != 0) {
101 tst_resm(TFAIL
, "unexpected message 2");
104 len
= strlen(MESSAGE3
);
107 if (strncmp(buf
, MESSAGE3
,
108 strlen(MESSAGE3
)) != 0) {
109 tst_resm(TFAIL
, "unexpected message 3");
114 tst_resm(TFAIL
, "unexpected data message");
121 tst_resm(TFAIL
,"poll");
124 while (wait(&status
) != childpid
) {
128 tst_resm(TFAIL
, "child process exited with status %d", status
);
131 tst_resm(TPASS
,"Pass");
140 * Child process for hangup test. Write three messages to the slave
141 * pty, with a hangup after each.
150 if ((slavename
= ptsname(masterfd
)) == (char *)0) {
151 tst_resm(TBROK
,"ptsname");
154 if ((slavefd
= open(slavename
, O_RDWR
)) < 0) {
155 tst_resm(TBROK
,slavename
);
158 if (write(slavefd
, MESSAGE1
, strlen(MESSAGE1
)) != strlen(MESSAGE1
)) {
159 tst_resm(TBROK
,"write");
162 if (close(slavefd
) != 0) {
163 tst_resm(TBROK
,"close");
166 if ((slavefd
= open(slavename
, O_RDWR
)) < 0) {
167 tst_resm(TBROK
,"open %s",slavename
);
170 if (write(slavefd
, MESSAGE2
, strlen(MESSAGE2
)) != strlen(MESSAGE2
)) {
171 tst_resm(TBROK
,"write");
174 if (close(slavefd
) != 0) {
175 tst_resm(TBROK
,"close");
178 if ((slavefd
= open(slavename
, O_RDWR
)) < 0) {
179 tst_resm(TBROK
,"open %s",slavename
);
182 if (write(slavefd
, MESSAGE3
, strlen(MESSAGE3
)) != strlen(MESSAGE3
)) {
183 tst_resm(TBROK
,"write");
186 if (close(slavefd
) != 0) {
187 tst_resm(TBROK
,"close");
195 int main(int argc
, char **argv
)
197 int masterfd
; /* master pty fd */
201 /*--------------------------------------------------------------------*/
202 masterfd
= open(MASTERCLONE
, O_RDWR
);
204 tst_resm(TBROK
,"open %s",MASTERCLONE
);
208 slavename
= ptsname(masterfd
);
209 if (slavename
== (char *)0) {
210 tst_resm(TBROK
,"ptsname");
214 if (grantpt(masterfd
) != 0) {
215 tst_resm(TBROK
,"grantpt");
219 if (unlockpt(masterfd
) != 0) {
220 tst_resm(TBROK
,"unlockpt");
225 if (childpid
== -1) {
226 tst_resm(TBROK
,"fork");
228 } else if (childpid
== 0) {
232 parent(masterfd
, childpid
);
234 /*--------------------------------------------------------------------*/