netutils/rexec/rexecd: supports remote execution and interaction

using popen with r+,w+ mode to interact with the remote service on
the command line, supporting input and output until the local
voluntarily exits or the remote service ends.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2023-07-29 09:52:59 +08:00 committed by Xiang Xiao
parent e86f6c12a5
commit fb7dafc7e0
2 changed files with 81 additions and 13 deletions

View File

@ -22,6 +22,7 @@
* Included Files
****************************************************************************/
#include <sys/poll.h>
#include <sys/types.h>
#include <getopt.h>
#include <netdb.h>
@ -82,6 +83,7 @@ static void usage(FAR const char *progname)
static int do_rexec(FAR struct rexec_arg_s *arg)
{
char buffer[REXEC_BUFSIZE];
struct pollfd fds[2];
int sock;
int ret;
@ -93,16 +95,52 @@ static int do_rexec(FAR struct rexec_arg_s *arg)
return sock;
}
memset(fds, 0, sizeof(fds));
fds[0].fd = sock;
fds[0].events = POLLIN;
fds[1].fd = STDIN_FILENO;
fds[1].events = POLLIN;
while (1)
{
ret = read(sock, buffer, REXEC_BUFSIZE);
ret = poll(fds, 2, -1);
if (ret <= 0)
{
break;
continue;
}
ret = write(STDOUT_FILENO, buffer, ret);
if (ret < 0)
if (fds[0].revents & POLLIN)
{
ret = read(sock, buffer, REXEC_BUFSIZE);
if (ret <= 0)
{
break;
}
ret = write(STDOUT_FILENO, buffer, ret);
if (ret < 0)
{
break;
}
}
if (fds[1].revents & POLLIN)
{
ret = read(STDIN_FILENO, buffer, REXEC_BUFSIZE);
if (ret <= 0)
{
break;
}
ret = write(sock, buffer, ret);
if (ret < 0)
{
break;
}
}
if (((fds[0].revents | fds[1].revents) & POLLHUP) &&
((fds[0].revents | fds[1].revents) & POLLIN) == 0)
{
break;
}

View File

@ -80,10 +80,10 @@ static int getstr(int fd, FAR char *buf)
static FAR void *doit(pthread_addr_t pvarg)
{
char buf[REXECD_BUFSIZE];
struct pollfd fds[2];
FAR FILE *fp;
int sock = (int)pvarg;
int ret;
int len;
/* we need to read err_sock, user and passwd, but ignore them */
@ -94,31 +94,61 @@ static FAR void *doit(pthread_addr_t pvarg)
/* we need to read command */
getstr(sock, buf);
fp = popen(buf, "r");
fp = popen(buf, "r+");
if (!fp)
{
goto errout;
}
memset(fds, 0, sizeof(fds));
fds[0].fd = fileno(fp);
fds[0].events = POLLIN;
fds[1].fd = sock;
fds[1].events = POLLIN;
while (1)
{
ret = fread(buf, 1, REXECD_BUFSIZE, fp);
ret = poll(fds, 2, -1);
if (ret <= 0)
{
break;
continue;
}
do
if (fds[0].revents & POLLIN)
{
len = write(sock, buf, ret);
if (len <= 0)
ret = read(fileno(fp), buf, REXECD_BUFSIZE);
if (ret <= 0)
{
break;
}
ret -= len;
ret = write(sock, buf, ret);
if (ret < 0)
{
break;
}
}
if (fds[1].revents & POLLIN)
{
ret = read(sock, buf, REXECD_BUFSIZE);
if (ret <= 0)
{
break;
}
ret = write(fileno(fp), buf, ret);
if (ret < 0)
{
break;
}
}
if (((fds[0].revents | fds[1].revents) & POLLHUP) &&
((fds[0].revents | fds[1].revents) & POLLIN) == 0)
{
break;
}
while (ret > 0);
}
pclose(fp);