Source
xxxxxxxxxx
void zbx_thread_start(ZBX_THREAD_ENTRY_POINTER(handler), zbx_thread_args_t *thread_args, ZBX_THREAD_HANDLE *thread)
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
static ZBX_THREAD_ENTRY(zbx_win_thread_entry, args)
{
__try
{
zbx_thread_args_t *thread_args = (zbx_thread_args_t *)args;
return thread_args->entry(thread_args);
}
__except(zbx_win_seh_handler(GetExceptionInformation()))
{
zbx_thread_exit(EXIT_SUCCESS);
}
}
void CALLBACK ZBXEndThread(ULONG_PTR dwParam)
{
_endthreadex(SUCCEED);
}
/******************************************************************************
* *
* Purpose: Flush stdout and stderr before forking *
* *
* Return value: same as system fork() function *
* *
******************************************************************************/
int zbx_fork(void)
{
fflush(stdout);
fflush(stderr);
return fork();
}
/******************************************************************************
* *
* Purpose: fork from master process and set SIGCHLD handler *
* *
* Return value: same as system fork() function *
* *
* Comments: use this function only for forks from the main process *
* *
******************************************************************************/
void zbx_child_fork(pid_t *pid)
{
sigset_t mask, orig_mask;
/* block signals during fork to avoid deadlock (we've seen one in __unregister_atfork()) */
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGUSR2);
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGQUIT);
sigaddset(&mask, SIGCHLD);
zbx_sigmask(SIG_BLOCK, &mask, &orig_mask);
/* set process id instead of returning, this is to avoid race condition when signal arrives before return */
*pid = zbx_fork();
zbx_sigmask(SIG_SETMASK, &orig_mask, NULL);
/* ignore SIGCHLD to avoid problems with exiting scripts in zbx_execute() and other cases */
if (0 == *pid)
signal(SIGCHLD, SIG_DFL);