1b975241f091e0d51c63d2e6eeb3306f0f1fd143
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 static void header(void)
30 printf("[tomd] Tom's Daemon, Copyright (C) 2018 Thomas Balzer\n");
31 printf("[tomd] GPL v3 or later license.\n");
35 static char socket_dirname
[100];
36 static char socket_filename
[100];
38 static void gen_socket_filename(void)
42 sprintf(socket_dirname
,
45 sprintf(socket_filename
,
46 "/run/user/%d/tomd/socket",
49 printf("[tomd] making dir '%s'\n", socket_dirname
);
52 sprintf(buf
, "mkdir -p %s", socket_dirname
);
54 /* if(mkdir(socket_dirname, S_IRWXU) != 0){ */
55 /* perror("socket mkdir"); */
56 /* exit(EXIT_FAILURE); */
59 printf("[tomd] removing file '%s'\n", socket_filename
);
60 if(unlink(socket_filename
) != 0){
62 printf("[tomd] socket file doesn't exist\n");
64 perror("[tomd] socket unlink");
70 static void init(void)
72 gen_socket_filename();
74 /* listen on socket, respond to requests. */
75 /* perform all registered 'tick' operations */
77 socket(PF_LOCAL
, /* namespace - local unix socket */
78 SOCK_STREAM
, /* style */
82 perror("[tomd] socket");
86 struct sockaddr_un name
;
89 name
.sun_family
= AF_LOCAL
;
90 strncpy(name
.sun_path
,
92 sizeof(name
.sun_path
));
94 size
= SUN_LEN(&name
);
95 printf("[tomd] attempting to bind to '%s'\n",
98 (struct sockaddr
*) &name
,
100 perror("[tomd] bind");
104 printf("[tomd] initialized tomd socket connections\n");
107 /* These defines are here to improve the local readability */
110 #define CLOSE_PIPES 0
111 #define DONT_CLOSE_PIPES 1
113 static void daemonize(void)
115 /* daemon is in unistd. */
116 /* arg1 - 0 changes dir to / */
117 /* arg2 - 0 closes all pipes (/dev/null) */
118 /* WARNING > THIS BEHAVIOR IS SILENT AND EASY TO MISPLACE THE
120 daemon(DONT_CHDIR
, DONT_CLOSE_PIPES
);
123 static void run(void)
127 /* 2 accept/select */
131 10); /* max connection limit is 10 for arbitrary reasons */
132 if(listen_bool
== -1){
133 perror("[tomd] listen");
138 struct sockaddr addr
;
142 &addr
, /* requester info */
143 &size
); /* len of requester info */
145 perror("[tomd] accept socket");
149 /* int getsockname(accept_sfd, */
153 printf("[tomd] accepted socket connection from '%s'\n",
154 ((struct sockaddr_un
*) &addr
)->sun_path
);
156 char accept_buf
[100] = {};
158 read(accept_sfd
, accept_buf
, 100);
159 printf("[tomd] got message '%s'\n, accept_buf");
164 static void cleanup(void)
171 int main(int argc
, char **argv
)