Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate: generate clone operations for deep-copy #135

Merged
merged 2 commits into from
Sep 5, 2024

Conversation

giuseppe
Copy link
Member

@giuseppe giuseppe commented Sep 5, 2024

No description provided.

@giuseppe
Copy link
Member Author

giuseppe commented Sep 5, 2024

with this in place we can simplify crun as (and not have to manually keep the code in sync):

diff --git a/src/libcrun/container.c b/src/libcrun/container.c
index be3c282..1bc20fb 100644
--- a/src/libcrun/container.c
+++ b/src/libcrun/container.c
@@ -554,31 +554,6 @@ make_container (runtime_spec_schema_config_schema *container_def, const char *pa
   return container;
 }
 
-runtime_spec_schema_config_schema_process_user *
-process_user_dup (const runtime_spec_schema_config_schema_process_user *const src)
-{
-  runtime_spec_schema_config_schema_process_user *const dst = xmalloc0 (sizeof (runtime_spec_schema_config_schema_process_user));
-
-  dst->uid = src->uid;
-  dst->uid_present = src->uid_present;
-  dst->gid = src->gid;
-  dst->gid_present = src->gid_present;
-  dst->umask = src->umask;
-  dst->umask_present = src->umask_present;
-
-  if (src->additional_gids)
-    {
-      dst->additional_gids_len = src->additional_gids_len;
-      const size_t additional_gids_size = src->additional_gids_len * sizeof (gid_t);
-      dst->additional_gids = xmalloc (additional_gids_size);
-      memcpy (dst->additional_gids, src->additional_gids, additional_gids_size);
-    }
-
-  dst->username = xstrdup (src->username);
-
-  return dst;
-}
-
 libcrun_container_t *
 libcrun_container_load_from_memory (const char *json, libcrun_error_t *err)
 {
@@ -3662,7 +3637,11 @@ libcrun_container_exec_with_options (libcrun_context_t *context, const char *id,
         process->apparmor_profile = xstrdup (container->container_def->process->apparmor_profile);
 
       if (process->user == NULL && container->container_def->process->user)
-        process->user = process_user_dup (container->container_def->process->user);
+        {
+          process->user = clone_runtime_spec_schema_config_schema_process_user (container->container_def->process->user);
+          if (process->user == NULL)
+            OOM ();
+        }
     }
 
   ret = initialize_security (process, err);

@bduffany FYI

@giuseppe
Copy link
Member Author

giuseppe commented Sep 5, 2024

for comparison, the generated code is:

runtime_spec_schema_config_schema_process_user *
clone_runtime_spec_schema_config_schema_process_user (runtime_spec_schema_config_schema_process_user *src)
{
    __auto_cleanup(free_runtime_spec_schema_config_schema_process_user) runtime_spec_schema_config_schema_process_user *ret = NULL;
    ret = calloc (1, sizeof (*ret));
    if (ret == NULL)
      return NULL;
    ret->uid = src->uid;
    ret->uid_present = src->uid_present;
    ret->gid = src->gid;
    ret->gid_present = src->gid_present;
    ret->umask = src->umask;
    ret->umask_present = src->umask_present;
    if (src->additional_gids)
      {
        ret->additional_gids_len = src->additional_gids_len;
        ret->additional_gids = calloc (src->additional_gids_len + 1, sizeof (*ret->additional_gids));
        if (ret->additional_gids == NULL)
          return NULL;
        for (size_t i = 0; i < src->additional_gids_len; i++)
          {
            ret->additional_gids[i] = src->additional_gids[i];
          }
      }
    if (src->username)
      {
        ret->username = strdup (src->username);
        if (ret->username == NULL)
          return NULL;
      }
    return move_ptr (ret);
}

Copy link
Collaborator

@flouthoc flouthoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@giuseppe giuseppe merged commit 27e4cd0 into containers:main Sep 5, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants