From 63519d6ae4cb20ae4871310e09a28fd59f5c9fc7 Mon Sep 17 00:00:00 2001 From: Johann Latocha Date: Tue, 31 Jan 2012 23:52:24 +0100 Subject: [ALL] Some refactoring --- src/server/server.c | 178 +++++++++------------------------------------------- 1 file changed, 30 insertions(+), 148 deletions(-) (limited to 'src/server/server.c') diff --git a/src/server/server.c b/src/server/server.c index 0b327e3..e15249b 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -20,33 +20,26 @@ #include #include -#include -#include -#include -#include -#include -#include #include -#include #include #include -#include -#include -#include - #include #include "../types.h" #include "../version.h" +#include "server.h" #include "utils.h" #include "hashtable.h" +#include "signal.h" +#include "net.h" int _sock; -pthread_spinlock_t spinlock; -char *config_file_name = DEFAULT_CONFIG_FILE; -void print_help(char* argv_0) +pthread_spinlock_t _spinlock; +char *_config_file_name = DEFAULT_CONFIG_FILE; + +void dnbd3_print_help(char* argv_0) { printf("Usage: %s [OPTIONS]...\n", argv_0); printf("Start the DNBD3 server\n"); @@ -59,104 +52,19 @@ void print_help(char* argv_0) exit(0); } -void print_version() +void dnbd3_print_version() { printf("Version: %s\n", VERSION_STRING); exit(0); } -void cleanup() +void dnbd3_cleanup() { close(_sock); dnbd3_delete_pid_file(); exit(EXIT_SUCCESS); } -void handle_sigpipe(int signum) -{ - printf("ERROR: Received signal SIGPIPE, Broken pipe (errno: %i)\n", errno); - return; -} - -void handle_sighup(int signum) -{ - printf("INFO: SIGHUP received!\n"); - printf("INFO: Reloading configuration...\n"); - pthread_spin_lock(&spinlock); - dnbd3_reload_config(config_file_name); - pthread_spin_unlock(&spinlock); -} - -void handle_sigterm(int signum) -{ - printf("INFO: SIGTERM or SIGINT received!\n"); - cleanup(); -} - -void *handle_query(void *client_socket) -{ - int image_file = -1; - off_t filesize = 0; - int sock = (int) (uintptr_t) client_socket; - struct dnbd3_request request; - struct dnbd3_reply reply; - uint16_t cmd; - - while (recv(sock, &request, sizeof(struct dnbd3_request), MSG_WAITALL) > 0) - { - cmd = request.cmd; - switch (cmd) - { - case CMD_PING: - reply.cmd = request.cmd; - memcpy(reply.handle, request.handle, sizeof(request.handle)); - send(sock, (char *) &reply, sizeof(struct dnbd3_reply), 0); - break; - - case CMD_GET_SIZE: - pthread_spin_lock(&spinlock); // because of reloading config - image_file = open(dnbd3_ht_search(request.image_id), O_RDONLY); - pthread_spin_unlock(&spinlock); - if (image_file < 0) - { - printf("ERROR: Client requested an unknown image id.\n"); - filesize = 0; - } - else - { - struct stat st; - fstat(image_file, &st); - filesize = st.st_size; - } - reply.cmd = request.cmd; - reply.filesize = filesize; - send(sock, (char *) &reply, sizeof(struct dnbd3_reply), 0); - break; - - case CMD_GET_BLOCK: - if (image_file < 0) - break; - - reply.cmd = request.cmd; - memcpy(reply.handle, request.handle, sizeof(request.handle)); - send(sock, (char *) &reply, sizeof(struct dnbd3_reply), 0); - - if (sendfile(sock, image_file, (off_t *) &request.offset, request.size) < 0) - printf("ERROR: sendfile returned -1\n"); - - break; - - default: - printf("ERROR: Unknown command\n"); - break; - } - - } - close(sock); - printf("INFO: Client exit.\n"); - pthread_exit((void *) 0); -} - int main(int argc, char* argv[]) { int demonize = 1; @@ -179,7 +87,7 @@ int main(int argc, char* argv[]) switch (opt) { case 'f': - config_file_name = optarg; + _config_file_name = optarg; break; case 'n': demonize = 0; @@ -193,13 +101,13 @@ int main(int argc, char* argv[]) dnbd3_send_signal(SIGTERM); return EXIT_SUCCESS; case 'h': - print_help(argv[0]); + dnbd3_print_help(argv[0]); break; case 'v': - print_version(); + dnbd3_print_version(); break; case '?': - exit(1); + dnbd3_print_help(argv[0]); } opt = getopt_long(argc, argv, optString, longOpts, &longIndex); } @@ -208,63 +116,37 @@ int main(int argc, char* argv[]) daemon(1, 0); // load config file - pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE); - dnbd3_load_config(config_file_name); + pthread_spin_init(&_spinlock, PTHREAD_PROCESS_PRIVATE); + dnbd3_load_config(_config_file_name); // setup signal handler - signal(SIGPIPE, handle_sigpipe); - signal(SIGHUP, handle_sighup); - signal(SIGTERM, handle_sigterm); - signal(SIGINT, handle_sigterm); + signal(SIGPIPE, dnbd3_handle_sigpipe); + signal(SIGHUP, dnbd3_handle_sighup); + signal(SIGTERM, dnbd3_handle_sigterm); + signal(SIGINT, dnbd3_handle_sigterm); // setup network - struct sockaddr_in server; - struct sockaddr_in client; - int fd; - unsigned int len; - - // Create socket - _sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + _sock = dnbd3_setup_socket(); if (_sock < 0) - { - printf("ERROR: Socket failure\n"); - exit(EXIT_FAILURE); - } - - memset(&server, 0, sizeof(server)); - server.sin_family = AF_INET; // IPv4 - server.sin_addr.s_addr = htonl(INADDR_ANY); // Take all IPs - server.sin_port = htons(PORT); // set port number - - // Bind to socket - if (bind(_sock, (struct sockaddr*) &server, sizeof(server)) < 0) - { - printf("ERROR: Bind failure\n"); - exit(EXIT_FAILURE); - } - - // Listen on socket - if (listen(_sock, 50) == -1) - { - printf("ERROR: Listen failure\n"); exit(EXIT_FAILURE); - } - - dnbd3_write_pid_file(getpid()); - printf("INFO: Server is ready...\n"); - + struct sockaddr_in client; + unsigned int len = sizeof(client); + int fd; struct timeval timeout; timeout.tv_sec = SERVER_SOCKET_TIMEOUT; timeout.tv_usec = 0; + dnbd3_write_pid_file(getpid()); + printf("INFO: Server is ready...\n"); + + // main loop while (1) { - len = sizeof(client); fd = accept(_sock, (struct sockaddr*) &client, &len); if (fd < 0) { printf("ERROR: Accept failure\n"); - exit(EXIT_FAILURE); + continue; } printf("INFO: Client: %s connected\n", inet_ntoa(client.sin_addr)); @@ -273,9 +155,9 @@ int main(int argc, char* argv[]) // FIXME: catch SIGKILL/SIGTERM and close all socket before exit pthread_t thread; - pthread_create(&(thread), NULL, handle_query, (void *) (uintptr_t) fd); + pthread_create(&(thread), NULL, dnbd3_handle_query, (void *) (uintptr_t) fd); pthread_detach(thread); } - cleanup(); + dnbd3_cleanup(); } -- cgit v1.2.3-55-g7522