registry_machine.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. dictionary_set(m->machine_urls, u->url, mu, sizeof(REGISTRY_MACHINE_URL));
  20. registry_url_link(u);
  21. return mu;
  22. }
  23. REGISTRY_MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
  24. debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(REGISTRY_MACHINE));
  25. REGISTRY_MACHINE *m = mallocz(sizeof(REGISTRY_MACHINE));
  26. strncpyz(m->guid, machine_guid, GUID_LEN);
  27. debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
  28. m->machine_urls = dictionary_create(DICTIONARY_FLAGS);
  29. m->first_t = m->last_t = (uint32_t)when;
  30. m->usages = 0;
  31. registry.machines_memory += sizeof(REGISTRY_MACHINE);
  32. registry.machines_count++;
  33. dictionary_set(registry.machines, m->guid, m, sizeof(REGISTRY_MACHINE));
  34. return m;
  35. }
  36. // 1. validate machine GUID
  37. // 2. if it is valid, find it or create it and return it
  38. // 3. if it is not valid, return NULL
  39. REGISTRY_MACHINE *registry_machine_get(const char *machine_guid, time_t when) {
  40. REGISTRY_MACHINE *m = NULL;
  41. if(likely(machine_guid && *machine_guid)) {
  42. // validate it is a GUID
  43. char buf[GUID_LEN + 1];
  44. if(unlikely(regenerate_guid(machine_guid, buf) == -1))
  45. info("Registry: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid);
  46. else {
  47. machine_guid = buf;
  48. m = registry_machine_find(machine_guid);
  49. if(!m) m = registry_machine_allocate(machine_guid, when);
  50. }
  51. }
  52. return m;
  53. }
  54. // ----------------------------------------------------------------------------
  55. // LINKING OF OBJECTS
  56. REGISTRY_MACHINE_URL *registry_machine_link_to_url(REGISTRY_MACHINE *m, REGISTRY_URL *u, time_t when) {
  57. debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): searching for URL in machine", m->guid, u->url);
  58. REGISTRY_MACHINE_URL *mu = dictionary_get(m->machine_urls, u->url);
  59. if(!mu) {
  60. debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): not found", m->guid, u->url);
  61. mu = registry_machine_url_allocate(m, u, when);
  62. registry.machines_urls_count++;
  63. }
  64. else {
  65. debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): found", m->guid, u->url);
  66. mu->usages++;
  67. if(likely(mu->last_t < (uint32_t)when)) mu->last_t = (uint32_t)when;
  68. }
  69. m->usages++;
  70. if(likely(m->last_t < (uint32_t)when)) m->last_t = (uint32_t)when;
  71. if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
  72. debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): accessing an expired URL.", m->guid, u->url);
  73. mu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
  74. }
  75. return mu;
  76. }