C remote shell - How to detect if exec sent any data












0















I'm attempting to write a simple remote shell program using sockets in C on Linux. I have the socket communication relatively down and can successfully send commands run them with execvp() and redirect the output of the command back to the client. However i'm having the issues that when I run a command that does not have any output (for example, cp), my client program simply hangs and waits for output.



I have to use a blocking read call in the client since I have no idea when the server will finish executing the command, but this blocking read call is the reason that my client is hanging. I've tried having the server write something to the socket after the execvp() process completes, but this causes issues when a command does have output.



Is there an elegant way of signaling to my client program that a command has completed that works whether or not anything has been written to the socket?



Relevant code from Server:



if ((pid = fork()) == -1) {
perror("Fork failed");
continue;
}
if (pid > 0) {
//wait for the command to complete before checking for another input
waitpid(pid, &pid_status, WUNTRACED);
}
else if (pid == 0) { //Child process
fflush(stdout);
fflush(stderr);
dup2(sock, STDOUT_FILENO);
dup2(sock, STDERR_FILENO);

close(sock); //close socket in the child process
execvp(args[0], args);
//Following code will not execute unless exec fails
perror("execvp failure");
exit(0);
}
}


I'd prefer to do this without retooling my entire approach if possible.










share|improve this question


















  • 1





    What happens when the child process has some output? Does the client get the connection closed?

    – n.m.
    Nov 20 '18 at 4:41











  • No, the client remains connected until a specific disconnect command is given. Ideally I would be able to handle multiple calls in succession without having to reconnect to the socket.

    – zulu1310
    Nov 20 '18 at 4:54






  • 1





    So how does the client know that the remote command has finished?

    – n.m.
    Nov 20 '18 at 4:55
















0















I'm attempting to write a simple remote shell program using sockets in C on Linux. I have the socket communication relatively down and can successfully send commands run them with execvp() and redirect the output of the command back to the client. However i'm having the issues that when I run a command that does not have any output (for example, cp), my client program simply hangs and waits for output.



I have to use a blocking read call in the client since I have no idea when the server will finish executing the command, but this blocking read call is the reason that my client is hanging. I've tried having the server write something to the socket after the execvp() process completes, but this causes issues when a command does have output.



Is there an elegant way of signaling to my client program that a command has completed that works whether or not anything has been written to the socket?



Relevant code from Server:



if ((pid = fork()) == -1) {
perror("Fork failed");
continue;
}
if (pid > 0) {
//wait for the command to complete before checking for another input
waitpid(pid, &pid_status, WUNTRACED);
}
else if (pid == 0) { //Child process
fflush(stdout);
fflush(stderr);
dup2(sock, STDOUT_FILENO);
dup2(sock, STDERR_FILENO);

close(sock); //close socket in the child process
execvp(args[0], args);
//Following code will not execute unless exec fails
perror("execvp failure");
exit(0);
}
}


I'd prefer to do this without retooling my entire approach if possible.










share|improve this question


















  • 1





    What happens when the child process has some output? Does the client get the connection closed?

    – n.m.
    Nov 20 '18 at 4:41











  • No, the client remains connected until a specific disconnect command is given. Ideally I would be able to handle multiple calls in succession without having to reconnect to the socket.

    – zulu1310
    Nov 20 '18 at 4:54






  • 1





    So how does the client know that the remote command has finished?

    – n.m.
    Nov 20 '18 at 4:55














0












0








0








I'm attempting to write a simple remote shell program using sockets in C on Linux. I have the socket communication relatively down and can successfully send commands run them with execvp() and redirect the output of the command back to the client. However i'm having the issues that when I run a command that does not have any output (for example, cp), my client program simply hangs and waits for output.



I have to use a blocking read call in the client since I have no idea when the server will finish executing the command, but this blocking read call is the reason that my client is hanging. I've tried having the server write something to the socket after the execvp() process completes, but this causes issues when a command does have output.



