Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gawk's inet hanging?

1 view
Skip to first unread message

Jeff Chua

unread,
Nov 5, 2009, 5:07:40 PM11/5/09
to Aharon Robbins, bug-gn...@gnu.org
I'm trying put gawk inet connection to the local smtp port and would like it
to detect error conditions, but don't know how.

Here's the code ...

awk '
BEGIN {
GAWK_SOCK_RETRIES = 2; # this doesn't seem to do anything
serv = "/inet/tcp/0/localhost/25";
print "help" |& serv;
}
'


It works when sendmail is running. But when I stop sendmail, running the
above code just hangs without reporting any error. Running "telnet localhost
25" returns error right away.

Thanks,
Jeff

Jeff Chua

unread,
Nov 5, 2009, 10:27:24 PM11/5/09
to BugGnuUtils, Aharon Robbins

Arnold,

> I'm trying put gawk inet connection to the local smtp port and would like
> it to detect error conditions, but don't know how.


I looked into the source code and it seems the code is testing "unsigned
long" for negative value ... and that won't work.

I've also modify the code to return ERRNO to user instead of exiting with
a fatal error in case the socket is no available, and also modified
sleep() to usleep() and allow GAWK_MSEC_SLEEP to retry in milliseconds
interval instead of the default 1 second.

Tested with this script.


export GAWK_SOCK_RETRIES=3;
export GAWK_MSEC_SLEEP=200;
gawk 'BEGIN {
serv = "/inet/tcp/0/localhost/25";
status = (serv |& getline);
printf("(%s) (%s) [%s]\n", ERRNO, status, $0);
}'


Patch below. Please consider.


Thanks,
Jeff.


--- gawk-stable/io.c 2009-10-30 21:46:43.000000000 +0800
+++ a/io.c 2009-11-06 10:55:01.000000000 +0800
@@ -708,10 +708,12 @@
if (! two_way_open(str, rp)) {
#ifdef HAVE_SOCKETS
/* multiple messages make life easier for translators */
- if (STREQN(str, "/inet/", 6))
- fatal(_("can't open two way socket `%s' for input/output (%s)"),
- str, strerror(errno));
- else
+ if (STREQN(str, "/inet/", 6)) {
+ *errflg = errno;
+ free_temp(tmp);
+ free_rp(rp);
+ return NULL;
+ } else
#endif
fatal(_("can't open two way pipe `%s' for input/output (%s)"),
str, strerror(errno));
@@ -1457,27 +1459,41 @@

{
#define DEFAULT_RETRIES 20
- static unsigned long def_retries = DEFAULT_RETRIES;
+ static long def_retries = DEFAULT_RETRIES;
static int first_time = TRUE;
- unsigned long retries = 0;
+ long retries = 0;
+ static long msleep = 1000;

if (first_time) {
char *cp, *end;
- unsigned long count = 0;
+ long count = 0;
+ char *ms2;

first_time = FALSE;
- if ((cp = getenv("GAWK_SOCK_RETRIES")) != NULL) {
+
+ if((cp = getenv("GAWK_SOCK_RETRIES")) != NULL) {
count = strtoul(cp, &end, 10);
if (end != cp && count > 0)
def_retries = count;
+ //warning(_("retries %d %d"), def_retries, count);
+ }
+
+ if((ms2 = getenv("GAWK_MSEC_SLEEP")) != NULL) {
+ msleep = atoi(ms2);
+ if(msleep < 0)
+ msleep = 1000;
+ //warning(_("msleep %d"), msleep);
}
}
retries = def_retries;

+ msleep *= 1000;
+
do {
openfd = socketopen(protocol, localpname, cp, hostname);
+ //warning(_("retries %d"), retries);
retries--;
- } while (openfd == INVALID_HANDLE && retries >= 0 && sleep(1) == 0);
+ } while (openfd == INVALID_HANDLE && retries >= 0 && usleep(msleep) == 0);
}

*localpnamelastcharp = '/';


0 new messages