]> arthur.barton.de Git - netatalk.git/blobdiff - etc/papd/session.c
autoconf POSIX.1 sys/wait.h check
[netatalk.git] / etc / papd / session.c
index 4d73255da4f87e2b6080dbe0480e37cec211ebf0..69d3f5e8b18b4d5dff05503d25265a97c6692fc5 100644 (file)
@@ -1,8 +1,22 @@
 /*
+ * $Id: session.c,v 1.11 2001-09-06 19:04:40 rufustfirefly Exp $
+ *
  * Copyright (c) 1990,1994 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#ifdef HAVE_SYS_ERRNO_H
+#include <sys/errno.h>
+#endif /* HAVE_SYS_ERRNO_H */
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif /* HAVE_ERRNO_H */
+
+#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/syslog.h>
 #include <atalk/pap.h>
 
 #include "file.h"
+#include "lp.h"
+#include "session.h"
+
+int ps(struct papfile *infile, struct papfile *outfile, struct sockaddr_at *sat);
 
 extern unsigned char   connid, quantum, oquantum;
 
@@ -44,7 +62,7 @@ struct iovec  iov[ PAP_MAXQUANTUM ] = {
  * Read lines of a file, until the client sends eof, after
  * which we'll send eof also.
  */
-session( atp, sat )
+int session( atp, sat )
     ATP                        atp;
     struct sockaddr_at *sat;
 {
@@ -77,7 +95,7 @@ session( atp, sat )
     cbuf[ 1 ] = PAP_READ;
     if (++seq == 0) seq = 1;
     netseq = htons( seq );
-    bcopy( &netseq, &cbuf[ 2 ], sizeof( netseq ));
+    memcpy( &cbuf[ 2 ], &netseq, sizeof( netseq ));
     atpb.atp_saddr = sat;
     atpb.atp_sreqdata = cbuf;
     atpb.atp_sreqdlen = 4;             /* bytes in SendData request */
@@ -85,7 +103,7 @@ session( atp, sat )
     atpb.atp_sreqtries = -1;           /* infinite retries */
     if ( atp_sreq( atp, &atpb, oquantum, ATP_XO )) {
        syslog( LOG_ERR, "atp_sreq: %m" );
-       exit( 1 );
+       return( -1 );
     }
 
     for (;;) {
@@ -101,15 +119,20 @@ session( atp, sat )
        FD_ZERO( &fds );
        FD_SET( atp_fileno( atp ), &fds );
 
-       if (( cc = select( FD_SETSIZE, &fds, 0, 0, &tv )) < 0 ) {
-           syslog( LOG_ERR, "select: %m" );
-           exit( 1 );
+       do { /* do list until success or an unrecoverable error occurs */
+         if (( cc = select( FD_SETSIZE, &fds, 0, 0, &tv )) < 0 )
+             syslog( LOG_ERR, "select: %m" ); /* log all errors */
+       } while (( cc < 0 ) && (errno == 4));
+
+       if ( cc < 0 ) {
+         syslog( LOG_ERR, "select: Error is unrecoverable" );
+         return( -1 );
        }
        if ( cc == 0 ) {
            if ( timeout++ > 2 ) {
                syslog( LOG_ERR, "connection timed out" );
                lp_cancel();
-               exit( 1 );
+               return( -1 );
            }
 
            /*
@@ -125,14 +148,14 @@ session( atp, sat )
            atpb.atp_sreqtries = 1;             /* try once */
            if ( atp_sreq( atp, &atpb, 0, 0 )) {
                syslog( LOG_ERR, "atp_sreq: %m" );
-               exit( 1 );
+               return( -1 );
            }
            continue;
        } else {
            timeout = 0;
        }
 
-       bzero( &ssat, sizeof( struct sockaddr_at ));
+       memset( &ssat, 0, sizeof( struct sockaddr_at ));
        switch( atp_rsel( atp, &ssat, ATP_TRESP | ATP_TREQ )) {
        case ATP_TREQ :
            atpb.atp_saddr = &ssat;
@@ -140,7 +163,7 @@ session( atp, sat )
            atpb.atp_rreqdlen = sizeof( cbuf );
            if ( atp_rreq( atp, &atpb ) < 0 ) {
                syslog( LOG_ERR, "atp_rreq: %m" );
-               exit( 1 );
+               return( -1 );
            }
            /* sanity */
            if ( (unsigned char)cbuf[ 0 ] != connid ) {
@@ -153,7 +176,7 @@ session( atp, sat )
                /*
                 * Other side is ready for some data.
                 */
-               bcopy( &cbuf[ 2 ], &netseq, sizeof( netseq ));
+               memcpy( &netseq, &cbuf[ 2 ], sizeof( netseq ));
                if ( netseq != 0 ) {
                    if ( rseq != ntohs( netseq )) {
                        break;
@@ -188,7 +211,7 @@ session( atp, sat )
                    syslog( LOG_ERR, "atp_sresp: %m" );
                    exit( 1 );
                }
-               exit( 0 );
+               return( 0 );
                break;
 
            case PAP_TICKLE :
@@ -208,7 +231,7 @@ session( atp, sat )
            atpb.atp_rresiovcnt = oquantum;
            if ( atp_rresp( atp, &atpb ) < 0 ) {
                syslog( LOG_ERR, "atp_rresp: %m" );
-               exit( 1 );
+               return( -1 );
            }
 
            /* sanity */
@@ -228,9 +251,9 @@ session( atp, sat )
            }
 
            /* move data */
-           if ( ps( &infile, &outfile ) < 0 ) {
+           if ( ps( &infile, &outfile, sat ) < 0 ) {
                syslog( LOG_ERR, "parse: bad return" );
-               exit( 1 );      /* really?  close? */
+               return( -1 );   /* really?  close? */
            }
 
            /*
@@ -240,7 +263,7 @@ session( atp, sat )
            cbuf[ 1 ] = PAP_READ;
            if ( ++seq == 0 ) seq = 1;
            netseq = htons( seq );
-           bcopy( &netseq, &cbuf[ 2 ], sizeof( netseq ));
+           memcpy( &cbuf[ 2 ], &netseq, sizeof( netseq ));
            atpb.atp_saddr = sat;
            atpb.atp_sreqdata = cbuf;
            atpb.atp_sreqdlen = 4;              /* bytes in SendData request */
@@ -248,7 +271,7 @@ session( atp, sat )
            atpb.atp_sreqtries = -1;            /* infinite retries */
            if ( atp_sreq( atp, &atpb, oquantum, ATP_XO )) {
                syslog( LOG_ERR, "atp_sreq: %m" );
-               exit( 1 );
+               return( -1 );
            }
            break;
 
@@ -257,7 +280,7 @@ session( atp, sat )
 
        default :
            syslog( LOG_ERR, "atp_rsel: %m" );
-           exit( 1 );
+           return( -1 );
        }
 
        /* send any data that we have */
@@ -281,7 +304,7 @@ session( atp, sat )
                }
 
                niov[ i ].iov_len = 4 + cc;
-               bcopy( outfile.pf_data, niov[ i ].iov_base + 4, cc );
+               memcpy( niov[ i ].iov_base + 4, outfile.pf_data, cc );
                CONSUME( &outfile, cc );
                if ( outfile.pf_datalen == 0 ) {
                    i++;
@@ -294,7 +317,7 @@ session( atp, sat )
            atpb.atp_sresiovcnt = i;    /* reported by stevebn@pc1.eos.co.uk */
            if ( atp_sresp( atp, &atpb ) < 0 ) {
                syslog( LOG_ERR, "atp_sresp: %m" );
-               exit( 1 );
+               return( -1 );
            }
            readpending = 0;
        }