2005-07-19 18:03:39 +02:00
|
|
|
/*
|
|
|
|
* Test name: test14.c
|
|
|
|
*
|
|
|
|
* Objetive: The purpose of this test is to make sure that select works
|
|
|
|
* with ptys.
|
|
|
|
*
|
|
|
|
* Adapted from test11.c (pipe test).
|
|
|
|
*
|
|
|
|
* Ben Gras
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/asynchio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <libutil.h>
|
|
|
|
|
2005-08-05 15:47:12 +02:00
|
|
|
char name[100];
|
|
|
|
|
2005-07-19 18:03:39 +02:00
|
|
|
void pipehandler(int sig)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-08-05 18:48:44 +02:00
|
|
|
#define CHILDFD 1
|
|
|
|
#define PARENTFD 0
|
|
|
|
|
2005-07-19 18:03:39 +02:00
|
|
|
void do_child(int pty_fds[])
|
|
|
|
{
|
|
|
|
/* reads from pipe and prints out the data */
|
|
|
|
char data[2048];
|
|
|
|
int retval;
|
|
|
|
fd_set fds_read;
|
|
|
|
fd_set fds_exception;
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
signal(SIGPIPE, pipehandler);
|
|
|
|
signal(SIGUSR1, pipehandler);
|
|
|
|
|
|
|
|
/* first, close the write part, since it is not needed */
|
2005-08-05 18:48:44 +02:00
|
|
|
close(pty_fds[PARENTFD]);
|
2005-07-19 18:03:39 +02:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
FD_ZERO(&fds_read);
|
|
|
|
FD_ZERO(&fds_exception);
|
2005-08-05 18:48:44 +02:00
|
|
|
FD_SET(pty_fds[CHILDFD], &fds_read);
|
|
|
|
FD_SET(pty_fds[CHILDFD], &fds_exception);
|
2005-07-19 18:03:39 +02:00
|
|
|
timeout.tv_sec = 5;
|
|
|
|
timeout.tv_usec = 0;
|
2005-08-05 21:19:18 +02:00
|
|
|
retval = select(pty_fds[CHILDFD]+2, &fds_read, NULL, &fds_exception, &timeout);
|
2005-07-19 18:03:39 +02:00
|
|
|
if (retval == -1) {
|
|
|
|
perror("select");
|
|
|
|
fprintf(stderr, "child: Error in select\n");
|
|
|
|
continue;
|
|
|
|
} else printf("child select: %d\n", retval);
|
2005-08-05 18:48:44 +02:00
|
|
|
if (FD_ISSET(pty_fds[CHILDFD], &fds_exception)) {
|
2005-07-19 18:03:39 +02:00
|
|
|
printf("child: exception fd set. quitting.\n");
|
|
|
|
break;
|
|
|
|
}
|
2005-08-05 18:48:44 +02:00
|
|
|
if (FD_ISSET(pty_fds[CHILDFD], &fds_read)) {
|
2005-07-19 18:03:39 +02:00
|
|
|
printf("child: read fd set. reading.\n");
|
2005-08-05 18:48:44 +02:00
|
|
|
if ((retval = read(pty_fds[CHILDFD], data, sizeof(data))) < 0) {
|
2005-07-19 18:03:39 +02:00
|
|
|
perror("read");
|
2005-08-05 18:48:44 +02:00
|
|
|
fprintf(stderr, "child: couldn't read from pty\n");
|
2005-07-19 18:03:39 +02:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if(retval == 0) {
|
2005-08-05 18:48:44 +02:00
|
|
|
fprintf(stderr, "child: eof on pty\n");
|
2005-07-19 18:03:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
data[retval] = '\0';
|
2005-08-05 18:48:44 +02:00
|
|
|
printf("pid %d pty reads (%d): %s\n", getpid(), retval, data);
|
2005-07-19 18:03:39 +02:00
|
|
|
} else printf("child: no fd set\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_parent(int pty_fds[])
|
|
|
|
{
|
|
|
|
char data[1024];
|
|
|
|
int retval;
|
|
|
|
fd_set fds_write;
|
|
|
|
|
|
|
|
signal(SIGPIPE, pipehandler);
|
|
|
|
signal(SIGUSR1, pipehandler);
|
|
|
|
|
|
|
|
/* first, close the read part of pty, since it is not needed */
|
2005-08-05 18:48:44 +02:00
|
|
|
close(pty_fds[CHILDFD]);
|
2005-07-19 18:03:39 +02:00
|
|
|
|
|
|
|
/* now enter a loop of read user input, and writing it to the pty */
|
|
|
|
while (1) {
|
|
|
|
FD_ZERO(&fds_write);
|
2005-08-05 18:48:44 +02:00
|
|
|
FD_SET(pty_fds[PARENTFD], &fds_write);
|
2005-08-05 15:47:12 +02:00
|
|
|
printf("pid %d Waiting for pty ready to write on %s...\n",
|
|
|
|
getpid(), name);
|
2005-08-05 21:19:18 +02:00
|
|
|
retval = select(pty_fds[PARENTFD]+2, NULL, &fds_write, NULL, NULL);
|
2005-07-19 18:03:39 +02:00
|
|
|
if (retval == -1) {
|
|
|
|
perror("select");
|
|
|
|
fprintf(stderr, "Parent: Error in select\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Input data: ");
|
|
|
|
if(!gets(data)) {
|
|
|
|
printf("parent: eof; exiting\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!strcmp(data, "exit"))
|
|
|
|
break;
|
2005-08-05 18:48:44 +02:00
|
|
|
if (!FD_ISSET(pty_fds[PARENTFD], &fds_write)) {
|
2005-07-19 18:03:39 +02:00
|
|
|
fprintf(stderr, "parent: write fd not set?! retrying\n");
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-05 21:19:18 +02:00
|
|
|
retval = write(pty_fds[PARENTFD], data, 1024);
|
2005-07-19 18:03:39 +02:00
|
|
|
if (retval == -1) {
|
|
|
|
perror("write");
|
|
|
|
fprintf(stderr, "Error writing on pty\n");
|
|
|
|
exit(-1);
|
2005-08-05 21:19:18 +02:00
|
|
|
} else printf("wrote %d\n", retval);
|
2005-07-19 18:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* got exit from user */
|
2005-08-05 18:48:44 +02:00
|
|
|
close(pty_fds[PARENTFD]); /* close pty, let child know we're done */
|
2005-07-19 18:03:39 +02:00
|
|
|
wait(&retval);
|
|
|
|
printf("Child exited with status: %d\n", retval);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2005-08-05 15:47:12 +02:00
|
|
|
int ptys[2];
|
2005-07-19 18:03:39 +02:00
|
|
|
int retval;
|
|
|
|
int pid;
|
|
|
|
|
2005-08-05 15:47:12 +02:00
|
|
|
if(openpty(&ptys[0], &ptys[1], name, NULL, NULL) < 0) {
|
2005-07-19 18:03:39 +02:00
|
|
|
perror("openpty");
|
|
|
|
return 1;
|
|
|
|
}
|
2005-08-05 15:47:12 +02:00
|
|
|
printf("Using %s\n", name);
|
2005-07-19 18:03:39 +02:00
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
fprintf(stderr, "Error forking\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0) /* child proc */
|
2005-08-05 15:47:12 +02:00
|
|
|
do_child(ptys);
|
2005-07-19 18:03:39 +02:00
|
|
|
else
|
2005-08-05 15:47:12 +02:00
|
|
|
do_parent(ptys);
|
2005-07-19 18:03:39 +02:00
|
|
|
|
|
|
|
/* not reached */
|
|
|
|
return 0;
|
|
|
|
}
|