Keyphrase not detected and timeout
Run applications under pseudo-terminal (PTY) sessions
Brought to you by:
mezantrop
Keyphrase is not detected and timeout when -w option.
When keyphrase is at the end of a large text and when read is split between two reads then empty will not detect it and it will fail with "empty: Data stream is empty. Keyphrase wasn't found. Exit on timeout". The reason lies in watch4str: largv=0 assignement after each read. Due to the part of the keyphrase from the first read() is overwriten by the second part of the keyphrase (and not appended, as it suppose to) in the next read(), therefor not detected. The watch4str will make another attempt to read() and because this is the last line in the text it will remain blocked select() will exit on timeout.
Hi. Let me suggest a patch that (i hope) can fix this issue. Note, that this patch slightly changes behavior of -w option: empty will flush buffers after each '\n' received. So regexp like "assword:\n\n" will never match. But does anybody need it?.
PS I know, looks like it is bad place for patch :) Where should I send it?
From 154a1849caefb764987386433639a0e0a4894e33 Mon Sep 17 00:00:00 2001
From: Sergey Mironov <ierton@gmail.com>
Date: Tue, 20 Jul 2010 16:50:38 +0400
Subject: [PATCH] Fix -w regex match issues (probably)
---
empty.c | 24 ++++++++++++++++--------
1 files changed, 16 insertions(+), 8 deletions(-)
diff --git a/empty.c b/empty.c
index 555dcb5..86ca53e 100644
--- a/empty.c
+++ b/empty.c
@@ -105,7 +105,7 @@
#define tmpdir "/tmp"
#define program "empty"
-#define version "0.6.18b"
+#define version "0.6.19b"
/* -------------------------------------------------------------------------- */
static void usage(void);
@@ -874,8 +874,10 @@ int watch4str(int ifd, int ofd, int argc, char *argv[],
struct timeval tv;
int argt = 0;
- int largv = 0;
+ int bread = 0;
char *resp = NULL;
+ int found;
+ int i;
stime = time(0);
@@ -893,11 +895,10 @@ int watch4str(int ifd, int ofd, int argc, char *argv[],
perrx(255, "Fatal select()");
if (n > 0 && FD_ISSET(ifd, &rfd)) {
- largv = 0;
- if ((cc = read(ifd, buf + largv, sizeof(buf) - largv)) > 0) {
+ if ((cc = read(ifd, buf + bread, sizeof(buf)-bread-1)) > 0) {
stime = time(0);
- buf[cc + largv] = '\0';
+ buf[cc + bread] = '\0';
if (vflg)
(void)printf("%s", buf);
@@ -914,10 +915,17 @@ int watch4str(int ifd, int ofd, int argc, char *argv[],
return (argt + 1) / 2;
}
- if (largv == 0)
- largv = longargv(argc, argv);
+ for(found=0,i=bread; i<bread+cc; i++) {
+ if(buf[i] == '\n') {
+ memmove(buf, buf+i+1, bread+cc-i-1);
+ bread = bread+cc-i-1;
+ found = 1;
+ break;
+ }
+ }
- memmove(buf, buf + cc - largv, largv);
+ if(!found)
+ bread += cc;
}
if (cc <= 0) {
--
1.5.2.1
I've made a git branch of empty which contains patch reported and some other. Please take a look at http://git.ierton.ru/?p=empty.git;a=summary
Thank you so much. Accepted. Better late than never, right? :)
Added here https://sourceforge.net/projects/empty/files/empty/empty-0.6.23b