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.
Fixed a bug calling killproc_nagios() from stop
Using TERM as suggested in the comment here: https://github.com/NagiosEnterprises/nagioscore/pull/6#discussion_r14309447
Remove unused killproc_nagios() arg, and pass signal name with -s.
Put nagios.configtest in Nagios' var dir.

The file isn't strictly related to the system run state, and it may be
useful to preserve the last configuration errors across reboots.
Init script: Set variables before sourcing sysconfig.

Doing this after setting the variables allows sysconfig settings to
override the configure/install-time defaults.
Init script: Whitespace and formatting cleanup.
Init script: Don't try to start if already running.

Running start with an instance of nagios already running would result
in the running instance's command pipe being removed. Test that the
lock file does not hold the PID of a running process before attempting
to start a new instance.

Also remove some unused arguments from status_nagios calls.
Init script: Quote kill's signal specification.

This gives a little bit less confusing error message if someone were to
invoke killproc_nagios without specifying a signal to send.
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 'rk295-master'
Merge branch 'emislivec-tracker0582-runcmd'
Merge branch 'emislivec-scheduling'
Fix small memory leak in reset_variables().

init_main_cfg_vars() points object_precache_file at a copy of its
default value string, so we don't need to do it again in
reset_variables().