Skip to content

Commit

Permalink
netutils/rexec/rexecd: Supports remote execution and interaction
Browse files Browse the repository at this point in the history
and support popen in "r+ w+" mode

Signed-off-by: dongjiuzhu1 <[email protected]>
  • Loading branch information
Donny9 committed Jul 29, 2023
1 parent 7b066ed commit debdc89
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 35 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
78 changes: 56 additions & 22 deletions system/popen/popen.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ FILE *popen(FAR const char *command, FAR const char *mode)
posix_spawn_file_actions_t file_actions;
FAR char *argv[4];
int fd[2];
int oldfd;
int newfd;
int oldfd[2];
int newfd[2];
int retfd;
int errcode;
int result;
int result = 0;
bool rw = false;

/* Allocate a container for returned FILE stream */

Expand All @@ -130,34 +131,51 @@ FILE *popen(FAR const char *command, FAR const char *mode)
goto errout;
}

oldfd[1] = 0;
newfd[1] = 0;

/* Create a pipe. fd[0] refers to the read end of the pipe; fd[1] refers
* to the write end of the pipe.
* Is the pipe the input to the shell? Or the output?
*/

result = pipe(fd);
if (result < 0)
{
errcode = errno;
goto errout_with_container;
}

/* Is the pipe the input to the shell? Or the output? */

if (strcmp(mode, "r") == 0)
if (strcmp(mode, "r") == 0 && (result = pipe(fd)) >= 0)
{
/* Pipe is the output from the shell */

oldfd = 1; /* Replace stdout with the write side of the pipe */
newfd = fd[1];
retfd = fd[0]; /* Use read side of the pipe to create the return stream */
oldfd[0] = 1; /* Replace stdout with the write side of the pipe */
newfd[0] = fd[1];
retfd = fd[0]; /* Use read side of the pipe to create the return stream */
}
else if (strcmp(mode, "w") == 0)
else if (strcmp(mode, "w") == 0 && (result = pipe(fd)) >= 0)
{
/* Pipe is the input to the shell */

oldfd = 0; /* Replace stdin with the read side of the pipe */
newfd = fd[0];
retfd = fd[1]; /* Use write side of the pipe to create the return stream */
oldfd[0] = 0; /* Replace stdin with the read side of the pipe */
newfd[0] = fd[0];
retfd = fd[1]; /* Use write side of the pipe to create the return stream */
}

/* Create a socketpair. Using fd[0] as the input and output to the shell */

#if defined(CONFIG_NET_LOCAL) && defined(CONFIG_NET_LOCAL_STREAM)
else if ((strcmp(mode, "r+") == 0 || strcmp(mode, "w+") == 0) &&
(result = socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) >= 0)
{
/* Socketpair is the input/output to the shell */

rw = true;
oldfd[0] = 0; /* Replace stdin with the one side of a socket pair */
newfd[0] = fd[0];
oldfd[1] = 1; /* Replace stdout with the one side of a socket pair */
newfd[1] = fd[0];
retfd = fd[1]; /* Use other side of the socket pair to create the return stream */
}
#endif
else if (result < 0)
{
errcode = errno;
goto errout_with_container;
}
else
{
Expand Down Expand Up @@ -236,12 +254,23 @@ FILE *popen(FAR const char *command, FAR const char *mode)

/* Redirect input or output as determined by the mode parameter */

errcode = posix_spawn_file_actions_adddup2(&file_actions, newfd, oldfd);
errcode = posix_spawn_file_actions_adddup2(&file_actions,
newfd[0], oldfd[0]);
if (errcode != 0)
{
goto errout_with_actions;
}

if (rw)
{
errcode = posix_spawn_file_actions_adddup2(&file_actions,
newfd[1], oldfd[1]);
if (errcode != 0)
{
goto errout_with_actions;
}
}

/* Call task_spawn() (or posix_spawn), re-directing stdin or stdout
* appropriately.
*/
Expand Down Expand Up @@ -273,7 +302,12 @@ FILE *popen(FAR const char *command, FAR const char *mode)
* the interface.
*/

close(newfd);
close(newfd[0]);

if (rw)
{
close(newfd[1]);
}

/* Free attributes and file actions. Ignoring return values in the case
* of an error.
Expand Down

0 comments on commit debdc89

Please sign in to comment.