aboutsummaryrefslogtreecommitdiff
path: root/src/daemon.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-10 09:07:29 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-10 09:11:50 +0000
commit6a7a6c4d82d26043c3fdf87b1a086ab4513a4062 (patch)
treea0e3aa049e39f38ad32a5e0448d97d36840fb294 /src/daemon.c
downloadlab-03-6a7a6c4d82d26043c3fdf87b1a086ab4513a4062.tar.gz
publishHEADmaster
Diffstat (limited to 'src/daemon.c')
-rw-r--r--src/daemon.c243
1 files changed, 243 insertions, 0 deletions
diff --git a/src/daemon.c b/src/daemon.c
new file mode 100644
index 0000000..e3225d1
--- /dev/null
+++ b/src/daemon.c
@@ -0,0 +1,243 @@
1#include <stdlib.h>
2#include <stdint.h>
3#include <unistd.h>
4#include <stdbool.h>
5#include <fcntl.h>
6#include <sys/stat.h>
7#include <string.h>
8#include <errno.h>
9#include <syslog.h>
10#include <signal.h>
11#include <dirent.h>
12#include <time.h>
13#include "utils.h"
14#include "dynamic-array.h"
15
16struct filename {
17 char *items;
18 size_t count;
19 size_t capacity;
20};
21
22#define PROGRAM_ARGUMENTS \
23 PROGRAM_ARGUMENT_BOOL (help, 'h', "help", "Show this help message" ) \
24 PROGRAM_ARGUMENT_BOOL (daemonize, 'd', "daemonize", "Daemonize process on startup" ) \
25 PROGRAM_ARGUMENT_UINT (log_upto, 'l', "log-upto", "Log up to this Syslog priority", LOG_NOTICE ) \
26 PROGRAM_ARGUMENT_STRING (config, 'c', "config", "Read config from specified filename", "/etc/daemon")
27#include "program-arguments.h"
28
29#define CONFIG \
30 CONFIG_FILENAME (directory, "directory", NULL) \
31 CONFIG_UINT (interval, "interval", 10)
32#include "config.h"
33
34bool
35check_config (struct config *config)
36{
37 if (config->directory.count == 0) {
38 ERROR ("Directory not specified in config");
39 return false;
40 }
41 return true;
42}
43
44volatile bool should_stop = false;
45volatile bool should_reload_config = false;
46
47void
48signal_handler (int sig)
49{
50 switch (sig) {
51 case SIGHUP:
52 DEBUG ("Received SIGHUP");
53 should_reload_config = true;
54 break;
55
56 case SIGINT:
57 DEBUG ("Received SIGINT");
58 should_stop = true;
59 break;
60
61 case SIGTERM:
62 DEBUG ("Received SIGTERM");
63 should_stop = true;
64 break;
65
66 default: DEBUG ("Received unhandled %s", strsignal (sig));
67 }
68}
69
70struct {
71 struct known_file {
72 char *name;
73 uint8_t type;
74 } *items;
75 uint32_t count;
76 uint32_t capacity;
77} known_files = {0};
78
79void
80check_directory (struct filename *filename, time_t time)
81{
82 INFO ("Checking directory %s", filename->items);
83
84 DIR *stream = opendir (filename->items);
85 if (stream == NULL) {
86 WARNING ("Unable to open directory %s: %s",
87 filename->items, strerror (errno));
88 return;
89 }
90
91 size_t filename_len = filename->count - 1;
92 struct dirent *entry;
93 while ((entry = readdir (stream)) != NULL) {
94 if (entry->d_name[0] == '.') {
95 if (entry->d_name[1] == '\0') continue;
96 if (entry->d_name[1] == '.') {
97 if (entry->d_name[2] == '\0') continue;
98 }
99 }
100
101 const char *basename = entry->d_name;
102 size_t basename_len = strlen (basename);
103 filename->count = filename_len;
104 DYNAMIC_ARRAY_APPEND (*filename, '/');
105 DYNAMIC_ARRAY_APPEND_MANY (*filename, basename, basename_len);
106 DYNAMIC_ARRAY_APPEND (*filename, '\0');
107
108 struct stat st;
109 if (lstat (filename->items, &st) == -1) {
110 WARNING ("Cannot stat %s: %s", filename->items, strerror (errno));
111 continue;
112 }
113
114 bool new = true;
115 DYNAMIC_ARRAY_FOREACH (known_file, known_files) {
116 if (entry->d_type == known_file->type &&
117 strcmp (filename->items, known_file->name) == 0) {
118 new = false;
119 break;
120 }
121 }
122 if (new) {
123 struct known_file new_file = {
124 .name = malloc (filename->count),
125 .type = entry->d_type,
126 };
127 memcpy (new_file.name, filename->items, filename->count);
128 DYNAMIC_ARRAY_APPEND (known_files, new_file);
129 NOTICE ("%s was created `%s' at %s",
130 entry->d_type == DT_DIR ? "Directory" : "File",
131 filename->items, ctime (&st.st_mtime));
132 } else if (st.st_mtime >= time) {
133 NOTICE ("%s `%s' was modified at %s",
134 S_ISDIR (st.st_mode) ? "Directory" : "File",
135 filename->items, ctime (&st.st_mtime));
136 } else {
137 DEBUG ("Found known %s `%s', age = %lu",
138 entry->d_type == DT_DIR ? "directory" : "file",
139 filename->items, time - st.st_mtime);
140 }
141
142 if (S_ISDIR (st.st_mode))
143 check_directory (filename, time);
144 } closedir (stream);
145
146 filename->items[filename_len] = '\0';
147 filename->count = filename_len + 1;
148}
149
150int
151main (int argc, char *argv[])
152{
153 struct program_arguments pa = parse_program_arguments (argc, argv);
154 if (pa.help) {
155 print_help (pa.program_name);
156 fprintf (stderr, "\n");
157 fprintf (stderr, "Example configuration:\n");
158 fprintf (stderr, " directory = /path/to/directory\n");
159 fprintf (stderr, " interval = 5\n");
160 exit (EXIT_SUCCESS);
161 }
162
163 setlogmask (LOG_UPTO (pa.log_upto));
164 openlog (basename (pa.program_name),
165 LOG_PID | (pa.daemonize ? 0 : LOG_PERROR), LOG_DAEMON);
166
167 if (pa.daemonize) {
168 close (STDIN_FILENO);
169 close (STDOUT_FILENO);
170 close (STDERR_FILENO);
171 DEBUG ("Closed standard file descriptors");
172 }
173
174 struct sigaction sa = {
175 .sa_handler = signal_handler,
176 .sa_flags = SA_RESTART
177 };
178 sigaction (SIGHUP, &sa, NULL);
179 sigaction (SIGINT, &sa, NULL);
180 sigaction (SIGTERM, &sa, NULL);
181 DEBUG ("Registered signal handlers");
182
183 if (pa.daemonize) {
184 pid_t pid = fork ();
185 if (pid == -1) {
186 EMERGENCY ("Unable to fork: %s", strerror (errno));
187 exit (EXIT_FAILURE);
188 }
189
190 if (pid > 0)
191 exit (EXIT_SUCCESS);
192 DEBUG ("Forked");
193
194 pid_t sid = setsid ();
195 if (sid == -1) {
196 EMERGENCY ("Unable to set SID: %s", strerror (errno));
197 exit (EXIT_FAILURE);
198 } DEBUG ("Set sid to %d", sid);
199 }
200
201 umask (0);
202
203 if (chdir ("/") == -1) {
204 EMERGENCY ("Unable to chdir to /: %s", strerror (errno));
205 exit (EXIT_FAILURE);
206 } DEBUG ("Changed directory to /");
207
208 struct config config;
209 if (!read_config (pa.config, &config)) {
210 EMERGENCY ("Failed to read config file: %s", pa.config);
211 exit (EXIT_FAILURE);
212 } INFO ("Read config: %s", pa.config);
213
214 NOTICE ("Daemon started");
215 time_t check_time__last = time (NULL);
216 while (!should_stop) {
217 if (should_reload_config) {
218 should_reload_config = false;
219 if (!read_config (pa.config, &config)) {
220 EMERGENCY ("Failed to reload config: %s", pa.config);
221 exit (EXIT_FAILURE);
222 } INFO ("Reloaded config: %s", pa.config);
223 }
224
225 time_t check_time__new = time (NULL);
226 check_directory (&config.directory, check_time__last);
227 DYNAMIC_ARRAY_FOREACH (known_file, known_files) {
228 struct stat st;
229 if (lstat (known_file->name, &st) == -1) {
230 NOTICE ("%s `%s' was deleted at %s",
231 known_file->type == DT_DIR ? "Directory" : "File",
232 known_file->name, ctime (&st.st_mtime));
233 free (known_file->name);
234 *known_file-- = DYNAMIC_ARRAY_POP (known_files);
235 }
236 }
237 check_time__last = check_time__new;
238
239 sleep (config.interval);
240 } NOTICE ("Daemon stopped");
241
242 return EXIT_SUCCESS;
243}