lists.arthurdejong.org
RSS feed

nss-pam-ldapd commit: r1319 - nss-pam-ldapd/nslcd

[Date Prev][Date Next] [Thread Prev][Thread Next]

nss-pam-ldapd commit: r1319 - nss-pam-ldapd/nslcd



Author: arthur
Date: Wed Dec  8 23:54:06 2010
New Revision: 1319
URL: http://arthurdejong.org/viewvc/nss-pam-ldapd?view=rev&revision=1319

Log:
in each worker wake up once in a while to check whether any existing LDAP 
connections should be closed

Modified:
   nss-pam-ldapd/nslcd/myldap.c
   nss-pam-ldapd/nslcd/myldap.h
   nss-pam-ldapd/nslcd/nslcd.c

Modified: nss-pam-ldapd/nslcd/myldap.c
==============================================================================
--- nss-pam-ldapd/nslcd/myldap.c        Fri Dec  3 17:16:39 2010        (r1318)
+++ nss-pam-ldapd/nslcd/myldap.c        Wed Dec  8 23:54:06 2010        (r1319)
@@ -600,10 +600,7 @@
   }
 }
 
-/* This checks the timeout value of the session and closes the connection
-   to the LDAP server if the timeout has expired and there are no pending
-   searches. */
-static void myldap_session_check(MYLDAP_SESSION *session)
+void myldap_session_check(MYLDAP_SESSION *session)
 {
   int i;
   time_t current_time;
@@ -902,8 +899,10 @@
     if (nexttry>=endtime)
     {
       if (search->session->binddn[0]=='\0')
-        log_log(LOG_ERR,"no available LDAP server found");
-      return rc;
+      {
+        log_log(LOG_ERR,"no available LDAP server found: 
%s",ldap_err2string(rc));
+        return LDAP_UNAVAILABLE;
+      }
     }
     /* sleep between tries */
     sleeptime=nexttry-time(NULL);

Modified: nss-pam-ldapd/nslcd/myldap.h
==============================================================================
--- nss-pam-ldapd/nslcd/myldap.h        Fri Dec  3 17:16:39 2010        (r1318)
+++ nss-pam-ldapd/nslcd/myldap.h        Wed Dec  8 23:54:06 2010        (r1319)
@@ -76,6 +76,11 @@
    with these searches. This does not close the session. */
 void myldap_session_cleanup(MYLDAP_SESSION *session);
 
+/* This checks the timeout value of the session and closes the connection
+   to the LDAP server if the timeout has expired and there are no pending
+   searches. */
+void myldap_session_check(MYLDAP_SESSION *session);
+
 /* Close the session and free all the resources allocated for the session.
    After a call to this function the referenced handle is invalid. */
 void myldap_session_close(MYLDAP_SESSION *session);

Modified: nss-pam-ldapd/nslcd/nslcd.c
==============================================================================
--- nss-pam-ldapd/nslcd/nslcd.c Fri Dec  3 17:16:39 2010        (r1318)
+++ nss-pam-ldapd/nslcd/nslcd.c Wed Dec  8 23:54:06 2010        (r1319)
@@ -278,6 +278,7 @@
 static int create_socket(void)
 {
   int sock;
+  int i;
   struct sockaddr_un addr;
   /* create a socket */
   if ( (sock=socket(PF_UNIX,SOCK_STREAM,0))<0 )
@@ -291,6 +292,21 @@
     log_log(LOG_DEBUG,"unlink() of "NSLCD_SOCKET" failed (ignored): %s",
             strerror(errno));
   }
+  /* do not block on accept() */
+  if ((i=fcntl(sock,F_GETFL,0))<0)
+  {
+    log_log(LOG_ERR,"fctnl(F_GETFL) failed: %s",strerror(errno));
+    if (close(sock))
+      log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno));
+    exit(1);
+  }
+  if (fcntl(sock,F_SETFL,i|O_NONBLOCK)<0)
+  {
+    log_log(LOG_ERR,"fctnl(F_SETFL,O_NONBLOCK) failed: %s",strerror(errno));
+    if (close(sock))
+      log_log(LOG_WARNING,"problem closing socket: %s",strerror(errno));
+    exit(1);
+  }
   /* create socket address structure */
   memset(&addr,0,sizeof(struct sockaddr_un));
   addr.sun_family=AF_UNIX;
@@ -520,6 +536,8 @@
   int j;
   struct sockaddr_storage addr;
   socklen_t alen;
+  fd_set fds;
+  struct timeval tv;
   /* create a new LDAP session */
   session=myldap_create_session();
   /* clean up the session if we're done */
@@ -527,20 +545,40 @@
   /* start waiting for incoming connections */
   while (1)
   {
+    /* time out connection to LDAP server if needed */
+    myldap_session_check(session);
+    /* set up the set of fds to wait on */
+    FD_ZERO(&fds);
+    FD_SET(nslcd_serversocket,&fds);
+    /* set up our timeout value */
+    tv.tv_sec=nslcd_cfg->ldc_idle_timelimit;
+    tv.tv_usec=0;
     /* wait for a new connection */
-    alen=(socklen_t)sizeof(struct sockaddr_storage);
-    csock=accept(nslcd_serversocket,(struct sockaddr *)&addr,&alen);
+    
j=select(nslcd_serversocket+1,&fds,NULL,NULL,nslcd_cfg->ldc_idle_timelimit>0?&tv:NULL);
     /* see if we should exit before doing anything else */
     if (nslcd_exitsignal!=0)
       return NULL;
+    /* check result of select() */
+    if (j<0)
+    {
+      if (errno==EINTR)
+        log_log(LOG_DEBUG,"debug: select() failed (ignored): 
%s",strerror(errno));
+      else
+        log_log(LOG_ERR,"select() failed: %s",strerror(errno));
+      continue;
+    }
+    /* see if our file descriptor is actually ready */
+    if (!FD_ISSET(nslcd_serversocket,&fds))
+      continue;
+    /* wait for a new connection */
+    alen=(socklen_t)sizeof(struct sockaddr_storage);
+    csock=accept(nslcd_serversocket,(struct sockaddr *)&addr,&alen);
     if (csock<0)
     {
       if ((errno==EINTR)||(errno==EAGAIN)||(errno==EWOULDBLOCK))
-      {
         log_log(LOG_DEBUG,"accept() failed (ignored): %s",strerror(errno));
-        continue;
-      }
-      log_log(LOG_ERR,"accept() failed: %s",strerror(errno));
+      else
+        log_log(LOG_ERR,"accept() failed: %s",strerror(errno));
       continue;
     }
     /* make sure O_NONBLOCK is not inherited */
--
To unsubscribe send an email to
nss-pam-ldapd-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/nss-pam-ldapd-commits