+/***** event loop management ****/
+
+/* struct for hanging on to a socket listener event */
+struct tr_socket_event {
+ int sock_fd; /* the fd for the socket */
+ struct event *ev; /* its event */
+};
+
+/* Allocate and set up the event base, return a pointer
+ * to the new event_base or NULL on failure.
+ * Does not currently enable thread-safe mode. */
+static struct event_base *tr_event_loop_init(void)
+{
+ struct event_base *base=NULL;
+
+ base=event_base_new();
+ if (base==NULL) {
+ tr_crit("Error initializing event loop.");
+ return NULL;
+ }
+ return base;
+}
+
+/* run the loop, does not normally return */
+static int tr_event_loop_run(struct event_base *base)
+{
+ return event_base_dispatch(base);
+}
+
+/* called when a connection to the TIDS port is received */
+static void tr_tids_event_cb(int listener, short event, void *arg)
+{
+ TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
+
+ if (0==(event & EV_READ))
+ tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
+ else
+ tids_accept(tids, listener);
+}
+
+/* Configure the tids instance and set up its event handler.
+ * Returns 0 on success, nonzero on failure. Fills in
+ * *tids_event (which should be allocated). */
+static int tr_tids_event_init(struct event_base *base,
+ TR_INSTANCE *tr,
+ struct tr_socket_event *tids_ev)
+{
+ if (tids_ev == NULL) {
+ tr_debug("tr_tids_event_init: Null tids_ev.");
+ return 1;
+ }
+
+ /* get a tids listener */
+ tids_ev->sock_fd=tids_get_listener(tr->tids,
+ tr_tids_req_handler,
+ tr_tids_gss_handler,
+ tr->active_cfg->internal->hostname,
+ tr->active_cfg->internal->tids_port,
+ (void *)tr);
+ if (tids_ev->sock_fd < 0) {
+ tr_crit("Error opening TID server socket.");
+ return 1;
+ }
+
+ /* and its event */
+ tids_ev->ev=event_new(base,
+ tids_ev->sock_fd,
+ EV_READ|EV_PERSIST,
+ tr_tids_event_cb,
+ (void *)tr->tids);
+ event_add(tids_ev->ev, NULL);
+
+ return 0;
+}
+
+
+/***** config file watching *****/
+
+struct tr_fstat {
+ char *name;
+ struct timespec mtime;
+};
+
+struct tr_cfgwatch_data {
+ struct timeval poll_interval;
+ char *config_dir;
+ struct tr_fstat *fstat_list;
+ int n_files;
+};
+
+/* Obtain the file modification time as seconds since epoch. Returns 0 on success. */
+static int tr_get_mtime(const char *path, struct timespec *ts)
+{
+ struct stat file_status;
+
+ if (stat(path, &file_status) != 0) {
+ return -1;
+ } else {
+ (*ts)=file_status.st_mtim;
+ }
+ return 0;
+}
+
+static char *tr_join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
+{
+ return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
+}
+
+static int tr_fstat_namecmp(const void *p1_arg, const void *p2_arg)
+{
+ struct tr_fstat *p1=(struct tr_fstat *) p1_arg;
+ struct tr_fstat *p2=(struct tr_fstat *) p2_arg;
+
+ return strcmp(p1->name, p2->name);
+}
+
+static int tr_fstat_mtimecmp(const void *p1_arg, const void *p2_arg)
+{
+ struct tr_fstat *p1=(struct tr_fstat *) p1_arg;
+ struct tr_fstat *p2=(struct tr_fstat *) p2_arg;
+
+ if (p1->mtime.tv_sec == p2->mtime.tv_sec)
+ return (p1->mtime.tv_nsec) - (p2->mtime.tv_nsec);
+ else
+ return (p1->mtime.tv_sec) - (p2->mtime.tv_sec);
+}
+
+/* Get status of all files in cfg_files. Returns list, or NULL on error.
+ * Files are sorted by filename.
+ * After success, caller must eventually free result with talloc_free. */
+static struct tr_fstat *tr_fstat_get_all(TALLOC_CTX *mem_ctx,
+ const char *config_path,
+ struct dirent **cfg_files,
+ int n_files)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ struct tr_fstat *fstat_list=NULL;
+ int ii=0;
+
+ /* create a new fstat list (may be discarded) */
+ fstat_list=talloc_array(tmp_ctx, struct tr_fstat, n_files);
+ if (fstat_list==NULL) {
+ tr_err("tr_fstat_get_all: Could not allocate fstat list.");
+ goto cleanup;
+ }
+
+ for (ii=0; ii<n_files; ii++) {
+ fstat_list[ii].name=talloc_strdup(fstat_list, cfg_files[ii]->d_name);
+ if (0 != tr_get_mtime(tr_join_paths(tmp_ctx, config_path, fstat_list[ii].name),
+ &(fstat_list[ii].mtime))) {
+ tr_warning("tr_fstat_get_all: Could not obtain mtime for file %s", fstat_list[ii].name);
+ }
+ }
+
+ /* sort the list */
+ qsort(fstat_list, n_files, sizeof(struct tr_fstat), tr_fstat_namecmp);
+
+ /* put list in the caller's context and return it */
+ talloc_steal(mem_ctx, fstat_list);
+ cleanup:
+ talloc_free(tmp_ctx);
+ return fstat_list;
+}
+
+/* Checks whether any config files have appeared/disappeared/modified.
+ * Returns 1 if so, 0 otherwise. */
+static int tr_cfgwatch_update_needed(struct tr_cfgwatch_data *cfg_status)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ struct tr_fstat *fstat_list=NULL;
+ int n_files=0;
+ int ii=0;
+ struct dirent **cfg_files=NULL;
+ int update_needed=0; /* return value */
+
+ /* get the list, must free cfg_files later with tr_free_cfg_file_list */
+ n_files = tr_find_config_files(cfg_status->config_dir, &cfg_files);
+ if (n_files <= 0) {
+ tr_warning("tr_cfgwatch_update: configuration files disappeared, skipping update.");
+ goto cleanup;
+ }
+
+ /* create a new fstat list (will be discarded) */
+ fstat_list=tr_fstat_get_all(tmp_ctx, cfg_status->config_dir, cfg_files, n_files);
+ if (fstat_list==NULL) {
+ tr_err("tr_cfgwatch_update: Error getting fstat list.");
+ goto cleanup;
+ }
+
+ /* see if the number of files change, if so need to update */
+ if (n_files != cfg_status->n_files) {
+ tr_debug("tr_cfgwatch_update: Changed number of config files (was %d, now %d).",
+ cfg_status->n_files,
+ n_files);
+ update_needed=1;
+ goto cleanup;
+ }
+
+ /* See if any files have a changed mtime. Both are sorted by name so this is easy. */
+ for (ii=0; ii<n_files; ii++) {
+ if ((0 != tr_fstat_mtimecmp(&fstat_list[ii], &cfg_status->fstat_list[ii]))
+ || (0 != tr_fstat_namecmp(&fstat_list[ii], &cfg_status->fstat_list[ii]))){
+ update_needed=1;
+ goto cleanup;
+ }
+ }
+
+ cleanup:
+ tr_free_config_file_list(n_files, &cfg_files);
+ talloc_free(tmp_ctx);
+ return update_needed;
+}
+
+static void tr_cfgwatch_event_cb(int listener, short event, void *arg)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ struct tr_cfgwatch_data *cfg_status=(struct tr_cfgwatch_data *) arg;
+
+ if (tr_cfgwatch_update_needed(cfg_status)) {
+ tr_notice("tr_cfgwatch_event_cb: update needed!"); /* remove */
+ }
+
+ talloc_free(tmp_ctx);
+}
+
+
+/* Configure the cfgwatch instance and set up its event handler.
+ * Returns 0 on success, nonzero on failure. Points
+ * *cfgwatch_ev to the event struct. */
+static int tr_cfgwatch_event_init(struct event_base *base,
+ struct tr_cfgwatch_data *cfg_status,
+ struct event **cfgwatch_ev)
+{
+ if (cfgwatch_ev == NULL) {
+ tr_debug("tr_cfgwatch_event_init: Null cfgwatch_ev.");
+ return 1;
+ }
+
+ *cfgwatch_ev=event_new(base, -1, EV_TIMEOUT|EV_PERSIST, tr_cfgwatch_event_cb, (void *)cfg_status);
+ event_add(*cfgwatch_ev, &(cfg_status->poll_interval));
+
+ tr_info("tr_cfgwatch_event_init: Added configuration file watcher with %0d.%06d second poll interval.",
+ cfg_status->poll_interval.tv_sec,
+ cfg_status->poll_interval.tv_usec);
+ return 0;
+}
+
+
+/***** command-line option handling / setup *****/
+