registry_db.c 13 KB

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