1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker /* 12/24/2002 Port to LTP [email protected] */
21*49cdfc7eSAndroid Build Coastguard Worker /* 06/30/2001 Port to Linux [email protected] */
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/poll.h>
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker /** LTP Port **/
34*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
35*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "hangup01"; /* Test program identifier. */
38*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 5; /* Total number of test cases. */
39*49cdfc7eSAndroid Build Coastguard Worker /**************/
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker /*
42*49cdfc7eSAndroid Build Coastguard Worker * pty master clone device
43*49cdfc7eSAndroid Build Coastguard Worker */
44*49cdfc7eSAndroid Build Coastguard Worker #define MASTERCLONE "/dev/ptmx"
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker #define MESSAGE1 "I love Linux!"
47*49cdfc7eSAndroid Build Coastguard Worker #define MESSAGE2 "Use the LTP for all your Linux testing needs."
48*49cdfc7eSAndroid Build Coastguard Worker #define MESSAGE3 "For the latest version of the LTP tests, visit http://ltp.sourceforge.net"
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker #define NUMMESSAGES 3
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker #define BUFSZ 4096
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker pid_t childpid;
57*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)58*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker int status;
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker if (0 < childpid) {
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker /* If the PID is still alive... */
66*49cdfc7eSAndroid Build Coastguard Worker if (kill(childpid, 0) == 0 || errno == ESRCH) {
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker /* KILL IT! */
69*49cdfc7eSAndroid Build Coastguard Worker (void)kill(childpid, 15);
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker /* And take care of any leftover zombies. */
72*49cdfc7eSAndroid Build Coastguard Worker if (waitpid(childpid, &status, WNOHANG) < 0) {
73*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN | TERRNO,
74*49cdfc7eSAndroid Build Coastguard Worker "waitpid(%d, ...) failed", childpid);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker }
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker /*
84*49cdfc7eSAndroid Build Coastguard Worker * parent process for hangup test
85*49cdfc7eSAndroid Build Coastguard Worker */
parent(int masterfd,int childpid)86*49cdfc7eSAndroid Build Coastguard Worker void parent(int masterfd, int childpid)
87*49cdfc7eSAndroid Build Coastguard Worker {
88*49cdfc7eSAndroid Build Coastguard Worker char buf[BUFSZ];
89*49cdfc7eSAndroid Build Coastguard Worker struct pollfd pollfds[1];
90*49cdfc7eSAndroid Build Coastguard Worker size_t len = strlen(MESSAGE1);
91*49cdfc7eSAndroid Build Coastguard Worker int hangupcount = 0;
92*49cdfc7eSAndroid Build Coastguard Worker int datacount = 0;
93*49cdfc7eSAndroid Build Coastguard Worker int status;
94*49cdfc7eSAndroid Build Coastguard Worker int i;
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker pollfds[0].fd = masterfd;
97*49cdfc7eSAndroid Build Coastguard Worker pollfds[0].events = POLLIN;
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker sleep(1);
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker while ((i = poll(pollfds, 1, -1)) == 1) {
102*49cdfc7eSAndroid Build Coastguard Worker if (read(masterfd, buf, len) == -1) {
103*49cdfc7eSAndroid Build Coastguard Worker ++hangupcount;
104*49cdfc7eSAndroid Build Coastguard Worker #ifdef DEBUG
105*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "hangup %d", hangupcount);
106*49cdfc7eSAndroid Build Coastguard Worker #endif
107*49cdfc7eSAndroid Build Coastguard Worker if (hangupcount == NUMMESSAGES) {
108*49cdfc7eSAndroid Build Coastguard Worker break;
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker } else {
111*49cdfc7eSAndroid Build Coastguard Worker ++datacount;
112*49cdfc7eSAndroid Build Coastguard Worker switch (datacount) {
113*49cdfc7eSAndroid Build Coastguard Worker case 1:
114*49cdfc7eSAndroid Build Coastguard Worker if (strncmp(buf, MESSAGE1,
115*49cdfc7eSAndroid Build Coastguard Worker strlen(MESSAGE1)) != 0) {
116*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, cleanup,
117*49cdfc7eSAndroid Build Coastguard Worker "unexpected message 1");
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker len = strlen(MESSAGE2);
120*49cdfc7eSAndroid Build Coastguard Worker break;
121*49cdfc7eSAndroid Build Coastguard Worker case 2:
122*49cdfc7eSAndroid Build Coastguard Worker if (strncmp(buf, MESSAGE2,
123*49cdfc7eSAndroid Build Coastguard Worker strlen(MESSAGE2)) != 0) {
124*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, cleanup,
125*49cdfc7eSAndroid Build Coastguard Worker "unexpected message 2");
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker len = strlen(MESSAGE3);
128*49cdfc7eSAndroid Build Coastguard Worker break;
129*49cdfc7eSAndroid Build Coastguard Worker case 3:
130*49cdfc7eSAndroid Build Coastguard Worker if (strncmp(buf, MESSAGE3,
131*49cdfc7eSAndroid Build Coastguard Worker strlen(MESSAGE3)) != 0) {
132*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, cleanup,
133*49cdfc7eSAndroid Build Coastguard Worker "unexpected message 3");
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker break;
136*49cdfc7eSAndroid Build Coastguard Worker default:
137*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, cleanup,
138*49cdfc7eSAndroid Build Coastguard Worker "unexpected data message");
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker }
141*49cdfc7eSAndroid Build Coastguard Worker }
142*49cdfc7eSAndroid Build Coastguard Worker }
143*49cdfc7eSAndroid Build Coastguard Worker if (i != 1) {
144*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, cleanup, "poll");
145*49cdfc7eSAndroid Build Coastguard Worker }
146*49cdfc7eSAndroid Build Coastguard Worker while (waitpid(childpid, &status, WNOHANG) < 0 && errno != ESRCH) ;
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker tst_resm((status == 0 ? TPASS : TFAIL),
149*49cdfc7eSAndroid Build Coastguard Worker "child process exited with status %d", status);
150*49cdfc7eSAndroid Build Coastguard Worker }
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard Worker /*
153*49cdfc7eSAndroid Build Coastguard Worker * Child process for hangup test. Write three messages to the slave
154*49cdfc7eSAndroid Build Coastguard Worker * pty, with a hangup after each.
155*49cdfc7eSAndroid Build Coastguard Worker */
child(int masterfd)156*49cdfc7eSAndroid Build Coastguard Worker int child(int masterfd)
157*49cdfc7eSAndroid Build Coastguard Worker {
158*49cdfc7eSAndroid Build Coastguard Worker int slavefd;
159*49cdfc7eSAndroid Build Coastguard Worker char *slavename;
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker if ((slavename = ptsname(masterfd)) == NULL) {
162*49cdfc7eSAndroid Build Coastguard Worker printf("ptsname[child] failed: %s\n", strerror(errno));
163*49cdfc7eSAndroid Build Coastguard Worker return 1;
164*49cdfc7eSAndroid Build Coastguard Worker }
165*49cdfc7eSAndroid Build Coastguard Worker if ((slavefd = open(slavename, O_RDWR)) < 0) {
166*49cdfc7eSAndroid Build Coastguard Worker printf("open[1] failed: %s\n", strerror(errno));
167*49cdfc7eSAndroid Build Coastguard Worker return 1;
168*49cdfc7eSAndroid Build Coastguard Worker }
169*49cdfc7eSAndroid Build Coastguard Worker if (write(slavefd, MESSAGE1, strlen(MESSAGE1)) != strlen(MESSAGE1)) {
170*49cdfc7eSAndroid Build Coastguard Worker printf("write failed: %s\n", strerror(errno));
171*49cdfc7eSAndroid Build Coastguard Worker return 1;
172*49cdfc7eSAndroid Build Coastguard Worker }
173*49cdfc7eSAndroid Build Coastguard Worker if (close(slavefd) != 0) {
174*49cdfc7eSAndroid Build Coastguard Worker printf("close[1] failed: %s\n", strerror(errno));
175*49cdfc7eSAndroid Build Coastguard Worker return 1;
176*49cdfc7eSAndroid Build Coastguard Worker }
177*49cdfc7eSAndroid Build Coastguard Worker if ((slavefd = open(slavename, O_RDWR)) < 0) {
178*49cdfc7eSAndroid Build Coastguard Worker printf("open[2] failed: %s\n", strerror(errno));
179*49cdfc7eSAndroid Build Coastguard Worker return 1;
180*49cdfc7eSAndroid Build Coastguard Worker }
181*49cdfc7eSAndroid Build Coastguard Worker if (write(slavefd, MESSAGE2, strlen(MESSAGE2)) != strlen(MESSAGE2)) {
182*49cdfc7eSAndroid Build Coastguard Worker printf("write[2] failed: %s\n", strerror(errno));
183*49cdfc7eSAndroid Build Coastguard Worker return 1;
184*49cdfc7eSAndroid Build Coastguard Worker }
185*49cdfc7eSAndroid Build Coastguard Worker if (close(slavefd) != 0) {
186*49cdfc7eSAndroid Build Coastguard Worker printf("close[2] failed: %s\n", strerror(errno));
187*49cdfc7eSAndroid Build Coastguard Worker return 1;
188*49cdfc7eSAndroid Build Coastguard Worker }
189*49cdfc7eSAndroid Build Coastguard Worker if ((slavefd = open(slavename, O_RDWR)) < 0) {
190*49cdfc7eSAndroid Build Coastguard Worker printf("open[3] failed: %s\n", strerror(errno));
191*49cdfc7eSAndroid Build Coastguard Worker return 1;
192*49cdfc7eSAndroid Build Coastguard Worker }
193*49cdfc7eSAndroid Build Coastguard Worker if (write(slavefd, MESSAGE3, strlen(MESSAGE3)) != strlen(MESSAGE3)) {
194*49cdfc7eSAndroid Build Coastguard Worker printf("write[3] failed: %s\n", strerror(errno));
195*49cdfc7eSAndroid Build Coastguard Worker return 1;
196*49cdfc7eSAndroid Build Coastguard Worker }
197*49cdfc7eSAndroid Build Coastguard Worker if (close(slavefd) != 0) {
198*49cdfc7eSAndroid Build Coastguard Worker printf("close[3] failed: %s\n", strerror(errno));
199*49cdfc7eSAndroid Build Coastguard Worker return 1;
200*49cdfc7eSAndroid Build Coastguard Worker }
201*49cdfc7eSAndroid Build Coastguard Worker return 0;
202*49cdfc7eSAndroid Build Coastguard Worker }
203*49cdfc7eSAndroid Build Coastguard Worker
204*49cdfc7eSAndroid Build Coastguard Worker /*
205*49cdfc7eSAndroid Build Coastguard Worker * main test driver
206*49cdfc7eSAndroid Build Coastguard Worker */
main(void)207*49cdfc7eSAndroid Build Coastguard Worker int main(void)
208*49cdfc7eSAndroid Build Coastguard Worker {
209*49cdfc7eSAndroid Build Coastguard Worker int masterfd; /* master pty fd */
210*49cdfc7eSAndroid Build Coastguard Worker char *slavename;
211*49cdfc7eSAndroid Build Coastguard Worker pid_t childpid;
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker /*--------------------------------------------------------------------*/
214*49cdfc7eSAndroid Build Coastguard Worker masterfd = SAFE_OPEN(NULL, MASTERCLONE, O_RDWR);
215*49cdfc7eSAndroid Build Coastguard Worker
216*49cdfc7eSAndroid Build Coastguard Worker slavename = ptsname(masterfd);
217*49cdfc7eSAndroid Build Coastguard Worker if (slavename == NULL)
218*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, NULL, "ptsname");
219*49cdfc7eSAndroid Build Coastguard Worker
220*49cdfc7eSAndroid Build Coastguard Worker if (grantpt(masterfd) != 0)
221*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, NULL, "grantpt");
222*49cdfc7eSAndroid Build Coastguard Worker
223*49cdfc7eSAndroid Build Coastguard Worker if (unlockpt(masterfd) != 0)
224*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, NULL, "unlockpt");
225*49cdfc7eSAndroid Build Coastguard Worker
226*49cdfc7eSAndroid Build Coastguard Worker childpid = fork();
227*49cdfc7eSAndroid Build Coastguard Worker if (childpid == -1)
228*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, NULL, "fork");
229*49cdfc7eSAndroid Build Coastguard Worker else if (childpid == 0)
230*49cdfc7eSAndroid Build Coastguard Worker exit(child(masterfd));
231*49cdfc7eSAndroid Build Coastguard Worker else
232*49cdfc7eSAndroid Build Coastguard Worker parent(masterfd, childpid);
233*49cdfc7eSAndroid Build Coastguard Worker /*--------------------------------------------------------------------*/
234*49cdfc7eSAndroid Build Coastguard Worker cleanup();
235*49cdfc7eSAndroid Build Coastguard Worker
236*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
237*49cdfc7eSAndroid Build Coastguard Worker }
238