| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/communication_node.c | 177 | ||||
| -rw-r--r-- | src/server/communication_thread.c | 55 | ||||
| -rw-r--r-- | src/server/conversion_main_loop.c (renamed from src/server/main_loop.c) | 8 | ||||
| -rw-r--r-- | src/server/daemon.c | 113 | ||||
| -rw-r--r-- | src/server/server.c | 15 | 
5 files changed, 356 insertions, 12 deletions
| diff --git a/src/server/communication_node.c b/src/server/communication_node.c new file mode 100644 index 0000000..70c7323 --- /dev/null +++ b/src/server/communication_node.c @@ -0,0 +1,177 @@ +/**** POSIX *******************************************************************/ +#include <sys/socket.h> +#include <sys/un.h> + +#include <errno.h> +#include <fcntl.h> +#include <string.h> +#include <unistd.h> + +/**** RELABSD *****************************************************************/ +#include <relabsd/debug.h> + +/******************************************************************************/ +/**** LOCAL FUNCTIONS *********************************************************/ +/******************************************************************************/ + +static int create_socket (int result [const restrict static 1]) +{ +   errno = 0; + +   *result = socket(AF_UNIX, SOCK_STREAM, 0); + +   if (*result == -1) +   { +      RELABSD_FATAL +      ( +         "Unable to create server socket: %s.", +         strerror(errno) +      ); + +      return -1; +   } + +   return 0; +} + +static int bind_socket +( +   const char socket_name [const restrict static 1], +   const int socket +) +{ +   struct sockaddr_un addr; + + +   memset(&addr, 0, sizeof(struct sockaddr_un)); + +   addr.sun_family = AF_UNIX; + +   strncpy +   ( +      (void *) addr.sun_path, +      (const void *) socket_name, +      (sizeof(addr.sun_path) - 1) +   ); + +   errno = 0; + +   if +   ( +      bind +      ( +         socket, +         (const struct sockaddr *) &addr, +         (socklen_t) sizeof(struct sockaddr_un) +      ) +      == -1 +   ) +   { +      RELABSD_FATAL +      ( +         "Unable to bind communication socket to %s: %s.", +         socket_name, +         strerror(errno) +      ); + +      return -1; +   } + +   return 0; +} + +static int set_socket_to_unblocking (const int socket) +{ +   int current_flags; + +   errno = 0; +   current_flags = fcntl(socket, F_GETFD); + +   if (current_flags == -1) +   { +      RELABSD_FATAL +      ( +         "Unable to get communication socket properties: %s.", +         strerror(errno) +      ); + +      return -1; +   } + +   errno = 0; +   current_flags = fcntl(socket, F_SETFD, (current_flags | O_NONBLOCK)); + +   if (current_flags == -1) +   { +      RELABSD_FATAL +      ( +         "Unable to set communication socket properties: %s.", +         strerror(errno) +      ); + +      return -2; +   } + +   return 0; +} + +static int set_socket_as_listener (const int socket) +{ +   errno = 0; + +   if (listen(socket, 0) == -1) +   { +      RELABSD_FATAL +      ( +         "Unable to set server socket properties: %s.", +         strerror(errno) +      ); + +      return -1; +   } + +   return 0; +} + +/******************************************************************************/ +/**** EXPORTED FUNCTIONS ******************************************************/ +/******************************************************************************/ +int relabsd_server_create_communication_node +( +   const char socket_name [const restrict static 1], +   int socket [const restrict static 1] +) +{ +   if (create_socket(socket) < 0) +   { +      return -1; +   } + +   if (bind_socket(socket_name, *socket) < 0) +   { +      /* TODO: err message. */ +      (void) close(*socket); + +      return -1; +   } + +   /* +   if (set_socket_to_unblocking(*socket) < 0) +   { +      /* TODO: err message. *//* +      (void) close(*socket); + +      return -1; +   } +   */ + +   if (set_socket_as_listener(*socket) < 0) +   { +      /* TODO: err message. */ +      (void) close(*socket); + +      return -1; +   } + +   return 0; +} diff --git a/src/server/communication_thread.c b/src/server/communication_thread.c new file mode 100644 index 0000000..2ed1aae --- /dev/null +++ b/src/server/communication_thread.c @@ -0,0 +1,55 @@ +/**** POSIX *******************************************************************/ +#include <pthread.h> +#include <string.h> + +/**** RELABSD *****************************************************************/ +#include <relabsd/debug.h> + +/******************************************************************************/ +/**** LOCAL FUNCTIONS *********************************************************/ +/******************************************************************************/ +void main_loop (struct relabsd_server server [const static 1]) +{ + +} + +void * posix_main_loop (void * params) +{ +   main_loop((struct relabsd_server *) params); + +   return NULL; +} + +/******************************************************************************/ +/**** EXPORTED FUNCTIONS ******************************************************/ +/******************************************************************************/ + +int relabsd_server_create_communication_thread +( +   struct relabsd_server server [const static 1] +) +{ +   int err; + +   err = +      pthread_create +      ( +         &(server->communication_thread), +         (const pthread_attr_t *) NULL, +         posix_main_loop, +         (void *) server +      ); + +   if (err != 0) +   { +      RELABSD_FATAL +      ( +         "Unable to create the communication thread: %s", +         strerror(err) +      ); + +      return -1; +   } + +   return 0; +} diff --git a/src/server/main_loop.c b/src/server/conversion_main_loop.c index 7db1423..0180e80 100644 --- a/src/server/main_loop.c +++ b/src/server/conversion_main_loop.c @@ -163,3 +163,11 @@ int main (int argc, char ** argv)     return 0;  }  */ + +int relabsd_server_conversion_loop +( +   struct relabsd_server server [const static 1] +) +{ +   return 0; +} diff --git a/src/server/daemon.c b/src/server/daemon.c index 7b8e736..c8561f3 100644 --- a/src/server/daemon.c +++ b/src/server/daemon.c @@ -1,14 +1,13 @@  /**** POSIX *******************************************************************/ -#include <sys/types.h> +#include <sys/stat.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h>  #include <unistd.h>  /**** RELABSD *****************************************************************/ -#include <relabsd/config.h>  #include <relabsd/debug.h> -#include <relabsd/server.h> - -#include <relabsd/config/parameters.h>  /******************************************************************************/  /**** LOCAL FUNCTIONS *********************************************************/ @@ -26,10 +25,24 @@   */  int relabsd_server_create_daemon (void)  { +   int unnamed_pipe[2];     pid_t proc_id; +   errno = 0; + +   if (pipe(unnamed_pipe) == -1) +   { +      RELABSD_FATAL +      ( +         "Unable to create an unnamed pipe for the daemon creation process: %s", +         strerror(errno) +      ); + +      return -1; +   } +     /* 1/ Close all open file descriptors ... **********************************/ -   /* None were opened at this point. */ +   /* None were opened at this point, except the pipe, which is still needed. */     /* 2/ Reset all signal handlers ... ****************************************/     /* Those were not modified at this point. */ @@ -57,10 +70,52 @@ int relabsd_server_create_daemon (void)     if (proc_id != ((pid_t) 0))     { +      char buffer; + +      /* Close the writing end of the pipe, as this process does not use it */ +      errno = 0; + +      if (close(unnamed_pipe[1]) == -1) +      { +         RELABSD_ERROR +         ( +            "Unable to close writing end of an unnamed pipe during the daemon" +            " creation process: %s.", +            strerror(errno) +         ); +      } +        /* Awaiting step 14...*/ -      /* TODO: insert unnamed pipe */ +      errno = 0; + +      if (read(unnamed_pipe[0], &buffer, (size_t) 1) == -1) +      { +         RELABSD_ERROR +         ( +            "Unable to read from reading end of an unnamed pipe during the " +            " daemon creation process: %s.", +            strerror(errno) +         ); +      } + +      if (close(unnamed_pipe[0]) == -1) +      { +         RELABSD_ERROR +         ( +            "Unable to close reading end of an unnamed pipe during the daemon" +            " creation process: %s.", +            strerror(errno) +         ); +      } +        /* 15/ Original process exits *******************************************/ -      exit(); +      exit(0); +   } + +   /* Close reading on the pipe, as this process does not use it */ +   if (close(unnamed_pipe[0])) +   { +     }     /* 6/ setsid() *************************************************************/ @@ -96,13 +151,26 @@ int relabsd_server_create_daemon (void)     if (proc_id != ((pid_t) 0))     { +      if (close(unnamed_pipe[1]) == -1) +      { +         RELABSD_ERROR +         ( +            "Unable to close writing end of an unnamed pipe during the daemon" +            " creation process: %s.", +            strerror(errno) +         ); +      } +        /* 8/ First child process exits *****************************************/ -      exit(); +      exit(0);     }     /* 9/ /dev/null for standard input/outputs *********************************/ +   /* TODO. */     /* 10/ reset umask to 0 ****************************************************/ +   /* Can't fail, returns previous mask. */ +   (void) umask(0);     /* 11/ Set current directory to / ******************************************/     errno = 0; @@ -110,6 +178,13 @@ int relabsd_server_create_daemon (void)     if (chdir("/") == -1)     {        // Can't print an error message at that point though... +      RELABSD_FATAL +      ( +         "Step 11 of the daemon creation process, fork(), failed: %s.", +         strerror(errno) +      ); + +      /* TODO: boop main process. */        return -1;     } @@ -117,10 +192,30 @@ int relabsd_server_create_daemon (void)     /* Don't want to limit to a single instance. */     /* 13/ Drop privileges *****************************************************/ +   /* We need those. */     /* 14/ Signal completion ***************************************************/ +   if (write(unnamed_pipe[0], (void *) "!", (size_t) 1) == -1) +   { +      RELABSD_ERROR +      ( +         "Unable to write to writing end of an unnamed pipe during the daemon" +         " creation process: %s.", +         strerror(errno) +      ); +   }     /* Step 15 is done on the very first process. */ +   if (close(unnamed_pipe[1]) == -1) +   { +      RELABSD_ERROR +      ( +         "Unable to close writing end of an unnamed pipe during the daemon" +         " creation process: %s.", +         strerror(errno) +      ); +   } +     return 0;  } diff --git a/src/server/server.c b/src/server/server.c index debeb91..ddd9bdf 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -33,7 +33,7 @@ int initialize     (        relabsd_virtual_device_create        ( - +         relabsd_parameters_get_virtual_device_name(params),           &(server->virtual_device)        )        < 0 @@ -56,14 +56,23 @@ int initialize        return -3;     } +   if (relabsd_parameters_get_communication_node(params) != ((char *) NULL)) +   { +      relabsd_server_create_communication_thread(&server); +   } +     return 0;  }  void finalize (struct relabsd_server server [const static 1])  { -   if (relabsd_parameters_get_communication_node(params) != ((...) NULL)) +   if +   ( +      relabsd_parameters_get_communication_node(server->parameters) +      != ((char *) NULL) +   )     { -      relabsd_server_join_communication_node(&server); +      relabsd_server_join_communication_thread(&server);     }     relabsd_virtual_device_destroy(&(server->virtual_device)); | 