Is there an elegant way of signaling to my client program that a command has completed that works whether or not anything has been written to the socket?



Relevant code from Server:



if ((pid = fork()) == -1) {
perror("Fork failed");
continue;
}
if (pid > 0) {
//wait for the command to complete before checking for another input
waitpid(pid, &pid_status, WUNTRACED);
}
else if (pid == 0) { //Child process
fflush(stdout);
fflush(stderr);
dup2(sock, STDOUT_FILENO);
dup2(sock, STDERR_FILENO);

close(sock); //close socket in the child process
execvp(args[0], args);
//Following code will not execute unless exec fails
perror("execvp failure");
exit(0);
}
}


I'd prefer to do this without retooling my entire approach if possible.










share|improve this question














I'm attempting to write a simple remote shell program using sockets in C on Linux. I have the socket communication relatively down and can successfully send commands run them with execvp() and redirect the output of the command back to the client. However i'm having the issues that when I run a command that does not have any output (for example, cp), my client program simply hangs and waits for output.



I have to use a blocking read call in the client since I have no idea when the server will finish executing the command, but this blocking read call is the reason that my client is hanging. I've tried having the server write something to the socket after the execvp() process completes, but this causes issues when a command does have output.



Is there an elegant way of signaling to my client program that a command has completed that works whether or not anything has been written to the socket?



Relevant code from Server:



if ((pid = fork()) == -1) {
perror("Fork failed");
continue;
}
if (pid > 0) {
//wait for the command to complete before checking for another input
waitpid(pid, &pid_status, WUNTRACED);
}
else if (pid == 0) { //Child process
fflush(stdout);
fflush(stderr);
dup2(sock, STDOUT_FILENO);
dup2(sock, STDERR_FILENO);

close(sock); //close socket in the child process
execvp(args[0], args);
//Following code will not execute unless exec fails
perror("execvp failure");
exit(0);
}
}


I'd prefer to do this without retooling my entire approach if possible.







c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 3:36









zulu1310zulu1310

1




1








  • 1





    What happens when the child process has some output? Does the client get the connection closed?

    – n.m.
    Nov 20 '18 at 4:41











  • No, the client remains connected until a specific disconnect command is given. Ideally I would be able to handle multiple calls in succession without having to reconnect to the socket.

    – zulu1310
    Nov 20 '18 at 4:54






  • 1





    So how does the client know that the remote command has finished?

    – n.m.
    Nov 20 '18 at 4:55














  • 1





    What happens when the child process has some output? Does the client get the connection closed?

    – n.m.
    Nov 20 '18 at 4:41











  • No, the client remains connected until a specific disconnect command is given. Ideally I would be able to handle multiple calls in succession without having to reconnect to the socket.

    – zulu1310
    Nov 20 '18 at 4:54






  • 1





    So how does the client know that the remote command has finished?

    – n.m.
    Nov 20 '18 at 4:55








1




1





What happens when the child process has some output? Does the client get the connection closed?

– n.m.
Nov 20 '18 at 4:41





What happens when the child process has some output? Does the client get the connection closed?

– n.m.
Nov 20 '18 at 4:41













No, the client remains connected until a specific disconnect command is given. Ideally I would be able to handle multiple calls in succession without having to reconnect to the socket.

– zulu1310
Nov 20 '18 at 4:54





No, the client remains connected until a specific disconnect command is given. Ideally I would be able to handle multiple calls in succession without having to reconnect to the socket.

– zulu1310
Nov 20 '18 at 4:54




1




1





So how does the client know that the remote command has finished?

– n.m.
Nov 20 '18 at 4:55





So how does the client know that the remote command has finished?

– n.m.
Nov 20 '18 at 4:55












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385847%2fc-remote-shell-how-to-detect-if-exec-sent-any-data%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385847%2fc-remote-shell-how-to-detect-if-exec-sent-any-data%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini