/*
-** Copyright 2000-2006 Double Precision, Inc. See COPYING for
+** Copyright 2000-2007 Double Precision, Inc. See COPYING for
** distribution information.
*/
#include "liblock.h"
#include <stdio.h>
#include <signal.h>
+#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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);
static int start1(const char *lockfile, int fd)
{
-int lockfd;
+int lockfd, maxfd;
#if HAVE_SETPGRP
#if SETPGRP_VOID
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)
{