Skip to content

Commit

Permalink
increase type size for command name and improve handling read of pipe…
Browse files Browse the repository at this point in the history
…s after command exits
  • Loading branch information
JacobBarthelmeh committed Dec 11, 2023
1 parent 22d1ab6 commit c382240
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 20 additions & 4 deletions apps/wolfsshd/wolfsshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,11 +1166,15 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,

#ifndef EXAMPLE_BUFFER_SZ
#define EXAMPLE_BUFFER_SZ 4096
#endif
#ifndef MAX_IDLE_COUNT
#define MAX_IDLE_COUNT 2
#endif
byte shellBuffer[EXAMPLE_BUFFER_SZ];
byte channelBuffer[EXAMPLE_BUFFER_SZ];
char* forcedCmd;
int windowFull = 0;
int idle = 0;

forcedCmd = wolfSSHD_ConfigGetForcedCmd(usrConf);

Expand Down Expand Up @@ -1381,14 +1385,16 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
close(stderrPipe[1]);
}

while (ChildRunning) {
while (idle < MAX_IDLE_COUNT) {
byte tmp[2];
fd_set readFds;
WS_SOCKET_T maxFd;
int cnt_r;
int cnt_w;
int pending = 0;

idle++; /* increment idle count, gets reset if not idle */

FD_ZERO(&readFds);
FD_SET(sshFd, &readFds);
maxFd = sshFd;
Expand Down Expand Up @@ -1416,6 +1422,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
}
else {
pending = 1; /* found some pending SSH data */
idle = 0;
}

if (windowFull || pending || FD_ISSET(sshFd, &readFds)) {
Expand All @@ -1431,6 +1438,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
if (cnt_r < 0) {
rc = wolfSSH_get_error(ssh);
if (rc == WS_CHAN_RXD) {
idle = 0;
if (lastChannel == shellChannelId) {
cnt_r = wolfSSH_ChannelIdRead(ssh, shellChannelId,
channelBuffer,
Expand Down Expand Up @@ -1458,6 +1466,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
shellBuffer, cnt_r);
if (cnt_w == WS_WINDOW_FULL) {
windowFull = 1;
idle = 0;
continue;
}
else {
Expand All @@ -1472,12 +1481,13 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
/* This read will return 0 on EOF */
if (cnt_r <= 0) {
int err = errno;
if (err != EAGAIN) {
if (err != EAGAIN && err != 0) {
break;
}
}
else {
if (cnt_r > 0) {
idle = 0;
cnt_w = wolfSSH_extended_data_send(ssh, shellBuffer,
cnt_r);
if (cnt_w == WS_WINDOW_FULL) {
Expand All @@ -1497,12 +1507,13 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
/* This read will return 0 on EOF */
if (cnt_r <= 0) {
int err = errno;
if (err != EAGAIN) {
if (err != EAGAIN && err != 0) {
break;
}
}
else {
if (cnt_r > 0) {
idle = 0;
cnt_w = wolfSSH_ChannelIdSend(ssh, shellChannelId,
shellBuffer, cnt_r);
if (cnt_w == WS_WINDOW_FULL) {
Expand All @@ -1521,12 +1532,13 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
/* This read will return 0 on EOF */
if (cnt_r <= 0) {
int err = errno;
if (err != EAGAIN) {
if (err != EAGAIN && err != 0) {
break;
}
}
else {
if (cnt_r > 0) {
idle = 0;
cnt_w = wolfSSH_ChannelIdSend(ssh, shellChannelId,
shellBuffer, cnt_r);
if (cnt_w == WS_WINDOW_FULL) {
Expand All @@ -1539,6 +1551,10 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
}
}
}

if (ChildRunning && idle) {
idle = 0; /* waiting on child process */
}
}

/* check for any left over data in pipes then close them */
Expand Down
2 changes: 1 addition & 1 deletion wolfssh/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ struct WOLFSSH {
word32 defaultPeerChannelId;
word32 connectChannelId;
byte channelName[WOLFSSH_MAX_CHN_NAMESZ];
byte channelNameSz;
word32 channelNameSz;
word32 lastRxId;

WOLFSSH_BUFFER inputBuffer;
Expand Down

0 comments on commit c382240

Please sign in to comment.