Skip to content
Snippets Groups Projects
Commit a0671d04 authored by Joey Le's avatar Joey Le
Browse files

Even more debug statements in rsh_server

parent bd88c3f1
Branches
No related tags found
No related merge requests found
......@@ -225,7 +225,6 @@ int process_cli_requests(int svr_socket) {
}
printf("SERVER: Client connected. Socket: %d\n", cli_socket);
// Receive data from the client
bytes_received = recv(cli_socket, buffer, sizeof(buffer), 0);
if (bytes_received < 0) {
perror("SERVER: recv failed");
......@@ -241,7 +240,8 @@ int process_cli_requests(int svr_socket) {
// Null-terminate the received data
buffer[bytes_received] = '\0';
printf("SERVER: Received command: %s\n", buffer);
printf("SERVER: Received %zd bytes: %s\n", bytes_received, buffer);
// Check for special commands
if (strncmp(buffer, "stop-server", strlen("stop-server")) == 0) {
......@@ -505,39 +505,45 @@ int send_message_string(int cli_socket, char *buff){
* get this value.
*/
int rsh_execute_pipeline(int cli_sock, command_list_t *clist) {
int pipes[clist->num - 1][2]; // Array of pipes
pid_t pids[clist->num];
int pids_st[clist->num]; // Array to store process statuses
int exit_code;
printf("SERVER: Executing pipeline with %d commands.\n", clist->num);
// Create all necessary pipes
for (int i = 0; i < clist->num - 1; i++) {
if (pipe(pipes[i]) == -1) {
perror("pipe");
perror("SERVER: pipe failed");
return ERR_RDSH_COMMUNICATION;
}
printf("SERVER: Created pipe %d.\n", i);
}
// Fork and execute each command in the pipeline
for (int i = 0; i < clist->num; i++) {
printf("SERVER: Forking for command %d: %s\n", i, clist->commands[i].argv[0]);
pids[i] = fork();
if (pids[i] == -1) {
perror("fork");
perror("SERVER: fork failed");
return ERR_RDSH_COMMUNICATION;
}
if (pids[i] == 0) { // Child process
printf("SERVER: Child process %d executing command: %s\n", i, clist->commands[i].argv[0]);
// Redirect input for the first command
if (i == 0) {
if (dup2(cli_sock, STDIN_FILENO) == -1) {
perror("dup2 stdin");
perror("SERVER: dup2 stdin failed");
exit(EXIT_FAILURE);
}
} else {
// Redirect stdin to the read end of the previous pipe
if (dup2(pipes[i - 1][0], STDIN_FILENO) == -1) {
perror("dup2 stdin");
perror("SERVER: dup2 stdin failed");
exit(EXIT_FAILURE);
}
}
......@@ -546,17 +552,17 @@ int rsh_execute_pipeline(int cli_sock, command_list_t *clist) {
if (i == clist->num - 1) {
// Redirect stdout and stderr to the client socket
if (dup2(cli_sock, STDOUT_FILENO) == -1) {
perror("dup2 stdout");
perror("SERVER: dup2 stdout failed");
exit(EXIT_FAILURE);
}
if (dup2(cli_sock, STDERR_FILENO) == -1) {
perror("dup2 stderr");
perror("SERVER: dup2 stderr failed");
exit(EXIT_FAILURE);
}
} else {
// Redirect stdout to the write end of the current pipe
if (dup2(pipes[i][1], STDOUT_FILENO) == -1) {
perror("dup2 stdout");
perror("SERVER: dup2 stdout failed");
exit(EXIT_FAILURE);
}
}
......@@ -569,7 +575,7 @@ int rsh_execute_pipeline(int cli_sock, command_list_t *clist) {
// Execute the command
execvp(clist->commands[i].argv[0], clist->commands[i].argv);
perror("execvp");
perror("SERVER: execvp failed");
exit(EXIT_FAILURE);
}
}
......@@ -581,8 +587,10 @@ int rsh_execute_pipeline(int cli_sock, command_list_t *clist) {
}
// Wait for all children
printf("SERVER: Parent process waiting for children...\n");
for (int i = 0; i < clist->num; i++) {
waitpid(pids[i], &pids_st[i], 0);
printf("SERVER: Child process %d exited with status: %d\n", i, WEXITSTATUS(pids_st[i]));
}
// Determine the exit code
......@@ -594,5 +602,6 @@ int rsh_execute_pipeline(int cli_sock, command_list_t *clist) {
}
}
printf("SERVER: Pipeline execution complete. Exit code: %d\n", exit_code);
return exit_code;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment