1 /* Copyright (C) 2018 Thomas Balzer */
3 /* This file is part of tomd. */
5 /* tomd is free software: you can redistribute it and/or modify */
6 /* it under the terms of the GNU General Public License as published by */
7 /* the Free Software Foundation, either version 3 of the License, or */
8 /* (at your option) any later version. */
10 /* tomd is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU General Public License for more details. */
15 /* You should have received a copy of the GNU General Public License */
16 /* along with tomd. If not, see <http://www.gnu.org/licenses/>. */
26 #include <sys/socket.h>
28 #include "../../include/macros.h"
29 #include "../../include/manifest.h"
31 static void header(void)
33 tomd_p("Tom's Daemon, Copyright (C) 2018 Thomas Balzer");
34 tomd_p("GPL v3 or later license.");
38 static char socket_dirname
[100];
39 static char socket_filename
[100];
41 static void gen_socket_filename(void)
45 sprintf(socket_dirname
,
48 sprintf(socket_filename
,
49 "/run/user/%d/tomd/socket",
52 tomd_p("making dir '%s'", socket_dirname
);
55 sprintf(buf
, "mkdir -p %s", socket_dirname
);
57 /* if(mkdir(socket_dirname, S_IRWXU) != 0){ */
58 /* perror("socket mkdir"); */
59 /* exit(EXIT_FAILURE); */
62 tomd_p("removing file '%s'", socket_filename
);
63 if(unlink(socket_filename
) != 0){
65 tomd_p("socket file doesn't exist");
67 perror("[tomd] socket unlink");
73 static void socket_init(void)
75 gen_socket_filename();
77 /* listen on socket, respond to requests. */
78 /* perform all registered 'tick' operations */
80 socket(PF_LOCAL
, /* namespace - local unix socket */
81 SOCK_STREAM
, /* style */
85 perror("[tomd] socket");
89 struct sockaddr_un name
;
92 name
.sun_family
= AF_LOCAL
;
93 strncpy(name
.sun_path
,
95 sizeof(name
.sun_path
));
97 size
= SUN_LEN(&name
);
98 tomd_p("attempting to bind to '%s'",
101 (struct sockaddr
*) &name
,
103 perror("[tomd] bind");
107 tomd_p("initialized tomd socket connections");
110 static void init(void)
116 /* These defines are here to improve the local readability */
119 #define CLOSE_PIPES 0
120 #define DONT_CLOSE_PIPES 1
122 static void daemonize(void)
124 /* daemon is in unistd. */
125 /* arg1 - 0 changes dir to / */
126 /* arg2 - 0 closes all pipes (/dev/null) */
127 /* WARNING > THIS BEHAVIOR IS SILENT AND EASY TO MISPLACE THE
129 daemon(DONT_CHDIR
, DONT_CLOSE_PIPES
);
132 static void run(void)
136 /* 2 accept/select */
140 10); /* max connection limit is 10 for arbitrary reasons */
141 if(listen_bool
== -1){
142 perror("[tomd] listen");
147 struct sockaddr addr
;
151 &addr
, /* requester info */
152 &size
); /* len of requester info */
154 perror("[tomd] accept socket");
158 /* int getsockname(accept_sfd, */
162 tomd_p("accepted socket connection from '%s'",
163 ((struct sockaddr_un
*) &addr
)->sun_path
);
165 char accept_buf
[100] = {};
167 read(accept_sfd
, accept_buf
, 100);
168 tomd_p("got message '%s', accept_buf");
173 static void cleanup(void)
180 int main(int argc
, char **argv
)