crun icon indicating copy to clipboard operation
crun copied to clipboard

libcrun: add crun_error_get_errno

Open eriksjolund opened this issue 10 months ago • 3 comments

Summary by Sourcery

Improve error handling by extracting and restoring original errno from libcrun error objects across cgroup setup, directory creation, and filesystem path operations.

Enhancements:

  • Use crun_error_get_errno to retrieve and restore the original errno when cgroup directory creation fails.
  • Preserve and reuse the saved errno in the directory creation helper before reporting errors.
  • Restore errno from libcrun error objects in the masked path setup to ensure consistent error evaluation.

eriksjolund avatar Jun 17 '25 06:06 eriksjolund

Reviewer's Guide

This PR enhances error handling by extracting and restoring the original errno from libcrun error objects in cgroup setup, directory creation, and masked path operations, ensuring that subsequent error checks and reports use the accurate errno value.

Sequence diagram for error handling in cgroup directory creation

sequenceDiagram
    participant enter_cgroup_subsystem
    participant crun_ensure_directory
    participant crun_error_get_errno
    participant crun_make_error
    participant crun_error_release
    enter_cgroup_subsystem->>crun_ensure_directory: create cgroup directory
    crun_ensure_directory-->>enter_cgroup_subsystem: ret < 0, err set
    enter_cgroup_subsystem->>crun_error_get_errno: get errno from err
    crun_error_get_errno-->>enter_cgroup_subsystem: errno
    alt errno != EROFS
        enter_cgroup_subsystem->>crun_make_error: report error with errno
        crun_make_error-->>enter_cgroup_subsystem: error code
    else errno == EROFS
        enter_cgroup_subsystem->>crun_error_release: release err
        crun_error_release-->>enter_cgroup_subsystem: (void)
        enter_cgroup_subsystem-->>enter_cgroup_subsystem: return 0
    end

Sequence diagram for error handling in ensure_directory_internal_at

sequenceDiagram
    participant ensure_directory_internal_at
    participant crun_make_error
    participant crun_error_release
    ensure_directory_internal_at->>ensure_directory_internal_at: mkdir fails
    ensure_directory_internal_at->>ensure_directory_internal_at: save errno as saved_errno
    alt parent_created or errno != ENOENT
        ensure_directory_internal_at->>crun_make_error: report error with saved_errno
        crun_make_error-->>ensure_directory_internal_at: error code
    else
        ensure_directory_internal_at-->>ensure_directory_internal_at: continue to create parent directories
    end

Sequence diagram for error handling in do_masked_or_readonly_path

sequenceDiagram
    participant do_masked_or_readonly_path
    participant safe_openat
    participant crun_error_get_errno
    do_masked_or_readonly_path->>safe_openat: open rel_path
    safe_openat-->>do_masked_or_readonly_path: pathfd < 0, err set
    do_masked_or_readonly_path->>crun_error_get_errno: get errno from err
    crun_error_get_errno-->>do_masked_or_readonly_path: errno
    alt errno != ENOENT and errno != EACCES
        do_masked_or_readonly_path-->>do_masked_or_readonly_path: return pathfd (error)
    else
        do_masked_or_readonly_path-->>do_masked_or_readonly_path: continue
    end

Class diagram for enhanced error handling with crun_error_get_errno

classDiagram
    class libcrun_error_t {
    }
    class crun_error_get_errno {
        +int crun_error_get_errno(libcrun_error_t *err)
    }
    class crun_ensure_directory {
        +int crun_ensure_directory(const char *path, int mode, bool, libcrun_error_t **err)
    }
    class crun_make_error {
        +int crun_make_error(libcrun_error_t **err, int errno, const char *fmt, ...)
    }
    class crun_error_release {
        +void crun_error_release(libcrun_error_t **err)
    }
    crun_error_get_errno --|> libcrun_error_t
    crun_ensure_directory --> libcrun_error_t
    crun_make_error --> libcrun_error_t
    crun_error_release --> libcrun_error_t

File-Level Changes

Change Details Files
Restore original errno before returning from cgroup directory creation failures
  • Invoke crun_error_get_errno on the error object
  • Assign restored errno to errno and check for EROFS
  • Return the original ret value when not EROFS
src/libcrun/cgroup-setup.c
Preserve and reuse errno in the directory creation helper
  • Save errno to a local variable before further processing
  • Use saved_errno when calling crun_make_error for directory creation failures
src/libcrun/utils.c
Set errno from libcrun errors in masked path setup
  • Assign errno from crun_error_get_errno(err) on path open failures
  • Use the restored errno to determine if the error should be propagated
src/libcrun/linux.c

Possibly linked issues

  • #123: PR fixes error handling where negative values were mishandled by preserving and restoring errno.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an issue from a review comment by replying to it. You can also reply to a review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull request title to generate a title at any time. You can also comment @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment @sourcery-ai summary on the pull request to (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Jun 17 '25 06:06 sourcery-ai[bot]

Ephemeral COPR build failed. @containers/packit-build please check.

TMT tests failed. @containers/packit-build please check.

still a WIP?

giuseppe avatar Jun 17 '25 12:06 giuseppe

still a WIP?

it's ready

eriksjolund avatar Jun 17 '25 15:06 eriksjolund

Ephemeral COPR build failed. @containers/packit-build please check.

TMT tests failed. @containers/packit-build please check.