#include <sys/stat.h>
#include <sys/socket.h>
+#include "../../include/macros.h"
+#include "../../include/manifest.h"
+
static void header(void)
{
- printf("Tom's Daemon, Copyright (C) 2018 Thomas Balzer\n");
- printf("GPL v3 or later license.\n");
+ tomd_p("Tom's Daemon, Copyright (C) 2018 Thomas Balzer");
+ tomd_p("GPL v3 or later license.");
}
static int sfd;
"/run/user/%d/tomd/socket",
this_user_id);
- printf("making dir '%s'\n", socket_dirname);
+ tomd_p("making dir '%s'", socket_dirname);
/* lazy way */
char buf[100];
sprintf(buf, "mkdir -p %s", socket_dirname);
/* exit(EXIT_FAILURE); */
/* } */
- printf("removing file '%s'\n", socket_filename);
+ tomd_p("removing file '%s'", socket_filename);
if(unlink(socket_filename) != 0){
if(errno == ENOENT){
- printf("socket file doesn't exist\n");
+ tomd_p("socket file doesn't exist");
}else{
- perror("socket unlink");
+ perror("[tomd] socket unlink");
exit(EXIT_FAILURE);
}
}
}
-static void init(void)
+static void socket_init(void)
{
gen_socket_filename();
0); /* protocol */
if(sfd < 0){
- perror("socket");
+ perror("[tomd] socket");
exit(EXIT_FAILURE);
}
sizeof(name.sun_path));
size = SUN_LEN(&name);
- printf("attempting to bind to '%s'\n",
+ tomd_p("attempting to bind to '%s'",
socket_filename);
if(bind(sfd,
(struct sockaddr *) &name,
size) < 0){
- perror("bind");
+ perror("[tomd] bind");
exit(EXIT_FAILURE);
}
- printf("initialized tomd socket connections\n");
+ tomd_p("initialized tomd socket connections");
+}
+
+static void init(void)
+{
+ socket_init();
+ load_jobs();
}
/* These defines are here to improve the local readability */
listen(sfd,
10); /* max connection limit is 10 for arbitrary reasons */
if(listen_bool == -1){
- perror("listen");
+ perror("[tomd] listen");
exit(EXIT_FAILURE);
}
for(;;){
+ struct sockaddr addr;
+ socklen_t size;
int accept_sfd =
accept(sfd,
- NULL, /* requester info */
- NULL); /* len of requester info */
+ &addr, /* requester info */
+ &size); /* len of requester info */
if(accept_sfd < 0){
- perror("accept socket");
+ perror("[tomd] accept socket");
exit(EXIT_FAILURE);
}
- printf("accepted socket connection\n");
+ /* int getsockname(accept_sfd, */
+ /* addr, */
+ /* size); */
+
+ tomd_p("accepted socket connection from '%s'",
+ ((struct sockaddr_un *) &addr)->sun_path);
+
+ char accept_buf[100] = {};
+
+ read(accept_sfd, accept_buf, 100);
+ tomd_p("got message '%s', accept_buf");
close(accept_sfd);
- break;
}
}
{
header();
init();
+ run_jobs();
daemonize();
run();
cleanup();