-
Notifications
You must be signed in to change notification settings - Fork 363
Refactor some complex functions #1832
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
Conversation
Reviewer's GuideThis PR refactors large and complex functions in container.c and linux.c by extracting logical steps into dedicated static helpers, replaces manual memory allocation and OOM handling with a unified xmalloc0 wrapper, and modularizes mount operations for easier comprehension and maintenance. Class diagram for refactored container initialization helpersclassDiagram
class container_entrypoint_s
class libcrun_container_t
class runtime_spec_schema_config_schema
class libcrun_error_t
class container_init_setup {
+int container_init_setup(void *args, pid_t own_pid, char *notify_socket, int sync_socket, char **exec_path, libcrun_error_t *err)
}
class resolve_rootfs_path {
+int resolve_rootfs_path(runtime_spec_schema_config_schema *def, char **rootfs, libcrun_error_t *err)
}
class setup_terminal_socketpair {
+int setup_terminal_socketpair(struct container_entrypoint_s *entrypoint_args, int *console_socketpair)
}
class setup_environment {
+int setup_environment(runtime_spec_schema_config_schema *def, uid_t container_uid, libcrun_error_t *err)
}
class setup_terminal {
+int setup_terminal(struct container_entrypoint_s *entrypoint_args, libcrun_container_t *container, int console_socket, int console_socketpair, int has_terminal, libcrun_error_t *err)
}
class setup_executable_path {
+int setup_executable_path(struct container_entrypoint_s *entrypoint_args, runtime_spec_schema_config_schema *def, char **exec_path, libcrun_error_t *err)
}
class apply_security_settings {
+int apply_security_settings(struct container_entrypoint_s *entrypoint_args, runtime_spec_schema_config_schema *def, libcrun_container_t *container, pid_t own_pid, libcrun_error_t *err)
}
container_init_setup --> resolve_rootfs_path
container_init_setup --> setup_terminal_socketpair
container_init_setup --> setup_environment
container_init_setup --> setup_terminal
container_init_setup --> setup_executable_path
container_init_setup --> apply_security_settings
Class diagram for modularized mount operations in linux.cclassDiagram
class do_mounts {
+int do_mounts(libcrun_container_t *container, const char *rootfs, libcrun_error_t *err)
}
class process_single_mount {
+int process_single_mount(libcrun_container_t *container, const char *rootfs, runtime_spec_schema_defs_mount *mount, struct libcrun_fd_map *mount_fds, size_t mount_index, const char *systemd_cgroup_v1, libcrun_error_t *err)
}
class handle_copy_symlink {
+int handle_copy_symlink(libcrun_container_t *container, const char *rootfs, runtime_spec_schema_defs_mount *mount, libcrun_error_t *err)
}
class prepare_sysfs_or_proc_mount {
+int prepare_sysfs_or_proc_mount(libcrun_container_t *container, const char *target, int *targetfd, libcrun_error_t *err)
}
class handle_tmpcopyup {
+int handle_tmpcopyup(libcrun_container_t *container, const char *rootfs, const char *target, int copy_from_fd, libcrun_error_t *err)
}
class get_mount_label_how {
+int get_mount_label_how(const char *type, bool is_sysfs_or_proc)
}
do_mounts --> process_single_mount
process_single_mount --> handle_copy_symlink
process_single_mount --> prepare_sysfs_or_proc_mount
process_single_mount --> handle_tmpcopyup
process_single_mount --> get_mount_label_how
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @giuseppe - I've reviewed your changes - here's some feedback:
- The new setup_* helper functions in container.c could be moved into a dedicated initialization module to keep container.c from growing too large and improve readability.
- In resolve_rootfs_path, consider explicitly handling or documenting the case where def->root is NULL so that callers always know whether *rootfs is set or must be defaulted.
- process_single_mount remains quite long and covers multiple distinct cases; splitting bind, tmpcopyup, and sysfs/proc logic into separate smaller functions would reduce complexity and improve maintainability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new setup_* helper functions in container.c could be moved into a dedicated initialization module to keep container.c from growing too large and improve readability.
- In resolve_rootfs_path, consider explicitly handling or documenting the case where def->root is NULL so that callers always know whether *rootfs is set or must be defaulted.
- process_single_mount remains quite long and covers multiple distinct cases; splitting bind, tmpcopyup, and sysfs/proc logic into separate smaller functions would reduce complexity and improve maintainability.
## Individual Comments
### Comment 1
<location> `src/libcrun/container.c:1372` </location>
<code_context>
- close_and_reset (&console_socketpair);
- }
- }
+ ret = setup_terminal (entrypoint_args, container, console_socket, console_socketpair, has_terminal, err);
+ if (UNLIKELY (ret < 0))
+ return ret;
</code_context>
<issue_to_address>
setup_terminal does not close console_socket or console_socketpair on error.
Ensure that console_socket and console_socketpair are closed if setup_terminal fails to prevent resource leaks. Clarify ownership if necessary.
</issue_to_address>
### Comment 2
<location> `src/libcrun/linux.c:2277` </location>
<code_context>
}
+ }
- if (extra_flags & OPTION_TMPCOPYUP)
- {
- if (strcmp (type, "tmpfs") != 0)
</code_context>
<issue_to_address>
handle_tmpcopyup may not handle ENOTDIR for non-directories.
Please verify if ignoring ENOTDIR is appropriate for all mount types when OPTION_TMPCOPYUP is set, or if further validation should be added.
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
close_and_reset (&console_socketpair); | ||
} | ||
} | ||
ret = setup_terminal (entrypoint_args, container, console_socket, console_socketpair, has_terminal, err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): setup_terminal does not close console_socket or console_socketpair on error.
Ensure that console_socket and console_socketpair are closed if setup_terminal fails to prevent resource leaks. Clarify ownership if necessary.
ret = safe_create_symlink (get_private_data (container)->rootfsfd, rootfs, target, def->mounts[i]->destination, err); | ||
if (S_ISLNK (src_mode) && (extra_flags & OPTION_DEST_NOFOLLOW) && source_mountfd < 0) | ||
{ | ||
ret = get_bind_mount (AT_FDCWD, mount->source, true, true, extra_flags & OPTION_SRC_NOFOLLOW, err); | ||
if (UNLIKELY (ret < 0)) | ||
return ret; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: handle_tmpcopyup may not handle ENOTDIR for non-directories.
Please verify if ignoring ENOTDIR is appropriate for all mount types when OPTION_TMPCOPYUP is set, or if further validation should be added.
@kolyshkin @flouthoc PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restarted failing test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only left one nit for the first commit, need more time to review the rest as it's lots of changes without much description. Will take a closer look tomorrow.
Also, from the quick glance it seems that commit 4 ("container: do not call libcrun_set_io_priority twice") needs to be before commit 3 ("container: refactor libcrun_container_run_internal"), unless it fixes an issue in commit 3, in which case these two may better be squashed?
src/libcrun/container.c
Outdated
*out = malloc (buf_len + 1); | ||
if (*out == NULL) | ||
OOM (); | ||
*out = xmalloc0 (buf_len + 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: shouldn't this be xmalloc
not xmalloc0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that is better. Fixed now
Signed-off-by: Giuseppe Scrivano <[email protected]>
Signed-off-by: Giuseppe Scrivano <[email protected]>
Signed-off-by: Giuseppe Scrivano <[email protected]>
Signed-off-by: Giuseppe Scrivano <[email protected]>
d1e91d1
to
e76c745
Compare
I don't think we even need it, it was a mistake and I've dropped the patch from the PR. Thanks for noticing it! |
Ephemeral COPR build failed. @containers/packit-build please check. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
More details in each commit.
🤖 Assisted by: https://claude.ai/code
Summary by Sourcery
Refactor and modularize core container and mount logic by extracting large monolithic functions into smaller helper routines, improving readability and maintainability.
Enhancements: