registry_machine.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "daemon/common.h"
  3. #include "registry_internals.h"
  4. // ----------------------------------------------------------------------------
  5. // MACHINE
  6. REGISTRY_MACHINE *registry_machine_find(const char *machine_guid) {
  7. debug(D_REGISTRY, "Registry: registry_machine_find('%s')", machine_guid);
  8. return dictionary_get(registry.machines, machine_guid);
  9. }
  10. REGISTRY_MACHINE_URL *registry_machine_url_allocate(REGISTRY_MACHINE *m, REGISTRY_URL *u, time_t when) {
  11. debug(D_REGISTRY, "registry_machine_url_allocate('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(REGISTRY_MACHINE_URL));
  12. REGISTRY_MACHINE_URL *mu = mallocz(sizeof(REGISTRY_MACHINE_URL));
  13. mu->first_t = mu->last_t = (uint32_t)when;
  14. mu->usages = 1;
  15. mu->url = u;
  16. mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
  17. registry.machines_urls_memory += sizeof(REGISTRY_MACHINE_URL);
  18. debug(D_REGISTRY, "registry_machine_url_allocate('%s', '%s'): indexing URL in machine", m->guid, u->url);
  19. registry.machines_urls_memory -= dictionary_stats_for_registry(m->machine_urls);
  20. dictionary_set(m->machine_urls, u->url, mu, sizeof(REGISTRY_MACHINE_URL));
  21. registry.machines_urls_memory += dictionary_stats_for_registry(m->machine_urls);
  22. registry_url_link(u);
  23. return mu;
  24. }
  25. REGISTRY_MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
  26. debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(REGISTRY_MACHINE));
  27. REGISTRY_MACHINE *m = mallocz(sizeof(REGISTRY_MACHINE));
  28. strncpyz(m->guid, machine_guid, GUID_LEN);
  29. debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
  30. m->machine_urls = dictionary_create(REGISTRY_DICTIONARY_OPTIONS);
  31. m->first_t = m->last_t = (uint32_t)when;
  32. m->usages = 0;
  33. registry.machines_memory += sizeof(REGISTRY_MACHINE);
  34. registry.machines_count++;
  35. registry.machines_urls_memory -= dictionary_stats_for_registry(m->machine_urls);
  36. dictionary_set(registry.machines, m->guid, m, sizeof(REGISTRY_MACHINE));
  37. registry.machines_urls_memory += dictionary_stats_for_registry(m->machine_urls);
  38. return m;
  39. }
  40. // 1. validate machine GUID
  41. // 2. if it is valid, find it or create it and return it
  42. // 3. if it is not valid, return NULL
  43. REGISTRY_MACHINE *registry_machine_get(const char *machine_guid, time_t when) {
  44. REGISTRY_MACHINE *m = NULL;
  45. if(likely(machine_guid && *machine_guid)) {
  46. // validate it is a GUID
  47. char buf[GUID_LEN + 1];
  48. if(unlikely(regenerate_guid(machine_guid, buf) == -1))
  49. info("Registry: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid);
  50. else {
  51. machine_guid = buf;
  52. m = registry_machine_find(machine_guid);
  53. if(!m) m = registry_machine_allocate(machine_guid, when);
  54. }
  55. }
  56. return m;
  57. }
  58. // ----------------------------------------------------------------------------
  59. // LINKING OF OBJECTS
  60. REGISTRY_MACHINE_URL *registry_machine_link_to_url(REGISTRY_MACHINE *m, REGISTRY_URL *u, time_t when) {
  61. debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): searching for URL in machine", m->guid, u->url);
  62. REGISTRY_MACHINE_URL *mu = dictionary_get(m->machine_urls, u->url);
  63. if(!mu) {
  64. debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): not found", m->guid, u->url);
  65. mu = registry_machine_url_allocate(m, u, when);
  66. registry.machines_urls_count++;
  67. }
  68. else {
  69. debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): found", m->guid, u->url);
  70. mu->usages++;
  71. if(likely(mu->last_t < (uint32_t)when)) mu->last_t = (uint32_t)when;
  72. }
  73. m->usages++;
  74. if(likely(m->last_t < (uint32_t)when)) m->last_t = (uint32_t)when;
  75. if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
  76. debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): accessing an expired URL.", m->guid, u->url);
  77. mu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
  78. }
  79. return mu;
  80. }