registry_db.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "daemon/common.h"
  3. #include "registry_internals.h"
  4. int registry_db_should_be_saved(void) {
  5. debug(D_REGISTRY, "log entries %llu, max %llu", registry.log_count, registry.save_registry_every_entries);
  6. return registry.log_count > registry.save_registry_every_entries;
  7. }
  8. // ----------------------------------------------------------------------------
  9. // INTERNAL FUNCTIONS FOR SAVING REGISTRY OBJECTS
  10. static int registry_machine_save_url(const char *name, void *entry, void *file) {
  11. (void)name;
  12. REGISTRY_MACHINE_URL *mu = entry;
  13. FILE *fp = file;
  14. debug(D_REGISTRY, "Registry: registry_machine_save_url('%s')", mu->url->url);
  15. int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
  16. mu->first_t,
  17. mu->last_t,
  18. mu->usages,
  19. mu->flags,
  20. mu->url->url
  21. );
  22. // error handling is done at registry_db_save()
  23. return ret;
  24. }
  25. static int registry_machine_save(const char *name, void *entry, void *file) {
  26. (void)name;
  27. REGISTRY_MACHINE *m = entry;
  28. FILE *fp = file;
  29. debug(D_REGISTRY, "Registry: registry_machine_save('%s')", m->guid);
  30. int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
  31. m->first_t,
  32. m->last_t,
  33. m->usages,
  34. m->guid
  35. );
  36. if(ret >= 0) {
  37. int ret2 = dictionary_walkthrough_read(m->machine_urls, registry_machine_save_url, fp);
  38. if(ret2 < 0) return ret2;
  39. ret += ret2;
  40. }
  41. // error handling is done at registry_db_save()
  42. return ret;
  43. }
  44. static inline int registry_person_save_url(void *entry, void *file) {
  45. REGISTRY_PERSON_URL *pu = entry;
  46. FILE *fp = file;
  47. debug(D_REGISTRY, "Registry: registry_person_save_url('%s')", pu->url->url);
  48. int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\t%s\n",
  49. pu->first_t,
  50. pu->last_t,
  51. pu->usages,
  52. pu->flags,
  53. pu->machine->guid,
  54. pu->machine_name,
  55. pu->url->url
  56. );
  57. // error handling is done at registry_db_save()
  58. return ret;
  59. }
  60. static inline int registry_person_save(const char *name, void *entry, void *file) {
  61. (void)name;
  62. REGISTRY_PERSON *p = entry;
  63. FILE *fp = file;
  64. debug(D_REGISTRY, "Registry: registry_person_save('%s')", p->guid);
  65. int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
  66. p->first_t,
  67. p->last_t,
  68. p->usages,
  69. p->guid
  70. );
  71. if(ret >= 0) {
  72. //int ret2 = dictionary_walkthrough_read(p->person_urls, registry_person_save_url, fp);
  73. int ret2 = avl_traverse(&p->person_urls, registry_person_save_url, fp);
  74. if (ret2 < 0) return ret2;
  75. ret += ret2;
  76. }
  77. // error handling is done at registry_db_save()
  78. return ret;
  79. }
  80. // ----------------------------------------------------------------------------
  81. // SAVE THE REGISTRY DATABASE
  82. int registry_db_save(void) {
  83. if(unlikely(!registry.enabled))
  84. return -1;
  85. if(unlikely(!registry_db_should_be_saved()))
  86. return -2;
  87. error_log_limit_unlimited();
  88. char tmp_filename[FILENAME_MAX + 1];
  89. char old_filename[FILENAME_MAX + 1];
  90. snprintfz(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
  91. snprintfz(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
  92. debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
  93. FILE *fp = fopen(tmp_filename, "w");
  94. if(!fp) {
  95. error("Registry: Cannot create file: %s", tmp_filename);
  96. error_log_limit_reset();
  97. return -1;
  98. }
  99. // dictionary_walkthrough_read() has its own locking, so this is safe to do
  100. debug(D_REGISTRY, "Saving all machines");
  101. int bytes1 = dictionary_walkthrough_read(registry.machines, registry_machine_save, fp);
  102. if(bytes1 < 0) {
  103. error("Registry: Cannot save registry machines - return value %d", bytes1);
  104. fclose(fp);
  105. error_log_limit_reset();
  106. return bytes1;
  107. }
  108. debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
  109. debug(D_REGISTRY, "Saving all persons");
  110. int bytes2 = dictionary_walkthrough_read(registry.persons, registry_person_save, fp);
  111. if(bytes2 < 0) {
  112. error("Registry: Cannot save registry persons - return value %d", bytes2);
  113. fclose(fp);
  114. error_log_limit_reset();
  115. return bytes2;
  116. }
  117. debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
  118. // save the totals
  119. fprintf(fp, "T\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\n",
  120. registry.persons_count,
  121. registry.machines_count,
  122. registry.usages_count + 1, // this is required - it is lost on db rotation
  123. registry.urls_count,
  124. registry.persons_urls_count,
  125. registry.machines_urls_count
  126. );
  127. fclose(fp);
  128. errno = 0;
  129. // remove the .old db
  130. debug(D_REGISTRY, "Registry: Removing old db '%s'", old_filename);
  131. if(unlink(old_filename) == -1 && errno != ENOENT)
  132. error("Registry: cannot remove old registry file '%s'", old_filename);
  133. // rename the db to .old
  134. debug(D_REGISTRY, "Registry: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
  135. if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
  136. error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", registry.db_filename, old_filename);
  137. else {
  138. // remove the database (it is saved in .old)
  139. debug(D_REGISTRY, "Registry: removing db '%s'", registry.db_filename);
  140. if (unlink(registry.db_filename) == -1 && errno != ENOENT)
  141. error("Registry: cannot remove old registry file '%s'", registry.db_filename);
  142. // move the .tmp to make it active
  143. debug(D_REGISTRY, "Registry: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
  144. if (link(tmp_filename, registry.db_filename) == -1) {
  145. error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
  146. registry.db_filename);
  147. // move the .old back
  148. debug(D_REGISTRY, "Registry: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
  149. if(link(old_filename, registry.db_filename) == -1)
  150. error("Registry: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
  151. }
  152. else {
  153. debug(D_REGISTRY, "Registry: removing tmp db '%s'", tmp_filename);
  154. if(unlink(tmp_filename) == -1)
  155. error("Registry: cannot remove tmp registry file '%s'", tmp_filename);
  156. // it has been moved successfully
  157. // discard the current registry log
  158. registry_log_recreate();
  159. registry.log_count = 0;
  160. }
  161. }
  162. // continue operations
  163. error_log_limit_reset();
  164. return -1;
  165. }
  166. // ----------------------------------------------------------------------------
  167. // LOAD THE REGISTRY DATABASE
  168. size_t registry_db_load(void) {
  169. char *s, buf[4096 + 1];
  170. REGISTRY_PERSON *p = NULL;
  171. REGISTRY_MACHINE *m = NULL;
  172. REGISTRY_URL *u = NULL;
  173. size_t line = 0;
  174. debug(D_REGISTRY, "Registry: loading active db from: '%s'", registry.db_filename);
  175. FILE *fp = fopen(registry.db_filename, "r");
  176. if(!fp) {
  177. error("Registry: cannot open registry file: '%s'", registry.db_filename);
  178. return 0;
  179. }
  180. size_t len = 0;
  181. buf[4096] = '\0';
  182. while((s = fgets_trim_len(buf, 4096, fp, &len))) {
  183. line++;
  184. debug(D_REGISTRY, "Registry: read line %zu to length %zu: %s", line, len, s);
  185. switch(*s) {
  186. case 'T': // totals
  187. 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')) {
  188. error("Registry totals line %zu is wrong (len = %zu).", line, len);
  189. continue;
  190. }
  191. registry.persons_count = strtoull(&s[2], NULL, 16);
  192. registry.machines_count = strtoull(&s[19], NULL, 16);
  193. registry.usages_count = strtoull(&s[36], NULL, 16);
  194. registry.urls_count = strtoull(&s[53], NULL, 16);
  195. registry.persons_urls_count = strtoull(&s[70], NULL, 16);
  196. registry.machines_urls_count = strtoull(&s[87], NULL, 16);
  197. break;
  198. case 'P': // person
  199. m = NULL;
  200. // verify it is valid
  201. if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
  202. error("Registry person line %zu is wrong (len = %zu).", line, len);
  203. continue;
  204. }
  205. s[1] = s[10] = s[19] = s[28] = '\0';
  206. p = registry_person_allocate(&s[29], strtoul(&s[2], NULL, 16));
  207. p->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
  208. p->usages = (uint32_t)strtoul(&s[20], NULL, 16);
  209. debug(D_REGISTRY, "Registry loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
  210. break;
  211. case 'M': // machine
  212. p = NULL;
  213. // verify it is valid
  214. if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
  215. error("Registry person line %zu is wrong (len = %zu).", line, len);
  216. continue;
  217. }
  218. s[1] = s[10] = s[19] = s[28] = '\0';
  219. m = registry_machine_allocate(&s[29], strtoul(&s[2], NULL, 16));
  220. m->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
  221. m->usages = (uint32_t)strtoul(&s[20], NULL, 16);
  222. debug(D_REGISTRY, "Registry loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
  223. break;
  224. case 'U': // person URL
  225. if(unlikely(!p)) {
  226. error("Registry: ignoring line %zu, no person loaded: %s", line, s);
  227. continue;
  228. }
  229. // verify it is valid
  230. if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t') {
  231. error("Registry person URL line %zu is wrong (len = %zu).", line, len);
  232. continue;
  233. }
  234. s[1] = s[10] = s[19] = s[28] = s[31] = s[68] = '\0';
  235. // skip the name to find the url
  236. char *url = &s[69];
  237. while(*url && *url != '\t') url++;
  238. if(!*url) {
  239. error("Registry person URL line %zu does not have a url.", line);
  240. continue;
  241. }
  242. *url++ = '\0';
  243. // u = registry_url_allocate_nolock(url, strlen(url));
  244. u = registry_url_get(url, strlen(url));
  245. time_t first_t = strtoul(&s[2], NULL, 16);
  246. m = registry_machine_find(&s[32]);
  247. if(!m) m = registry_machine_allocate(&s[32], first_t);
  248. REGISTRY_PERSON_URL *pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
  249. pu->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
  250. pu->usages = (uint32_t)strtoul(&s[20], NULL, 16);
  251. pu->flags = (uint8_t)strtoul(&s[29], NULL, 16);
  252. 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);
  253. break;
  254. case 'V': // machine URL
  255. if(unlikely(!m)) {
  256. error("Registry: ignoring line %zu, no machine loaded: %s", line, s);
  257. continue;
  258. }
  259. // verify it is valid
  260. if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t') {
  261. error("Registry person URL line %zu is wrong (len = %zu).", line, len);
  262. continue;
  263. }
  264. s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
  265. // u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
  266. u = registry_url_get(&s[32], strlen(&s[32]));
  267. REGISTRY_MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
  268. mu->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
  269. mu->usages = (uint32_t)strtoul(&s[20], NULL, 16);
  270. mu->flags = (uint8_t)strtoul(&s[29], NULL, 16);
  271. 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);
  272. break;
  273. default:
  274. error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
  275. break;
  276. }
  277. }
  278. fclose(fp);
  279. return line;
  280. }