Generate scheduled updates. Untested, but builds.
[trust_router.git] / tr / tr_cfgwatch.c
1 /***** config file watching *****/
2
3 #include <sys/stat.h>
4 #include <talloc.h>
5
6 #include <tr_config.h>
7 #include <tr_debug.h>
8 #include <tr_event.h>
9 #include <tr_cfgwatch.h>
10
11 /* Initialize a new tr_cfgwatch_data struct. Free this with talloc. */
12 TR_CFGWATCH *tr_cfgwatch_create(TALLOC_CTX *mem_ctx)
13 {
14   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
15   TR_CFGWATCH *new_cfg;
16   
17   new_cfg=talloc_zero(tmp_ctx, TR_CFGWATCH);
18   if (new_cfg == NULL) {
19     tr_debug("tr_cfgwatch_create: Allocation failed.");
20   } 
21   talloc_steal(mem_ctx, new_cfg);
22   talloc_free(tmp_ctx);
23   return new_cfg;
24 }
25
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)
28 {
29   struct stat file_status;
30
31   if (stat(path, &file_status) != 0) {
32     return -1;
33   } else {
34     (*ts)=file_status.st_mtim;
35   }
36   return 0;
37 }
38
39 static char *tr_join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
40 {
41   return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
42 }
43
44 static int tr_fstat_namecmp(const void *p1_arg, const void *p2_arg)
45 {
46   struct tr_fstat *p1=(struct tr_fstat *) p1_arg;
47   struct tr_fstat *p2=(struct tr_fstat *) p2_arg;
48
49   return strcmp(p1->name, p2->name);
50 }
51
52 static int tr_fstat_mtimecmp(const void *p1_arg, const void *p2_arg)
53 {
54   struct tr_fstat *p1=(struct tr_fstat *) p1_arg;
55   struct tr_fstat *p2=(struct tr_fstat *) p2_arg;
56
57   if (p1->mtime.tv_sec == p2->mtime.tv_sec)
58     return (p1->mtime.tv_nsec) - (p2->mtime.tv_nsec);
59   else
60     return (p1->mtime.tv_sec) - (p2->mtime.tv_sec);
61 }
62
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,
69                                          int n_files)
70 {
71   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
72   struct tr_fstat *fstat_list=NULL;
73   int ii=0;
74
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.");
79     goto cleanup;
80   }
81
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);
87     }
88   }
89
90   /* sort the list */
91   qsort(fstat_list, n_files, sizeof(struct tr_fstat), tr_fstat_namecmp);
92
93   /* put list in the caller's context and return it */
94   talloc_steal(mem_ctx, fstat_list);
95  cleanup:
96   talloc_free(tmp_ctx);
97   return fstat_list;
98 }
99
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)
103 {
104   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
105   struct tr_fstat *fstat_list=NULL;
106   int n_files=0;
107   int ii=0;
108   struct dirent **cfg_files=NULL;
109   int update_needed=0; /* return value */
110
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);
113   if (n_files <= 0) {
114     tr_warning("tr_cfgwatch_update: configuration files disappeared, skipping update.");
115     goto cleanup;
116   }
117
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.");
122     goto cleanup;
123   }
124
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).",
128              cfg_status->n_files,
129              n_files);
130     update_needed=1;
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);
135     goto cleanup;
136   }
137
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]))){
142       update_needed=1;
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);
147       goto cleanup;
148     }
149   }
150
151  cleanup:
152   tr_free_config_file_list(n_files, &cfg_files);
153   talloc_free(tmp_ctx);
154   return update_needed;
155 }
156
157 /* must specify the ctx and tr in cfgwatch! */
158 int tr_read_and_apply_config(TR_CFGWATCH *cfgwatch)
159 {
160   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
161   char *config_dir=cfgwatch->config_dir;
162   int n_files = 0;
163   struct dirent **cfg_files=NULL;
164   TR_CFG_RC rc = TR_CFG_SUCCESS;        /* presume success */
165   struct tr_fstat *new_fstat_list=NULL;
166   int retval=0;
167
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);
172   if (n_files <= 0) {
173     tr_debug("tr_read_and_apply_config: No configuration files.");
174     retval=1; goto cleanup;
175   }
176
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;
184   }
185   
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;
190   }
191
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;
196   }
197
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);
202   }
203   cfgwatch->n_files=n_files;
204   cfgwatch->fstat_list=new_fstat_list;
205   talloc_steal(cfgwatch, new_fstat_list);
206   new_fstat_list=NULL;
207
208  cleanup:
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 */
212   return retval;
213 }
214
215
216 static void tr_cfgwatch_event_cb(int listener, short event, void *arg)
217 {
218   TR_CFGWATCH *cfg_status=(TR_CFGWATCH *) arg;
219   struct timeval now, diff;;
220
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;
224
225     if (0 != gettimeofday(&cfg_status->last_change_detected, NULL)) {
226       tr_err("tr_cfgwatch_event_cb: gettimeofday() failed (1).");
227     }
228   }
229
230   if (cfg_status->change_detected) {
231     if (0 != gettimeofday(&now, NULL)) {
232       tr_err("tr_cfgwatch_event_cb: gettimeofday() failed (2).");
233     }
234     timersub(&now, &cfg_status->last_change_detected, &diff);
235     if (!timercmp(&diff, &cfg_status->settling_time, <)) {
236       tr_notice("Configuration file change settled, attempting to update configuration.");
237       if (0 != tr_read_and_apply_config(cfg_status))
238         tr_warning("Configuration file update failed. Using previous configuration.");
239       else {
240         if (cfg_status->update_cb!=NULL)
241           cfg_status->update_cb(cfg_status->cfg_mgr->active, cfg_status->update_cookie);
242         tr_notice("Configuration updated successfully.");
243       }
244       cfg_status->change_detected=0;
245     }
246   }
247 }
248
249
250 /* Configure the cfgwatch instance and set up its event handler.
251  * Returns 0 on success, nonzero on failure. Points
252  * *cfgwatch_ev to the event struct. */
253 int tr_cfgwatch_event_init(struct event_base *base,
254                            TR_CFGWATCH *cfg_status,
255                            struct event **cfgwatch_ev)
256 {
257   if (cfgwatch_ev == NULL) {
258     tr_debug("tr_cfgwatch_event_init: Null cfgwatch_ev.");
259     return 1;
260   }
261
262   /* zero out the change detection fields */
263   cfg_status->change_detected=0;
264   cfg_status->last_change_detected.tv_sec=0;
265   cfg_status->last_change_detected.tv_usec=0;
266
267   /* create the event and enable it */
268   *cfgwatch_ev=event_new(base, -1, EV_TIMEOUT|EV_PERSIST, tr_cfgwatch_event_cb, (void *)cfg_status);
269   event_add(*cfgwatch_ev, &(cfg_status->poll_interval));
270
271   tr_info("tr_cfgwatch_event_init: Added configuration file watcher with %0d.%06d second poll interval.",
272            cfg_status->poll_interval.tv_sec,
273            cfg_status->poll_interval.tv_usec);
274   return 0;
275 }
276