diff --git a/TODO.gmi b/TODO.gmi index ada657b..cb57a5f 100644 --- a/TODO.gmi +++ b/TODO.gmi @@ -2,10 +2,13 @@ A list of things I'd like to accomplish ## In Progress -* Expose a socket or pipe on the daemon for the client -* Create client to connect to daemon +* Initialise a world again +* Send world data to client +* Have client render world data (return to pre-daemonised equivalent state) ## Completed +* Expose a socket or pipe on the daemon for the client +* Create client to connect to daemon * Daemonise the serverside * Parse command line arguments * Split into client, server, and common library diff --git a/client/src/main.c b/client/src/main.c index 6052ea9..50a0446 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -1,8 +1,34 @@ // main.c -#include +#include +#include +#include +#include + +#include "error.h" +#include "sock.h" int main(void) { + enum error_t err = ERR_OK; - return EXIT_SUCCESS; + int sock; + err = sock_init(&sock); + if (err) goto handle_error; + + char ibuf[1028] = { 0 }; + char obuf[1028] = { 0 }; + strcpy(obuf, "Hello, world!\n"); + + // Just echoing everything back for now + write(sock, obuf, 1028); + int rc = read(sock, ibuf, 1028); + + if (rc > 0) printf("%s", ibuf); + + close(sock); + return ERR_OK; + +handle_error: + printf("ERROR: %s\n", ERROR_STRS[err]); + return err; } diff --git a/client/src/sock.c b/client/src/sock.c new file mode 100644 index 0000000..40b8517 --- /dev/null +++ b/client/src/sock.c @@ -0,0 +1,27 @@ +// sock.c + +#include +#include +#include + +#include "sock.h" + +enum error_t sock_init(int *sockptr) { + int const sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) goto sock_init_error; + + struct sockaddr_un sa = { 0 }; + sa.sun_family = AF_UNIX; + strcpy(sa.sun_path, "/tmp/swd.sock"); + // Socket path should be a shared setting between server and client + + if (connect(sock, (struct sockaddr *)&sa, sizeof(sa))) goto sock_error; + + *sockptr = sock; + return ERR_OK; + +sock_error: + close(sock); +sock_init_error: + return ERR_SOCKET; +} diff --git a/client/src/sock.h b/client/src/sock.h new file mode 100644 index 0000000..fa54a69 --- /dev/null +++ b/client/src/sock.h @@ -0,0 +1,8 @@ +#ifndef SOCK_H +#define SOCK_H + +#include "error.h" + +enum error_t sock_init(int *); + +#endif diff --git a/common/include/error.h b/common/include/error.h index 4ea7b9d..91adee6 100644 --- a/common/include/error.h +++ b/common/include/error.h @@ -8,6 +8,7 @@ enum error_t { ERR_NOTFOUND, ERR_FORK, ERR_SETSID, + ERR_SOCKET, __ERR_COUNT, }; diff --git a/common/src/error.c b/common/src/error.c index 9ab5dc4..191bd9d 100644 --- a/common/src/error.c +++ b/common/src/error.c @@ -9,6 +9,7 @@ char const *const ERROR_STRS[] = { "NOT FOUND", "FORK", "SETSID", + "SOCKET", }; diff --git a/server/src/daemon.c b/server/src/daemon.c index 7681f32..f955206 100644 --- a/server/src/daemon.c +++ b/server/src/daemon.c @@ -9,6 +9,7 @@ #include "daemon.h" + enum error_t daemonise(void) { pid_t pid = fork(); if (pid < 0) return ERR_FORK; // return if error @@ -27,7 +28,7 @@ enum error_t daemonise(void) { umask(0); chdir("/"); - for (int i = sysconf(_SC_OPEN_MAX); i >= 0; i++) close(i); + for (int i = sysconf(_SC_OPEN_MAX); i >= 0; i--) close(i); return ERR_OK; } diff --git a/server/src/main.c b/server/src/main.c index e88317a..6916522 100644 --- a/server/src/main.c +++ b/server/src/main.c @@ -1,29 +1,68 @@ // main.c #include +#include +#include #include +#include +#include #include "error.h" + #include "opts.h" +#include "sock.h" + +static void handle_signal(int _) { + remove("/tmp/swd.sock"); + exit(0); +} + int main(int argc, char **argv) { enum error_t err = ERR_OK; + signal(SIGINT, handle_signal); + signal(SIGTERM, handle_signal); + struct options_t options = { 0 }; opts_default(&options); err = opts_parse(&options, argc, argv); - if (err) goto handle_error; + if (err) goto handle_error_pre_opts; err = opts_init(&options); if (err) goto handle_error; - // Open a socket and connect to clients + int sock; + err = sock_init(&sock); + if (err) goto handle_error; + // Can I move this into its own function that takes a function pointer? + for (int sock_accept; ;) { + sock_accept = accept(sock, NULL, NULL); + if (sock_accept < 0) { err = ERR_SOCKET; goto handle_error_socket; } + + char ibuf[1028] = { 0 }; + + // Just echoing everything back for now + read(sock_accept, ibuf, 1028); + write(sock_accept, ibuf, 1028); + + close(sock_accept); + } + + close(sock); + remove("/tmp/swd.sock"); opts_free(&options); return ERR_OK; +handle_error_socket: + close(sock); + remove("/tmp/swd.sock"); handle_error: + if (options.daemonise) syslog(LOG_ERR, "%s", ERROR_STRS[err]); + opts_free(&options); +handle_error_pre_opts: printf("ERROR: %s\n", ERROR_STRS[err]); return err; } diff --git a/server/src/opts.c b/server/src/opts.c index e6dd37c..67f9dd2 100644 --- a/server/src/opts.c +++ b/server/src/opts.c @@ -54,5 +54,8 @@ enum error_t opts_init(struct options_t const *options) { void opts_free(struct options_t const *options) { assert(options != NULL); - if (options->daemonise) closelog(); + if (options->daemonise) { + syslog(LOG_NOTICE, "Daemon deinitialised :-("); + closelog(); + } } diff --git a/server/src/sock.c b/server/src/sock.c new file mode 100644 index 0000000..989e339 --- /dev/null +++ b/server/src/sock.c @@ -0,0 +1,33 @@ +// sock.c + +#include +#include +#include +#include + +#include "sock.h" + +enum error_t sock_init(int *sockptr) { + int const sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) goto sock_init_error; + + struct sockaddr_un sa = { 0 }; + sa.sun_family = AF_UNIX; + strcpy(sa.sun_path, "/tmp/swd.sock"); + // Socket path should be a shared setting between server and client + + remove("/tmp/swd.sock"); + + if (bind(sock, (struct sockaddr *)&sa, sizeof(sa))) goto sock_error; + + if (listen(sock, 4096) < 0) goto sock_error; + + *sockptr = sock; + return ERR_OK; + +sock_error: + close(sock); + remove("/tmp/swd.sock"); +sock_init_error: + return ERR_SOCKET; +} diff --git a/server/src/sock.h b/server/src/sock.h new file mode 100644 index 0000000..fa54a69 --- /dev/null +++ b/server/src/sock.h @@ -0,0 +1,8 @@ +#ifndef SOCK_H +#define SOCK_H + +#include "error.h" + +enum error_t sock_init(int *); + +#endif