Repository navigation
Expand file tree
/
Copy pathtest14.c
More file actions
144 lines (142 loc) · 3.46 KB
/
Copy pathtest14.c
File metadata and controls
144 lines (142 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* 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>
char name[100];
void pipehandler(int sig)
{
}
#define CHILDFD 1
#define PARENTFD 0
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 */
close(pty_fds[PARENTFD]);
while(1) {
FD_ZERO(&fds_read);
FD_ZERO(&fds_exception);
FD_SET(pty_fds[CHILDFD], &fds_read);
FD_SET(pty_fds[CHILDFD], &fds_exception);
timeout.tv_sec = 5;
timeout.tv_usec = 0;
retval = select(pty_fds[CHILDFD]+2, &fds_read, NULL, &fds_exception, &timeout);
if (retval == -1) {
perror("select");
fprintf(stderr, "child: Error in select\n");
continue;
} else printf("child select: %d\n", retval);
if (FD_ISSET(pty_fds[CHILDFD], &fds_exception)) {
printf("child: exception fd set. quitting.\n");
break;
}
if (FD_ISSET(pty_fds[CHILDFD], &fds_read)) {
printf("child: read fd set. reading.\n");
if ((retval = read(pty_fds[CHILDFD], data, sizeof(data))) < 0) {
perror("read");
fprintf(stderr, "child: couldn't read from pty\n");
exit(-1);
}
if(retval == 0) {
fprintf(stderr, "child: eof on pty\n");
break;
}
data[retval] = '\
74B6
0';
printf("pid %d pty reads (%d): %s\n", getpid(), retval, data);
} 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 */
close(pty_fds[CHILDFD]);
/* now enter a loop of read user input, and writing it to the pty */
while (1) {
FD_ZERO(&fds_write);
FD_SET(pty_fds[PARENTFD], &fds_write);
printf("pid %d Waiting for pty ready to write on %s...\n",
getpid(), name);
retval = select(pty_fds[PARENTFD]+2, NULL, &fds_write, NULL, NULL);
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;
if (!FD_ISSET(pty_fds[PARENTFD], &fds_write)) {
fprintf(stderr, "parent: write fd not set?! retrying\n");
continue;
}
retval = write(pty_fds[PARENTFD], data, 1024);
if (retval == -1) {
perror("write");
fprintf(stderr, "Error writing on pty\n");
exit(-1);
} else printf("wrote %d\n", retval);
}
/* got exit from user */
close(pty_fds[PARENTFD]); /* close pty, let child know we're done */
wait(&retval);
printf("Child exited with status: %d\n", retval);
exit(0);
}
int main(int argc, char *argv[])
{
int ptys[2];
int retval;
int pid;
if(openpty(&ptys[0], &ptys[1], name, NULL, NULL) < 0) {
perror("openpty");
return 1;
}
printf("Using %s\n", name);
pid = fork();
if (pid == -1) {
fprintf(stderr, "Error forking\n");
exit(-1);
}
if (pid == 0) /* child proc */
do_child(ptys);
else
do_parent(ptys);
/* not reached */
return 0;
}