Redmine #5380: Introduce privilege elevation in Windows testing.

This is needed in order to run tests as a user other than
Administrator, even if that user has admin privileges. The reason is
that Windows by default runs every process with non-admin rights, so
we need to elevate the privileges ourselves.
(cherry picked from commit dad7fd582bc88e7a20ede90150ef076f69ab901b)
Redmine #4751: Fix crash in 07_packages/907.cf test case.

The package_name_convention can be unset, so we need to check for null
pointer.
(cherry picked from commit ba40441c38b1680b70ae79fc298065adeaffd894)
Fix SIGBUS on Solaris.
(cherry picked from commit 96e28ce19b0b15ae48bd8c739639ed0dc3431f07)
Why have two identical ifdefs? This had been fixed in the past, but it regressed!
Scan for policy change right after the select() call returns.

The way we did it before, it was almost certain that at the point of
checking there would always be one active thread (the one serving the
incoming connection) so CheckFileChanges() would not run. Only exception
would have been when select() returned because of timeout, which would
never happen in a slightly loaded hub.
Moved code around for clarity.

Mostly whitespace changes.
Fixed 100% CPU burning loop if listening socket descriptor is -1

There is a feature in enterprise ("listen" attribute) that disables
listening to any port. In that case the while loop would never
stop. Fixed it so that it goes through the select() call.

Mostly whitespace changes.
CLOEXEC must be called even when NO_FORK.
Merge pull request #1667 from jimis/server_reload_policy-3.6.x

Server reload policy 3.6.x
Redmine #5557: Fix promises wrongfully reported with kept status.

We need to swap the priority of the promise SKIPPED and NOOP states.
SKIPPED should have higher priority when calculating promise status
because most promises start out with NOOP status, and SKIPPED is
something they receive later when they discover that their promise
lock cannot be obtained because they have already run. If SKIPPED has
the lowest priority, then the combined result will almost always be
KEPT, so this fix avoids that.

(cherry picked from commit 1cb35d8c56a1effd081dc856b7ae2722ac4ed2a8)
Revert "Redmine #5557: Fix promises wrongfully reported with kept status."

This reverts commit 1cb35d8c56a1effd081dc856b7ae2722ac4ed2a8.

The change, while in principle correct, break the test. Need to review
before letting this go in.

(cherry picked from commit 73b553ca9810519f241022c03749ba6f042ff31e)
Fix subindexing for maparray function

Close #5452

(cherry picked from commit 5ec9a6cc16a421497a1f702658135ba82820d147)
Redmine #5557: Fix files promises having wrong status report.

In functions where we invoke sub promises, we need to start with the
assumption that the promise will be skipped, instead of it being a
kept promise. The reason is that the skipped status has the lowest
priority, so if we end up skipping a promise, the status will still be
kept if that's what we started with.

(cherry picked from commit 9f36a7d053e7038a19b3146ca4f934bf11a6ca8b)
Correctly report repaired compliance for commands promises.

For compliance, commands promises are interpreted to have been KEPT
for 0 returns status, and NOTKEPT for non-zero. In order to maintain
compatibility with repaired classes set via body classes, a REPAIRED
state from VerifyCommandRetcode is overwritten with KEPT.

If the body classes also defines return codes for which the compliance
should be REPAIRED, then honour this and don't overwrite with KEPT.

Fixes: #5489
(cherry picked from commit 3f3c3a4471ef7a4e4cb98fe97e2f5d3bf2c6a267)
Redmine #5621: Fix failing safe_open in create race.

On single core systems with certain kernel versions (CentOS), the
ordering of open calls is deterministic, and if two threads try to
open the same file with O_CREAT, one will fail because the other
thread creates it at the same moment.

This is actually the very thing that safe_open was designed to protect
against, but since it can happen even in daily operation when there is
no foul play, we need to be a bit more tolerant. So if we find that we
have hit a race condition, instead of failing immediately, retry the
operation to see if we hit the race condition again. Only after a few
attempts do we give up.

This changes the expected result of one of the unit tests from
safe_open returning -1, to returning a file descriptor.
Add an additional test for opening a dangling symlink, not just creating
Fix possible file open race condition on platforms without openat().

When opening a relative path, the first path component opened is the
current directory. However, this is opened using normal open(). This
is fine on most platforms, but on those where we emulate openat(), we
do not protect the open() call using the lock inside generic_at.c,
which means we can potentially be in a different directory at the time
of the call.

The fix is to switch to openat() with the AT_FDCWD flag, so that we
run the emulation code with the lock also for the first file open.
Rename internal test hook to make it clear that it itself isn't a test
Attempt expansion of naked nested scalars in ExpandListEntry

Close #5480

(cherry picked from commit d2033f2554c743f60bf64301cb35773578543fd3)
Test db->mc only once and store result on stack.

This reduces the possibility of race conditions in multi threaded
client code, where one thread opens a cursor while another thread
operates on the DB without cursors. In that case, db->mc changes
in the middle of the operation.

This is a quick workaround, the correct fix is to either remove this
optimization or protect the db structure with a mutex.

(cherry picked from commit d81d0ea68654cc4702f045d7ea0248b596b0d067)