Newsgroups: comp.sys.next.programmer
Subject: Problem with [Listener run] and DPSAddFD().
From: scott@nic.gac.edu (Scott Hess)
Date: 15 Oct 92 07:34:12
Distribution: world
Organization: Gustavus Adolphus College


The included program demonstrates a problem I've been having with
DPSAddFD() when used in a Listener run-loop.  For some obscure
reason, I get loads of unwelcome error output, and it apparently
isn't related to anything _I'm_ doing.  The example program is an
_extremely_ simplified version of the program I'm working on.  I
get the same results when using other types of file descriptor,
such as ptys and sockets, which leads me to believe that it's
something entirely within the Listener run-loop code.

Anyone have any ideas on why this happens, and how to suppress it?
This happens under NS2.1 - it'd be interesting to hear what happens
under NS3.0, I guess.

------ tester.m ------
/* Compile this program with "cc -o tester tester.m -lNeXT_s -lsys_s".
 *
 * Run "tester".  Type a line and hit return (or, heck, just hit
 * return).  Note the output:
 *
 *	Error raised in Listener run loop, continuing ...
 *
 * This is simply unacceptable for daemons doing any non-trivial
 * amount of processing, since you'd end up with a console full of
 * this output.  Anyone know why I'm seeing this, and how to avoid it?
 */
#import <appkit/Listener.h>
#import <appkit/Application.h>
#import <sys/errno.h>
#import <libc.h>

/* Read the data from fd and send it to stdout.
 */
static void reader( int fd, void *data)
{
#define BUFFSIZE 4096
    static unsigned char inBuffer[ BUFFSIZE];
    int ret=read( fd, inBuffer, BUFFSIZE);
    if( ret>0) {
	write( 1, inBuffer, ret);
    } else if( ret && cthread_errno()!=EWOULDBLOCK && cthread_errno()!=EINTR) {
	perror( "reading input");
    } else if( !ret) {
	exit( 1);
    }
}
void main( void)
{
    /* Make stdin non-blocking. */
    int flags;
    flags=fcntl( 0, F_GETFL, 0);
    flags|=FNDELAY;
    fcntl( 0, F_SETFL, flags);
    
    /* Register to watch stdin for input. */
    DPSAddFD( 0, (DPSFDProc)reader, NULL, NX_BASETHRESHOLD);
    
    /* Enter the event loop. */
    [Listener run];
}
------ tester.m ------

Well, not to answer my own question, or anything, but based on a
long-shot hunch, the following program does work correctly without
the unwanted output:

------ tester1.m ------
/* Compile this program with "cc -o tester1 tester1.m -lNeXT_s -lsys_s".
 */
#import <appkit/Listener.h>
#import <appkit/Application.h>
#import <sys/errno.h>
#import <libc.h>
#import <sys/notify.h>

/* Read the data from fd and send it to stdout.
 */
static void reader( int fd, void *data)
{
#define BUFFSIZE 4096
    static unsigned char inBuffer[ BUFFSIZE];
    int ret=read( fd, inBuffer, BUFFSIZE);
    if( ret>0) {
	write( 1, inBuffer, ret);
    } else if( ret && cthread_errno()!=EWOULDBLOCK && cthread_errno()!=EINTR) {
	perror( "reading input");
    } else if( !ret) {
	exit( 1);
    }
}
static void barf( notification_t *msg, void *data)
{
}
void main( void)
{
    /* Make stdin non-blocking. */
    int flags;
    flags=fcntl( 0, F_GETFL, 0);
    flags|=FNDELAY;
    fcntl( 0, F_SETFL, flags);
    
    /* Register to watch stdin for input. */
    DPSAddFD( 0, (DPSFDProc)reader, NULL, NX_BASETHRESHOLD);
    
    DPSAddPort( task_self(), (void *)barf, 64, NULL, 31);
    
    /* Enter the event loop. */
    [Listener run];
}
------ tester1.m ------

The only change is that I add an arbitrary Mach port to be listened
to, also.  And then it works.  [It's probably not a great idea to
be using task_self() as this port, but it was at hand, and it's
not like the program above is all that important in the grand scheme
of things.]

So, though I've got something that _appears_ to make things work like
I want them to, it does is in a non-intuitive, and frankly hacked
manner.  It's obviously _not_ how this is supposed to work!  I'd still
be interested in a relatively decent explaination.

Thanks,
--
scott hess <shess@ssesco.com>     <I will not spin the turtle -Bart Simpson>
12901 Upton Avenue South, #326  Burnsville, MN 55337 (612) 895-1208 Anytime!
  * ... like a big church choir with all it's members' mouths wide open, *
               * singing silence   -Blood Music, Greg Bear *