open a socket on server/client that just echos; also fixed a critical bug in the daemon(++ -> -- lol) so now it actually daemonises

This commit is contained in:
snit 2024-10-05 03:58:16 -05:00
parent 9d255f7948
commit f2478951a2
11 changed files with 158 additions and 8 deletions

View File

@ -2,10 +2,13 @@
A list of things I'd like to accomplish A list of things I'd like to accomplish
## In Progress ## In Progress
* Expose a socket or pipe on the daemon for the client * Initialise a world again
* Create client to connect to daemon * Send world data to client
* Have client render world data (return to pre-daemonised equivalent state)
## Completed ## Completed
* Expose a socket or pipe on the daemon for the client
* Create client to connect to daemon
* Daemonise the serverside * Daemonise the serverside
* Parse command line arguments * Parse command line arguments
* Split into client, server, and common library * Split into client, server, and common library

View File

@ -1,8 +1,34 @@
// main.c // main.c
#include <stdlib.h> #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include "error.h"
#include "sock.h"
int main(void) { 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;
} }

27
client/src/sock.c Normal file
View File

@ -0,0 +1,27 @@
// sock.c
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#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;
}

8
client/src/sock.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef SOCK_H
#define SOCK_H
#include "error.h"
enum error_t sock_init(int *);
#endif

View File

@ -8,6 +8,7 @@ enum error_t {
ERR_NOTFOUND, ERR_NOTFOUND,
ERR_FORK, ERR_FORK,
ERR_SETSID, ERR_SETSID,
ERR_SOCKET,
__ERR_COUNT, __ERR_COUNT,
}; };

View File

@ -9,6 +9,7 @@ char const *const ERROR_STRS[] = {
"NOT FOUND", "NOT FOUND",
"FORK", "FORK",
"SETSID", "SETSID",
"SOCKET",
}; };

View File

@ -9,6 +9,7 @@
#include "daemon.h" #include "daemon.h"
enum error_t daemonise(void) { enum error_t daemonise(void) {
pid_t pid = fork(); pid_t pid = fork();
if (pid < 0) return ERR_FORK; // return if error if (pid < 0) return ERR_FORK; // return if error
@ -27,7 +28,7 @@ enum error_t daemonise(void) {
umask(0); umask(0);
chdir("/"); 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; return ERR_OK;
} }

View File

@ -1,29 +1,68 @@
// main.c // main.c
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h> #include <unistd.h>
#include <sys/syslog.h>
#include <sys/socket.h>
#include "error.h" #include "error.h"
#include "opts.h" #include "opts.h"
#include "sock.h"
static void handle_signal(int _) {
remove("/tmp/swd.sock");
exit(0);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
enum error_t err = ERR_OK; enum error_t err = ERR_OK;
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
struct options_t options = { 0 }; struct options_t options = { 0 };
opts_default(&options); opts_default(&options);
err = opts_parse(&options, argc, argv); err = opts_parse(&options, argc, argv);
if (err) goto handle_error; if (err) goto handle_error_pre_opts;
err = opts_init(&options); err = opts_init(&options);
if (err) goto handle_error; 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); opts_free(&options);
return ERR_OK; return ERR_OK;
handle_error_socket:
close(sock);
remove("/tmp/swd.sock");
handle_error: 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]); printf("ERROR: %s\n", ERROR_STRS[err]);
return err; return err;
} }

View File

@ -54,5 +54,8 @@ enum error_t opts_init(struct options_t const *options) {
void opts_free(struct options_t const *options) { void opts_free(struct options_t const *options) {
assert(options != NULL); assert(options != NULL);
if (options->daemonise) closelog(); if (options->daemonise) {
syslog(LOG_NOTICE, "Daemon deinitialised :-(");
closelog();
}
} }

33
server/src/sock.c Normal file
View File

@ -0,0 +1,33 @@
// sock.c
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#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;
}

8
server/src/sock.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef SOCK_H
#define SOCK_H
#include "error.h"
enum error_t sock_init(int *);
#endif