Merge from debian.
[hcoop/debian/courier-authlib.git] / liblock / lockdaemon.c
index 8f75f36..0b0151a 100644 (file)
@@ -1,5 +1,5 @@
 /*
-** Copyright 2000-2006 Double Precision, Inc.  See COPYING for
+** Copyright 2000-2007 Double Precision, Inc.  See COPYING for
 ** distribution information.
 */
 
@@ -7,6 +7,7 @@
 #include       "liblock.h"
 #include       <stdio.h>
 #include       <signal.h>
+#include       <limits.h>
 #include       <stdlib.h>
 #include       <string.h>
 #include       <unistd.h>
@@ -32,7 +33,7 @@
 
 #define exit(_a_) _exit(_a_)
 
-static const char rcsid[]="$Id: lockdaemon.c,v 1.10 2006/05/28 15:29:52 mrsam Exp $";
+static const char rcsid[]="$Id: lockdaemon.c,v 1.14 2007/10/11 01:46:34 mrsam Exp $";
 
 static int start1(const char *, int);
 
@@ -110,7 +111,7 @@ int i;
 
 static int start1(const char *lockfile, int fd)
 {
-int    lockfd;
+int    lockfd, maxfd;
 
 #if     HAVE_SETPGRP
 #if     SETPGRP_VOID
@@ -145,12 +146,28 @@ int       lockfd;
                lockfd=open(lockfile, O_RDWR|O_CREAT, 0600);
        }
 
-       if (lockfd < 0)
+#if HAVE_GETDTABLESIZE
+       maxfd=getdtablesize()-1;
+#elif defined(OPEN_MAX)
+       maxfd=OPEN_MAX-1;
+#elif HAVE_SYSCONF && defined(_SC_OPEN_MAX)
+       if ((maxfd=sysconf(_SC_OPEN_MAX)) < 0)
+               maxfd=63;
+       else if (maxfd > 0)
+               maxfd--;
+#else
+       maxfd=63;
+#endif
+
+       if (lockfd < 0 || dup2(lockfd, maxfd) != maxfd)
        {
                perror(lockfile);
                exit(1);
        }
 
+       close(lockfd);
+       lockfd=maxfd;
+
 #ifdef FD_CLOEXEC
        if (fcntl(lockfd, F_SETFD, FD_CLOEXEC) < 0)
        {