/* You should have received a copy of the GNU General Public License */
/* along with tomd. If not, see <http://www.gnu.org/licenses/>. */
-#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
-
-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 <stdio.h>
-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;
}
--- /dev/null
+/* 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 <http://www.gnu.org/licenses/>. */
+
+#include <stddef.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/un.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+
+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;
+}