# This file is part of shellfire core. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT. No part of shellfire core, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire core. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT.


core_usesIn core trap
core_usesIn core/variable array

core_children_initialise()
{
	core_trap_addOnCleanUp core_children_killAll
}
core_functions_register _core_functions core_children_initialise

core_dependency_requires '*' kill
_core_children_killAll_callback()
{	
	local pid
	for pid in "$@"
	do
		kill -TERM "$pid" 2>/dev/null || true	
	done
}

core_children_killAll()
{
	if core_variable_array_isSet _core_children_childrenToRemoveOnExit; then
		core_variable_array_passToFunctionAsArguments _core_children_childrenToRemoveOnExit _core_children_killAll_callback
	fi
}

core_children_killOnExit()
{
	local childPid
	for childPid in "$@"
	do
		core_variable_array_append _core_children_childrenToRemoveOnExit "$childPid"
	done
}
