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)
Remove all transaction sharing code between cursor and regular updates.

Modifying the db structure makes the individual functions non-reentrant,
and can cause race conditions and data corruptions in multi threaded
programs, such as cf-serverd.

(cherry picked from commit 48a63e90bf635e78081da97ac61d24f4b5710e65)
Revert "Remove all transaction sharing code between cursor and regular updates."

This reverts commit 91b0fe5deca0c908152c67a77bed4477b0ab94f4.
Introduced hanging in migration tests. In master, those are now disabled, but
need to first find out where the deadlocks come from in those tests.
Try to simplify apoptosis/DeleteConnection locking mess.
Select() error code is no longer lost because of recv().
(cherry picked from commit 56afde78024563bc1daf6e3a5f8f44b65f3b04e5)
Merge pull request #1677 from jimis/serverd_deadlock_fix-3.6.x

Serverd deadlock fix 3.6.x
Don't use Read/WriteDB inside a cursor loop.

LMDB doesn't allow multiple transactions per thread. Opening a cursor
opens a transaction, and so does Read/WriteDB. So, either re-use values
from cursor, or populate a sequence with keys, and then lookup the
individual values in a separate loop.

Note: Previously, the WriteDB code reused the transaction from an open
cursor, but that is making the entire LMDB driver not thread-safe.

(cherry picked from commit 683df34e32c822e6d26dd4c5cc562881269acd6a)
Remove all transaction sharing code between cursor and regular updates.

Modifying the db structure makes the individual functions non-reentrant,
and can cause race conditions and data corruptions in multi threaded
programs, such as cf-serverd.

(cherry picked from commit 48a63e90bf635e78081da97ac61d24f4b5710e65)
Disable irrelevant migration tests for LMDB builds.

(cherry picked from commit fce7530cfa0e5ea1fdc3787f9817fa09d9f7fadb)

These tests fail because the used functions use WriteDB inside
a cursor loop. This opens two transactions at the same time, which
LMDB doesn't allow - the result is a deadlock after removing
transaction sharing between cursors and WriteDB calls.

Since LMDB was introduced in 3.6, the migration code won't work
anyway - the old database would be a TCDB database.
Initialize the global transaction to null at initialization.

Otherwise, we have an uninitialized pointer in case of error.
Also abort the opening transaction in case of error.

No need to reset the pointer on the stack at end of function.

(cherry picked from commit 88e3e9623273df3ac9a86ee0147492f7ac6d5005)
Add LoadDatabaseToStringMap utility function

(cherry picked from commit 976464db62381a598b470304cebaf0e490ea5227)
Fix linker errors in unit tests.
(cherry picked from commit 7a85c9a0b6e825a164e5b5c9921bc869e95759f4)
Redmine #5496: Fix enterprise_extension_test for ARM processors.

Because of function argument padding, the "char" argument is not
enough to trigger the stack inconsistency we are looking for in the
test. This also highlights that not all forms of stack corruption will
be detected when loading the plugin, it is just a safeguard.
(cherry picked from commit ecb5833019794cec2f03f349530699e2274b01b0)