1 /***** config file watching *****/
9 #include <tr_cfgwatch.h>
11 /* Initialize a new tr_cfgwatch_data struct. Free this with talloc. */
12 TR_CFGWATCH *tr_cfgwatch_create(TALLOC_CTX *mem_ctx)
14 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
17 new_cfg=talloc_zero(tmp_ctx, TR_CFGWATCH);
18 if (new_cfg == NULL) {
19 tr_debug("tr_cfgwatch_create: Allocation failed.");
21 talloc_steal(mem_ctx, new_cfg);
26 /* Obtain the file modification time as seconds since epoch. Returns 0 on success. */
27 static int tr_get_mtime(const char *path, struct timespec *ts)
29 struct stat file_status;
31 if (stat(path, &file_status) != 0) {
34 (*ts)=file_status.st_mtim;
39 static char *tr_join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
41 return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
44 static int tr_fstat_namecmp(const void *p1_arg, const void *p2_arg)
46 struct tr_fstat *p1=(struct tr_fstat *) p1_arg;
47 struct tr_fstat *p2=(struct tr_fstat *) p2_arg;
49 return strcmp(p1->name, p2->name);
52 static int tr_fstat_mtimecmp(const void *p1_arg, const void *p2_arg)
54 struct tr_fstat *p1=(struct tr_fstat *) p1_arg;
55 struct tr_fstat *p2=(struct tr_fstat *) p2_arg;
57 if (p1->mtime.tv_sec == p2->mtime.tv_sec)
58 return (p1->mtime.tv_nsec) - (p2->mtime.tv_nsec);
60 return (p1->mtime.tv_sec) - (p2->mtime.tv_sec);
63 /* Get status of all files in cfg_files. Returns list, or NULL on error.
64 * Files are sorted by filename.
65 * After success, caller must eventually free result with talloc_free. */
66 static struct tr_fstat *tr_fstat_get_all(TALLOC_CTX *mem_ctx,
67 const char *config_path,
68 struct dirent **cfg_files,
71 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
72 struct tr_fstat *fstat_list=NULL;
75 /* create a new fstat list (may be discarded) */
76 fstat_list=talloc_array(tmp_ctx, struct tr_fstat, n_files);
77 if (fstat_list==NULL) {
78 tr_err("tr_fstat_get_all: Could not allocate fstat list.");
82 for (ii=0; ii<n_files; ii++) {
83 fstat_list[ii].name=talloc_strdup(fstat_list, cfg_files[ii]->d_name);
84 if (0 != tr_get_mtime(tr_join_paths(tmp_ctx, config_path, fstat_list[ii].name),
85 &(fstat_list[ii].mtime))) {
86 tr_warning("tr_fstat_get_all: Could not obtain mtime for file %s", fstat_list[ii].name);
91 qsort(fstat_list, n_files, sizeof(struct tr_fstat), tr_fstat_namecmp);
93 /* put list in the caller's context and return it */
94 talloc_steal(mem_ctx, fstat_list);
100 /* Checks whether any config files have appeared/disappeared/modified.
101 * Returns 1 if so, 0 otherwise. */
102 static int tr_cfgwatch_update_needed(TR_CFGWATCH *cfg_status)
104 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
105 struct tr_fstat *fstat_list=NULL;
108 struct dirent **cfg_files=NULL;
109 int update_needed=0; /* return value */
111 /* get the list, must free cfg_files later with tr_free_cfg_file_list */
112 n_files = tr_find_config_files(cfg_status->config_dir, &cfg_files);
114 tr_warning("tr_cfgwatch_update: configuration files disappeared, skipping update.");
118 /* create a new fstat list (will be discarded) */
119 fstat_list=tr_fstat_get_all(tmp_ctx, cfg_status->config_dir, cfg_files, n_files);
120 if (fstat_list==NULL) {
121 tr_err("tr_cfgwatch_update: Error getting fstat list.");
125 /* see if the number of files change, if so need to update */
126 if (n_files != cfg_status->n_files) {
127 tr_debug("tr_cfgwatch_update: Changed number of config files (was %d, now %d).",
131 talloc_free(cfg_status->fstat_list);
132 cfg_status->n_files=n_files;
133 cfg_status->fstat_list=fstat_list;
134 talloc_steal(cfg_status, fstat_list);
138 /* See if any files have a changed mtime. Both are sorted by name so this is easy. */
139 for (ii=0; ii<n_files; ii++) {
140 if ((0 != tr_fstat_mtimecmp(&fstat_list[ii], &cfg_status->fstat_list[ii]))
141 || (0 != tr_fstat_namecmp(&fstat_list[ii], &cfg_status->fstat_list[ii]))){
143 talloc_free(cfg_status->fstat_list);
144 cfg_status->n_files=n_files;
145 cfg_status->fstat_list=fstat_list;
146 talloc_steal(cfg_status, fstat_list);
152 tr_free_config_file_list(n_files, &cfg_files);
153 talloc_free(tmp_ctx);
154 return update_needed;
157 /* must specify the ctx and tr in cfgwatch! */
158 int tr_read_and_apply_config(TR_CFGWATCH *cfgwatch)
160 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
161 char *config_dir=cfgwatch->config_dir;
163 struct dirent **cfg_files=NULL;
164 TR_CFG_RC rc = TR_CFG_SUCCESS; /* presume success */
165 struct tr_fstat *new_fstat_list=NULL;
168 /* find the configuration files -- n.b., tr_find_config_files()
169 * allocates memory to cfg_files which we must later free */
170 tr_debug("Reading configuration files from %s/", config_dir);
171 n_files = tr_find_config_files(config_dir, &cfg_files);
173 tr_debug("tr_read_and_apply_config: No configuration files.");
174 retval=1; goto cleanup;
177 /* Get the list of update times.
178 * Do this before loading in case they change between obtaining their timestamp
179 * and reading the file---this way they will immediately reload if this happens. */
180 new_fstat_list=tr_fstat_get_all(tmp_ctx, config_dir, cfg_files, n_files);
181 if (new_fstat_list==NULL) {
182 tr_debug("tr_read_and_apply_config: Could not allocate config file status list.");
183 retval=1; goto cleanup;
186 /* now fill it in (tr_parse_config allocates space for new config) */
187 if (TR_CFG_SUCCESS != (rc = tr_parse_config(cfgwatch->cfg_mgr, config_dir, n_files, cfg_files))) {
188 tr_debug("tr_read_and_apply_config: Error parsing configuration information, rc=%d.", rc);
189 retval=1; goto cleanup;
192 /* apply new configuration (nulls new, manages context ownership) */
193 if (TR_CFG_SUCCESS != (rc = tr_apply_new_config(cfgwatch->cfg_mgr))) {
194 tr_debug("tr_read_and_apply_config: Error applying configuration, rc = %d.", rc);
195 retval=1; goto cleanup;
198 /* give ownership of the new_fstat_list to caller's context */
199 if (cfgwatch->fstat_list != NULL) {
200 /* free the old one */
201 talloc_free(cfgwatch->fstat_list);
203 cfgwatch->n_files=n_files;
204 cfgwatch->fstat_list=new_fstat_list;
205 talloc_steal(cfgwatch, new_fstat_list);
209 tr_free_config_file_list(n_files, &cfg_files);
210 talloc_free(tmp_ctx);
211 cfgwatch->cfg_mgr->new=NULL; /* this has been freed, either explicitly or with tmp_ctx */
216 static void tr_cfgwatch_event_cb(int listener, short event, void *arg)
218 TR_CFGWATCH *cfg_status=(TR_CFGWATCH *) arg;
219 struct timeval now, diff;;
221 if (tr_cfgwatch_update_needed(cfg_status)) {
222 tr_notice("Configuration file change detected, waiting for changes to settle.");
223 cfg_status->change_detected=1;
225 if (0 != gettimeofday(&cfg_status->last_change_detected, NULL)) {
226 tr_err("tr_cfgwatch_event_cb: gettimeofday() failed (1).");
230 if (cfg_status->change_detected) {
231 if (0 != gettimeofday(&now, NULL)) {
232 tr_err("tr_cfgwatch_event_cb: gettimeofday() failed (2).");
234 timersub(&now, &cfg_status->last_change_detected, &diff);
235 if (!timercmp(&diff, &cfg_status->settling_time, <)) {
236 tr_notice("Configuration file change settled, updating configuration.");
237 if (0 != tr_read_and_apply_config(cfg_status))
238 tr_warning("Configuration file update failed. Using previous configuration.");
240 tr_notice("Configuration updated successfully.");
241 cfg_status->change_detected=0;
247 /* Configure the cfgwatch instance and set up its event handler.
248 * Returns 0 on success, nonzero on failure. Points
249 * *cfgwatch_ev to the event struct. */
250 int tr_cfgwatch_event_init(struct event_base *base,
251 TR_CFGWATCH *cfg_status,
252 struct event **cfgwatch_ev)
254 if (cfgwatch_ev == NULL) {
255 tr_debug("tr_cfgwatch_event_init: Null cfgwatch_ev.");
259 /* zero out the change detection fields */
260 cfg_status->change_detected=0;
261 cfg_status->last_change_detected.tv_sec=0;
262 cfg_status->last_change_detected.tv_usec=0;
264 /* create the event and enable it */
265 *cfgwatch_ev=event_new(base, -1, EV_TIMEOUT|EV_PERSIST, tr_cfgwatch_event_cb, (void *)cfg_status);
266 event_add(*cfgwatch_ev, &(cfg_status->poll_interval));
268 tr_info("tr_cfgwatch_event_init: Added configuration file watcher with %0d.%06d second poll interval.",
269 cfg_status->poll_interval.tv_sec,
270 cfg_status->poll_interval.tv_usec);