code sample from server:
dup2( client, STDOUT_FILENO ); /* duplicate socket on stdout */
dup2( client, STDERR_FILENO ); /* duplicate socket on stderr too */
char * msgP = NULL;
int len = 0;
while (len == 0) {
ioctl(client, FIONREAD, &len);
}
if (len > 0) {
msgP = malloc(len * sizeof(char));
len = read(client, msgP, len);
system(msgP);
fflush(stdout);
fflush(stderr);
}
When I send a command from the client I call the system function. This function is sufficient for many commands but not for all. I tried several different commands and I had problems with a few (ex: nano). The problem I'm facing is that after I call the system function I can not send any input any more for that command (if necessary).I can still send other commands. My question is how can I solve this problem?
P.S. i did some test and cd command also dont work . who can explain me why?
Thanks for the help !
The test
and cd
commands are built into command-line shells: The shells do not execute them as external programs. They read those commands and process them by making changes inside the shell program itself.
When you execute a program with system
or a routine from the exec
family, it creates a separate process that runs the program. A separate process can read input, write output, change files, and communicate on the network, but it cannot change things inside the process that created it (except that it can send some information to that process, by providing a status code when it exits or by various means of interprocess communication). This is why cd
cannot be executed with system
: A separate process cannot change the working directory of another process. In order to execute a cd
command, you must call chdir
or fchdir
to change the working directory for your own process.
There is a separate test
command, but some shells choose to implement it internally instead of using the external program. Regarding nano
, I do not know why it is not working for you. It works for me when I use system("nano")
or system("nano xyz")
. You would have to provide more information about the specific problem you are seeing with nano
.
The way that ssh
provides remote command execution is that it executes a shell process on the server. A shell is a program that reads commands from its input and executes them. Some of the commands, like cd
, it executes internally. Other commands it executes by calling external programs. To provide a similar service, you could either write your own shell or execute one of the existing shells. On Unix systems, standard shells may be found in /bin
with names ending in sh
, such as /bin/bash
and /bin/csh
. (Not everything ending in sh
is necessarily a shell, though.)
Even if you execute a shell, there are a number of details to doing it properly, including: