aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c87
1 files changed, 83 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index bc554b5..11fdc54 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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 {