Export command variable assignments to the environment.

In the POSIX sh language, variable assignments preceding a command are
exported to the enviroment of the command. Previously runcmd_open() was
not handling this for simple commands that don't require a subshell to
run (the shell handles this for more complex commands that we run in a
subshell).

These changes export the variable assignments without requiring a
complete subshell.
runcmd: Handle child errors more thoroughly and uniformly.

runcmd_open() was not detecting some error conditions that would result
in the child runtime environment not being established correctly. With
the exception of close(), calls in the newly created child process that
can fail within reason are now checked and reported to stderr before
exiting.
runcmd: Correct comments and cleanup some code.

Corrected comments regarding 'async-safety', code attribution, and code
function. runcmd_init() is definitely NOT reentrant, and incorrectly
calling this function as if it were would invalidate the reentrancy of
the other functions that access the pids global variable.

Moved Doxygen section end (/** @} */) in runcmd.h inside include guard
in order to not confuse Doxygen. Removed unused and incorrect SIG_ERR
define, and one declaration per line.
runcmd: Improve error handling & memory management.

runcmd_cmd2strv():
Check for valid arguments, test for failed allocation, and
null-terminate the output argv array. Also avoid a potential leak if
returning early on unexpected end of string (really shouldn't ever
happen).

runcmd_open():
Check all arguments are non-null: __attribute__((__nonnull__(1, 2, 3, 5,
6))) is not a guarantee, it is still perfectly possible to pass NULL in
the specified positions and not pass "-Wnonnull" to gcc, or just ignore
the warning if it is generated. Handle failed allocation from
runcmd_cmd2strv(), consider commands with unbalanced quotes an error
(the shell does), and return the correct error code if pipe(pfd) fails.

test-runcmd:
Free our allocated memory. The only unfreed memory is now the pids array
internal to runcmd.c.
Checks: Reimplement auto rescheduling.

adjust_check_scheduling() is intended to smooth out load by more evenly
scheduling checks, but wasn't updated to work with the new heap based
scheduling queue.

These changes closely replicate the original implementation while using
the new data structures reasonably efficiently, and providing
sub-second resolution when calculating new event run times.

The rescheduling algorithm makes some assumptions about per-check
overhead that may be overly pessimistic, and possibly not needed to
generate a smooth schedule. When rescheduled, the next run of an event
may be earlier or later than dictated by its check interval, but will
run at its regular check interval when no schedule adjustment is
needed.
Checks: Increase minimum spacing for auto-reschedulinng.

The projected check overhead determines the how close check events can
be without trigger an auto-reshcedule. Increase from 1/16th to 1/8th
seconds to keep schedules smoother. This should ideally be determined
dynamically based on configured checks.
wproc: Make wproc_reap() behave as advertised.

Although only called when checking if an event should be run when job
control is enabled, this function did not behave as advertised.
Additionally waiting up to 3 seconds to reap 100 jobs may be excessive,
especially when fewer than 100 jobs are running. We'll wait up to one
millisecond for one job instead.
Check events: More accurately determine if auto-rescheduling is needed.

Previously we were looking at timed_event.run_time which has second
precision. This would cause rescheduling to be run only when events
occured in the same second.

By looking at squeue_event.when we get the actual run times used by the
event scheduling priority queue with microsecond precision.
Check events: Don't estimate exec. time in auto-rescheduling.

Estimates of check execution time weren't too sophisticated or uniform
(we only have real measurments for host checks). By removing these
estimates the algirithm is simpler and the new schedule is smoother.

Also use cheduling_info.service_inter_check_delay/4 as a threshold for
determining if check events are too close in time and need to be
rescheduled. This removes hardcoded thresholds and makes the
rescheduling criteria dependent on the acutal configured checks.
Merge branch 'master' of github.com:NagiosEnterprises/nagioscore into emislivec-scheduling
wproc: Correctly calculate remaining time to wait in wproc_reap().
test-runcmd: Fix missing-field-initializers

I'm not quite clear on exactly why I can't get my fedora-gcc to complain
about this, but travis' ubuntu-gcc seems to complain.

Signed-off-by: Robin Sonefors <robin.sonefors@op5.com>
runcmd: Don't crash when exposed to ampersands

An off-by-one error caused us to copy both a null-byte and the job
control character when exposed to commands containing job control
characters. If there are more than 10 of those, we'd run out of memory
and write to uninitialized memory. If we were unlucky, the uninitialized
memory was "badly" located, which means worker crash and bug op5#8182.

The fun thing is, the way the "embedded shell" thingie works, every time
you call add_ret(), you might as well just return - alas, we don't, for
reasons that are hard to see clearly but I assume involve having
something to speed up in a future version. Oh well.

So, back to the bug: we discovered a case where knew we would never,
ever read the output, and proceeded to handle that by... Writing too
much to the output to the point where the worker crashed.

Proving, with a test, that we didn't really do anything is hard. The
extra test case added to test-runcmd doesn't fail or generate a crash
with the old code, but at least it generates memory errors when run
through valgrind, and those errors are now gone.

So, this should fix the crash in bug op5#8182, when using the initial,
incorrect quoting - obviously, the check result still won't make sense
as it's wrong, but now you get to see that rather than infinite crashes.

Change-Id: I0b7ebd9a782125bc060ea9eb785d70de7bb69228
Signed-off-by: Robin Sonefors <robin.sonefors@op5.com>
Merge branch 'master' of github.com:NagiosEnterprises/nagioscore into emislivec-tracker0582-runcmd
runcmd: Continue to fork() unbalanced commands.

The worker layer & event/check scheduling aren't handling an error
return from runcmd_open() well. To get sensible handling of commands
with unbalanced quotes at the higher layers, continue on to fork a
child process that will exit with an error status and write a message
to stderr that will get picked up by the worker and sent to Core.
Currently, if we just return an error from the parent (worker) we can
end up in situations where the failing command will be repeatedly
rescheduled to run immediately.
Merge branch 'emislivec-tracker0582-runcmd'
Merge branch 'emislivec-scheduling'