From 788d46d0718e65ae5da421f7a036e7b1e22d0e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Sat, 24 Feb 2024 02:59:22 +0100 Subject: [PATCH] Added functions to convert container ids to domain ids --- src/domain.c | 38 ++++++++++++++++++++++++++++++++++++++ src/domain.h | 15 +++++++++++++++ src/xml.c | 15 ++++++++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/domain.c create mode 100644 src/domain.h diff --git a/src/domain.c b/src/domain.c new file mode 100644 index 0000000..1fc2dbf --- /dev/null +++ b/src/domain.c @@ -0,0 +1,38 @@ +#include "domain.h" + +#include "sandbox.h" + +#include + +result_t container_to_domain(char** _domain, const char* container) { + // Initialize the output parameter + *_domain = NULL; + + // Convert the container identifier to a domain identifier + result_t result = format(_domain, "%s%s", DOMAIN_IDENTIFIER_PREFIX, container); + if (result != success()) + return result; + + return success(); +} + +result_t domain_to_container(char** _container, const char* domain) { + // Initialize the output parameter + *_container = NULL; + + // Check that the domain identifier is not too short + size_t prefix_length = strlen(DOMAIN_IDENTIFIER_PREFIX); + if (strlen(domain) <= prefix_length) + return failure("Invalid domain identifier. The domain identifier is too short. A domain identifier must be prefixed with '%s'.", DOMAIN_IDENTIFIER_PREFIX); + + // Check that the domain identifier has the correct prefix + if (strncmp(domain, DOMAIN_IDENTIFIER_PREFIX, prefix_length) != 0) + return failure("Invalid domain identifier. The domain identifier must be prefixed with '%s'.", DOMAIN_IDENTIFIER_PREFIX); + + // Convert the domain identifier to a container identifier + result_t result = format(_container, "%s", domain + prefix_length); + if (result != success()) + return result; + + return success(); +} diff --git a/src/domain.h b/src/domain.h new file mode 100644 index 0000000..eb6003b --- /dev/null +++ b/src/domain.h @@ -0,0 +1,15 @@ +#pragma once + +#include "utils.h" + +/// @brief Converts a container identifier to a domain identifier. +/// @param _domain The string pointer to store the resulting domain identifier in. The caller is responsible for freeing the string. +/// @param container The container identifier to convert. +/// @return The result of the operation. +result_t container_to_domain(char** _domain, const char* container); + +/// @brief Converts a domain identifier to a container identifier. +/// @param _container The string pointer to store the resulting container identifier in. The caller is responsible for freeing the string. +/// @param domain The domain identifier to convert. +/// @return The result of the operation. +result_t domain_to_container(char** _container, const char* domain); diff --git a/src/xml.c b/src/xml.c index 031ce52..b81e1e2 100644 --- a/src/xml.c +++ b/src/xml.c @@ -2,6 +2,7 @@ #include "pci.h" #include "container.h" +#include "domain.h" #include #include @@ -45,6 +46,17 @@ result_t generate_domain_xml(char** _xml, const char* container, uint64_t memory return result; } + // Get the domain identifier + char* domain; + result = container_to_domain(&domain, container); + if (result != success()) { + free(pci_xml); + free(iso_xml); + free(vnc_xml); + free(container_path); + return result; + } + // Generate the XML configuration for the domain char* xml; result = format(&xml, "\n" @@ -98,11 +110,12 @@ result_t generate_domain_xml(char** _xml, const char* container, uint64_t memory "\n" "\n" "\n", - container, memory, vcpus, container_path, pci_xml, iso_xml, vnc_xml); + domain, memory, vcpus, container_path, pci_xml, iso_xml, vnc_xml); free(pci_xml); free(iso_xml); free(vnc_xml); free(container_path); + free(domain); if (result != success()) return result;