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 /* call callback to notify system of new configuration */
199 tr_debug("tr_read_and_apply_config: calling update callback function.");
200 if (cfgwatch->update_cb!=NULL)
201 cfgwatch->update_cb(cfgwatch->cfg_mgr->active, cfgwatch->update_cookie);
203 /* give ownership of the new_fstat_list to caller's context */
204 if (cfgwatch->fstat_list != NULL) {
205 /* free the old one */
206 talloc_free(cfgwatch->fstat_list);
208 cfgwatch->n_files=n_files;
209 cfgwatch->fstat_list=new_fstat_list;
210 talloc_steal(cfgwatch, new_fstat_list);
214 tr_free_config_file_list(n_files, &cfg_files);
215 talloc_free(tmp_ctx);
216 cfgwatch->cfg_mgr->new=NULL; /* this has been freed, either explicitly or with tmp_ctx */
221 static void tr_cfgwatch_event_cb(int listener, short event, void *arg)
223 TR_CFGWATCH *cfg_status=(TR_CFGWATCH *) arg;
224 struct timeval now, diff;;
226 if (tr_cfgwatch_update_needed(cfg_status)) {
227 tr_notice("Configuration file change detected, waiting for changes to settle.");
228 cfg_status->change_detected=1;
230 if (0 != gettimeofday(&cfg_status->last_change_detected, NULL)) {
231 tr_err("tr_cfgwatch_event_cb: gettimeofday() failed (1).");
235 if (cfg_status->change_detected) {
236 if (0 != gettimeofday(&now, NULL)) {
237 tr_err("tr_cfgwatch_event_cb: gettimeofday() failed (2).");
239 timersub(&now, &cfg_status->last_change_detected, &diff);
240 if (!timercmp(&diff, &cfg_status->settling_time, <)) {
241 tr_notice("Configuration file change settled, attempting to update configuration.");
242 if (0 != tr_read_and_apply_config(cfg_status))
243 tr_warning("Configuration file update failed. Using previous configuration.");
245 tr_notice("Configuration updated successfully.");
246 cfg_status->change_detected=0;
252 /* Configure the cfgwatch instance and set up its event handler.
253 * Returns 0 on success, nonzero on failure. Points
254 * *cfgwatch_ev to the event struct. */
255 int tr_cfgwatch_event_init(struct event_base *base,
256 TR_CFGWATCH *cfg_status,
257 struct event **cfgwatch_ev)
259 if (cfgwatch_ev == NULL) {
260 tr_debug("tr_cfgwatch_event_init: Null cfgwatch_ev.");
264 /* zero out the change detection fields */
265 cfg_status->change_detected=0;
266 cfg_status->last_change_detected.tv_sec=0;
267 cfg_status->last_change_detected.tv_usec=0;
269 /* create the event and enable it */
270 *cfgwatch_ev=event_new(base, -1, EV_TIMEOUT|EV_PERSIST, tr_cfgwatch_event_cb, (void *)cfg_status);
271 event_add(*cfgwatch_ev, &(cfg_status->poll_interval));
273 tr_info("tr_cfgwatch_event_init: Added configuration file watcher with %0d.%06d second poll interval.",
274 cfg_status->poll_interval.tv_sec,
275 cfg_status->poll_interval.tv_usec);