From 6a7a6c4d82d26043c3fdf87b1a086ab4513a4062 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Tue, 10 Nov 2026 12:07:29 +0300 Subject: publish --- .gitignore | 6 ++ README.org | 21 +++++ UNLICENSE | 24 +++++ build.sh | 6 ++ doc/notes.org | 8 ++ res/config | 2 + src/config.h | 174 ++++++++++++++++++++++++++++++++++ src/daemon.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++++ src/dynamic-array.h | 62 ++++++++++++ src/program-arguments.h | 89 ++++++++++++++++++ src/utils.h | 32 +++++++ 11 files changed, 667 insertions(+) create mode 100644 .gitignore create mode 100644 README.org create mode 100644 UNLICENSE create mode 100755 build.sh create mode 100644 doc/notes.org create mode 100644 res/config create mode 100644 src/config.h create mode 100644 src/daemon.c create mode 100644 src/dynamic-array.h create mode 100644 src/program-arguments.h create mode 100644 src/utils.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b6b621 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.html +*.pdf +*.tex +*.dot +*.png +/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 @@ +#+title: Linux daemon +#+subtitle: Directory watcher +#+date: 2025H2 + +* Task +:PROPERTIES: +:CUSTOM_ID: task +:END: +Write a Linux (actually POSIX) daemon that reads configuration file, +containing: directory, check time. After that, check the specified +directory (recursively), at specified time intervals, for the +presence/absence of file modifications and record the results in a log +file, providing signal handling: =SIGHUP= -- for re-reading the +configuration file, and =SIGTERM= -- for controlled termination of the +daemon (recording output to the log file). + +* Unlicense +:PROPERTIES: +:CUSTOM_ID: unlicense +:END: +#+include: UNLICENSE verse diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..efb9808 --- /dev/null +++ b/UNLICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..194f38a --- /dev/null +++ b/build.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +set -xe + +gcc -Wall -Wextra -Wno-nonnull -ggdb3 -std=gnu99 -D_GNU_SOURCE -O3 -march=native \ + -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 @@ +* Задание +Нужно написать демон под линукс который бы читал конфигурационный +файл, брал оттуда параметры: каталог, время проверки. После чего +проверял заданный каталог(рекурсивно), через заданные промежутки +времени, на предмет наличия/отсутствия модификации файлов и записывал +результаты в файл журнала, предусмотреть обработку сигналов: SIGHUP - +для перечитывания конфигурационного файла, и SIGTERM - для +контролируемого завершения демона (запись о выходе в файл журнала). diff --git a/res/config b/res/config new file mode 100644 index 0000000..9db4b56 --- /dev/null +++ b/res/config @@ -0,0 +1,2 @@ +directory = /home/cmmm/notfound +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 @@ +#ifndef INCLUDED_CONFIG_H +#define INCLUDED_CONFIG_H + +#define CONFIG_FILENAME(sym, ...) struct filename sym; +#define CONFIG_UINT( sym, ...) unsigned int sym; +struct config { + CONFIG +}; +#undef CONFIG_FILENAME +#undef CONFIG_UINT + +static bool +parse_filename (const char **head, const char *end, const char *key, + size_t key_len, const char *name, struct filename *filename) +{ + if (strncmp (key, name, key_len)) + return false; + + while (strchr (" \t", **head)) { + *head += 1; + if (*head == end) { + WARNING ("Unexpected EOF while looking for '='"); + return false; + } + } + + if (**head != '=') { + WARNING ("Expected '='"); + return false; + } *head += 1; + if (*head == end) { + WARNING ("Unexpected EOF after '='"); + return false; + } + + while (strchr (" \t", **head)) { + *head += 1; + if (*head == end) { + WARNING ("Unexpected EOF while looking for value"); + return false; + } + } + + const char *value = *head; + while (!strchr (" \t\n\0", **head)) { + *head += 1; + if (*head == end) { + WARNING ("unexpected EOF while parsing value"); + return false; + } + } + + size_t value_len = *head - value; + filename->count = 0; + DYNAMIC_ARRAY_APPEND_MANY (*filename, value, value_len); + DYNAMIC_ARRAY_APPEND (*filename, '\0'); + + DEBUG ("Parsed filename `%.*s' from config: %s", (int) key_len, key, filename->items); + return true; +} + + +static bool +parse_uint (const char **head, const char *end, const char *key, + size_t key_len, const char *name, unsigned int *uint) +{ + if (strncmp (key, name, key_len)) + return false; + + while (strchr (" \t", **head)) { + *head += 1; + if (*head == end) { + WARNING ("unexpected EOF while looking for '='"); + return false; + } + } + + if (**head != '=') { + WARNING ("expected '='"); + } *head += 1; + if (*head == end) { + WARNING ("unexpected EOF while looking for value"); + return false; + } + + while (strchr (" \t", **head)) { + *head += 1; + if (*head == end) { + WARNING ("unexpected EOF while looking for value"); + return false; + } + } + + int value_len = 0; + if (sscanf (*head, "%u%n", uint, &value_len) == 0) { + LOG (ERR, "unsigned int"); + return false; + } *head += value_len; + + DEBUG ("Parsed uint `%.*s' from config: %u", (int) key_len, key, *uint); + return true; +} + +bool check_config (struct config *); + +bool +read_config (const char *filename, struct config *config) +{ + bool success = false; + char *contents = NULL; + int fd = -1; +#define CONFIG_FILENAME(SYM, KEY, INITIAL) { \ + DYNAMIC_ARRAY_RESET (config->SYM); \ + if (INITIAL) \ + DYNAMIC_ARRAY_APPEND_MANY (config->SYM, INITIAL, sizeof (INITIAL)); \ + else DYNAMIC_ARRAY_APPEND (config->SYM, '\0'); \ + } +#define CONFIG_UINT(SYM, KEY, INITIAL) config->SYM = INITIAL; + CONFIG +#undef CONFIG_FILENAME +#undef CONFIG_UINT + + fd = open (filename, O_RDONLY); + if (fd == -1) { + ERROR ("Unable to open config file `%s' for reading: %s", filename, strerror (errno)); + goto defer; + } DEBUG ("Opened config file `%s' for reading", filename); + + struct stat st; + if (fstat (fd, &st) == -1) { + ERROR ("Unable to fstat config file `%s': %s", filename, strerror (errno)); + goto defer; + } DEBUG ("Config file size: %lu", st.st_size); + + contents = malloc (st.st_size + 1); + contents[st.st_size] = '\0'; + if (read (fd, contents, st.st_size) == -1) { + ERROR ("Unable to read config from `%s': %s", filename, strerror (errno)); + goto defer; + } close (fd); fd = -1; + DEBUG ("Read config file into buffer"); + + const char *end = contents + st.st_size; + for (const char *head = contents; head < end; ++head) { + if (strchr (" \t\n", *head)) continue; + const char *key = head; + while (!strchr ("= \t", *head)) { + head += 1; + if (head == end) { + ERROR ("Unexpected EOF while reading key from config"); + goto defer; + } + } + size_t key_len = head - key; +#define PARSE(parse, sym, name) \ + if (parse (&head, end, key, key_len, name, &config->sym)) continue; +#define CONFIG_FILENAME(sym, name, initial) PARSE (parse_filename, sym, name) +#define CONFIG_UINT( sym, name, initial) PARSE (parse_uint, sym, name) + CONFIG; +#undef PARSE +#undef CONFIG_FILENAME +#undef CONFIG_UINT + + ERROR ("Unknown config key: %.*s", (int) key_len, key); + goto defer; + } success = true; + +defer: + if (fd == -1) close (fd); + if (contents) free (contents); + return success && check_config (config); +} + +#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 @@ +#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; +} 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 @@ +#ifndef INCLUDED_DYNAMIC_ARRAY_H +#define INCLUDED_DYNAMIC_ARRAY_H + +#include "utils.h" + +#ifndef DYNAMIC_ARRAY_INIT_CAP +#define DYNAMIC_ARRAY_INIT_CAP 256 +#endif + +#define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ + ({ \ + if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ + if ((DYNAMIC_ARRAY).capacity == 0) \ + (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ + while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ + (DYNAMIC_ARRAY).capacity *= 2; \ + (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ + (DYNAMIC_ARRAY).capacity * \ + sizeof (*(DYNAMIC_ARRAY).items)); \ + ASSERT((DYNAMIC_ARRAY).items); \ + } \ + }) + +#define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ + ({ \ + (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ + (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ + (DYNAMIC_ARRAY).capacity * \ + sizeof (*(DYNAMIC_ARRAY).items)); \ + ASSERT((DYNAMIC_ARRAY).items); \ + \ + }) + +#define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ + ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ + (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ + }) + +#define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ + ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ + memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ + (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ + (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ + }) + +#define DYNAMIC_ARRAY_LAST(DYNAMIC_ARRAY) \ + (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count - 1] + +#define DYNAMIC_ARRAY_POP(DYNAMIC_ARRAY) \ + (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count--] + +#define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) + +#define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) + +#define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ + typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ + ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) + +#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 @@ +#ifndef INCLUDED_PROGRAM_ARGUMENTS_H +#define INCLUDED_PROGRAM_ARGUMENTS_H + +void +print_help (const char *program_name) +{ + fprintf (stderr, "Usage: %s [OPTION...]\n", program_name); + fprintf (stderr, "\n"); + fprintf (stderr, "Options:\n"); +#define PROGRAM_ARGUMENT_BOOL( sym, s, l, h, ...) fprintf (stderr, " -%c, --%s %*s\n", s, l, (int) (10 + sizeof (h) - sizeof (l)), h); +#define PROGRAM_ARGUMENT_UINT( sym, s, l, h, d, ...) fprintf (stderr, " -%c, --%s %*s (default: %u)\n", s, l, (int) (10 + sizeof (h) - sizeof (l)), h, d); +#define PROGRAM_ARGUMENT_STRING(sym, s, l, h, d, ...) fprintf (stderr, " -%c, --%s %*s (default: %s)\n", s, l, (int) (10 + sizeof (h) - sizeof (l)), h, d); + PROGRAM_ARGUMENTS +#undef PROGRAM_ARGUMENT_BOOL +#undef PROGRAM_ARGUMENT_UINT +#undef PROGRAM_ARGUMENT_STRING +} + +#define PROGRAM_ARGUMENT_BOOL( sym, ...) bool sym; +#define PROGRAM_ARGUMENT_UINT( sym, ...) unsigned int sym; +#define PROGRAM_ARGUMENT_STRING(sym, ...) const char *sym; +struct program_arguments { + const char *program_name; + PROGRAM_ARGUMENTS +}; +#undef PROGRAM_ARGUMENT_BOOL +#undef PROGRAM_ARGUMENT_UINT +#undef PROGRAM_ARGUMENT_STRING + +struct program_arguments +parse_program_arguments (int argc, char *argv[]) +{ + UNUSED (argc); +#define PROGRAM_ARGUMENT_BOOL(...) +#define PROGRAM_ARGUMENT_UINT( sym, s, l, h, d, ...) .sym = d, +#define PROGRAM_ARGUMENT_STRING(sym, s, l, h, d, ...) .sym = d, + struct program_arguments pa = { + .program_name = *argv++, + PROGRAM_ARGUMENTS + }; +#undef PROGRAM_ARGUMENT_BOOL +#undef PROGRAM_ARGUMENT_UINT +#undef PROGRAM_ARGUMENT_STRING + +#define PROGRAM_ARGUMENT_BOOL(sym, s, l, ...) MATCH (s, l) { \ + pa.sym = true; \ + continue; \ + } +#define PROGRAM_ARGUMENT_UINT(sym, s, l, ...) MATCH (s, l) { \ + arg += 1; \ + if (*arg == NULL) { \ + fprintf (stderr, "error"); \ + exit (EXIT_FAILURE); \ + } \ + if (sscanf (*arg, "%u", &pa.sym) == 0) { \ + fprintf (stderr, "error: wrong format for %s\n", arg[-1]); \ + exit (EXIT_FAILURE); \ + } \ + continue; \ + } +#define PROGRAM_ARGUMENT_STRING(sym, s, l, ...) MATCH (s, l) { \ + arg += 1; \ + pa.sym = *arg; \ + continue; \ + } + for (char **arg = argv; *arg; ++arg) { + if ((*arg)[0] == '-') { + if ((*arg)[1] == '-') { +#define MATCH(s, l) if (!strcmp (*arg + 2, l)) + PROGRAM_ARGUMENTS; + fprintf (stderr, "error: unknown option %s\n", *arg); + exit (EXIT_FAILURE); +#undef MATCH + } +#define MATCH(s, l) if ((*arg)[1] == s && (*arg)[2] == '\0') + PROGRAM_ARGUMENTS; + fprintf (stderr, "error: unknown option %s\n", *arg); + exit (EXIT_FAILURE); +#undef MATCH + } + } +#undef PROGRAM_ARGUMENT_BOOL +#undef PROGRAM_ARGUMENT_UINT +#undef PROGRAM_ARGUMENT_STRING + + return pa; +} + +#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 @@ +#ifndef INCLUDED_UTILS_H +#define INCLUDED_UTILS_H + +#include +#include +#include +#include + +#define STRINGIFY(ARG) #ARG +#define TO_STRING(ARG) STRINGIFY (ARG) +#define LOG(PRIORITY, FMT, ...) \ + syslog (LOG_MAKEPRI (LOG_DAEMON, LOG_##PRIORITY), \ + FMT __VA_OPT__ (,) __VA_ARGS__) +#define DEBUG( FMT, ...) \ + LOG (DEBUG, __FILE__ ":" TO_STRING (__LINE__) ": %s: " FMT, __func__ __VA_OPT__ (,) __VA_ARGS__) +#define EMERGENCY(FMT, ...) LOG (EMERG, "emergency: " FMT __VA_OPT__ (,) __VA_ARGS__) +#define ALERT( FMT, ...) LOG (ALERT, "alert: " FMT __VA_OPT__ (,) __VA_ARGS__) +#define CRITICAL( FMT, ...) LOG (CRIT, "critical: " FMT __VA_OPT__ (,) __VA_ARGS__) +#define ERROR( FMT, ...) LOG (ERR, "error: " FMT __VA_OPT__ (,) __VA_ARGS__) +#define WARNING( FMT, ...) LOG (WARNING, "warning: " FMT __VA_OPT__ (,) __VA_ARGS__) +#define NOTICE( FMT, ...) LOG (NOTICE, "notice: " FMT __VA_OPT__ (,) __VA_ARGS__) +#define INFO( FMT, ...) LOG (INFO, "info: " FMT __VA_OPT__ (,) __VA_ARGS__) + +#define UNUSED(X) ((void) (X)) +#define UNREACHABLE() ASSERT (!"reached the unreachable...") + +#define MALLOC malloc +#define REALLOC realloc +#define FREE free +#define ASSERT assert + +#endif -- cgit v1.3.1