From 952aba97bf1a6cd337e29f5b5d481e26c3aeb56e Mon Sep 17 00:00:00 2001 From: Tom Balzer Date: Wed, 27 Jun 2018 05:35:17 -0500 Subject: [PATCH] Some socket stuff in tomc, start of support in tomd --- src/request-documentation | 22 ++++++++ src/tomc/main.c | 111 +++++++++++++++++++++++++++++++++----- src/tomd/main.c | 4 +- 3 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 src/request-documentation diff --git a/src/request-documentation b/src/request-documentation new file mode 100644 index 0000000..0b85824 --- /dev/null +++ b/src/request-documentation @@ -0,0 +1,22 @@ +| kill | status | start | stop | meaning | +|------+--------+-------+------+----------------------------------------------------| +| 0 | 0 | 0 | 0 | no request | +| 0 | 0 | 0 | 1 | stop only | +| 0 | 0 | 1 | 0 | start only | +| 0 | 0 | 1 | 1 | start + stop (error) | +| 0 | 1 | 0 | 0 | status only | +| 0 | 1 | 0 | 1 | status + stop | +| 0 | 1 | 1 | 0 | status + start | +| 0 | 1 | 1 | 1 | status + start + stop (error, show status | +| 1 | 0 | 0 | 0 | kill only | +| 1 | 0 | 0 | 1 | kill + stop (error, kill) | +| 1 | 0 | 1 | 0 | kill + start (error, kill) | +| 1 | 0 | 1 | 1 | kill + start + stop (error, kill) | +| 1 | 1 | 0 | 0 | kill + status | +| 1 | 1 | 0 | 1 | kill + status + stop (error, kill, status) | +| 1 | 1 | 1 | 0 | kill + status + start (error, kill) | +| 1 | 1 | 1 | 1 | kill + status + start + stop (error, kill, status) | + + + + diff --git a/src/tomc/main.c b/src/tomc/main.c index 6fb6030..9e09809 100644 --- a/src/tomc/main.c +++ b/src/tomc/main.c @@ -53,16 +53,6 @@ static void init(void) } } -#define HALLO "hallo there from dumb-client" - -void write_hallo(void) -{ - printf("writing hallo\n"); - ssize_t wrote = - write(sfd, HALLO, sizeof HALLO); - printf("wrote %d bytes.\n", wrote); -} - static char *default_name = "no_name"; static struct{ char status, stop, kill, start; @@ -102,7 +92,7 @@ static void extract_options(int argc, char **argv) } } -static void print_options() +static void print_options(void) { tomc_p("---------"); tomc_p(" name: %s", options.name); @@ -113,13 +103,110 @@ static void print_options() tomc_p("---------"); } +#define HALLO "hallo there from dumb-client" + +void write_hallo(void) +{ + printf("writing hallo\n"); + ssize_t wrote = + write(sfd, HALLO, sizeof HALLO); + printf("wrote %d bytes.\n", wrote); +} + +static char write_buf[100]; +static char socket_buf[100]; + +#define SOCK_WRITE(X) { \ + strcpy(write_buf, X); \ + int write_len = strlen(write_buf); \ + ssize_t wrote = \ + write(sfd, write_buf, write_len); \ + if(wrote != write_len){ \ + perror("[tomc] write size mismatch"); \ + exit(EXIT_FAILURE); \ + } \ + } + +#define SOCK_READ { read(sfd, socket_buf, 100); } +#define SOCK_READ_X(X){ \ + SOCK_READ; \ + if(strcmp(socket_buf, X) != 0){ \ + tomc_p("protocol error. instead of ACK we got '%s'", socket_buf); \ + exit(EXIT_FAILURE); \ + } \ + } +#define SOCK_ACK {SOCK_READ_X("ACK")}; +#define SOCK_REQUEST(X) { \ + SOCK_WRITE(X); \ + SOCK_ACK; \ + SOCK_WRITE(options.name); \ + SOCK_ACK; \ + SOCK_READ; \ + tomc_p("tomd reports '%s'", socket_buf); \ + } + +static void write_client(void) +{ + SOCK_WRITE("client:tomc"); + SOCK_ACK; +} + +static void write_start(void) +{ + +} + +static void write_stop(void) +{ + SOCK_REQUEST("stop"); +} + +static void write_kill(void) +{ + SOCK_REQUEST("kill"); +} + +static void write_status(void) +{ + SOCK_REQUEST("status"); +} + +static void write_requests(void) +{ + write_client(); + + /* go through options to figure out what we want to do */ + if(options.kill){ + SOCK_REQUEST("kill"); + + if(options.start == 1 || + options.stop == 1){ + tomc_p("can only kill and status at once."); + } + } else { + if(options.start){ + if(options.stop){ + tomc_p("can't start and stop at once."); + } + SOCK_REQUEST("start"); + } + if(options.stop){ + SOCK_REQUEST("stop"); + } + } + + if(options.status){ + SOCK_REQUEST("status"); + } +} + int main(int argc, char **argv) { header(); extract_options(argc, argv); print_options(); init(); - + write_requests(); return EXIT_SUCCESS; } diff --git a/src/tomd/main.c b/src/tomd/main.c index 1ce9465..1f9b207 100644 --- a/src/tomd/main.c +++ b/src/tomd/main.c @@ -181,9 +181,9 @@ int main(int argc, char **argv) { header(); init(); - /* daemonize(); */ - /* run(); */ run_jobs(); + daemonize(); + run(); cleanup(); return EXIT_SUCCESS; -- 2.20.1