Remove obsolete service() function

This commit is contained in:
flyingscorpio@clevo 2022-10-21 16:31:07 +02:00
parent d2983ff1d3
commit dd7beccdf8

View file

@ -172,72 +172,6 @@ void *ClientSender(void *arg) {
pthread_exit(0);
}
/* Handles a client after its connection */
// TODO: remove this function
void service(int sockid) {
int i, n, t;
pid_t pid;
char buf[BUF_LEN], buf2[BUF_LEN], eot = EOT;
void *memory = NULL;
char **command_line;
char delim[] = {' ', '\t', '\n', '\0'};
char *token, *text;
while (1) {
if ((n = readline(sockid, buf, BUF_LEN)) == -1) {
sprintf(buf, "ERROR: receive message - probably too long\n");
write(sockid, buf, strlen(buf) + 1);
} else {
if ((pid = fork()) == -1) {
sprintf(buf, "ERROR: system\n");
write(sockid, buf, strlen(buf) + 1);
close(sockid);
return;
} else if (pid == 0) {
/* The child has the command to execute in buf */
/* Redirect stdout and stderr to the socket */
dup2(sockid, STDOUT_FILENO);
dup2(sockid, STDERR_FILENO);
strcpy(buf2, buf);
text = buf;
t = 0;
while ((token = strtok_r(text, delim, &text)) != NULL) {
t++;
}
t++; // To add NULL at the end
if ((memory = malloc(t * sizeof(memory))) == NULL) {
sprintf(buf, "ERROR: memory\n");
write(sockid, buf, strlen(buf) + 1);
close(sockid);
return;
}
command_line = (char**) memory;
text = buf2;
for (i = 0; ((token = strtok_r(text, delim, &text)) != NULL); i++) {
command_line[i] = token;
}
command_line[i] = NULL;
execvp(command_line[0], command_line);
/* If execvp worked, the following code will never be executed.
* No need for a test */
perror("execvp");
exit(EXIT_FAILURE);
} else {
/* Back to parent */
wait(NULL);
write(sockid, &eot, 1);
}
}
}
close(sockid);
}
/* Handler for signal() */
void interrupt(int signal) {
switch (signal) {