diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-07-14 07:49:07 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-14 07:49:07 +0200 |
commit | 5b20475c7faf89bbabc9eab43c7e5622317a18fc (patch) | |
tree | b526f7cadb0fe0867ed18c64d289073628504077 /src/main.c | |
parent | 94c45e3c4d185b3f0d70f0d2d761b72c6561e1b5 (diff) |
Implement first part of command_sync
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 87 |
1 files changed, 83 insertions, 4 deletions
@@ -14,6 +14,10 @@ #include <dirent.h> #include <libgen.h> +#include <fcntl.h> +#include <errno.h> +#include <signal.h> +#include <unistd.h> #define NAME_MAX_LEN 250 @@ -261,11 +265,86 @@ static void command_add(int argc, char **argv, char *rss_config_dir, char *html_ } } -static void command_sync(int argc, char **argv) { - if(argc < 2) +static void sync_rss_html(char *rss_config_dir, char *html_config_dir, const char *download_dir, int sync_rate_sec) { + (void)rss_config_dir; + (void)html_config_dir; + (void)download_dir; + (void)sync_rate_sec; +} + +static int cmdline_contains_str(const char *cmdline, const char *str) { + for(;;) { + int arg_len = strlen(cmdline); + if(arg_len == 0) + return -1; + + if(strstr(cmdline, str)) + return 0; + + cmdline += arg_len; + } +} + +static void automedia_pid_signal_handler(int signum) { + (void)signum; + unlink("/tmp/automedia.pid"); + exit(1); +} + +static void command_sync(int argc, char **argv, char *rss_config_dir, char *html_config_dir) { + if(argc < 1) usage_sync(); - (void)argv; + const char *download_dir = argv[0]; + const char automedia_pid_path[] = "/tmp/automedia.pid"; + + int pid_file = open(automedia_pid_path, O_CREAT | O_EXCL | O_RDWR); + if(pid_file == -1 && errno == EEXIST) { + char *running_automedia_pid; + long running_automedia_pid_size; + if(file_get_content(automedia_pid_path, &running_automedia_pid, &running_automedia_pid_size) == 0) { + char cmdline_file_path[128]; + sprintf(cmdline_file_path, "/proc/%s/cmdline", running_automedia_pid); + free(running_automedia_pid); + + char *cmdline; + long cmdline_size; + if(file_get_content(cmdline_file_path, &cmdline, &cmdline_size) == 0 + && cmdline_contains_str(cmdline, "automedia") == 0 + && cmdline_contains_str(cmdline, "sync") == 0) { + fprintf(stderr, "Automedia is already running with sync"); + free(cmdline); + exit(0); + } + free(cmdline); + } else { + free(running_automedia_pid); + } + + remove(automedia_pid_path); + pid_file = open(automedia_pid_path, O_CREAT | O_EXCL | O_RDWR); + } + + if(pid_file == -1 && errno != EEXIST) { + fprintf(stderr, "Failed to create %s\n", automedia_pid_path); + exit(1); + } + + signal(SIGINT, automedia_pid_signal_handler); + + char process_pid_str[32]; + sprintf(process_pid_str, "%d", getpid()); + int process_pid_str_len = strlen(process_pid_str); + if(write(pid_file, process_pid_str, process_pid_str_len) != process_pid_str_len) { + fprintf(stderr, "Failed to write pid to %s\n", automedia_pid_path); + unlink(automedia_pid_path); + exit(1); + } + + close(pid_file); + const int sync_rate_sec = 15 * 60; /* every 15 min */ + sync_rss_html(rss_config_dir, html_config_dir, download_dir, sync_rate_sec); + unlink(automedia_pid_path); } static void command_downloaded(const char *rss_config_dir, const char *html_config_dir) { @@ -317,7 +396,7 @@ int main(int argc, char **argv) { if(strcmp(command, "add") == 0) { command_add(argc - 2, argv + 2, rss_config_dir, html_config_dir, dirname(argv[0])); } else if(strcmp(command, "sync") == 0) { - command_sync(argc - 2, argv + 2); + command_sync(argc - 2, argv + 2, rss_config_dir, html_config_dir); } else if(strcmp(command, "downloaded") == 0) { command_downloaded(rss_config_dir, html_config_dir); } else { |