4 * Objetive: The purpose of this test is to make sure that select works
7 * Adapted from test11.c (pipe test).
12 #include <sys/types.h>
14 #include <sys/asynchio.h>
17 #include <sys/select.h>
25 void pipehandler(int sig
)
30 void do_child(int pty_fds
[])
32 /* reads from pipe and prints out the data */
37 struct timeval timeout
;
38 signal(SIGPIPE
, pipehandler
);
39 signal(SIGUSR1
, pipehandler
);
40 /* first, close the write part, since it is not needed */
41 close(pty_fds
[PARENTFD
]);
45 FD_ZERO(&fds_exception
);
46 FD_SET(pty_fds
[CHILDFD
], &fds_read
);
47 FD_SET(pty_fds
[CHILDFD
], &fds_exception
);
50 retval
= select(pty_fds
[CHILDFD
]+2, &fds_read
, NULL
, &fds_exception
, &timeout
);
53 fprintf(stderr
, "child: Error in select\n");
55 } else printf("child select: %d\n", retval
);
56 if (FD_ISSET(pty_fds
[CHILDFD
], &fds_exception
)) {
57 printf("child: exception fd set. quitting.\n");
60 if (FD_ISSET(pty_fds
[CHILDFD
], &fds_read
)) {
61 printf("child: read fd set. reading.\n");
62 if ((retval
= read(pty_fds
[CHILDFD
], data
, sizeof(data
))) < 0) {
64 fprintf(stderr
, "child: couldn't read from pty\n");
68 fprintf(stderr
, "child: eof on pty\n");
72 printf("pid %d pty reads (%d): %s\n", getpid(), retval
, data
);
73 } else printf("child: no fd set\n");
78 void do_parent(int pty_fds
[])
83 signal(SIGPIPE
, pipehandler
);
84 signal(SIGUSR1
, pipehandler
);
85 /* first, close the read part of pty, since it is not needed */
86 close(pty_fds
[CHILDFD
]);
87 /* now enter a loop of read user input, and writing it to the pty */
90 FD_SET(pty_fds
[PARENTFD
], &fds_write
);
91 printf("pid %d Waiting for pty ready to write on %s...\n",
93 retval
= select(pty_fds
[PARENTFD
]+2, NULL
, &fds_write
, NULL
, NULL
);
96 fprintf(stderr
, "Parent: Error in select\n");
99 printf("Input data: ");
101 printf("parent: eof; exiting\n");
104 if (!strcmp(data
, "exit"))
106 if (!FD_ISSET(pty_fds
[PARENTFD
], &fds_write
)) {
107 fprintf(stderr
, "parent: write fd not set?! retrying\n");
110 retval
= write(pty_fds
[PARENTFD
], data
, 1024);
113 fprintf(stderr
, "Error writing on pty\n");
115 } else printf("wrote %d\n", retval
);
117 /* got exit from user */
118 close(pty_fds
[PARENTFD
]); /* close pty, let child know we're done */
120 printf("Child exited with status: %d\n", retval
);
123 int main(int argc
, char *argv
[])
128 if(openpty(&ptys
[0], &ptys
[1], name
, NULL
, NULL
) < 0) {
132 printf("Using %s\n", name
);
135 fprintf(stderr
, "Error forking\n");
138 if (pid
== 0) /* child proc */