Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cs283
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Joey Le
Cs283
Commits
a0671d04
Commit
a0671d04
authored
2 months ago
by
Joey Le
Browse files
Options
Downloads
Patches
Plain Diff
Even more debug statements in rsh_server
parent
bd88c3f1
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Assignment-06/starter/rsh_server.c
+20
-11
20 additions, 11 deletions
Assignment-06/starter/rsh_server.c
with
20 additions
and
11 deletions
Assignment-06/starter/rsh_server.c
+
20
−
11
View file @
a0671d04
...
...
@@ -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
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment