s3:smbd: Don't record a child with pid -1 when fork() fails

The smbd listener permanently loses a process slot every time fork() fails, and eventually stops serving connections at all.

In smbd_accept_connection(), add_child_pid() is called under if (pid != 0), which is also true for the fork() failure return of -1. That entry is never reaped, because remove_child_pid() is only reached from the SIGCHLD handler with a real pid, so parent->num_children grows with every failed fork(). Once it reaches "max smbd processes", allowable_number_of_smbd_processes() makes the parent close every incoming connection even though no worker child is running, and only restarting smbd recovers. The bogus entry is also a messaging target in messaging_send_to_children().

The other fork() callers in server.c already check for -1 before add_child_pid(), and winbindd does the same in winbindd_dual.c. This makes the accept path consistent with them.

Bug: https://bugzilla.samba.org/show_bug.cgi?id=16328

Reproduced on master (5553b34f) with "max smbd processes = 5" and an LD_PRELOAD shim making fork() return EAGAIN. Unpatched, num_children reached the limit with only cleanupd and notifyd actually running, and smbclient got NT_STATUS_CONNECTION_RESET. Patched, the count stayed at 2 and connections kept working. The reproducer, example smb.conf and raw output are attached to the bug.

No test suite change: triggering this needs fork() to fail, which selftest has no way to induce. Happy to add a test if a reviewer can suggest a workable approach.

I can provide backports for the maintained branches once this is in master.

Checklist

  • Commits have Signed-off-by: with name/author being identical to the commit author
  • (optional) This MR is just one part towards a larger feature.
  • (optional, if backport required) Bugzilla bug filed and BUG: tag added
  • Test suite updated with functionality tests
  • Test suite updated with negative tests
  • Documentation updated
  • CI timeout is 3h or higher (see Settings/CICD/General pipelines/Timeout)

Reviewer's checklist:

  • There is a test suite reasonably covering new functionality or modifications
  • Function naming, parameters, return values, types, etc., are consistent and according to README.Coding.md
  • This feature/change has adequate documentation added
  • No obvious mistakes in the code
Edited by Muhammad Daffa

Merge request reports

Loading
Loading