From 72af8b5974a7e8e14b77d832053dbbe922a5035e Mon Sep 17 00:00:00 2001 From: Tom Balzer Date: Tue, 19 Jun 2018 02:04:02 -0500 Subject: [PATCH] Tooling changes, some socket code. --- Makefile | 2 +- kill-tomd.sh | 1 + run.sh | 1 + src/{ => dumb-client}/main.c | 43 ++------- src/tomd/main.c | 169 +++++++++++++++++++++++++++++++++++ 5 files changed, 179 insertions(+), 37 deletions(-) create mode 100755 kill-tomd.sh create mode 100755 run.sh rename src/{ => dumb-client}/main.c (51%) create mode 100644 src/tomd/main.c diff --git a/Makefile b/Makefile index bf84ead..325d894 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ # TODO > Use GNU automake tools tomd: - gcc -o tomd ./src/main.c + gcc -o tomd ./src/tomd/main.c diff --git a/kill-tomd.sh b/kill-tomd.sh new file mode 100755 index 0000000..be6a602 --- /dev/null +++ b/kill-tomd.sh @@ -0,0 +1 @@ +kill -n 15 $(pgrep tomd) diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..13cfbe1 --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +sudo ./kill-tomd.sh; rm ./tomd; make && sudo ./tomd; diff --git a/src/main.c b/src/dumb-client/main.c similarity index 51% rename from src/main.c rename to src/dumb-client/main.c index 54e375e..6fca06f 100644 --- a/src/main.c +++ b/src/dumb-client/main.c @@ -15,49 +15,20 @@ /* You should have received a copy of the GNU General Public License */ /* along with tomd. If not, see . */ -#include #include -#include - -static void header(void) -{ - printf("Tom's Daemon, Copyright (C) 2018 Thomas Balzer\n"); - printf("GPL v3 or later license.\n"); -} - -static void init(void) -{ - /* TODO */ -} - -/* These defines are here to improve the local readability */ -#define CHDIR 0 -#define DONT_CHDIR 1 -#define CLOSE_PIPES 0 -#define DONT_CLOSE_PIPES 1 - -static void daemonize(void) -{ - /* daemon is in unistd. */ - /* arg1 - 0 changes dir to / */ - /* arg2 - 0 closes all pipes (/dev/null) */ - /* WARNING > THIS BEHAVIOR IS SILENT AND EASY TO MISPLACE THE - PROCESS */ - daemon(DONT_CHDIR, CLOSE_PIPES); -} +#include -static void run(void) +void init(void) { - /* listen on socket, respond to requests. */ - /* perform all registered 'tick' operations */ + /* init socket connection */ + } int main(int argc, char **argv) { - header(); - init(); - daemonize(); - run(); + printf("dumb client startup.\n"); + + return EXIT_SUCCESS; } diff --git a/src/tomd/main.c b/src/tomd/main.c new file mode 100644 index 0000000..14c7397 --- /dev/null +++ b/src/tomd/main.c @@ -0,0 +1,169 @@ +/* Copyright (C) 2018 Thomas Balzer */ + +/* This file is part of tomd. */ + +/* tomd is free software: you can redistribute it and/or modify */ +/* it under the terms of the GNU General Public License as published by */ +/* the Free Software Foundation, either version 3 of the License, or */ +/* (at your option) any later version. */ + +/* tomd is distributed in the hope that it will be useful, */ +/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ +/* GNU General Public License for more details. */ + +/* You should have received a copy of the GNU General Public License */ +/* along with tomd. If not, see . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void header(void) +{ + printf("Tom's Daemon, Copyright (C) 2018 Thomas Balzer\n"); + printf("GPL v3 or later license.\n"); +} + +static int sfd; +static char socket_dirname[100]; +static char socket_filename[100]; + +static void gen_socket_filename(void) +{ + uid_t this_user_id = + getuid(); + sprintf(socket_dirname, + "/run/user/%d/tomd", + this_user_id); + sprintf(socket_filename, + "/run/user/%d/tomd/socket", + this_user_id); + + printf("making dir '%s'\n", socket_dirname); + /* lazy way */ + char buf[100]; + sprintf(buf, "mkdir -p %s", socket_dirname); + system(buf); + /* if(mkdir(socket_dirname, S_IRWXU) != 0){ */ + /* perror("socket mkdir"); */ + /* exit(EXIT_FAILURE); */ + /* } */ + + printf("removing file '%s'\n", socket_filename); + if(unlink(socket_filename) != 0){ + if(errno == ENOENT){ + printf("socket file doesn't exist\n"); + }else{ + perror("socket unlink"); + exit(EXIT_FAILURE); + } + } +} + +static void init(void) +{ + gen_socket_filename(); + + /* listen on socket, respond to requests. */ + /* perform all registered 'tick' operations */ + sfd = + socket(PF_LOCAL, /* namespace - local unix socket */ + SOCK_STREAM, /* style */ + 0); /* protocol */ + + if(sfd < 0){ + perror("socket"); + exit(EXIT_FAILURE); + } + + struct sockaddr_un name; + size_t size; + + name.sun_family = AF_LOCAL; + strncpy(name.sun_path, + socket_filename, + sizeof(name.sun_path)); + + size = SUN_LEN(&name); + printf("attempting to bind to '%s'\n", + socket_filename); + if(bind(sfd, + (struct sockaddr *) &name, + size) < 0){ + perror("bind"); + exit(EXIT_FAILURE); + } + + printf("initialized tomd socket connections\n"); +} + +/* These defines are here to improve the local readability */ +#define CHDIR 0 +#define DONT_CHDIR 1 +#define CLOSE_PIPES 0 +#define DONT_CLOSE_PIPES 1 + +static void daemonize(void) +{ + /* daemon is in unistd. */ + /* arg1 - 0 changes dir to / */ + /* arg2 - 0 closes all pipes (/dev/null) */ + /* WARNING > THIS BEHAVIOR IS SILENT AND EASY TO MISPLACE THE + PROCESS */ + daemon(DONT_CHDIR, DONT_CLOSE_PIPES); +} + +static void run(void) +{ + /* loop: */ + /* 1 listen */ + /* 2 accept/select */ + /* 3 handle dgram */ + int listen_bool = + listen(sfd, + 10); /* max connection limit is 10 for arbitrary reasons */ + if(listen_bool == -1){ + perror("listen"); + exit(EXIT_FAILURE); + } + + for(;;){ + int accept_sfd = + accept(sfd, + NULL, /* requester info */ + NULL); /* len of requester info */ + if(accept_sfd < 0){ + perror("accept socket"); + exit(EXIT_FAILURE); + } + + printf("accepted socket connection\n"); + close(accept_sfd); + break; + } +} + +static void cleanup(void) +{ + if(sfd >= 0){ + close(sfd); + } +} + +int main(int argc, char **argv) +{ + header(); + init(); + daemonize(); + run(); + cleanup(); + + return EXIT_SUCCESS; +} -- 2.20.1