Refine debug log output in hopes of clues to why test fails on Aches.

(cherry picked from commit 349c77433f71489a9dc55c0f8ce46bdca329aa4e)
Add some debug output to laterthan.cf

... so we have some clue as to what's failing.

(cherry picked from commit 2483a513a056c11e65e5724ea90ba6f992dddd6a)
Advance ap if socket(2) fails

In the case where socket(2) fails in SocketConnect, ap will advance,
causing an infinite loop.

https://dev.cfengine.com/issues/6560
(cherry picked from commit d119322e0b6ad5061670839dffb954b4708331ef)
Redmine #6558: Fix LookupAccountName error message on Windows.

Don't report error for user name "root" on Windows, since it is not
expected to exist.

(cherry picked from commit fb72b05d47b8f5d08b8ac2c3c1b2ee8d4157062b)
Change: Fix laterthan test

The or() function returns a string, not a boolean.
https://docs.cfengine.com/latest/reference-functions-or.html

(cherry picked from commit 95e31650aa324b94287ffcc2ae5234f41c885f62)
Document change to input validation for integer ranges
Building 3.6.2 packages
Add le32toh macro & friends to platform that lack it, like Windows.

Macros are preferable, because Linux internally uses macros, and
attempting to use a function messes with that.

(cherry picked from commit 3f2510803e6751893e5b4be982d6e8eee42894c3)
Wrap HACKING.md where lines exceed 78 columns.

(cherry picked from commit b10f1eba3dbfe0014c192c98fff8733e2346b658)
HACKING.md: add do-while exception, and rewrap some text around it.

(cherry picked from commit 83b579d51824b06778a6495f3b62f96d71d5e13a)
Fixing LocalExec in cf-execd-runner.c

The premature return was only doing most of the tidy-up needed, so
make it a break instead and add a flag for whether the command
actually completed; use that to condition the one Log() after the loop
that wasn't appropriate to run after this failure.  The alternate
branch to that return's if was in fact the only path out of the loop
for which the Log() was relevant, so made it set the flag for that
Log().  Made the Log() say something sensible as an alternative when
the work isn't complete.  Simplified the code to detect a non-blank
string.

Code skipping spaces at the start of a string could be simplified to
avoid the need for a bool flag to say whether we found anything else.

(cherry picked from commit 0f31fe051f0e8917938055296f497af687738e36)
Kill some stray apostrophes.

Some function names were formatted using '%s'' with two dangling
apostrophes; there should only be one before and one after !
Also: split a long line, purged dangling hspace, added braces demanded
by coding style.

(cherry picked from commit 2cd2a2d35ca96dbf6690b2688958f96ea098827c)
Leak-fixes and clean-up in FnCallPackagesMatching().

Move computation of regex outside the loop - no need to recompute the
same regex for every line of the CSV file ! - and pre-compile it so
that we can search using it more efficiently.  Localise all the
variables related to that at the same time.

Split up various long lines, notably when Log()ging failure to open
the inventory.

Move creation of some objects to where they're needed, to save the
need to tidy them away on failure in the interval.

Don't leak the allocated line when it doesn't match our expectations.

Restructure the for(;;) loop as a while with assignment in its
condition so as to move the failure-handling out of the loop and avoid
duplication of clean-up code.  Reviewed the error message on that
failure: it claimed fread() was involved, which is a lie, so re-worked
it.

(cherry picked from commit 3fc8d68041c9cae1cebb2e5438a3a040ded3e5d4)
Petty leak-fix: close socket on which we failed to listen.

About to exit with error anyway, so not crucial.

(cherry picked from commit a14023622adec2a250c1863708c62f4c295a0099)
Tidy up in SocketConnect().

PR #1952 added an ap = ap->ai_next in an if branch that duplicated one
in its else; replacing these with one after the whole branch put the
loop in the form of a for() unrolled as a while, so I rolled it up as
a for().

If an iteration of the loop managed to create a socket but failed to
connect it, we were leaking it (unless it happened to be the last time
round the loop); the "on failure close the socket" was after the loop,
where it belonged inside.  Further, the Log()ging of that tried to use
GetErrorStr() when enough other functions had been called since the
error that the error string might easily be from something unrelated
to the connection failure.  So Log() before closing the socket in the
loop and don't try to include an error string with the final "none of
it worked" Log().

Tweaked a couple of other Log() messages.

(cherry picked from commit ca9d2fb252d84788baee872707fa52ad83caf7c9)
Fix a badly-thought-out loop.

The BSD compiler warned that i >= 0 is always true for a size_t i; the
loop conditioned on that could have spun for ever.  So turn it into a
while that terminates correctly and simplify what's left.

(cherry picked from commit cad04bf29138210cebee41bf4771bdc09df3fd29)
Break out of thread main-loop on signal.

If we get a signal that we catch and use as reason to exit, we need
our threads to exit, not just the main thread.  I believe it's safe
for the threads to simply call IsPendingTermination(); it's not
super-critical that they synchronise round that, as long as they
notice after a few laps round their loops.

(cherry picked from commit de07da489e888a788c6c2b1ac1ebd05652b3d9b0)
Cleanup in FileReadFromFd().

It was possible to return without having set *truncated.
Closing the file was repeated in several places.
Our coding standard demands braces on a conditional's body, even when
it consists of just one statement.  So add those braces.

Restructuring the loop body's conditionals as an if/else-if/else
chain got rid of some continue;s.

(cherry picked from commit e86cc0d1174e0e9ef503f4098cfb874b5f42ae77)
Fix misguided strlen() in Chop() and StripTrailingNewline().

They check strlen() doesn't exceed max_length, which callers typically
pass as the buffer size; either this test is a waste of time or, when
it might fail, it'll involve reading past the end of the buffer to
discover the error - which is bad.  So use strnlen() instead, this is
what it's for.  Don't bother special-casing empty string; and re-use
the value strnlen has returned to us instead of calling again at the
start of the search for the last non-space.  Only '\0' out the first
trailing space, rather than filling the tail with '\0' over each
space.  (For "space" read isspace() in Chop() and '\n' in
StripTrailingNewline().)  Structure the two functions the same way.

(cherry picked from commit 49bfcf81735d02f39f98b1b60ffea3b56f94aeb8)
Cast is...() character class parameters to unsigned char.

These functions (isprint, isspace, etc.) take an int with "the value
of an unsigned char or EOF" according to spec.  Passing a char is thus
suspect, since char can be signed or unsigned, depending on platform.
(Many previously cast to int, which was pointless.)

(cherry picked from commit f48656d89f02aefa7fe8a7f5ff731c91170a97c8)