writting to master pty, but cannot read in slave :(
trying to write primitive test. Program must startup tcp-server, receive connection and redirect received data to forked program. Here is the code:
#include "TcpServer.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <termios.h>
#include <stropts.h>
#include <sys/ioctl.h>
//test
#include <pty.h>
int main() {
if (setsid() < 1) {
perror("cannot setsid()");
return 1;
}
TcpServer tcp_server;
int client;
char buf[1024];
int master, slave;
char * slave_name;
bzero(buf, sizeof(buf));
tcp_server.setPort(1025);
int server = tcp_server.startup();
client = accept(server, NULL, NULL);
fprintf(stderr, "forking...\n");
pid_t pid = forkpty(&master, NULL, NULL, NULL);
if (pid == 0) {
setsid();
fprintf(stderr, "slave pty is opened\n");
fprintf(stdout, "hello, client!\n");
char * cmd[] = {"upper.py", NULL};
fprintf(stderr, "slave: start to login\n");
int res = execvp("upper.py", cmd);
if (res < 0) {
fprintf(stderr, "trouble while running exec - '%s'\n", strerror(errno));
return 1;
}
fprintf(stderr, "slave: quit\n");
return 0;
} else if (pid < 0) {
perror("cannot fork off process");
return 1;
}
fd_set in;
int max_fd, n;
struct termios ot, t;
tcgetattr(master, &ot);
t = ot;
t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT );
t.c_iflag |= IGNBRK;
t.c_cc[VMIN] = 1;
t.c_cc[VTIME] = 0;
tcsetattr(master, TCSANOW, &t);
while (1) {
FD_ZERO(&in);
max_fd = (master > max_fd) ? master : max_fd;
FD_SET(master, &in);
max_fd = (client > max_fd) ? client : max_fd;
FD_SET(client, &in);
fprintf(stderr, "select starts\n");
if (select(max_fd + 1, &in, NULL, NULL, NULL) < 0) {
fprintf(stderr, "select returned incorrectly\n");
if (errno == EINTR)
continue;
perror("error while select");
return 1;
}
if (FD_ISSET(client, &in)) {
fprintf(stderr, "client is set\n");
n = recv(client, buf, sizeof(buf), 0);
if (n < 0) {
fprintf(stderr, "error while receiving data from client '%s'\n", strerror(errno));
return 1;
}
if (n <= sizeof(buf)) {
buf[n] = 0;
}
fprintf(stderr, "received from client: '%s'\n", buf);
n = write(master, buf, n);
tcflush(master, TCIOFLUSH);
}
if (FD_ISSET(master, &in)) {
fprintf(stderr, "master is set ");
n = read(master, buf, sizeof(buf));
if (n < 0) {
fprintf(stderr, ": %s - ", strerror(errno));
return 1;
}
if (n <= sizeof(buf))
buf[n] = 0;
fprintf(stderr, "sending data - '%s'\n", buf);
n = send(client, buf, n, 0);
}
}
return 0;
}
program's output:
[milo@milo telnetd]$ sudo ./server
forking...
select starts
master is set sending data - 'slave pty is opened
hello, client!
slave: start to login
'
select starts
master is set sending data - 'input string: '
select starts
client is set
received from client: 'hello'
select starts
The trouble in short: server sucessfully receives data from tcp-client, writes it to master pty, but slave-end don't receive it. 开发者_开发知识库I've seen a lot of examples, but cannot see error. Please suggest me...
UPD I've tried to int len = read(STDIN_FILENO, buf, sizeof(buf));
instead of execvp
and it works fine! I think I have to send some kind of controlling symbol like enter... Any thoughts?
Yahoo! I've solved this problem! The mistake was (as i said earlier in UPD) in absence of \n
symbol in input! otherside-program waits input until endline symbol is came!
The first thing you child does is call setsid
. From the POSIX documentation (emphasis mine):
The setsid() function shall create a new session, if the calling process is not a process group leader. Upon return the calling process shall be the session leader of this new session, shall be the process group leader of a new process group, and shall have no controlling terminal.
So the first thing your child did was to disassociate from its controlling terminal.
精彩评论