]> arthur.barton.de Git - netatalk.git/blobdiff - etc/papd/session.c
Remove bdb env on exit
[netatalk.git] / etc / papd / session.c
index 8065633688cfb2ad5bb73b29234b5f30dc914b41..7067f5f0acb18a25a5eed03a1edb89ad74c72c7d 100644 (file)
@@ -1,16 +1,25 @@
 /*
+ * $Id: session.c,v 1.20 2009-10-16 01:10:59 didg Exp $
+ *
  * Copyright (c) 1990,1994 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
  */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
+#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/logger.h>
 #include <sys/time.h>
 #include <sys/uio.h>
 #include <netatalk/endian.h>
@@ -26,8 +35,8 @@ int ps(struct papfile *infile, struct papfile *outfile, struct sockaddr_at *sat)
 
 extern unsigned char   connid, quantum, oquantum;
 
-char           buf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
-struct iovec   niov[ PAP_MAXQUANTUM ] = {
+static char            buf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
+static struct iovec    niov[ PAP_MAXQUANTUM ] = {
     { buf[ 0 ],        0 },
     { buf[ 1 ],        0 },
     { buf[ 2 ],        0 },
@@ -37,25 +46,13 @@ struct iovec        niov[ PAP_MAXQUANTUM ] = {
     { buf[ 6 ],        0 },
     { buf[ 7 ],        0 },
 };
-struct iovec   iov[ PAP_MAXQUANTUM ] = {
-    { buf[ 0 ] + 4,    0 },
-    { buf[ 1 ] + 4,    0 },
-    { buf[ 2 ] + 4,    0 },
-    { buf[ 3 ] + 4,    0 },
-    { buf[ 4 ] + 4,    0 },
-    { buf[ 5 ] + 4,    0 },
-    { buf[ 6 ] + 4,    0 },
-    { buf[ 7 ] + 4,    0 },
-};
 
 /*
  * Accept files until the client closes the connection.
  * Read lines of a file, until the client sends eof, after
  * which we'll send eof also.
  */
-int session( atp, sat )
-    ATP                        atp;
-    struct sockaddr_at *sat;
+int session(ATP atp, struct sockaddr_at *sat)
 {
     struct timeval     tv;
     struct atp_block   atpb;
@@ -65,19 +62,19 @@ int session( atp, sat )
     char               cbuf[ 578 ];
     int                        i, cc, timeout = 0, readpending = 0;
     u_int16_t          seq = 0, rseq = 1, netseq;
-    u_char             readport;
+    u_char             readport; /* uninitialized, OK 310105 */
 
     infile.pf_state = PF_BOT;
     infile.pf_bufsize = 0;
     infile.pf_datalen = 0;
-    infile.pf_buf = 0;
-    infile.pf_data = 0;
+    infile.pf_buf = NULL;
+    infile.pf_data = NULL;
 
     outfile.pf_state = PF_BOT;
     outfile.pf_bufsize = 0;
     outfile.pf_datalen = 0;
-    outfile.pf_buf = 0;
-    outfile.pf_data = 0;
+    outfile.pf_buf = NULL;
+    outfile.pf_data = NULL;
 
     /*
      * Ask for data.
@@ -86,14 +83,14 @@ int 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 */
     atpb.atp_sreqto = 5;               /* retry timer */
     atpb.atp_sreqtries = -1;           /* infinite retries */
     if ( atp_sreq( atp, &atpb, oquantum, ATP_XO )) {
-       syslog( LOG_ERR, "atp_sreq: %m" );
+       LOG(log_error, logtype_papd, "atp_sreq: %s", strerror(errno) );
        return( -1 );
     }
 
@@ -110,13 +107,18 @@ int 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" );
-           return( -1 );
+       do { /* do list until success or an unrecoverable error occurs */
+         if (( cc = select( FD_SETSIZE, &fds, NULL, NULL, &tv )) < 0 )
+             LOG(log_error, logtype_papd, "select: %s", strerror(errno) ); /* log all errors */
+       } while (( cc < 0 ) && (errno == 4));
+
+       if ( cc < 0 ) {
+         LOG(log_error, logtype_papd, "select: Error is unrecoverable" );
+         return( -1 );
        }
        if ( cc == 0 ) {
            if ( timeout++ > 2 ) {
-               syslog( LOG_ERR, "connection timed out" );
+               LOG(log_error, logtype_papd, "connection timed out" );
                lp_cancel();
                return( -1 );
            }
@@ -133,7 +135,7 @@ int session( atp, sat )
            atpb.atp_sreqto = 0;                /* best effort */
            atpb.atp_sreqtries = 1;             /* try once */
            if ( atp_sreq( atp, &atpb, 0, 0 )) {
-               syslog( LOG_ERR, "atp_sreq: %m" );
+               LOG(log_error, logtype_papd, "atp_sreq: %s", strerror(errno) );
                return( -1 );
            }
            continue;
@@ -148,12 +150,12 @@ int session( atp, sat )
            atpb.atp_rreqdata = cbuf;
            atpb.atp_rreqdlen = sizeof( cbuf );
            if ( atp_rreq( atp, &atpb ) < 0 ) {
-               syslog( LOG_ERR, "atp_rreq: %m" );
+               LOG(log_error, logtype_papd, "atp_rreq: %s", strerror(errno) );
                return( -1 );
            }
            /* sanity */
            if ( (unsigned char)cbuf[ 0 ] != connid ) {
-               syslog( LOG_ERR, "Bad ATP request!" );
+               LOG(log_error, logtype_papd, "Bad ATP request!" );
                continue;
            }
 
@@ -162,7 +164,7 @@ int 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;
@@ -194,7 +196,7 @@ int session( atp, sat )
                atpb.atp_sresiov = niov;
                atpb.atp_sresiovcnt = 1;
                if ( atp_sresp( atp, &atpb ) < 0 ) {
-                   syslog( LOG_ERR, "atp_sresp: %m" );
+                   LOG(log_error, logtype_papd, "atp_sresp: %s", strerror(errno) );
                    exit( 1 );
                }
                return( 0 );
@@ -203,7 +205,7 @@ int session( atp, sat )
            case PAP_TICKLE :
                break;
            default :
-               syslog( LOG_ERR, "Bad PAP request!" );
+               LOG(log_error, logtype_papd, "Bad PAP request!" );
            }
 
            break;
@@ -216,20 +218,20 @@ int session( atp, sat )
            atpb.atp_rresiov = niov;
            atpb.atp_rresiovcnt = oquantum;
            if ( atp_rresp( atp, &atpb ) < 0 ) {
-               syslog( LOG_ERR, "atp_rresp: %m" );
+               LOG(log_error, logtype_papd, "atp_rresp: %s", strerror(errno) );
                return( -1 );
            }
 
            /* sanity */
            if ( ((unsigned char *)niov[ 0 ].iov_base)[ 0 ] != connid ||
                    ((char *)niov[ 0 ].iov_base)[ 1 ] != PAP_DATA ) {
-               syslog( LOG_ERR, "Bad data response!" );
+               LOG(log_error, logtype_papd, "Bad data response!" );
                continue;
            }
 
            for ( i = 0; i < atpb.atp_rresiovcnt; i++ ) {
                append( &infile,
-                       niov[ i ].iov_base + 4, niov[ i ].iov_len - 4 );
+                       (char *)niov[ i ].iov_base + 4, niov[ i ].iov_len - 4 );
                if (( infile.pf_state & PF_EOF ) == 0 &&
                        ((char *)niov[ 0 ].iov_base)[ 2 ] ) {
                    infile.pf_state |= PF_EOF;
@@ -238,7 +240,7 @@ int session( atp, sat )
 
            /* move data */
            if ( ps( &infile, &outfile, sat ) < 0 ) {
-               syslog( LOG_ERR, "parse: bad return" );
+               LOG(log_error, logtype_papd, "parse: bad return" );
                return( -1 );   /* really?  close? */
            }
 
@@ -256,7 +258,7 @@ int session( atp, sat )
            atpb.atp_sreqto = 5;                /* retry timer */
            atpb.atp_sreqtries = -1;            /* infinite retries */
            if ( atp_sreq( atp, &atpb, oquantum, ATP_XO )) {
-               syslog( LOG_ERR, "atp_sreq: %m" );
+               LOG(log_error, logtype_papd, "atp_sreq: %s", strerror(errno) );
                return( -1 );
            }
            break;
@@ -265,7 +267,7 @@ int session( atp, sat )
            break;
 
        default :
-           syslog( LOG_ERR, "atp_rsel: %m" );
+           LOG(log_error, logtype_papd, "atp_rsel: %s", strerror(errno) );
            return( -1 );
        }
 
@@ -290,7 +292,7 @@ int session( atp, sat )
                }
 
                niov[ i ].iov_len = 4 + cc;
-               memcpy( niov[ i ].iov_base + 4, outfile.pf_data, cc );
+               memcpy( (char *)niov[ i ].iov_base + 4, outfile.pf_data, cc );
                CONSUME( &outfile, cc );
                if ( outfile.pf_datalen == 0 ) {
                    i++;
@@ -302,7 +304,7 @@ int session( atp, sat )
            atpb.atp_sresiov = niov;
            atpb.atp_sresiovcnt = i;    /* reported by stevebn@pc1.eos.co.uk */
            if ( atp_sresp( atp, &atpb ) < 0 ) {
-               syslog( LOG_ERR, "atp_sresp: %m" );
+               LOG(log_error, logtype_papd, "atp_sresp: %s", strerror(errno) );
                return( -1 );
            }
            readpending = 0;