f4bf04bc40fd30404200cd0188de5f4feb662f89
[tlb/tomd.git] / src / tomd / main.c
1 /* Copyright (C) 2018 Thomas Balzer */
2
3 /* This file is part of tomd. */
4
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. */
9
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. */
14
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/>. */
17
18 #include <fcntl.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <sys/un.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <sys/wait.h>
29
30 #include "../../include/macros.h"
31 #include "../../include/manifest.h"
32 #include "../../include/job.h"
33
34 static void header(void)
35 {
36 tomd_p("Tom's Daemon, Copyright (C) 2018 Thomas Balzer");
37 tomd_p("GPL v3 or later license.");
38 }
39
40 static int sfd;
41 static char socket_dirname[100];
42 static char socket_filename[100];
43
44 static void gen_socket_filename(void)
45 {
46 uid_t this_user_id =
47 getuid();
48 sprintf(socket_dirname,
49 "/run/user/%d/tomd",
50 this_user_id);
51 sprintf(socket_filename,
52 "/run/user/%d/tomd/socket",
53 this_user_id);
54
55 tomd_p("making dir '%s'", socket_dirname);
56 /* lazy way */
57 /* char buf[100]; */
58 /* sprintf(buf, "mkdir -p %s", socket_dirname); */
59 /* system(buf); */
60
61 if(mkdir(socket_dirname, S_IRWXU) != 0){
62 if(errno != EEXIST){
63 tomd_panic("socket mkdir");
64 }else{
65 tomd_p("socket dir exists.");
66 }
67 }
68
69 tomd_p("removing file '%s'", socket_filename);
70 if(unlink(socket_filename) != 0){
71 if(errno == ENOENT){
72 tomd_p("socket file doesn't exist");
73 }else{
74 tomd_panic("socket unlink");
75 }
76 }
77 }
78
79 static void socket_init(void)
80 {
81 gen_socket_filename();
82
83 /* listen on socket, respond to requests. */
84 /* perform all registered 'tick' operations */
85 sfd =
86 socket(PF_LOCAL, /* namespace - local unix socket */
87 SOCK_STREAM, /* style */
88 0); /* protocol */
89
90 if(sfd < 0){
91 tomd_panic("socket");
92 }
93
94 if(fcntl(sfd, F_SETFL, O_NONBLOCK) == -1){
95 tomd_panic("fnctl");
96 }
97
98 struct sockaddr_un name;
99 size_t size;
100
101 name.sun_family = AF_LOCAL;
102 strncpy(name.sun_path,
103 socket_filename,
104 sizeof(name.sun_path));
105
106 size = SUN_LEN(&name);
107 tomd_p("attempting to bind to '%s'",
108 socket_filename);
109 if(bind(sfd,
110 (struct sockaddr *) &name,
111 size) < 0){
112 tomd_panic("bind");
113 }
114
115 tomd_p("initialized tomd socket connections");
116 }
117
118 static void init(void)
119 {
120 socket_init();
121 load_jobs();
122 }
123
124 #define SEND_ACK { \
125 int size; \
126 char ACK[4]; \
127 ACK[0] = 'A'; \
128 ACK[1] = 'C'; \
129 ACK[2] = 'K'; \
130 ACK[3] = '\0'; \
131 if(size = write(asfd, ACK, sizeof(ACK)) != sizeof(ACK)) { \
132 tomd_p("didn't send as much as we though (%d != %d)", \
133 size, sizeof(ACK)); \
134 } \
135 }
136
137 #define CHDIR 0
138 #define DONT_CHDIR 1
139 #define CLOSE_PIPES 0
140 #define DONT_CLOSE_PIPES 1
141
142 static void daemonize(void)
143 {
144 /* daemon is in unistd. */
145 /* arg1 - 0 changes dir to / */
146 /* arg2 - 0 closes all pipes (/dev/null) */
147 /* WARNING > THIS BEHAVIOR IS SILENT AND EASY TO MISPLACE THE
148 PROCESS */
149 /* daemon will exit the original process. */
150 daemon(DONT_CHDIR, DONT_CLOSE_PIPES);
151 }
152
153 #define READ_SOCKET { \
154 memset(buf, 0, sizeof(buf)); \
155 int size = read(asfd, buf, 100); \
156 buf[size] = '\0'; \
157 }
158
159 static int validate_sender(int asfd, char *buf)
160 {
161 READ_SOCKET;
162 if(strcmp(buf, "client:tomc") == 0){
163 SEND_ACK;
164 return 0;
165 }
166 tomd_p("received '%s' instead of client:tomc", buf);
167 return -1;
168 }
169
170 enum requests { KILL, STATUS, STOP, START, UNKNOWN};
171 #define X(a, b) { a, b }
172 static struct {
173 char *str;
174 enum requests request;
175 } request_types[] =
176 {
177 X("kill", KILL),
178 X("status", STATUS),
179 X("start", START),
180 X("stop", STOP),
181 X(NULL, -1)
182 };
183 #undef X
184
185 static void handle_request(int asfd, char *buf)
186 {
187 READ_SOCKET;
188 enum requests request = UNKNOWN;
189 int i = 0;
190 while(1){
191 if(request_types[i].str == NULL){
192 break;
193 }
194
195 tomd_p("loop [%d]: comparing '%s' to '%s'",
196 i, buf, request_types[i].str);
197 if(strcmp(buf, request_types[i].str) == 0){
198 request = request_types[i].request;
199 break;
200 }
201
202 i++;
203 }
204
205 if(request == UNKNOWN){
206 tomd_p("unknown request type!");
207 return;
208 }
209
210 SEND_ACK;
211 READ_SOCKET;
212 // cross reference given name against known services
213 tomd_p("looking up '%s'", buf);
214 SEND_ACK;
215 struct job *jp = lookup_job(buf);
216 #define DOUBLE_DUTY(...) { \
217 char tmp[100]; \
218 tomd_p(__VA_ARGS__); \
219 sprintf(tmp, __VA_ARGS__); \
220 int tmplen = strlen(tmp); \
221 tomd_p("__debug__:'%s'", tmp); \
222 write(asfd, tmp, tmplen); \
223 }
224 if(jp == NULL){
225 DOUBLE_DUTY("lookup of '%s' failed.\n", buf);
226 } else {
227 tomd_p("-----");
228 DOUBLE_DUTY("found job record for '%s'\n", jp->name);
229
230 if(request == STATUS){
231 if(jp->pid != -1){
232 int status = 0;
233 int n_pid = waitpid(jp->pid, &status, WNOHANG);
234 if(n_pid < 0){
235 perror("[tomd] waitpid");
236 }else if(n_pid == 0){
237 DOUBLE_DUTY("waitpid == 0, check manual\n");
238 }else{
239 DOUBLE_DUTY("running: %s\n",
240 !WIFEXITED(status) ? "yes"
241 : "no");
242
243 if(WIFEXITED(status)){
244 DOUBLE_DUTY(" status: %d\n", WEXITSTATUS(status));
245 jp->pid = -1;
246 jp->last_status = status;
247 }
248 }
249 }else{
250 DOUBLE_DUTY("running: no\n");
251 DOUBLE_DUTY(" status: %d\n", jp->last_status);
252 }
253
254 if(jp->pid != -1){
255 DOUBLE_DUTY(" pid: %d\n", jp->pid);
256 }
257 }else{
258 DOUBLE_DUTY("don't know how to handle that request.\n");
259 }
260 }
261 tomd_p("-----");
262 tomd_p("sending ack.");
263 SEND_ACK;
264 tomd_p("ack is sent.");
265 get_ack:
266 READ_SOCKET;
267 if(strcmp(buf, "ACK") != 0)
268 goto get_ack;
269 tomd_p("ack was ack'd.");
270 }
271
272 static void handle_connection(int asfd)
273 {
274 char buf[100] = {};
275
276 if(validate_sender(asfd, buf) != 0){
277 tomd_p("invalid sender.");
278 return;
279 }
280
281 tomd_p("validated client");
282 handle_request(asfd, buf);
283
284 shutdown(asfd, SHUT_WR);
285 close(asfd);
286 }
287
288 static void run(void)
289 {
290 /* loop: */
291 /* 1 listen */
292 /* 2 accept/select */
293 /* 3 handle dgram */
294 int listen_bool =
295 listen(sfd,
296 10); /* max connection limit is 10 for arbitrary reasons */
297 if(listen_bool == -1){
298 tomd_panic("listen");
299 }
300
301 for(;;){
302 struct sockaddr addr;
303 /* sometimes gives EINVAL if not initialized. */
304 socklen_t size = sizeof(addr);
305 errno = 0;
306 int accept_sfd =
307 accept(sfd,
308 &addr, /* requester info */
309 &size); /* len of requester info */
310 if(accept_sfd < 0){
311 if(errno == EINVAL){
312 tomd_p("EINVAL");
313 }else if(errno == EWOULDBLOCK || errno == EAGAIN){
314 /* no worries */
315 }else {
316 tomd_panic("accept socket");
317 }
318 } else {
319 handle_connection(accept_sfd);
320 }
321
322 check_root_job();
323 usleep(10);
324 }
325 }
326
327 static void cleanup(void)
328 {
329 if(sfd >= 0){
330 close(sfd);
331 }
332 }
333
334 int main(int argc, char **argv)
335 {
336 header();
337 init();
338 /* daemonize(); */
339 run_jobs();
340 /* silent(); */
341 run();
342 cleanup();
343
344 return EXIT_SUCCESS;
345 }