2 * Copyright (C) 2014 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <sys/types.h>
39 /* RHEL 6 pcre did not define PCRE_PARTIAL_SOFT. However PCRE_PARTIAL
40 * is a synonym so use that.
42 #ifndef PCRE_PARTIAL_SOFT
43 #define PCRE_PARTIAL_SOFT PCRE_PARTIAL
46 #include "miniexpect.h"
53 mexp_h *h = malloc (sizeof *h);
57 /* Initialize the fields to default values. */
64 h->len = h->alloc = 0;
66 h->user1 = h->user2 = h->user3 = NULL;
72 clear_buffer (mexp_h *h)
76 h->alloc = h->len = 0;
81 mexp_close (mexp_h *h)
90 if (waitpid (h->pid, &status, 0) == -1)
100 mexp_spawnlf (unsigned flags, const char *file, const char *arg, ...)
102 char **argv, **new_argv;
107 argv = malloc (sizeof (char *));
110 argv[0] = (char *) arg;
112 va_start (args, arg);
113 for (i = 1; arg != NULL; ++i) {
114 arg = va_arg (args, const char *);
115 new_argv = realloc (argv, sizeof (char *) * (i+1));
116 if (new_argv == NULL) {
122 argv[i] = (char *) arg;
125 h = mexp_spawnvf (flags, file, argv);
132 mexp_spawnvf (unsigned flags, const char *file, char **argv)
140 fd = posix_openpt (O_RDWR|O_NOCTTY);
144 if (grantpt (fd) == -1)
147 if (unlockpt (fd) == -1)
150 /* Get the slave pty name now, but don't open it in the parent. */
151 if (ptsname_r (fd, slave, sizeof slave) != 0)
154 /* Create the handle last before we fork. */
155 h = create_handle ();
163 if (pid == 0) { /* Child. */
166 if (!(flags & MEXP_SPAWN_KEEP_SIGNALS)) {
170 /* Remove all signal handlers. See the justification here:
171 * https://www.redhat.com/archives/libvir-list/2008-August/msg00303.html
172 * We don't mask signal handlers yet, so this isn't completely
173 * race-free, but better than not doing it at all.
175 memset (&sa, 0, sizeof sa);
176 sa.sa_handler = SIG_DFL;
178 sigemptyset (&sa.sa_mask);
179 for (i = 1; i < NSIG; ++i)
180 sigaction (i, &sa, NULL);
185 /* Open the slave side of the pty. We must do this in the child
186 * after setsid so it becomes our controlling tty.
188 slave_fd = open (slave, O_RDWR);
192 if (!(flags & MEXP_SPAWN_COOKED_MODE)) {
193 struct termios termios;
196 tcgetattr (slave_fd, &termios);
197 cfmakeraw (&termios);
198 tcsetattr (slave_fd, TCSANOW, &termios);
201 /* Set up stdin, stdout, stderr to point to the pty. */
207 /* Close the master side of the pty - do this late to avoid a
208 * kernel bug, see sshpass source code.
212 if (!(flags & MEXP_SPAWN_KEEP_FDS)) {
215 /* Close all other file descriptors. This ensures that we don't
216 * hold open (eg) pipes from the parent process.
218 max_fd = sysconf (_SC_OPEN_MAX);
222 max_fd = 65536; /* bound the amount of work we do here */
223 for (i = 3; i < max_fd; ++i)
227 /* Run the subprocess. */
230 _exit (EXIT_FAILURE);
244 waitpid (pid, NULL, 0);
252 mexp_expect (mexp_h *h, const mexp_regexp *regexps, int *ovector, int ovecsize)
254 time_t start_t, now_t;
256 struct pollfd pfds[1];
262 if (h->next_match == -1) {
263 /* Fully clear the buffer, then read. */
266 /* See the comment in the manual about h->next_match. We have
267 * some data remaining in the buffer, so begin by matching that.
269 memmove (&h->buffer[0], &h->buffer[h->next_match], h->len - h->next_match);
270 h->len -= h->next_match;
271 h->buffer[h->len] = '\0';
277 /* If we've got a timeout then work out how many seconds are left.
278 * Timeout == 0 is not particularly well-defined, but it probably
279 * means "return immediately if there's no data to be read".
281 if (h->timeout >= 0) {
283 timeout = h->timeout - ((now_t - start_t) * 1000);
291 pfds[0].events = POLLIN;
293 r = poll (pfds, 1, timeout);
295 fprintf (stderr, "DEBUG: poll returned %d\n", r);
303 /* Otherwise we expect there is something to read from the file
306 if (h->alloc - h->len <= h->read_size) {
308 /* +1 here allows us to store \0 after the data read */
309 new_buffer = realloc (h->buffer, h->alloc + h->read_size + 1);
310 if (new_buffer == NULL)
312 h->buffer = new_buffer;
313 h->alloc += h->read_size;
315 rs = read (h->fd, h->buffer + h->len, h->read_size);
317 fprintf (stderr, "DEBUG: read returned %zd\n", rs);
320 /* Annoyingly on Linux (I'm fairly sure this is a bug) if the
321 * writer closes the connection, the entire pty is destroyed,
322 * and read returns -1 / EIO. Handle that special case here.
331 /* We read something. */
333 h->buffer[h->len] = '\0';
335 fprintf (stderr, "DEBUG: read %zd bytes from pty\n", rs);
336 fprintf (stderr, "DEBUG: buffer content: %s\n", h->buffer);
340 /* See if there is a full or partial match against any regexp. */
343 int can_clear_buffer = 1;
345 assert (h->buffer != NULL);
347 for (i = 0; regexps[i].r > 0; ++i) {
348 int options = regexps[i].options | PCRE_PARTIAL_SOFT;
350 r = pcre_exec (regexps[i].re, regexps[i].extra,
351 h->buffer, (int)h->len, 0,
358 if (ovector != NULL && ovecsize >= 1 && ovector[1] >= 0)
359 h->next_match = ovector[1];
365 else if (r == PCRE_ERROR_NOMATCH) {
366 /* No match at all. */
370 else if (r == PCRE_ERROR_PARTIAL) {
371 /* Partial match. Keep the buffer and keep reading. */
372 can_clear_buffer = 0;
376 /* An actual PCRE error. */
377 return MEXP_PCRE_ERROR;
381 /* If none of the regular expressions matched (not partially)
382 * then we can clear the buffer. This is an optimization.
384 if (can_clear_buffer)
392 mexp_printf (mexp_h *h, const char *fs, ...)
402 len = vasprintf (&msg, fs, args);
409 fprintf (stderr, "DEBUG: writing: %s\n", msg);
415 r = write (h->fd, p, n);
429 mexp_send_interrupt (mexp_h *h)
431 return write (h->fd, "\003", 1);