#include #include #include #include #include #include #include #include #include #include #include #include #include "utils.h" #include "dynamic-array.h" struct filename { char *items; size_t count; size_t capacity; }; #define PROGRAM_ARGUMENTS \ PROGRAM_ARGUMENT_BOOL (help, 'h', "help", "Show this help message" ) \ PROGRAM_ARGUMENT_BOOL (daemonize, 'd', "daemonize", "Daemonize process on startup" ) \ PROGRAM_ARGUMENT_UINT (log_upto, 'l', "log-upto", "Log up to this Syslog priority", LOG_NOTICE ) \ PROGRAM_ARGUMENT_STRING (config, 'c', "config", "Read config from specified filename", "/etc/daemon") #include "program-arguments.h" #define CONFIG \ CONFIG_FILENAME (directory, "directory", NULL) \ CONFIG_UINT (interval, "interval", 10) #include "config.h" bool check_config (struct config *config) { if (config->directory.count == 0) { ERROR ("Directory not specified in config"); return false; } return true; } volatile bool should_stop = false; volatile bool should_reload_config = false; void signal_handler (int sig) { switch (sig) { case SIGHUP: DEBUG ("Received SIGHUP"); should_reload_config = true; break; case SIGINT: DEBUG ("Received SIGINT"); should_stop = true; break; case SIGTERM: DEBUG ("Received SIGTERM"); should_stop = true; break; default: DEBUG ("Received unhandled %s", strsignal (sig)); } } struct { struct known_file { char *name; uint8_t type; } *items; uint32_t count; uint32_t capacity; } known_files = {0}; void check_directory (struct filename *filename, time_t time) { INFO ("Checking directory %s", filename->items); DIR *stream = opendir (filename->items); if (stream == NULL) { WARNING ("Unable to open directory %s: %s", filename->items, strerror (errno)); return; } size_t filename_len = filename->count - 1; struct dirent *entry; while ((entry = readdir (stream)) != NULL) { if (entry->d_name[0] == '.') { if (entry->d_name[1] == '\0') continue; if (entry->d_name[1] == '.') { if (entry->d_name[2] == '\0') continue; } } const char *basename = entry->d_name; size_t basename_len = strlen (basename); filename->count = filename_len; DYNAMIC_ARRAY_APPEND (*filename, '/'); DYNAMIC_ARRAY_APPEND_MANY (*filename, basename, basename_len); DYNAMIC_ARRAY_APPEND (*filename, '\0'); struct stat st; if (lstat (filename->items, &st) == -1) { WARNING ("Cannot stat %s: %s", filename->items, strerror (errno)); continue; } bool new = true; DYNAMIC_ARRAY_FOREACH (known_file, known_files) { if (entry->d_type == known_file->type && strcmp (filename->items, known_file->name) == 0) { new = false; break; } } if (new) { struct known_file new_file = { .name = malloc (filename->count), .type = entry->d_type, }; memcpy (new_file.name, filename->items, filename->count); DYNAMIC_ARRAY_APPEND (known_files, new_file); NOTICE ("%s was created `%s' at %s", entry->d_type == DT_DIR ? "Directory" : "File", filename->items, ctime (&st.st_mtime)); } else if (st.st_mtime >= time) { NOTICE ("%s `%s' was modified at %s", S_ISDIR (st.st_mode) ? "Directory" : "File", filename->items, ctime (&st.st_mtime)); } else { DEBUG ("Found known %s `%s', age = %lu", entry->d_type == DT_DIR ? "directory" : "file", filename->items, time - st.st_mtime); } if (S_ISDIR (st.st_mode)) check_directory (filename, time); } closedir (stream); filename->items[filename_len] = '\0'; filename->count = filename_len + 1; } int main (int argc, char *argv[]) { struct program_arguments pa = parse_program_arguments (argc, argv); if (pa.help) { print_help (pa.program_name); fprintf (stderr, "\n"); fprintf (stderr, "Example configuration:\n"); fprintf (stderr, " directory = /path/to/directory\n"); fprintf (stderr, " interval = 5\n"); exit (EXIT_SUCCESS); } setlogmask (LOG_UPTO (pa.log_upto)); openlog (basename (pa.program_name), LOG_PID | (pa.daemonize ? 0 : LOG_PERROR), LOG_DAEMON); if (pa.daemonize) { close (STDIN_FILENO); close (STDOUT_FILENO); close (STDERR_FILENO); DEBUG ("Closed standard file descriptors"); } struct sigaction sa = { .sa_handler = signal_handler, .sa_flags = SA_RESTART }; sigaction (SIGHUP, &sa, NULL); sigaction (SIGINT, &sa, NULL); sigaction (SIGTERM, &sa, NULL); DEBUG ("Registered signal handlers"); if (pa.daemonize) { pid_t pid = fork (); if (pid == -1) { EMERGENCY ("Unable to fork: %s", strerror (errno)); exit (EXIT_FAILURE); } if (pid > 0) exit (EXIT_SUCCESS); DEBUG ("Forked"); pid_t sid = setsid (); if (sid == -1) { EMERGENCY ("Unable to set SID: %s", strerror (errno)); exit (EXIT_FAILURE); } DEBUG ("Set sid to %d", sid); } umask (0); if (chdir ("/") == -1) { EMERGENCY ("Unable to chdir to /: %s", strerror (errno)); exit (EXIT_FAILURE); } DEBUG ("Changed directory to /"); struct config config; if (!read_config (pa.config, &config)) { EMERGENCY ("Failed to read config file: %s", pa.config); exit (EXIT_FAILURE); } INFO ("Read config: %s", pa.config); NOTICE ("Daemon started"); time_t check_time__last = time (NULL); while (!should_stop) { if (should_reload_config) { should_reload_config = false; if (!read_config (pa.config, &config)) { EMERGENCY ("Failed to reload config: %s", pa.config); exit (EXIT_FAILURE); } INFO ("Reloaded config: %s", pa.config); } time_t check_time__new = time (NULL); check_directory (&config.directory, check_time__last); DYNAMIC_ARRAY_FOREACH (known_file, known_files) { struct stat st; if (lstat (known_file->name, &st) == -1) { NOTICE ("%s `%s' was deleted at %s", known_file->type == DT_DIR ? "Directory" : "File", known_file->name, ctime (&st.st_mtime)); free (known_file->name); *known_file-- = DYNAMIC_ARRAY_POP (known_files); } } check_time__last = check_time__new; sleep (config.interval); } NOTICE ("Daemon stopped"); return EXIT_SUCCESS; }