-
Notifications
You must be signed in to change notification settings - Fork 201
ENT-14195 1. Refactored cf-reactor event handling with one unified interface #6353
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
Open
victormlg
wants to merge
1
commit into
cfengine:master
Choose a base branch
from
victormlg:event-handling-1-refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+228
−78
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| Copyright 2026 Northern.tech AS | ||
|
|
||
| This file is part of CFEngine 3 - written and maintained by Northern.tech AS. | ||
|
|
||
| This program is free software; you can redistribute it and/or modify it | ||
| under the terms of the GNU General Public License as published by the | ||
| Free Software Foundation; version 3. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program; if not, write to the Free Software | ||
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
|
|
||
| To the extent this program is licensed as part of the Enterprise | ||
| versions of CFEngine, the applicable Commercial Open Source License | ||
| (COSL) may apply to this file if you as a licensee so wish it. See | ||
| included file COSL.txt. | ||
| */ | ||
|
|
||
| #ifndef CFENGINE_REACTOR_H | ||
| #define CFENGINE_REACTOR_H | ||
|
|
||
| #include <platform.h> | ||
|
|
||
| /** | ||
| * @brief Shared state for the cf-reactor daemon's single select(2) loop. | ||
| * | ||
| * `all_fds` is a single flat array shared by every event source the daemon | ||
| * watches. Nova's fds always occupy the first `num_nova_fds` slots (Nova | ||
| * owns that sub-range and is the only thing allowed to populate it); any | ||
| * other event source (currently just the watcher subsystem, see watcher.h) | ||
| * appends its own fd(s) after that, and `num_fds` tracks the total number | ||
| * of slots in use. Adding a new event source means: | ||
| * | ||
| * 1. Have it report how many fds it needs, and add that to the capacity | ||
| * computed in ReactorContextInitialize() (reactor_context.c). | ||
| * 2. Give it an Initialize(fds, max_size, num_fds)/HandleEvents(readfds)/ | ||
| * Finalize(void) triplet shaped like ReactorNova*() or EventWatcher*(), | ||
| * and wire the three calls into reactor_context.c next to the existing | ||
| * ones. | ||
| * | ||
| * No other file needs to know how many event sources exist or in what | ||
| * order their fds appear. | ||
| */ | ||
| typedef struct ReactorContext | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this belongs in |
||
| { | ||
| int *all_fds; | ||
| size_t all_fds_capacity; | ||
| size_t num_nova_fds; | ||
| size_t num_fds; | ||
|
|
||
| fd_set readfds; | ||
| } ReactorContext; | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| Copyright 2026 Northern.tech AS | ||
|
|
||
| This file is part of CFEngine 3 - written and maintained by Northern.tech AS. | ||
|
|
||
| This program is free software; you can redistribute it and/or modify it | ||
| under the terms of the GNU General Public License as published by the | ||
| Free Software Foundation; version 3. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program; if not, write to the Free Software | ||
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
|
|
||
| To the extent this program is licensed as part of the Enterprise | ||
| versions of CFEngine, the applicable Commercial Open Source License | ||
| (COSL) may apply to this file if you as a licensee so wish it. See | ||
| included file COSL.txt. | ||
| */ | ||
|
|
||
| #include <reactor_context.h> | ||
| #include <prototypes3.h> /* ReactorNova*() */ | ||
| #include <signals.h> /* GetSignalPipe() */ | ||
| #include <alloc.h> | ||
|
|
||
| bool ReactorContextInitialize(ReactorContext *ctx) | ||
| { | ||
| assert(ctx != NULL); | ||
|
|
||
| // TODO: initialize watcher registry | ||
|
|
||
| size_t max_nova_fds = ReactorNovaMaxFds(); | ||
| ctx->all_fds_capacity = max_nova_fds + 1; | ||
| ctx->all_fds = xmalloc(ctx->all_fds_capacity * sizeof(int)); | ||
|
|
||
| size_t num_nova_fds = 0; | ||
| if (!ReactorNovaInitialize(ctx->all_fds, max_nova_fds, &num_nova_fds)) | ||
| { | ||
| WatcherRegistryFinalize(); | ||
| free(ctx->all_fds); | ||
| ctx->all_fds = NULL; | ||
| return false; | ||
| } | ||
| ctx->num_nova_fds = num_nova_fds; | ||
| ctx->num_fds = num_nova_fds; | ||
|
|
||
| // TODO: intialize event watcher | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int ReactorContextSetupFileDescriptors(ReactorContext *ctx) | ||
| { | ||
| assert(ctx != NULL); | ||
|
|
||
| FD_ZERO(&ctx->readfds); | ||
| int signal_pipe = GetSignalPipe(); | ||
| FD_SET(signal_pipe, &ctx->readfds); | ||
|
|
||
| int max_fd = signal_pipe; | ||
| for (size_t i = 0; i < ctx->num_fds; i++) | ||
| { | ||
| FD_SET(ctx->all_fds[i], &ctx->readfds); | ||
| max_fd = MAX(ctx->all_fds[i], max_fd); | ||
| } | ||
| return max_fd + 1; | ||
| } | ||
|
|
||
| static bool NovaHasTimedOut(ReactorContext *ctx) | ||
| { | ||
| assert(ctx != NULL); | ||
| for (size_t i = 0; i < ctx->num_nova_fds; i++) | ||
| { | ||
| if (FD_ISSET(ctx->all_fds[i], &ctx->readfds)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| void ReactorContextHandleEvents(ReactorContext *ctx, time_t *next_tick) | ||
| { | ||
| assert(ctx != NULL); | ||
|
|
||
| if (NovaHasTimedOut(ctx)) | ||
| { | ||
| ReactorNovaHandleTimeout(next_tick); | ||
| } | ||
| else | ||
| { | ||
| ReactorNovaHandleEvents(&ctx->readfds, ctx->all_fds, next_tick); | ||
| } | ||
|
|
||
| /* The signal pipe is always in the watched set so we wake up | ||
| * promptly on a pending signal, but (per its own contract in | ||
| * signals.c) it must be drained or it stays "ready" forever, which | ||
| * would stop select() from ever blocking again. */ | ||
| if (FD_ISSET(GetSignalPipe(), &ctx->readfds)) | ||
| { | ||
| unsigned char buf; | ||
| while (recv(GetSignalPipe(), &buf, 1, 0) > 0) { /* drain */ } | ||
| } | ||
|
|
||
| // TODO: handle watcher events | ||
| } | ||
|
|
||
| void ReactorContextFinalize(ReactorContext *ctx) | ||
| { | ||
| assert(ctx != NULL); | ||
|
|
||
| // TODO: finalize watcher events | ||
| ReactorNovaFinalize(); | ||
| free(ctx->all_fds); | ||
| ctx->all_fds = NULL; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| Copyright 2026 Northern.tech AS | ||
|
|
||
| This file is part of CFEngine 3 - written and maintained by Northern.tech AS. | ||
|
|
||
| This program is free software; you can redistribute it and/or modify it | ||
| under the terms of the GNU General Public License as published by the | ||
| Free Software Foundation; version 3. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program; if not, write to the Free Software | ||
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
|
|
||
| To the extent this program is licensed as part of the Enterprise | ||
| versions of CFEngine, the applicable Commercial Open Source License | ||
| (COSL) may apply to this file if you as a licensee so wish it. See | ||
| included file COSL.txt. | ||
| */ | ||
|
|
||
| #ifndef CFENGINE_REACTOR_CONTEXT_H | ||
| #define CFENGINE_REACTOR_CONTEXT_H | ||
|
|
||
| #include <cf-reactor.h> | ||
|
|
||
|
|
||
| bool ReactorContextInitialize(ReactorContext *ctx); | ||
| int ReactorContextSetupFileDescriptors(ReactorContext *ctx); | ||
| void ReactorContextHandleEvents(ReactorContext *ctx, time_t *next_tick); | ||
| void ReactorContextFinalize(ReactorContext *ctx); | ||
|
|
||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Did we not recently remove the watcher code you are referencing here?