Skip to content

Commit

Permalink
netutils/rexec/rexecd: supports remote execution and interaction
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Donny9 authored and xiaoxiang781216 committed Jul 30, 2023
1 parent e86f6c1 commit fb7dafc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 13 deletions.
46 changes: 42 additions & 4 deletions netutils/rexec/rexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* Included Files
****************************************************************************/

#include <sys/poll.h>
#include <sys/types.h>
#include <getopt.h>
#include <netdb.h>
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}

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;
}
}

ret = write(STDOUT_FILENO, buffer, ret);
if (ret < 0)
if (((fds[0].revents | fds[1].revents) & POLLHUP) &&
((fds[0].revents | fds[1].revents) & POLLIN) == 0)
{
break;
}
Expand Down
48 changes: 39 additions & 9 deletions netutils/rexecd/rexecd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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 = write(sock, buf, ret);
if (ret < 0)
{
break;
}
}

ret -= len;
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);
Expand Down

0 comments on commit fb7dafc

Please sign in to comment.