Skip to content

Conversation

@yuichiro-naito
Copy link

@yuichiro-naito yuichiro-naito commented Nov 28, 2025

While I'm studying the flb_util_set_daemon code, I see that stdout and stderr are simply closed and don’t reopened to anything. It is potentially dangerous because the number 1 and 2 file descriptors are reused in the preceding open(2), pipe(2), or socket(2) system calls. The hardcoded write to STDERR_FILENO always uses the number 2 file descriptor; however, it will no longer be the terminal. An unexpected write to a pipe, file, or socket will happen. If the peer of the pipe or socket will never read the message, the write(2) system call will eventually be blocked because of the buffer full. This will cause the daemon to hang up.

So, the stdin, stdout, and stderr file descriptors should be reopened to the '/dev/null'.


Enter [N/A] in the box, if an item is not applicable to your change.

Testing
Before we can approve your change; please submit the following in a comment:

  • Example configuration file for the change
  • Debug log output from testing the change
  • Attached Valgrind output that shows no leaks or memory corruption was found

If this is a change to packaging of containers or native binaries then please confirm it works for all targets.

  • Run local packaging test showing all targets (including any new ones) build.
  • Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • Documentation required for this feature

Backporting

  • Backport to latest stable release.

Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.

Summary by CodeRabbit

  • Bug Fixes
    • Improved daemon-mode startup by redirecting standard I/O to a safe null device to prevent stray handles.
    • Safer file-descriptor handling during backgrounding to avoid accidental closure of needed descriptors and improve stability.
    • Enhanced error detection and handling when configuring backgrounded process I/O so failures are surfaced.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Nov 28, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Daemonization in src/flb_utils.c was changed to redirect stdin, stdout, and stderr to /dev/null using open() and dup2() after forking; guarded includes for fcntl.h and unistd.h were added under FLB_HAVE_FORK. Error checks for open()/dup2() were introduced and an extra fd is closed only when fd > 2.

Changes

Cohort / File(s) Summary
Daemonization changes
src/flb_utils.c
Added guarded includes for fcntl.h and unistd.h under FLB_HAVE_FORK. Reworked flb_utils_set_daemon to declare a local int fd, open /dev/null and dup2() it onto stdin, stdout, and stderr; added error checks for open()/dup2() (returning -1 on failure); close the extra fd only when fd > 2.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify correct error handling and return paths for open() and dup2().
  • Confirm fd > 2 prevents closing standard fds and that no fd leaks remain.
  • Ensure guarded includes are correctly scoped under FLB_HAVE_FORK.

Poem

A rabbit nudges streams to rest, 🐰
/dev/null cradles every quest,
The parent leaves, the child stays true,
Silent outputs start anew,
Hopping off in daemonized hue. ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: redirecting stdin, stdout, and stderr to /dev/null during daemonization, which is the core modification in flb_utils_set_daemon.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dcc13b9 and 89f9d98.

📒 Files selected for processing (1)
  • src/flb_utils.c (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/flb_utils.c

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

src/flb_utils.c Outdated
Comment on lines 209 to 213
/* Redirect stdin, stdout, stderr to `/dev/null`. */
fd = open("/dev/null", O_RDWR);
if (fd != -1) {
dup2(fd, STDIN_FILENO);

Choose a reason for hiding this comment

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

P1 Badge Include fcntl for daemon fd redirection

In flb_utils_set_daemon (src/flb_utils.c), the new redirection block calls open("/dev/null", O_RDWR) and relies on O_RDWR, but this compilation unit does not include <fcntl.h>. On POSIX builds with FLB_HAVE_FORK enabled, O_RDWR and the declaration of open are only provided by <fcntl.h>, so the change introduces a compile-time failure (“O_RDWR undeclared”/implicit declaration of open). Adding the missing header is required for the daemonization build to succeed.

Useful? React with 👍 / 👎.

Copy link
Contributor

@cosmo0920 cosmo0920 left a comment

Choose a reason for hiding this comment

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

We need to modify monkey related codes in https://github.com/monkey/monkey instead of here.
After merging this type of modification there, we can synchronize that kind of changes.
So, could you send your monkey related patch into monkey repo at first?

@yuichiro-naito
Copy link
Author

The lib monkey update is optional. So, I revert the update. Please review the flb_utils_set_daemon function.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3bc9b47 and 6c1f8d3.

📒 Files selected for processing (1)
  • src/flb_utils.c (3 hunks)
🔇 Additional comments (2)
src/flb_utils.c (2)

25-28: LGTM! Headers properly guarded.

The conditional inclusion of fcntl.h and unistd.h correctly addresses the need for open(), O_RDWR, dup2(), and close() declarations used in the daemonization function.


184-184: LGTM!

Variable declaration is appropriately scoped for the file descriptor handling below.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/flb_utils.c (1)

210-226: Correct implementation of stdio redirection.

The logic properly addresses the fd reuse issue by redirecting standard streams to /dev/null:

  • Handles open() failure appropriately
  • Checks all dup2() calls and cleans up on failure
  • Correctly avoids closing fd when it's already 0, 1, or 2

Optional: Add braces for consistency.

Consider adding braces to the if statement for consistency with project style:

-if (fd > 2)
-    close(fd);
+if (fd > 2) {
+    close(fd);
+}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6c1f8d3 and 3cc5158.

📒 Files selected for processing (1)
  • src/flb_utils.c (3 hunks)
🔇 Additional comments (1)
src/flb_utils.c (1)

25-28: LGTM! Includes properly guarded.

The conditional includes are correct and necessary for the daemonization logic. fcntl.h provides O_RDWR and open(), while unistd.h provides dup2() and the standard file descriptor constants.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/flb_utils.c (1)

184-227: Daemon stdio redirection via /dev/null looks correct; clarify failure semantics

The open/dup2 sequence and fd > 2 close pattern are correct and address the fd‑reuse issue this PR targets. A couple of points to consider:

  • This function historically either succeeded or called exit(). The new paths now return -1 on open/dup2 failure after daemonization steps have already run. Callers must be checking the return value; otherwise failures here won’t stop startup.
  • For debugging/ops, you might want to include the OS error in the log (e.g., using flb_errno() or strerror(errno)) so it’s clearer why /dev/null handling failed.

If call sites already treat non‑zero returns as fatal, the change is fine; otherwise, consider either propagating the error in a way that aborts startup (to match other fatal branches in this function) or documenting the partial‑daemonization behavior.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3cc5158 and dcc13b9.

📒 Files selected for processing (1)
  • src/flb_utils.c (3 hunks)
🔇 Additional comments (1)
src/flb_utils.c (1)

25-28: Guarded inclusion of <fcntl.h> / <unistd.h> is appropriate

These headers are now correctly pulled in only when FLB_HAVE_FORK is enabled, matching the use of open, O_RDWR, dup2, and STDIN_FILENO/STDOUT_FILENO/STDERR_FILENO in the daemon code path while avoiding issues on non‑POSIX builds.

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.

2 participants