Source
/* restore original stdout and stderr, because we don't want our output to be confused with script's output */
/*
** 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.
**/
/* the size of temporary buffer used to read from output stream */
/******************************************************************************
* *
* Purpose: considers a difference between times in milliseconds *
* *
* Parameters: time1 - [IN] first time point *
* time2 - [IN] second time point *
* *
* Return value: difference between times in milliseconds *
* *
******************************************************************************/
static int zbx_get_timediff_ms(struct _timeb *time1, struct _timeb *time2)
{
int ms;
ms = (int)(time2->time - time1->time) * 1000;
ms += time2->millitm - time1->millitm;
if (0 > ms)
ms = 0;
return ms;
}
/******************************************************************************
* *
* Purpose: read data from pipe *
* *
* Parameters: hRead - [IN] a handle to the device *
* buf - [IN/OUT] a pointer to the buffer *
* buf_size - [IN] buffer size *
* offset - [IN/OUT] current position in the buffer *
* timeout_ms - [IN] timeout in milliseconds *
* *
* Return value: SUCCEED, FAIL or TIMEOUT_ERROR if timeout reached *
* *
******************************************************************************/
static int zbx_read_from_pipe(HANDLE hRead, char **buf, size_t *buf_size, size_t *offset, int timeout_ms)
{
DWORD in_buf_size, read_bytes;
struct _timeb start_time, current_time;
char tmp_buf[PIPE_BUFFER_SIZE];
_ftime(&start_time);
while (0 != PeekNamedPipe(hRead, NULL, 0, NULL, &in_buf_size, NULL))
{
_ftime(¤t_time);
if (zbx_get_timediff_ms(&start_time, ¤t_time) >= timeout_ms)
return TIMEOUT_ERROR;
if (MAX_EXECUTE_OUTPUT_LEN <= *offset + in_buf_size)
{
zabbix_log(LOG_LEVEL_ERR, "command output exceeded limit of %d KB",
MAX_EXECUTE_OUTPUT_LEN / ZBX_KIBIBYTE);
return FAIL;
}
if (0 != in_buf_size)
{
if (0 == ReadFile(hRead, tmp_buf, sizeof(tmp_buf) - 1, &read_bytes, NULL))