diff options
| author | 2026-11-10 09:07:29 +0000 | |
|---|---|---|
| committer | 2026-11-10 09:11:50 +0000 | |
| commit | 6a7a6c4d82d26043c3fdf87b1a086ab4513a4062 (patch) | |
| tree | a0e3aa049e39f38ad32a5e0448d97d36840fb294 | |
| download | lab-03-6a7a6c4d82d26043c3fdf87b1a086ab4513a4062.tar.gz | |
| -rw-r--r-- | .gitignore | 6 | ||||
| -rw-r--r-- | README.org | 21 | ||||
| -rw-r--r-- | UNLICENSE | 24 | ||||
| -rwxr-xr-x | build.sh | 6 | ||||
| -rw-r--r-- | doc/notes.org | 8 | ||||
| -rw-r--r-- | res/config | 2 | ||||
| -rw-r--r-- | src/config.h | 174 | ||||
| -rw-r--r-- | src/daemon.c | 243 | ||||
| -rw-r--r-- | src/dynamic-array.h | 62 | ||||
| -rw-r--r-- | src/program-arguments.h | 89 | ||||
| -rw-r--r-- | src/utils.h | 32 |
11 files changed, 667 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b6b621 --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | *.html | ||
| 2 | |||
| 3 | *.tex | ||
| 4 | *.dot | ||
| 5 | *.png | ||
| 6 | /daemon | ||
diff --git a/README.org b/README.org new file mode 100644 index 0000000..26bf240 --- /dev/null +++ b/README.org | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #+title: Linux daemon | ||
| 2 | #+subtitle: Directory watcher | ||
| 3 | #+date: 2025H2 | ||
| 4 | |||
| 5 | * Task | ||
| 6 | :PROPERTIES: | ||
| 7 | :CUSTOM_ID: task | ||
| 8 | :END: | ||
| 9 | Write a Linux (actually POSIX) daemon that reads configuration file, | ||
| 10 | containing: directory, check time. After that, check the specified | ||
| 11 | directory (recursively), at specified time intervals, for the | ||
| 12 | presence/absence of file modifications and record the results in a log | ||
| 13 | file, providing signal handling: =SIGHUP= -- for re-reading the | ||
| 14 | configuration file, and =SIGTERM= -- for controlled termination of the | ||
| 15 | daemon (recording output to the log file). | ||
| 16 | |||
| 17 | * Unlicense | ||
| 18 | :PROPERTIES: | ||
| 19 | :CUSTOM_ID: unlicense | ||
| 20 | :END: | ||
| 21 | #+include: UNLICENSE verse | ||
diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..efb9808 --- /dev/null +++ b/UNLICENSE | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | This is free and unencumbered software released into the public domain. | ||
| 2 | |||
| 3 | Anyone is free to copy, modify, publish, use, compile, sell, or | ||
| 4 | distribute this software, either in source code form or as a compiled | ||
| 5 | binary, for any purpose, commercial or non-commercial, and by any | ||
| 6 | means. | ||
| 7 | |||
| 8 | In jurisdictions that recognize copyright laws, the author or authors | ||
| 9 | of this software dedicate any and all copyright interest in the | ||
| 10 | software to the public domain. We make this dedication for the benefit | ||
| 11 | of the public at large and to the detriment of our heirs and | ||
| 12 | successors. We intend this dedication to be an overt act of | ||
| 13 | relinquishment in perpetuity of all present and future rights to this | ||
| 14 | software under copyright law. | ||
| 15 | |||
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
| 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
| 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
| 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
| 22 | OTHER DEALINGS IN THE SOFTWARE. | ||
| 23 | |||
| 24 | For more information, please refer to <https://unlicense.org/> | ||
diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..194f38a --- /dev/null +++ b/build.sh | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | set -xe | ||
| 4 | |||
| 5 | gcc -Wall -Wextra -Wno-nonnull -ggdb3 -std=gnu99 -D_GNU_SOURCE -O3 -march=native \ | ||
| 6 | -I./src -o daemon src/daemon.c | ||
diff --git a/doc/notes.org b/doc/notes.org new file mode 100644 index 0000000..33db99d --- /dev/null +++ b/doc/notes.org | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | * Задание | ||
| 2 | Нужно написать демон под линукс который бы читал конфигурационный | ||
| 3 | файл, брал оттуда параметры: каталог, время проверки. После чего | ||
| 4 | проверял заданный каталог(рекурсивно), через заданные промежутки | ||
| 5 | времени, на предмет наличия/отсутствия модификации файлов и записывал | ||
| 6 | результаты в файл журнала, предусмотреть обработку сигналов: SIGHUP - | ||
| 7 | для перечитывания конфигурационного файла, и SIGTERM - для | ||
| 8 | контролируемого завершения демона (запись о выходе в файл журнала). | ||
diff --git a/res/config b/res/config new file mode 100644 index 0000000..9db4b56 --- /dev/null +++ b/res/config | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | directory = /home/cmmm/notfound | ||
| 2 | interval = 5 | ||
diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..381ff30 --- /dev/null +++ b/src/config.h | |||
| @@ -0,0 +1,174 @@ | |||
| 1 | #ifndef INCLUDED_CONFIG_H | ||
| 2 | #define INCLUDED_CONFIG_H | ||
| 3 | |||
| 4 | #define CONFIG_FILENAME(sym, ...) struct filename sym; | ||
| 5 | #define CONFIG_UINT( sym, ...) unsigned int sym; | ||
| 6 | struct config { | ||
| 7 | CONFIG | ||
| 8 | }; | ||
| 9 | #undef CONFIG_FILENAME | ||
| 10 | #undef CONFIG_UINT | ||
| 11 | |||
| 12 | static bool | ||
| 13 | parse_filename (const char **head, const char *end, const char *key, | ||
| 14 | size_t key_len, const char *name, struct filename *filename) | ||
| 15 | { | ||
| 16 | if (strncmp (key, name, key_len)) | ||
| 17 | return false; | ||
| 18 | |||
| 19 | while (strchr (" \t", **head)) { | ||
| 20 | *head += 1; | ||
| 21 | if (*head == end) { | ||
| 22 | WARNING ("Unexpected EOF while looking for '='"); | ||
| 23 | return false; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | if (**head != '=') { | ||
| 28 | WARNING ("Expected '='"); | ||
| 29 | return false; | ||
| 30 | } *head += 1; | ||
| 31 | if (*head == end) { | ||
| 32 | WARNING ("Unexpected EOF after '='"); | ||
| 33 | return false; | ||
| 34 | } | ||
| 35 | |||
| 36 | while (strchr (" \t", **head)) { | ||
| 37 | *head += 1; | ||
| 38 | if (*head == end) { | ||
| 39 | WARNING ("Unexpected EOF while looking for value"); | ||
| 40 | return false; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | const char *value = *head; | ||
| 45 | while (!strchr (" \t\n\0", **head)) { | ||
| 46 | *head += 1; | ||
| 47 | if (*head == end) { | ||
| 48 | WARNING ("unexpected EOF while parsing value"); | ||
| 49 | return false; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | size_t value_len = *head - value; | ||
| 54 | filename->count = 0; | ||
| 55 | DYNAMIC_ARRAY_APPEND_MANY (*filename, value, value_len); | ||
| 56 | DYNAMIC_ARRAY_APPEND (*filename, '\0'); | ||
| 57 | |||
| 58 | DEBUG ("Parsed filename `%.*s' from config: %s", (int) key_len, key, filename->items); | ||
| 59 | return true; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | static bool | ||
| 64 | parse_uint (const char **head, const char *end, const char *key, | ||
| 65 | size_t key_len, const char *name, unsigned int *uint) | ||
| 66 | { | ||
| 67 | if (strncmp (key, name, key_len)) | ||
| 68 | return false; | ||
| 69 | |||
| 70 | while (strchr (" \t", **head)) { | ||
| 71 | *head += 1; | ||
| 72 | if (*head == end) { | ||
| 73 | WARNING ("unexpected EOF while looking for '='"); | ||
| 74 | return false; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | if (**head != '=') { | ||
| 79 | WARNING ("expected '='"); | ||
| 80 | } *head += 1; | ||
| 81 | if (*head == end) { | ||
| 82 | WARNING ("unexpected EOF while looking for value"); | ||
| 83 | return false; | ||
| 84 | } | ||
| 85 | |||
| 86 | while (strchr (" \t", **head)) { | ||
| 87 | *head += 1; | ||
| 88 | if (*head == end) { | ||
| 89 | WARNING ("unexpected EOF while looking for value"); | ||
| 90 | return false; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | int value_len = 0; | ||
| 95 | if (sscanf (*head, "%u%n", uint, &value_len) == 0) { | ||
| 96 | LOG (ERR, "unsigned int"); | ||
| 97 | return false; | ||
| 98 | } *head += value_len; | ||
| 99 | |||
| 100 | DEBUG ("Parsed uint `%.*s' from config: %u", (int) key_len, key, *uint); | ||
| 101 | return true; | ||
| 102 | } | ||
| 103 | |||
| 104 | bool check_config (struct config *); | ||
| 105 | |||
| 106 | bool | ||
| 107 | read_config (const char *filename, struct config *config) | ||
| 108 | { | ||
| 109 | bool success = false; | ||
| 110 | char *contents = NULL; | ||
| 111 | int fd = -1; | ||
| 112 | #define CONFIG_FILENAME(SYM, KEY, INITIAL) { \ | ||
| 113 | DYNAMIC_ARRAY_RESET (config->SYM); \ | ||
| 114 | if (INITIAL) \ | ||
| 115 | DYNAMIC_ARRAY_APPEND_MANY (config->SYM, INITIAL, sizeof (INITIAL)); \ | ||
| 116 | else DYNAMIC_ARRAY_APPEND (config->SYM, '\0'); \ | ||
| 117 | } | ||
| 118 | #define CONFIG_UINT(SYM, KEY, INITIAL) config->SYM = INITIAL; | ||
| 119 | CONFIG | ||
| 120 | #undef CONFIG_FILENAME | ||
| 121 | #undef CONFIG_UINT | ||
| 122 | |||
| 123 | fd = open (filename, O_RDONLY); | ||
| 124 | if (fd == -1) { | ||
| 125 | ERROR ("Unable to open config file `%s' for reading: %s", filename, strerror (errno)); | ||
| 126 | goto defer; | ||
| 127 | } DEBUG ("Opened config file `%s' for reading", filename); | ||
| 128 | |||
| 129 | struct stat st; | ||
| 130 | if (fstat (fd, &st) == -1) { | ||
| 131 | ERROR ("Unable to fstat config file `%s': %s", filename, strerror (errno)); | ||
| 132 | goto defer; | ||
| 133 | } DEBUG ("Config file size: %lu", st.st_size); | ||
| 134 | |||
| 135 | contents = malloc (st.st_size + 1); | ||
| 136 | contents[st.st_size] = '\0'; | ||
| 137 | if (read (fd, contents, st.st_size) == -1) { | ||
| 138 | ERROR ("Unable to read config from `%s': %s", filename, strerror (errno)); | ||
| 139 | goto defer; | ||
| 140 | } close (fd); fd = -1; | ||
| 141 | DEBUG ("Read config file into buffer"); | ||
| 142 | |||
| 143 | const char *end = contents + st.st_size; | ||
| 144 | for (const char *head = contents; head < end; ++head) { | ||
| 145 | if (strchr (" \t\n", *head)) continue; | ||
| 146 | const char *key = head; | ||
| 147 | while (!strchr ("= \t", *head)) { | ||
| 148 | head += 1; | ||
| 149 | if (head == end) { | ||
| 150 | ERROR ("Unexpected EOF while reading key from config"); | ||
| 151 | goto defer; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | size_t key_len = head - key; | ||
| 155 | #define PARSE(parse, sym, name) \ | ||
| 156 | if (parse (&head, end, key, key_len, name, &config->sym)) continue; | ||
| 157 | #define CONFIG_FILENAME(sym, name, initial) PARSE (parse_filename, sym, name) | ||
| 158 | #define CONFIG_UINT( sym, name, initial) PARSE (parse_uint, sym, name) | ||
| 159 | CONFIG; | ||
| 160 | #undef PARSE | ||
| 161 | #undef CONFIG_FILENAME | ||
| 162 | #undef CONFIG_UINT | ||
| 163 | |||
| 164 | ERROR ("Unknown config key: %.*s", (int) key_len, key); | ||
| 165 | goto defer; | ||
| 166 | } success = true; | ||
| 167 | |||
| 168 | defer: | ||
| 169 | if (fd == -1) close (fd); | ||
| 170 | if (contents) free (contents); | ||
| 171 | return success && check_config (config); | ||
| 172 | } | ||
| 173 | |||
| 174 | #endif // INCLUDED_CONFIG_H | ||
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 | |||
| 16 | struct 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 | |||
| 34 | bool | ||
| 35 | check_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 | |||
| 44 | volatile bool should_stop = false; | ||
| 45 | volatile bool should_reload_config = false; | ||
| 46 | |||
| 47 | void | ||
| 48 | signal_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 | |||
| 70 | struct { | ||
| 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 | |||
| 79 | void | ||
| 80 | check_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 | |||
| 150 | int | ||
| 151 | main (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 | } | ||
diff --git a/src/dynamic-array.h b/src/dynamic-array.h new file mode 100644 index 0000000..76bdde9 --- /dev/null +++ b/src/dynamic-array.h | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | #ifndef INCLUDED_DYNAMIC_ARRAY_H | ||
| 2 | #define INCLUDED_DYNAMIC_ARRAY_H | ||
| 3 | |||
| 4 | #include "utils.h" | ||
| 5 | |||
| 6 | #ifndef DYNAMIC_ARRAY_INIT_CAP | ||
| 7 | #define DYNAMIC_ARRAY_INIT_CAP 256 | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ | ||
| 11 | ({ \ | ||
| 12 | if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ | ||
| 13 | if ((DYNAMIC_ARRAY).capacity == 0) \ | ||
| 14 | (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ | ||
| 15 | while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ | ||
| 16 | (DYNAMIC_ARRAY).capacity *= 2; \ | ||
| 17 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 18 | (DYNAMIC_ARRAY).capacity * \ | ||
| 19 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 20 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 21 | } \ | ||
| 22 | }) | ||
| 23 | |||
| 24 | #define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ | ||
| 25 | ({ \ | ||
| 26 | (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ | ||
| 27 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 28 | (DYNAMIC_ARRAY).capacity * \ | ||
| 29 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 30 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 31 | \ | ||
| 32 | }) | ||
| 33 | |||
| 34 | #define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ | ||
| 35 | ({ \ | ||
| 36 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ | ||
| 37 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ | ||
| 38 | }) | ||
| 39 | |||
| 40 | #define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ | ||
| 41 | ({ \ | ||
| 42 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ | ||
| 43 | memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ | ||
| 44 | (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 45 | (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ | ||
| 46 | }) | ||
| 47 | |||
| 48 | #define DYNAMIC_ARRAY_LAST(DYNAMIC_ARRAY) \ | ||
| 49 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count - 1] | ||
| 50 | |||
| 51 | #define DYNAMIC_ARRAY_POP(DYNAMIC_ARRAY) \ | ||
| 52 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count--] | ||
| 53 | |||
| 54 | #define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) | ||
| 55 | |||
| 56 | #define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) | ||
| 57 | |||
| 58 | #define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ | ||
| 59 | typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ | ||
| 60 | ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) | ||
| 61 | |||
| 62 | #endif | ||
diff --git a/src/program-arguments.h b/src/program-arguments.h new file mode 100644 index 0000000..dc87f4b --- /dev/null +++ b/src/program-arguments.h | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | #ifndef INCLUDED_PROGRAM_ARGUMENTS_H | ||
| 2 | #define INCLUDED_PROGRAM_ARGUMENTS_H | ||
| 3 | |||
| 4 | void | ||
| 5 | print_help (const char *program_name) | ||
| 6 | { | ||
| 7 | fprintf (stderr, "Usage: %s [OPTION...]\n", program_name); | ||
| 8 | fprintf (stderr, "\n"); | ||
| 9 | fprintf (stderr, "Options:\n"); | ||
| 10 | #define PROGRAM_ARGUMENT_BOOL( sym, s, l, h, ...) fprintf (stderr, " -%c, --%s %*s\n", s, l, (int) (10 + sizeof (h) - sizeof (l)), h); | ||
| 11 | #define PROGRAM_ARGUMENT_UINT( sym, s, l, h, d, ...) fprintf (stderr, " -%c, --%s <unsigned> %*s (default: %u)\n", s, l, (int) (10 + sizeof (h) - sizeof (l)), h, d); | ||
| 12 | #define PROGRAM_ARGUMENT_STRING(sym, s, l, h, d, ...) fprintf (stderr, " -%c, --%s <string> %*s (default: %s)\n", s, l, (int) (10 + sizeof (h) - sizeof (l)), h, d); | ||
| 13 | PROGRAM_ARGUMENTS | ||
| 14 | #undef PROGRAM_ARGUMENT_BOOL | ||
| 15 | #undef PROGRAM_ARGUMENT_UINT | ||
| 16 | #undef PROGRAM_ARGUMENT_STRING | ||
| 17 | } | ||
| 18 | |||
| 19 | #define PROGRAM_ARGUMENT_BOOL( sym, ...) bool sym; | ||
| 20 | #define PROGRAM_ARGUMENT_UINT( sym, ...) unsigned int sym; | ||
| 21 | #define PROGRAM_ARGUMENT_STRING(sym, ...) const char *sym; | ||
| 22 | struct program_arguments { | ||
| 23 | const char *program_name; | ||
| 24 | PROGRAM_ARGUMENTS | ||
| 25 | }; | ||
| 26 | #undef PROGRAM_ARGUMENT_BOOL | ||
| 27 | #undef PROGRAM_ARGUMENT_UINT | ||
| 28 | #undef PROGRAM_ARGUMENT_STRING | ||
| 29 | |||
| 30 | struct program_arguments | ||
| 31 | parse_program_arguments (int argc, char *argv[]) | ||
| 32 | { | ||
| 33 | UNUSED (argc); | ||
| 34 | #define PROGRAM_ARGUMENT_BOOL(...) | ||
| 35 | #define PROGRAM_ARGUMENT_UINT( sym, s, l, h, d, ...) .sym = d, | ||
| 36 | #define PROGRAM_ARGUMENT_STRING(sym, s, l, h, d, ...) .sym = d, | ||
| 37 | struct program_arguments pa = { | ||
| 38 | .program_name = *argv++, | ||
| 39 | PROGRAM_ARGUMENTS | ||
| 40 | }; | ||
| 41 | #undef PROGRAM_ARGUMENT_BOOL | ||
| 42 | #undef PROGRAM_ARGUMENT_UINT | ||
| 43 | #undef PROGRAM_ARGUMENT_STRING | ||
| 44 | |||
| 45 | #define PROGRAM_ARGUMENT_BOOL(sym, s, l, ...) MATCH (s, l) { \ | ||
| 46 | pa.sym = true; \ | ||
| 47 | continue; \ | ||
| 48 | } | ||
| 49 | #define PROGRAM_ARGUMENT_UINT(sym, s, l, ...) MATCH (s, l) { \ | ||
| 50 | arg += 1; \ | ||
| 51 | if (*arg == NULL) { \ | ||
| 52 | fprintf (stderr, "error"); \ | ||
| 53 | exit (EXIT_FAILURE); \ | ||
| 54 | } \ | ||
| 55 | if (sscanf (*arg, "%u", &pa.sym) == 0) { \ | ||
| 56 | fprintf (stderr, "error: wrong format for %s\n", arg[-1]); \ | ||
| 57 | exit (EXIT_FAILURE); \ | ||
| 58 | } \ | ||
| 59 | continue; \ | ||
| 60 | } | ||
| 61 | #define PROGRAM_ARGUMENT_STRING(sym, s, l, ...) MATCH (s, l) { \ | ||
| 62 | arg += 1; \ | ||
| 63 | pa.sym = *arg; \ | ||
| 64 | continue; \ | ||
| 65 | } | ||
| 66 | for (char **arg = argv; *arg; ++arg) { | ||
| 67 | if ((*arg)[0] == '-') { | ||
| 68 | if ((*arg)[1] == '-') { | ||
| 69 | #define MATCH(s, l) if (!strcmp (*arg + 2, l)) | ||
| 70 | PROGRAM_ARGUMENTS; | ||
| 71 | fprintf (stderr, "error: unknown option %s\n", *arg); | ||
| 72 | exit (EXIT_FAILURE); | ||
| 73 | #undef MATCH | ||
| 74 | } | ||
| 75 | #define MATCH(s, l) if ((*arg)[1] == s && (*arg)[2] == '\0') | ||
| 76 | PROGRAM_ARGUMENTS; | ||
| 77 | fprintf (stderr, "error: unknown option %s\n", *arg); | ||
| 78 | exit (EXIT_FAILURE); | ||
| 79 | #undef MATCH | ||
| 80 | } | ||
| 81 | } | ||
| 82 | #undef PROGRAM_ARGUMENT_BOOL | ||
| 83 | #undef PROGRAM_ARGUMENT_UINT | ||
| 84 | #undef PROGRAM_ARGUMENT_STRING | ||
| 85 | |||
| 86 | return pa; | ||
| 87 | } | ||
| 88 | |||
| 89 | #endif // INCLUDED_PROGRAM_ARGUMENTS_H | ||
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..e5398e9 --- /dev/null +++ b/src/utils.h | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | #ifndef INCLUDED_UTILS_H | ||
| 2 | #define INCLUDED_UTILS_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdarg.h> | ||
| 6 | #include <assert.h> | ||
| 7 | #include <stdio.h> | ||
| 8 | |||
| 9 | #define STRINGIFY(ARG) #ARG | ||
| 10 | #define TO_STRING(ARG) STRINGIFY (ARG) | ||
| 11 | #define LOG(PRIORITY, FMT, ...) \ | ||
| 12 | syslog (LOG_MAKEPRI (LOG_DAEMON, LOG_##PRIORITY), \ | ||
| 13 | FMT __VA_OPT__ (,) __VA_ARGS__) | ||
| 14 | #define DEBUG( FMT, ...) \ | ||
| 15 | LOG (DEBUG, __FILE__ ":" TO_STRING (__LINE__) ": %s: " FMT, __func__ __VA_OPT__ (,) __VA_ARGS__) | ||
| 16 | #define EMERGENCY(FMT, ...) LOG (EMERG, "emergency: " FMT __VA_OPT__ (,) __VA_ARGS__) | ||
| 17 | #define ALERT( FMT, ...) LOG (ALERT, "alert: " FMT __VA_OPT__ (,) __VA_ARGS__) | ||
| 18 | #define CRITICAL( FMT, ...) LOG (CRIT, "critical: " FMT __VA_OPT__ (,) __VA_ARGS__) | ||
| 19 | #define ERROR( FMT, ...) LOG (ERR, "error: " FMT __VA_OPT__ (,) __VA_ARGS__) | ||
| 20 | #define WARNING( FMT, ...) LOG (WARNING, "warning: " FMT __VA_OPT__ (,) __VA_ARGS__) | ||
| 21 | #define NOTICE( FMT, ...) LOG (NOTICE, "notice: " FMT __VA_OPT__ (,) __VA_ARGS__) | ||
| 22 | #define INFO( FMT, ...) LOG (INFO, "info: " FMT __VA_OPT__ (,) __VA_ARGS__) | ||
| 23 | |||
| 24 | #define UNUSED(X) ((void) (X)) | ||
| 25 | #define UNREACHABLE() ASSERT (!"reached the unreachable...") | ||
| 26 | |||
| 27 | #define MALLOC malloc | ||
| 28 | #define REALLOC realloc | ||
| 29 | #define FREE free | ||
| 30 | #define ASSERT assert | ||
| 31 | |||
| 32 | #endif | ||
