bool add_path_to_sync (const char *path) { /* Note: it is required that the path in the file ends with "/*" */ if (sync_path_check (path)) { DEBUG ("SYNC INIT: about to watch path: %s\n", path); apteryx_watch (path, new_change); /* Lastly, add the path to our list for the resyncer thread. /* note: because we need to do a few things to this later, * remove the trailing '/*' */ char *new_path = strdup (path); char *end_ptr = NULL; if ((end_ptr = strstr (new_path, "/*")) != NULL) { end_ptr[0] = '\0'; } pthread_rwlock_wrlock (&paths_lock); paths = g_list_append (paths, strdup (new_path)); pthread_rwlock_unlock (&paths_lock); } else { ERROR ("Path %s is not valid for syncing\n", path); } return TRUE; }
int main (int argc, char *argv[]) { const char *pid_file = APTERYX_SYNC_PID; const char *config_dir = APTERYX_SYNC_CONFIG_DIR; int i = 0; bool background = false; FILE *fp = NULL; apteryx_init (false); /* Parse options */ while ((i = getopt (argc, argv, "hdbp:c:")) != -1) { switch (i) { case 'd': debug = true; background = false; break; case 'b': background = true; break; case 'p': pid_file = optarg; break; case 'c': config_dir = optarg; break; case '?': case 'h': default: help (argv[0]); return 0; } } /* Handle SIGTERM/SIGINT/SIGPIPE gracefully */ signal (SIGTERM, (__sighandler_t) termination_handler); signal (SIGINT, (__sighandler_t) termination_handler); signal (SIGPIPE, SIG_IGN); /* Daemonize */ if (background && fork () != 0) { /* Parent */ return 0; } /* Create pid file */ if (background) { fp = fopen (pid_file, "w"); if (!fp) { ERROR ("Failed to create PID file %s\n", pid_file); goto exit; } fprintf (fp, "%d\n", getpid ()); fclose (fp); } /* The sync path is how applications can register the nodes to sync to */ apteryx_watch (APTERYX_SYNC_PATH "/*", new_syncer); /* next, we need to check for any existing nodes and setup syncers for them */ register_existing_partners (); /* and finally, read the list of paths we should sync */ parse_config_files (config_dir); /* Now we have done the setup, we can start running */ pthread_t timer; pthread_create (&timer, NULL, periodic_syncer_thread, NULL); while (running) { pause (); } pthread_cancel (timer); pthread_join (timer, NULL); exit: /* Remove the pid file */ if (background) { unlink (pid_file); } }
/* Application entry point */ int main (int argc, char **argv) { const char *filter = NULL; APTERYX_MODE mode = -1; char *path = NULL; char *param = NULL; GList * _iter; int c; uint64_t value; /* Parse options */ while ((c = getopt (argc, argv, "hdsgftwpxlu::")) != -1) { switch (c) { case 'd': apteryx_debug = true; break; case 's': mode = MODE_SET; break; case 'g': mode = MODE_GET; break; case 'f': mode = MODE_FIND; break; case 't': mode = MODE_TRAVERSE; break; case 'w': mode = MODE_WATCH; break; case 'p': mode = MODE_PROVIDE; break; case 'x': mode = MODE_PROXY; break; case 'l': mode = MODE_TIMESTAMP; break; case 'u': mode = MODE_TEST; if (optarg && optarg[0] == '=') memmove(optarg, optarg+1, strlen(optarg)); filter = optarg; break; case '?': case 'h': default: usage (); return 0; } } for (c = optind; c < argc; c++) { if (path == NULL) path = argv[c]; else if (param == NULL) param = argv[c]; else { usage (); return 0; } } /* Handle SIGTERM/SIGINT/SIGPIPE gracefully */ if (mode != MODE_TEST) { signal (SIGTERM, (__sighandler_t) termination_handler); signal (SIGINT, (__sighandler_t) termination_handler); } switch (mode) { case MODE_GET: if (!path || param) { usage (); return 0; } apteryx_init (apteryx_debug); if ((param = apteryx_get (path))) { printf ("%s\n", param); free (param); } else printf ("Not found\n"); apteryx_shutdown (); break; case MODE_SET: if (!path) { usage (); return 0; } apteryx_init (apteryx_debug); if (!apteryx_set (path, param)) printf ("Failed\n"); apteryx_shutdown (); break; case MODE_FIND: if (!path || param) { usage (); return 0; } apteryx_init (apteryx_debug); GList *paths = apteryx_search (path); for (_iter = paths; _iter; _iter = _iter->next) printf (" %s\n", (char *) _iter->data); g_list_free_full (paths, free); apteryx_shutdown (); break; case MODE_TRAVERSE: if (param) { usage (); return 0; } if (!path) { path = ""; } apteryx_init (apteryx_debug); apteryx_dump (path, stdout); apteryx_shutdown (); break; case MODE_WATCH: if (param) { usage (); return 0; } if (!path) { path = "/"; } apteryx_init (apteryx_debug); apteryx_watch (path, watch_callback); while (running) pause (); apteryx_unwatch (path, watch_callback); apteryx_shutdown (); break; case MODE_PROVIDE: if (!path || !param) { usage (); return 0; } apteryx_init (apteryx_debug); provide_value = param; apteryx_provide (path, provide_callback); while (running) pause (); apteryx_unprovide (path, provide_callback); apteryx_shutdown (); break; case MODE_PROXY: if (!path || !param) { usage (); return 0; } apteryx_init (apteryx_debug); apteryx_proxy (path, param); apteryx_shutdown (); break; case MODE_TIMESTAMP: if (param) { usage (); return 0; } if (!path) { path = ""; } apteryx_init (apteryx_debug); value = apteryx_timestamp (path); printf ("%"PRIu64"\n", value); apteryx_shutdown (); break; case MODE_TEST: if (path || param) { usage (); return 0; } apteryx_init (apteryx_debug); run_unit_tests (filter); usleep (100000); apteryx_shutdown (); break; default: usage (); return 0; } return 0; }