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

refactor(userspace): re-implement plugin loader in C and split it in its own package #392

Merged

Conversation

jasondellaluce
Copy link
Contributor

@jasondellaluce jasondellaluce commented Jun 13, 2022

What type of PR is this?

/kind cleanup

/kind design

Any specific area of the project related to this PR?

/area libscap-engine-source-plugin

/area libsinsp

What this PR does / why we need it:

Which issue(s) this PR fixes:

This PR refactors the plugin definitions and loading logic. The plugin types, definitions, and loading logic are now all under an ad-hoc userspace/plugin directory on which both libscap and libsinsp dependencies. The goals and benefits are:

  • Better separate responsibilities: So far the plugin definitions were scattered across libsinsp and libscap, and all their definitions were placed inside the libscap's `plugin_info.h"
  • Logic is all in the same place: The plugin types and the plugin loader are now resident in a single code location, which is going to become the only reference for the official plugin system definitions. The class-based libsinsp code responsible of managing plugins now wraps around the new C definitions of userspace/plugin
  • Split plugin-related code: The old plugin_info.h file has been split into plugin_types.h and plugin_api.h, so that consumers can only import the definitions they need
  • Reusability in external projects: The new design allows the official plugin definitions and loader to be re-used in other external projects. An example in which this could be required is the Plugin SDK Go, that can potentially implement a plugin loader written in Go based on top of the official definition of libs. Automated tools, such as the upcoming plugin distribution system in falcoctl, could be built on top of this code.
  • Bug fixes in plugin loader: The old plugin loader logic that was bundled in libsinsp contained some flaws. For instance, it considered symbols such as plugin_init or plugin_destroy as optional. The new plugin loader also prompts more explicit error messages, such as when the semver checks fail.

Special notes for your reviewer:

~400 LOC are caused by moving plugin_info.h and splitting it into plugin/plugin_types.h and plugin/plugin_api.h.

Does this PR introduce a user-facing change?:

NONE

@jasondellaluce
Copy link
Contributor Author

falcosecurity/plugins#121 is a good example where the plugin loader code might be needed in an external project and must not be replicated to guarantee consistency.

Copy link
Contributor

@Molter73 Molter73 left a comment

Choose a reason for hiding this comment

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

I'm not super familiar with the plugins system, but the change looks cool! I left a few comments on the C code, they are mostly nitpicks, so don't worry too much about them.

userspace/plugin/plugin_loader.c Outdated Show resolved Hide resolved
userspace/plugin/plugin_loader.c Outdated Show resolved Hide resolved
userspace/plugin/plugin_loader.c Outdated Show resolved Hide resolved
userspace/plugin/plugin_loader.c Outdated Show resolved Hide resolved
if(a->api.s == NULL) \
{ \
strcpy(e, "symbol not implemented: "); \
strncat(e, #s, PLUGIN_MAX_ERRLEN - 1); \
Copy link
Contributor

Choose a reason for hiding this comment

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

Here we are not considering "symbol not implemented: " length when accounting for max size.
I don't think this will ever be an issue, still it would be great to deal with it given that we are using strncat :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just changed this thanks to the suggestions of @Molter73!

Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

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

This looks so cool!
Thank you!
Did you consider adding a library around it?

@jasondellaluce
Copy link
Contributor Author

This looks so cool! Thank you! Did you consider adding a library around it?

Thanks Fede! You mean building a .a lib out of this? This can be an idea! I didn't want to impact the CMake setup in this first PR (also to avoid interfering with the huge ongoing libscap refactoring), but I think this can be a good idea.

@jasondellaluce
Copy link
Contributor Author

/retest

@jasondellaluce
Copy link
Contributor Author

Thanks @Molter73 for the deep review! I appreciate it 🙏🏼

leogr
leogr previously approved these changes Jun 17, 2022
Copy link
Member

@leogr leogr left a comment

Choose a reason for hiding this comment

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

This is cool.

👍 for me :)

@poiana
Copy link
Contributor

poiana commented Jun 17, 2022

LGTM label has been added.

Git tree hash: ed493b873af12985ceeeb97ee45b6bdf4e2d5980

…ide of sinsp and scap

Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
…l directory

Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
jasondellaluce and others added 6 commits July 13, 2022 10:36
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>

Co-authored-by: Mauro Ezequiel Moltrasio <moltrasiom@hotmail.com>
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
Co-authored-by: Mauro Ezequiel Moltrasio <moltrasiom@hotmail.com>
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
Co-authored-by: Mauro Ezequiel Moltrasio <moltrasiom@hotmail.com>
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
@jasondellaluce
Copy link
Contributor Author

/retest

Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

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

/approve

@poiana
Copy link
Contributor

poiana commented Jul 14, 2022

LGTM label has been added.

Git tree hash: 5ea9263eaee5d65b445c072761767e003bea50fc

@poiana
Copy link
Contributor

poiana commented Jul 14, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: FedeDP, jasondellaluce

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Member

@leogr leogr left a comment

Choose a reason for hiding this comment

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

Again 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants