Use a saner variant for computing the number of groups in cpuctl tests
[ltp-debian.git] / testcases / kernel / pty / hangup01.c
blobd2ce747ed12f93e6c6f42047487b48ca370b7a50
1 /*
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 */
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE 1
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <sys/fcntl.h>
33 #include <sys/wait.h>
34 #include <sys/poll.h>
36 /** LTP Port **/
37 #include "test.h"
38 #include "usctest.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 */
44 /**************/
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"
55 #define NUMMESSAGES 3
57 #define BUFSZ 4096
60 * parent process for hangup test
62 int
63 parent(int masterfd, int childpid)
65 char buf[BUFSZ];
66 struct pollfd pollfds[1];
67 int hangupcount = 0;
68 int datacount = 0;
69 int status;
70 int i;
71 int len = strlen(MESSAGE1);
73 pollfds[0].fd = masterfd;
74 pollfds[0].events = POLLIN;
76 sleep(1);
78 while ((i = poll(pollfds, 1, -1)) == 1) {
79 if (read(masterfd, buf, len) == -1) {
80 ++hangupcount;
81 #ifdef DEBUG
82 tst_resm(TINFO,"hangup %d", hangupcount);
83 #endif
84 if (hangupcount == NUMMESSAGES) {
85 break;
87 } else {
88 ++datacount;
89 switch (datacount) {
90 case 1:
91 if (strncmp(buf, MESSAGE1,
92 strlen(MESSAGE1)) != 0) {
93 tst_resm(TFAIL, "unexpected message 1");
94 tst_exit();
96 len = strlen(MESSAGE2);
97 break;
98 case 2:
99 if (strncmp(buf, MESSAGE2,
100 strlen(MESSAGE2)) != 0) {
101 tst_resm(TFAIL, "unexpected message 2");
102 tst_exit();
104 len = strlen(MESSAGE3);
105 break;
106 case 3:
107 if (strncmp(buf, MESSAGE3,
108 strlen(MESSAGE3)) != 0) {
109 tst_resm(TFAIL, "unexpected message 3");
110 tst_exit();
112 break;
113 default:
114 tst_resm(TFAIL, "unexpected data message");
115 tst_exit();
116 /*NOTREACHED*/
120 if (i != 1) {
121 tst_resm(TFAIL,"poll");
122 tst_exit();
124 while (wait(&status) != childpid) {
127 if (status != 0) {
128 tst_resm(TFAIL, "child process exited with status %d", status);
129 tst_exit();
131 tst_resm(TPASS,"Pass");
132 tst_exit();
134 /*NOTREACHED*/
135 return 0;
140 * Child process for hangup test. Write three messages to the slave
141 * pty, with a hangup after each.
143 void
144 child(int masterfd)
146 int slavefd;
147 char *slavename;
150 if ((slavename = ptsname(masterfd)) == (char *)0) {
151 tst_resm(TBROK,"ptsname");
152 tst_exit();
154 if ((slavefd = open(slavename, O_RDWR)) < 0) {
155 tst_resm(TBROK,slavename);
156 tst_exit();
158 if (write(slavefd, MESSAGE1, strlen(MESSAGE1)) != strlen(MESSAGE1)) {
159 tst_resm(TBROK,"write");
160 tst_exit();
162 if (close(slavefd) != 0) {
163 tst_resm(TBROK,"close");
164 tst_exit();
166 if ((slavefd = open(slavename, O_RDWR)) < 0) {
167 tst_resm(TBROK,"open %s",slavename);
168 tst_exit();
170 if (write(slavefd, MESSAGE2, strlen(MESSAGE2)) != strlen(MESSAGE2)) {
171 tst_resm(TBROK,"write");
172 tst_exit();
174 if (close(slavefd) != 0) {
175 tst_resm(TBROK,"close");
176 tst_exit();
178 if ((slavefd = open(slavename, O_RDWR)) < 0) {
179 tst_resm(TBROK,"open %s",slavename);
180 tst_exit();
182 if (write(slavefd, MESSAGE3, strlen(MESSAGE3)) != strlen(MESSAGE3)) {
183 tst_resm(TBROK,"write");
184 tst_exit();
186 if (close(slavefd) != 0) {
187 tst_resm(TBROK,"close");
188 tst_exit();
193 * main test driver
195 int main(int argc, char **argv)
197 int masterfd; /* master pty fd */
198 char *slavename;
199 int childpid;
201 /*--------------------------------------------------------------------*/
202 masterfd = open(MASTERCLONE, O_RDWR);
203 if (masterfd < 0) {
204 tst_resm(TBROK,"open %s",MASTERCLONE);
205 tst_exit();
208 slavename = ptsname(masterfd);
209 if (slavename == (char *)0) {
210 tst_resm(TBROK,"ptsname");
211 tst_exit();
214 if (grantpt(masterfd) != 0) {
215 tst_resm(TBROK,"grantpt");
216 tst_exit();
219 if (unlockpt(masterfd) != 0) {
220 tst_resm(TBROK,"unlockpt");
221 tst_exit();
224 childpid = fork();
225 if (childpid == -1) {
226 tst_resm(TBROK,"fork");
227 tst_exit();
228 } else if (childpid == 0) {
229 child(masterfd);
230 tst_exit();
231 } else {
232 parent(masterfd, childpid);
234 /*--------------------------------------------------------------------*/
235 tst_exit();
236 /*NOTREACHED*/
237 return 0;