| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2019-12-26 18:32:26 +0100 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2019-12-26 18:32:26 +0100 |
| commit | 4a9df9b604cec6ee4b4a6f01ef940443583f7573 (patch) | |
| tree | 0f455bf9c07bb598f2701be0a61f1c3b3b512db2 | |
| parent | 390576c3839ee7abb845e27b7267de45495e6b2f (diff) | |
Still working on the daemonization (and rewrite).
I am not sure that what I am doing is going to provide what was
requested in https://github.com/nsensfel/relabsd/issues/3 but it will at
the very least turn relabsd into a proper daemon, and the additions will
help create some kind of relabsd device manager if someone needs such a
thing.
| -rw-r--r-- | src/config/parameters.c | 87 | ||||
| -rw-r--r-- | src/device/physical_device.c | 11 | ||||
| -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 |
7 files changed, 433 insertions, 33 deletions
diff --git a/src/config/parameters.c b/src/config/parameters.c index 308e5db..5e79fa2 100644 --- a/src/config/parameters.c +++ b/src/config/parameters.c @@ -18,16 +18,19 @@ static void print_usage (const char exec [const restrict static 1]) { printf ( - "USAGE: %s [<MODE>] [<OPTION>+]\n\n" + "USAGE: %s <MODE> [<OPTION>+]\n\n" "<MODE>:\n" + "\t[-? | --compatible] <physical_device_file>:\n" + "\t\tDevice compatibility test.\n\n" + "\t[-c | --client] <server_file>:\n" "\t\tSends the commands to a given server instance.\n\n" - "\t[-s | --server] <server_file>:\n" + "\t[-s | --server] <server_file> <physical_device_file>:\n" "\t\tCreates a named server instance.\n\n" - "\t[-1 | --self]:\n" + "\t[-1 | --self] <physical_device_file>:\n" "\t\tCreates a unnamed server instance.\n\n" "<OPTION>:\n" @@ -44,6 +47,10 @@ static void print_usage (const char exec [const restrict static 1]) "<options>:\n" "\t\t(Re)defines an axis.\n\n" + "\t[-m | --mod-axis] <name> <min> <max> <fuzz> <flat> <resolution> " + "<signed_options>:\n" + "\t\tModifies an axis (use + and - signs for the options).\n\n" + "\t[-f | --config] <config_file>" "<options>:\n" "\t\t(Re)defines an axis.\n", @@ -79,12 +86,44 @@ static int parse_axis axis = (axes + axis_index); - axis->min = atoi(argv[1]); - axis->max = atoi(argv[2]); - axis->fuzz = atoi(argv[3]); - axis->flat = atoi(argv[4]); - axis->resolution = atoi(argv[5]); + if (relabsd_util_parse_int(argv[1], INT_MIN, INT_MAX, &(axis->min)) < 0) + { + RELABSD_FATAL("Invalid <min> value for axis \"%s\".", argv[0]); + + return -1; + } + + if (relabsd_util_parse_int(argv[2], INT_MIN, INT_MAX, &(axis->max)) < 0) + { + RELABSD_FATAL("Invalid <max> value for axis \"%s\".", argv[0]); + + return -1; + } + + if (relabsd_util_parse_int(argv[3], INT_MIN, INT_MAX, &(axis->fuzz)) < 0) + { + RELABSD_FATAL("Invalid <fuzz> value for axis \"%s\".", argv[0]); + + return -1; + } + + if (relabsd_util_parse_int(argv[4], INT_MIN, INT_MAX, &(axis->flat)) < 0) + { + RELABSD_FATAL("Invalid <flat> value for axis \"%s\".", argv[0]); + + return -1; + } + + if + ( + relabsd_util_parse_int(argv[5], INT_MIN, INT_MAX, &(axis->resolution)) + < 0 + ) + { + RELABSD_FATAL("Invalid <resolution> value for axis \"%s\".", argv[0]); + return -1; + } return 0; } @@ -110,12 +149,23 @@ int relabsd_parameters_parse_execution_mode if ( + RELABSD_STRING_EQUALS("-?", argv[1]) + || RELABSD_STRING_EQUALS("--compatibility", argv[1]) + ) + { + params->mode = RELABSD_PARAMETERS_COMPATIBILITY_TEST_MODE; + params->physical_device_name = argv[2]; + params->read_argc = 2; + } + else if + ( RELABSD_STRING_EQUALS("-c", argv[1]) || RELABSD_STRING_EQUALS("--client", argv[1]) ) { params->mode = RELABSD_PARAMETERS_CLIENT_MODE; - params->node = argv[2]; + params->communication_node_name = argv[2]; + params->physical_device_name = (char *) NULL; params->read_argc = 2; } else if @@ -124,9 +174,10 @@ int relabsd_parameters_parse_execution_mode || RELABSD_STRING_EQUALS("--server", argv[1]) ) { - params->mode = RELABSD_PARAMETERS_CLIENT_MODE; - params->node = argv[2]; - params->read_argc = 2; + params->mode = RELABSD_PARAMETERS_SERVER_MODE; + params->communication_node_name = argv[2]; + params->physical_device_name = argv[3]; + params->read_argc = 3; } else if ( @@ -135,8 +186,9 @@ int relabsd_parameters_parse_execution_mode ) { params->mode = RELABSD_PARAMETERS_SERVER_MODE; - params->node = (char *) NULL; - params->read_argc = 1; + params->communication_node_name = (char *) NULL; + params->physical_device_name = argv[2]; + params->read_argc = 2; } else { @@ -163,7 +215,12 @@ int relabsd_parameters_parse_options set_default_options(params); - for (i = params->read_argc; i < argc; ++i) + /* + * i = (params->read_argc + 1) because reading 2 params is actually reaching + * the [2] element of the array, since the [0] element is the executable + * name. + */ + for (i = (params->read_argc + 1); i < argc; ++i) { if ( diff --git a/src/device/physical_device.c b/src/device/physical_device.c index a518750..7e365da 100644 --- a/src/device/physical_device.c +++ b/src/device/physical_device.c @@ -1,15 +1,14 @@ -#include <fcntl.h> +/**** RELABSD *****************************************************************/ #include <errno.h> +#include <fcntl.h> #include <string.h> #include <unistd.h> +/**** LIBEVDEV ****************************************************************/ #include <libevdev/libevdev.h> -#include "error.h" -#include "axis.h" -#include "config.h" - -#include "input.h" +/**** RELABSD *****************************************************************/ +#include <relabsd/debug.h> /* * Ensures that the input device has enabled the EV_REL axes mentioned 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)); |


