1 #include "registry_internals.h"
3 int registry_db_should_be_saved(void) {
4 debug(D_REGISTRY, "log entries %llu, max %llu", registry.log_count, registry.save_registry_every_entries);
5 return registry.log_count > registry.save_registry_every_entries;
8 // ----------------------------------------------------------------------------
9 // INTERNAL FUNCTIONS FOR SAVING REGISTRY OBJECTS
11 static int registry_machine_save_url(void *entry, void *file) {
12 REGISTRY_MACHINE_URL *mu = entry;
15 debug(D_REGISTRY, "Registry: registry_machine_save_url('%s')", mu->url->url);
17 int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
25 // error handling is done at registry_db_save()
30 static int registry_machine_save(void *entry, void *file) {
31 REGISTRY_MACHINE *m = entry;
34 debug(D_REGISTRY, "Registry: registry_machine_save('%s')", m->guid);
36 int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
44 int ret2 = dictionary_get_all(m->machine_urls, registry_machine_save_url, fp);
45 if(ret2 < 0) return ret2;
49 // error handling is done at registry_db_save()
54 static inline int registry_person_save_url(void *entry, void *file) {
55 REGISTRY_PERSON_URL *pu = entry;
58 debug(D_REGISTRY, "Registry: registry_person_save_url('%s')", pu->url->url);
60 int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\t%s\n",
70 // error handling is done at registry_db_save()
75 static inline int registry_person_save(void *entry, void *file) {
76 REGISTRY_PERSON *p = entry;
79 debug(D_REGISTRY, "Registry: registry_person_save('%s')", p->guid);
81 int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
89 //int ret2 = dictionary_get_all(p->person_urls, registry_person_save_url, fp);
90 int ret2 = avl_traverse(&p->person_urls, registry_person_save_url, fp);
91 if (ret2 < 0) return ret2;
95 // error handling is done at registry_db_save()
100 // ----------------------------------------------------------------------------
101 // SAVE THE REGISTRY DATABASE
103 int registry_db_save(void) {
104 if(unlikely(!registry.enabled))
107 if(unlikely(!registry_db_should_be_saved()))
110 error_log_limit_unlimited();
112 char tmp_filename[FILENAME_MAX + 1];
113 char old_filename[FILENAME_MAX + 1];
115 snprintfz(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
116 snprintfz(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
118 debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
119 FILE *fp = fopen(tmp_filename, "w");
121 error("Registry: Cannot create file: %s", tmp_filename);
122 error_log_limit_reset();
126 // dictionary_get_all() has its own locking, so this is safe to do
128 debug(D_REGISTRY, "Saving all machines");
129 int bytes1 = dictionary_get_all(registry.machines, registry_machine_save, fp);
131 error("Registry: Cannot save registry machines - return value %d", bytes1);
133 error_log_limit_reset();
136 debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
138 debug(D_REGISTRY, "Saving all persons");
139 int bytes2 = dictionary_get_all(registry.persons, registry_person_save, fp);
141 error("Registry: Cannot save registry persons - return value %d", bytes2);
143 error_log_limit_reset();
146 debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
149 fprintf(fp, "T\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\n",
150 registry.persons_count,
151 registry.machines_count,
152 registry.usages_count + 1, // this is required - it is lost on db rotation
154 registry.persons_urls_count,
155 registry.machines_urls_count
162 // remove the .old db
163 debug(D_REGISTRY, "Registry: Removing old db '%s'", old_filename);
164 if(unlink(old_filename) == -1 && errno != ENOENT)
165 error("Registry: cannot remove old registry file '%s'", old_filename);
167 // rename the db to .old
168 debug(D_REGISTRY, "Registry: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
169 if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
170 error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", registry.db_filename, old_filename);
173 // remove the database (it is saved in .old)
174 debug(D_REGISTRY, "Registry: removing db '%s'", registry.db_filename);
175 if (unlink(registry.db_filename) == -1 && errno != ENOENT)
176 error("Registry: cannot remove old registry file '%s'", registry.db_filename);
178 // move the .tmp to make it active
179 debug(D_REGISTRY, "Registry: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
180 if (link(tmp_filename, registry.db_filename) == -1) {
181 error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
182 registry.db_filename);
184 // move the .old back
185 debug(D_REGISTRY, "Registry: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
186 if(link(old_filename, registry.db_filename) == -1)
187 error("Registry: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
190 debug(D_REGISTRY, "Registry: removing tmp db '%s'", tmp_filename);
191 if(unlink(tmp_filename) == -1)
192 error("Registry: cannot remove tmp registry file '%s'", tmp_filename);
194 // it has been moved successfully
195 // discard the current registry log
196 registry_log_recreate();
197 registry.log_count = 0;
201 // continue operations
202 error_log_limit_reset();
207 // ----------------------------------------------------------------------------
208 // LOAD THE REGISTRY DATABASE
210 size_t registry_db_load(void) {
211 char *s, buf[4096 + 1];
212 REGISTRY_PERSON *p = NULL;
213 REGISTRY_MACHINE *m = NULL;
214 REGISTRY_URL *u = NULL;
217 debug(D_REGISTRY, "Registry: loading active db from: '%s'", registry.db_filename);
218 FILE *fp = fopen(registry.db_filename, "r");
220 error("Registry: cannot open registry file: '%s'", registry.db_filename);
226 while((s = fgets_trim_len(buf, 4096, fp, &len))) {
229 debug(D_REGISTRY, "Registry: read line %zu to length %zu: %s", line, len, s);
232 if(unlikely(len != 103 || s[1] != '\t' || s[18] != '\t' || s[35] != '\t' || s[52] != '\t' || s[69] != '\t' || s[86] != '\t' || s[103] != '\0')) {
233 error("Registry totals line %zu is wrong (len = %zu).", line, len);
236 registry.persons_count = strtoull(&s[2], NULL, 16);
237 registry.machines_count = strtoull(&s[19], NULL, 16);
238 registry.usages_count = strtoull(&s[36], NULL, 16);
239 registry.urls_count = strtoull(&s[53], NULL, 16);
240 registry.persons_urls_count = strtoull(&s[70], NULL, 16);
241 registry.machines_urls_count = strtoull(&s[87], NULL, 16);
246 // verify it is valid
247 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
248 error("Registry person line %zu is wrong (len = %zu).", line, len);
252 s[1] = s[10] = s[19] = s[28] = '\0';
253 p = registry_person_allocate(&s[29], strtoul(&s[2], NULL, 16));
254 p->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
255 p->usages = (uint32_t)strtoul(&s[20], NULL, 16);
256 debug(D_REGISTRY, "Registry loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
261 // verify it is valid
262 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
263 error("Registry person line %zu is wrong (len = %zu).", line, len);
267 s[1] = s[10] = s[19] = s[28] = '\0';
268 m = registry_machine_allocate(&s[29], strtoul(&s[2], NULL, 16));
269 m->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
270 m->usages = (uint32_t)strtoul(&s[20], NULL, 16);
271 debug(D_REGISTRY, "Registry loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
274 case 'U': // person URL
276 error("Registry: ignoring line %zu, no person loaded: %s", line, s);
280 // verify it is valid
281 if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t') {
282 error("Registry person URL line %zu is wrong (len = %zu).", line, len);
286 s[1] = s[10] = s[19] = s[28] = s[31] = s[68] = '\0';
288 // skip the name to find the url
290 while(*url && *url != '\t') url++;
292 error("Registry person URL line %zu does not have a url.", line);
297 // u = registry_url_allocate_nolock(url, strlen(url));
298 u = registry_url_get(url, strlen(url));
300 time_t first_t = strtoul(&s[2], NULL, 16);
302 m = registry_machine_find(&s[32]);
303 if(!m) m = registry_machine_allocate(&s[32], first_t);
305 REGISTRY_PERSON_URL *pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
306 pu->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
307 pu->usages = (uint32_t)strtoul(&s[20], NULL, 16);
308 pu->flags = (uint8_t)strtoul(&s[29], NULL, 16);
309 debug(D_REGISTRY, "Registry loaded person URL '%s' with name '%s' of machine '%s', first: %u, last: %u, usages: %u, flags: %02x", u->url, pu->machine_name, m->guid, pu->first_t, pu->last_t, pu->usages, pu->flags);
312 case 'V': // machine URL
314 error("Registry: ignoring line %zu, no machine loaded: %s", line, s);
318 // verify it is valid
319 if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t') {
320 error("Registry person URL line %zu is wrong (len = %zu).", line, len);
324 s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
325 // u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
326 u = registry_url_get(&s[32], strlen(&s[32]));
328 REGISTRY_MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
329 mu->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
330 mu->usages = (uint32_t)strtoul(&s[20], NULL, 16);
331 mu->flags = (uint8_t)strtoul(&s[29], NULL, 16);
332 debug(D_REGISTRY, "Registry loaded machine URL '%s', machine '%s', first: %u, last: %u, usages: %u, flags: %02x", u->url, m->guid, mu->first_t, mu->last_t, mu->usages, mu->flags);
336 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);