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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/* miniexpect
* Copyright (C) 2014 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <poll.h>
#include <errno.h>
#include <termios.h>
#include <time.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <pcre.h>
/* RHEL 6 pcre did not define PCRE_PARTIAL_SOFT. However PCRE_PARTIAL
* is a synonym so use that.
*/
#ifndef PCRE_PARTIAL_SOFT
#define PCRE_PARTIAL_SOFT PCRE_PARTIAL
#endif
#include "miniexpect.h"
#define DEBUG 0
static mexp_h *
create_handle (void)
{
mexp_h *h = malloc (sizeof *h);
if (h == NULL)
return NULL;
/* Initialize the fields to default values. */
h->fd = -1;
h->pid = 0;
h->timeout = 60000;
h->read_size = 1024;
h->pcre_error = 0;
h->buffer = NULL;
h->len = h->alloc = 0;
h->next_match = -1;
h->user1 = h->user2 = h->user3 = NULL;
return h;
}
static void
clear_buffer (mexp_h *h)
{
free (h->buffer);
h->buffer = NULL;
h->alloc = h->len = 0;
h->next_match = -1;
}
int
mexp_close (mexp_h *h)
{
int status = 0;
free (h->buffer);
if (h->fd >= 0)
close (h->fd);
if (h->pid > 0) {
if (waitpid (h->pid, &status, 0) == -1)
return -1;
}
free (h);
return status;
}
mexp_h *
mexp_spawnlf (unsigned flags, const char *file, const char *arg, ...)
{
char **argv, **new_argv;
size_t i;
va_list args;
mexp_h *h;
argv = malloc (sizeof (char *));
if (argv == NULL)
return NULL;
argv[0] = (char *) arg;
va_start (args, arg);
for (i = 1; arg != NULL; ++i) {
arg = va_arg (args, const char *);
new_argv = realloc (argv, sizeof (char *) * (i+1));
if (new_argv == NULL) {
free (argv);
va_end (args);
return NULL;
}
argv = new_argv;
argv[i] = (char *) arg;
}
h = mexp_spawnvf (flags, file, argv);
free (argv);
va_end (args);
return h;
}
mexp_h *
mexp_spawnvf (unsigned flags, const char *file, char **argv)
{
mexp_h *h = NULL;
int fd = -1;
int err;
char slave[1024];
pid_t pid = 0;
fd = posix_openpt (O_RDWR|O_NOCTTY);
if (fd == -1)
goto error;
if (grantpt (fd) == -1)
goto error;
if (unlockpt (fd) == -1)
goto error;
/* Get the slave pty name now, but don't open it in the parent. */
if (ptsname_r (fd, slave, sizeof slave) != 0)
goto error;
/* Create the handle last before we fork. */
h = create_handle ();
if (h == NULL)
goto error;
pid = fork ();
if (pid == -1)
goto error;
if (pid == 0) { /* Child. */
int slave_fd;
if (!(flags & MEXP_SPAWN_KEEP_SIGNALS)) {
struct sigaction sa;
int i;
/* Remove all signal handlers. See the justification here:
* https://www.redhat.com/archives/libvir-list/2008-August/msg00303.html
* We don't mask signal handlers yet, so this isn't completely
* race-free, but better than not doing it at all.
*/
memset (&sa, 0, sizeof sa);
sa.sa_handler = SIG_DFL;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
for (i = 1; i < NSIG; ++i)
sigaction (i, &sa, NULL);
}
setsid ();
/* Open the slave side of the pty. We must do this in the child
* after setsid so it becomes our controlling tty.
*/
slave_fd = open (slave, O_RDWR);
if (slave_fd == -1)
goto error;
if (!(flags & MEXP_SPAWN_COOKED_MODE)) {
struct termios termios;
/* Set raw mode. */
tcgetattr (slave_fd, &termios);
cfmakeraw (&termios);
tcsetattr (slave_fd, TCSANOW, &termios);
}
/* Set up stdin, stdout, stderr to point to the pty. */
dup2 (slave_fd, 0);
dup2 (slave_fd, 1);
dup2 (slave_fd, 2);
close (slave_fd);
/* Close the master side of the pty - do this late to avoid a
* kernel bug, see sshpass source code.
*/
close (fd);
if (!(flags & MEXP_SPAWN_KEEP_FDS)) {
int i, max_fd;
/* Close all other file descriptors. This ensures that we don't
* hold open (eg) pipes from the parent process.
*/
max_fd = sysconf (_SC_OPEN_MAX);
if (max_fd == -1)
max_fd = 1024;
if (max_fd > 65536)
max_fd = 65536; /* bound the amount of work we do here */
for (i = 3; i < max_fd; ++i)
close (i);
}
/* Run the subprocess. */
execvp (file, argv);
perror (file);
_exit (EXIT_FAILURE);
}
/* Parent. */
h->fd = fd;
h->pid = pid;
return h;
error:
err = errno;
if (fd >= 0)
close (fd);
if (pid > 0)
waitpid (pid, NULL, 0);
if (h != NULL)
mexp_close (h);
errno = err;
return NULL;
}
enum mexp_status
mexp_expect (mexp_h *h, const mexp_regexp *regexps, int *ovector, int ovecsize)
{
time_t start_t, now_t;
int timeout;
struct pollfd pfds[1];
int r;
ssize_t rs;
time (&start_t);
if (h->next_match == -1) {
/* Fully clear the buffer, then read. */
clear_buffer (h);
} else {
/* See the comment in the manual about h->next_match. We have
* some data remaining in the buffer, so begin by matching that.
*/
memmove (&h->buffer[0], &h->buffer[h->next_match], h->len - h->next_match);
h->len -= h->next_match;
h->buffer[h->len] = '\0';
h->next_match = -1;
goto try_match;
}
for (;;) {
/* If we've got a timeout then work out how many seconds are left.
* Timeout == 0 is not particularly well-defined, but it probably
* means "return immediately if there's no data to be read".
*/
if (h->timeout >= 0) {
time (&now_t);
timeout = h->timeout - ((now_t - start_t) * 1000);
if (timeout < 0)
timeout = 0;
}
else
timeout = 0;
pfds[0].fd = h->fd;
pfds[0].events = POLLIN;
pfds[0].revents = 0;
r = poll (pfds, 1, timeout);
#if DEBUG
fprintf (stderr, "DEBUG: poll returned %d\n", r);
#endif
if (r == -1)
return MEXP_ERROR;
if (r == 0)
return MEXP_TIMEOUT;
/* Otherwise we expect there is something to read from the file
* descriptor.
*/
if (h->alloc - h->len <= h->read_size) {
char *new_buffer;
/* +1 here allows us to store \0 after the data read */
new_buffer = realloc (h->buffer, h->alloc + h->read_size + 1);
if (new_buffer == NULL)
return MEXP_ERROR;
h->buffer = new_buffer;
h->alloc += h->read_size;
}
rs = read (h->fd, h->buffer + h->len, h->read_size);
#if DEBUG
fprintf (stderr, "DEBUG: read returned %zd\n", rs);
#endif
if (rs == -1) {
/* Annoyingly on Linux (I'm fairly sure this is a bug) if the
* writer closes the connection, the entire pty is destroyed,
* and read returns -1 / EIO. Handle that special case here.
*/
if (errno == EIO)
return MEXP_EOF;
return MEXP_ERROR;
}
if (rs == 0)
return MEXP_EOF;
/* We read something. */
h->len += rs;
h->buffer[h->len] = '\0';
#if DEBUG
fprintf (stderr, "DEBUG: read %zd bytes from pty\n", rs);
fprintf (stderr, "DEBUG: buffer content: %s\n", h->buffer);
#endif
try_match:
/* See if there is a full or partial match against any regexp. */
if (regexps) {
size_t i;
int can_clear_buffer = 1;
assert (h->buffer != NULL);
for (i = 0; regexps[i].r > 0; ++i) {
const int options = regexps[i].options | PCRE_PARTIAL_SOFT;
r = pcre_exec (regexps[i].re, regexps[i].extra,
h->buffer, (int)h->len, 0,
options,
ovector, ovecsize);
h->pcre_error = r;
if (r >= 0) {
/* A full match. */
if (ovector != NULL && ovecsize >= 1 && ovector[1] >= 0)
h->next_match = ovector[1];
else
h->next_match = -1;
return regexps[i].r;
}
else if (r == PCRE_ERROR_NOMATCH) {
/* No match at all. */
/* (nothing here) */
}
else if (r == PCRE_ERROR_PARTIAL) {
/* Partial match. Keep the buffer and keep reading. */
can_clear_buffer = 0;
}
else {
/* An actual PCRE error. */
return MEXP_PCRE_ERROR;
}
}
/* If none of the regular expressions matched (not partially)
* then we can clear the buffer. This is an optimization.
*/
if (can_clear_buffer)
clear_buffer (h);
} /* if (regexps) */
}
}
int
mexp_printf (mexp_h *h, const char *fs, ...)
{
va_list args;
char *msg;
int len;
size_t n;
ssize_t r;
char *p;
va_start (args, fs);
len = vasprintf (&msg, fs, args);
va_end (args);
if (len < 0)
return -1;
#if DEBUG
fprintf (stderr, "DEBUG: writing: %s\n", msg);
#endif
n = len;
p = msg;
while (n > 0) {
r = write (h->fd, p, n);
if (r == -1) {
free (msg);
return -1;
}
n -= r;
p += r;
}
free (msg);
return len;
}
int
mexp_send_interrupt (mexp_h *h)
{
return write (h->fd, "\003", 1);
}
|