1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
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; version 3.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <pipes.h>
#include <rlist.h>
#include <buffer.h>
#include <signals.h>
#include <string_lib.h>
bool PipeTypeIsOk(const char *type)
{
if (type[0] != 'r' && type[0] != 'w')
{
return false;
}
else if (type[1] != 't' && type[1] != '+')
{
if (type[1] == '\0')
{
return true;
}
else
{
return false;
}
}
else if (type[2] == '\0' || type[2] == 't')
{
return true;
}
else
{
return false;
}
}
/*******************************************************************/
/* Pipe read/write interface, originally in package modules */
/*******************************************************************/
Rlist *PipeReadData(const IOData *io, int pipe_timeout_secs, int pipe_termination_check_secs)
{
char buff[CF_BUFSIZE] = {0};
Buffer *data = BufferNew();
if (!data)
{
Log(LOG_LEVEL_VERBOSE,
"Unable to allocate buffer for handling pipe responses.");
return NULL;
}
int timeout_seconds_left = pipe_timeout_secs;
while (!IsPendingTermination() && timeout_seconds_left > 0)
{
int fd = PipeIsReadWriteReady(io, pipe_termination_check_secs);
if (fd < 0)
{
Log(LOG_LEVEL_DEBUG,
"Error reading data from application pipe %d", fd);
break;
}
else if (fd == io->read_fd)
{
ssize_t res = read(fd, buff, sizeof(buff) - 1);
if (res == -1)
{
if (errno == EINTR)
{
continue;
}
else
{
Log(LOG_LEVEL_ERR,
"Unable to read output from application pipe: %s",
GetErrorStr());
BufferDestroy(data);
return NULL;
}
}
else if (res == 0) /* reached EOF */
{
break;
}
Log(LOG_LEVEL_DEBUG, "Data read from application pipe: %zd [%s]",
res, buff);
BufferAppendString(data, buff);
memset(buff, 0, sizeof(buff));
}
else if (fd == 0) /* timeout */
{
timeout_seconds_left -= pipe_termination_check_secs;
continue;
}
}
char *read_string = BufferClose(data);
#ifdef __MINGW32__
bool detect_crlf = true;
#else
bool detect_crlf = false;
#endif
Rlist *response_lines = RlistFromStringSplitLines(read_string, detect_crlf);
free(read_string);
return response_lines;
}
ssize_t PipeWrite(IOData *io, const char *data)
{
/* If there is nothing to write close writing end of pipe. */
if (data == NULL || strlen(data) == 0)
{
if (io->write_fd >= 0)
{
cf_pclose_full_duplex_side(io->write_fd);
io->write_fd = -1;
}
return 0;
}
ssize_t wrt = write(io->write_fd, data, strlen(data));
/* Make sure to close write_fd after sending all data. */
if (io->write_fd >= 0)
{
cf_pclose_full_duplex_side(io->write_fd);
io->write_fd = -1;
}
return wrt;
}
int PipeWriteData(const char *base_cmd, const char *args, const char *data)
{
assert(base_cmd);
assert(args);
char *command = StringFormat("%s %s", base_cmd, args);
IOData io = cf_popen_full_duplex(command, false, true);
free(command);
if (io.write_fd == -1 || io.read_fd == -1)
{
Log(LOG_LEVEL_VERBOSE, "Error occurred while opening pipes for "
"communication with application '%s'.", base_cmd);
return -1;
}
Log(LOG_LEVEL_DEBUG, "Opened fds %d and %d for command '%s'.",
io.read_fd, io.write_fd, args);
int res = 0;
int written = PipeWrite(&io, data);
if (written < 0)
{
Log(LOG_LEVEL_ERR, "Failed to write to pipe (fd %d): %s",
io.write_fd, GetErrorStr());
res = -1;
}
else if ((size_t) written != strlen(data))
{
Log(LOG_LEVEL_VERBOSE,
"Was not able to send whole data to application '%s'.",
base_cmd);
res = -1;
}
/* If script returns non 0 status */
int close = cf_pclose_full_duplex(&io);
if (close != EXIT_SUCCESS)
{
Log(LOG_LEVEL_VERBOSE,
"Application '%s' returned with non zero return code: %d",
base_cmd, close);
res = -1;
}
return res;
}
/* In some cases the response is expected to be not filled out. Some requests
will have response filled only in case of errors. */
int PipeReadWriteData(const char *base_cmd, const char *args, const char *request,
Rlist **response, int pipe_timeout_secs, int pipe_termination_check_secs)
{
assert(base_cmd);
assert(args);
char *command = StringFormat("%s %s", base_cmd, args);
IOData io = cf_popen_full_duplex(command, false, true);
if (io.write_fd == -1 || io.read_fd == -1)
{
Log(LOG_LEVEL_INFO, "Some error occurred while communicating with %s", command);
free(command);
return -1;
}
Log(LOG_LEVEL_DEBUG, "Opened fds %d and %d for command '%s'.",
io.read_fd, io.write_fd, command);
int written = PipeWrite(&io, request);
if (written < 0) {
Log(LOG_LEVEL_ERR, "Failed to write to pipe (fd %d): %s",
io.write_fd, GetErrorStr());
return -1;
}
else if ((size_t) written != strlen(request))
{
Log(LOG_LEVEL_VERBOSE, "Couldn't send whole data to application '%s'.",
base_cmd);
free(command);
return -1;
}
/* We can have some error message here. */
Rlist *res = PipeReadData(&io, pipe_timeout_secs, pipe_termination_check_secs);
/* If script returns non 0 status */
int close = cf_pclose_full_duplex(&io);
if (close != EXIT_SUCCESS)
{
Log(LOG_LEVEL_VERBOSE,
"Command '%s' returned with non zero return code: %d",
command, close);
free(command);
RlistDestroy(res);
return -1;
}
free(command);
*response = res;
return 0;
}
IOData cf_popen_full_duplex_streams(
const char *command, bool capture_stderr, bool require_full_path)
{
IOData ret = cf_popen_full_duplex(
command, capture_stderr, require_full_path);
// On windows, these streams are already set up correctly:
if (ret.read_stream == NULL)
{
ret.read_stream = fdopen(ret.read_fd, "r");
}
if (ret.write_stream == NULL)
{
ret.write_stream = fdopen(ret.write_fd, "w");
}
return ret;
}
|