Skip to content
Snippets Groups Projects
Commit 93682a51 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Refactoring: function renaming and reformatting

parent 1f4444e2
Branches
No related tags found
No related merge requests found
......@@ -144,7 +144,7 @@ void on_signal(int sig) {
send_message(socket_fd, "EXIT");
cleanup();
fprintf(stderr, "Terminating due to signal %d.\n", sig);
exit(sig);
exit(128 + sig);
}
void cleanup() {
......
......@@ -129,5 +129,5 @@ void on_signal(int sig) {
}
}
printf("[STATUS]\tTerminating.\n");
exit(sig);
exit(128 + sig);
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ void manage_client(int socket_fd);
void repeat_messages(int *client_fd_list, pid_t *handler_pid_list, int list_length, char *message);
void add_client(int new_client_fd, int **client_fd_list, pid_t new_handler_pid, pid_t **handler_pid_list, int *list_length);
void remove_client(int exiting_client_fd, int *client_fd_list, pid_t *handler_pid_list);
void on_signal_daemon(int sig);
void on_signal_listener(int sig);
void on_signal_handler(int sig);
void on_sigusr1(int sig);
......@@ -31,11 +31,11 @@ int main(int argc, const char **argv) {
unsigned short port = (unsigned short)strtol(argv[1], NULL, 10);
struct sockaddr_in server_socket_address;
listening_socket_fd = listen_to_port(port, &server_socket_address);
sigset(SIGINT, on_signal_daemon);
sigset(SIGABRT, on_signal_daemon);
sigset(SIGKILL, on_signal_daemon);
sigset(SIGSEGV, on_signal_daemon);
sigset(SIGTERM, on_signal_daemon);
sigset(SIGINT, on_signal_listener);
sigset(SIGABRT, on_signal_listener);
sigset(SIGKILL, on_signal_listener);
sigset(SIGSEGV, on_signal_listener);
sigset(SIGTERM, on_signal_listener);
display_host_info(port);
pipe(pipe_to_repeater);
manage_server(listening_socket_fd);
......@@ -73,7 +73,7 @@ void manage_server(int connect_socket_fd) {
if (pid) {
// parent process, listen for the next client
add_client(accepted_socket_fd, &client_socket_fd_list, pid, &client_handler_pid_list, &client_list_length);
// many code samples would now close daemon's copy of the accepted socket, but ours needs it for the repeater
// many code samples would now close listener's copy of the accepted socket, but ours needs it for the repeater
} else {
// child process, handle the client
sigset(SIGINT, on_signal_handler);
......@@ -82,12 +82,12 @@ void manage_server(int connect_socket_fd) {
sigset(SIGSEGV, on_signal_handler);
sigset(SIGTERM, on_signal_handler);
sigset(SIGUSR1, on_sigusr1);
close(connect_socket_fd); // close the listening socket file descriptor **for this process**
close(pipe_to_repeater[0]); // while we're at it, close the reading end of the pipe
close(connect_socket_fd); // Close the listening socket file descriptor **for this process**
close(pipe_to_repeater[0]); // While we're at it, close the reading end of the pipe
for (int i = 0; i < client_list_length; i++) {
close(client_socket_fd_list[i]); // This process certainly doesn't need file descriptors for clients it
} // isn't handling
free(client_socket_fd_list); // the handler doesn't need these lists
close(client_socket_fd_list[i]); // This process certainly doesn't need file
} // descriptors for clients it isn't handling
free(client_socket_fd_list); // The handler doesn't need these lists
free(client_handler_pid_list);
fd_flags = fcntl(accepted_socket_fd, F_GETFL);
fcntl(accepted_socket_fd, F_SETFL, fd_flags & !O_NONBLOCK);
......@@ -188,14 +188,14 @@ void manage_client(int socket_fd) {
free(buffer);
}
void on_signal_daemon(int sig) {
void on_signal_listener(int sig) {
sigset(SIGINT, SIG_IGN);
sigset(SIGABRT, SIG_IGN);
sigset(SIGKILL, SIG_IGN);
sigset(SIGSEGV, SIG_IGN);
sigset(SIGTERM, SIG_IGN);
running = false;
printf("[STATUS]\tDaemon process %d received signal %d; notifying clients and client handlers.\n", getpid(), sig);
printf("[STATUS]\tListener process %d received signal %d; notifying clients and client handlers.\n", getpid(), sig);
for (int i = 0; i < client_list_length; i++) {
if (client_socket_fd_list[i]) {
send_message(client_socket_fd_list[i], "[SERVER]\tServer is terminating. EXIT");
......@@ -208,8 +208,8 @@ void on_signal_daemon(int sig) {
close(listening_socket_fd);
free(client_socket_fd_list);
free(client_handler_pid_list);
printf("[STATUS]\tDaemon process %d is terminating with signal %d.\n", getpid(), sig);
exit(sig);
printf("[STATUS]\tListener process %d is terminating with signal %d.\n", getpid(), sig);
exit(128 + sig);
}
void on_signal_handler(int sig) {
......@@ -220,7 +220,7 @@ void on_signal_handler(int sig) {
sigset(SIGTERM, SIG_IGN);
sigset(SIGUSR1, SIG_IGN);
running = false;
printf("[STATUS]\tProcess %d received signal %d; notifying client and daemon.\n", getpid(), sig);
printf("[STATUS]\tProcess %d received signal %d; notifying client and listener.\n", getpid(), sig);
send_message(accepted_socket_fd, "[SERVER]\tServer experienced abnormal condition. Please try reconnecting. EXIT");
char message_to_repeat[BUFFER_SIZE];
sprintf(message_to_repeat, "EXIT%d[SERVER]\t%s has left the chat due to server issue.", accepted_socket_fd, name);
......@@ -232,6 +232,6 @@ void on_sigusr1(int sig) {
printf("[STATUS]\tProcess %d is terminating with signal %d.\n", getpid(), sig);
close(pipe_to_repeater[1]);
close(accepted_socket_fd);
exit(sig);
exit(128 + sig);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment