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>
29 #include "../../include/macros.h"
30 #include "../../include/manifest.h"
31 #include "../../include/job.h"
33 static void header(void)
35 tomd_p("Tom's Daemon, Copyright (C) 2018 Thomas Balzer");
36 tomd_p("GPL v3 or later license.");
40 static char socket_dirname
[100];
41 static char socket_filename
[100];
43 static void gen_socket_filename(void)
47 sprintf(socket_dirname
,
50 sprintf(socket_filename
,
51 "/run/user/%d/tomd/socket",
54 tomd_p("making dir '%s'", socket_dirname
);
57 sprintf(buf
, "mkdir -p %s", socket_dirname
);
59 /* if(mkdir(socket_dirname, S_IRWXU) != 0){ */
60 /* perror("socket mkdir"); */
61 /* exit(EXIT_FAILURE); */
64 tomd_p("removing file '%s'", socket_filename
);
65 if(unlink(socket_filename
) != 0){
67 tomd_p("socket file doesn't exist");
69 perror("[tomd] socket unlink");
75 static void socket_init(void)
77 gen_socket_filename();
79 /* listen on socket, respond to requests. */
80 /* perform all registered 'tick' operations */
82 socket(PF_LOCAL
, /* namespace - local unix socket */
83 SOCK_STREAM
, /* style */
87 perror("[tomd] socket");
91 struct sockaddr_un name
;
94 name
.sun_family
= AF_LOCAL
;
95 strncpy(name
.sun_path
,
97 sizeof(name
.sun_path
));
99 size
= SUN_LEN(&name
);
100 tomd_p("attempting to bind to '%s'",
103 (struct sockaddr
*) &name
,
105 perror("[tomd] bind");
109 tomd_p("initialized tomd socket connections");
112 static void init(void)
125 if(size = write(asfd, ACK, sizeof(ACK)) != sizeof(ACK)) { \
126 tomd_p("didn't send as much as we though (%d != %d)",\
127 size, sizeof(ACK)); \
133 #define CLOSE_PIPES 0
134 #define DONT_CLOSE_PIPES 1
136 static void daemonize(void)
138 /* daemon is in unistd. */
139 /* arg1 - 0 changes dir to / */
140 /* arg2 - 0 closes all pipes (/dev/null) */
141 /* WARNING > THIS BEHAVIOR IS SILENT AND EASY TO MISPLACE THE
143 daemon(DONT_CHDIR
, DONT_CLOSE_PIPES
);
146 #define READ_SOCKET {\
147 memset(buf, 0, sizeof(buf)); \
148 int size = read(asfd, buf, 100); \
152 static int validate_sender(int asfd
, char *buf
)
155 if(strcmp(buf
, "client:tomc") == 0){
159 tomd_p("received '%s' instead of client:tomc", buf
);
163 enum requests
{ KILL
, STATUS
, STOP
, START
, UNKNOWN
};
164 #define X(a, b) { a, b }
167 enum requests request
;
178 static void handle_request(int asfd
, char *buf
)
181 enum requests request
= UNKNOWN
;
184 if(request_types
[i
].str
== NULL
){
188 tomd_p("loop [%d]: comparing '%s' to '%s'",
189 i
, buf
, request_types
[i
].str
);
190 if(strcmp(buf
, request_types
[i
].str
) == 0){
191 request
= request_types
[i
].request
;
198 if(request
== UNKNOWN
){
199 tomd_p("unknown request type!");
205 // cross reference given name against known services
206 tomd_p("looking up '%s'", buf
);
208 struct job
*jp
= lookup_job(buf
);
209 #define DOUBLE_DUTY(...) { \
211 tomd_p(__VA_ARGS__); \
212 sprintf(tmp, __VA_ARGS__); \
213 int tmplen = strlen(tmp); \
214 tomd_p("__debug__:'%s'", tmp);\
215 write(asfd, tmp, tmplen); \
218 DOUBLE_DUTY("lookup of '%s' failed.\n", buf
);
222 DOUBLE_DUTY("found job record for '%s'\n", jp
->name
);
224 if(request
== STATUS
){
227 int n_pid
= waitpid(jp
->pid
, &status
, WNOHANG
);
229 perror("[tomd] waitpid");
230 }else if(n_pid
== 0){
231 DOUBLE_DUTY("waitpid == 0, check manual\n");
233 DOUBLE_DUTY("running: %s\n",
234 !WIFEXITED(status
) ? "yes"
237 if(WIFEXITED(status
)){
238 DOUBLE_DUTY(" status: %d\n", WEXITSTATUS(status
));
240 jp
->last_status
= status
;
244 DOUBLE_DUTY("running: no\n");
245 DOUBLE_DUTY(" status: %d\n", jp
->last_status
);
249 DOUBLE_DUTY(" pid: %d\n", jp
->pid
);
252 DOUBLE_DUTY("don't know how to handle that request.\n");
256 tomd_p("sending ack.");
258 tomd_p("ack is sent.");
261 if(strcmp(buf
, "ACK") != 0)
263 tomd_p("ack was ack'd.");
266 static void handle_connection(int asfd
)
270 if(validate_sender(asfd
, buf
) != 0){
271 tomd_p("invalid sender.");
275 tomd_p("validated client");
276 handle_request(asfd
, buf
);
278 shutdown(asfd
, SHUT_WR
);
282 static void run(void)
286 /* 2 accept/select */
290 10); /* max connection limit is 10 for arbitrary reasons */
291 if(listen_bool
== -1){
292 perror("[tomd] listen");
298 struct sockaddr addr
;
299 /* sometimes gives EINVAL if not initialized. */
300 socklen_t size
= sizeof(addr
);
301 tomd_p("accept loop [%d]", i
++);
305 &addr
, /* requester info */
306 &size
); /* len of requester info */
311 perror("[tomd] accept socket");
315 handle_connection(accept_sfd
);
319 static void cleanup(void)
326 int main(int argc
, char **argv
)