]> granicus.if.org Git - cronie/commitdiff
Portability: File locking by fcntl, lockf or flock
authorSATOH Fumiyasu <fumiyas@osstech.co.jp>
Thu, 26 Jun 2008 08:42:46 +0000 (10:42 +0200)
committerMarcela Mašláňová <mmaslano@redhat.com>
Thu, 26 Jun 2008 08:42:46 +0000 (10:42 +0200)
Signed-off-by: Marcela Mašláňová <mmaslano@redhat.com>
configure.ac
src/misc.c

index f07e5694653fedf33bc83d0b8632ee0cf638b527..a33d8a880e4872e32d704a5dd4a80881039ac951 100644 (file)
@@ -41,6 +41,12 @@ AC_CHECK_HEADERS( \
         utime.h \
 )
 
+AC_CHECK_FUNCS( \
+       fcntl \
+       lockf \
+       flock \
+)
+
 dnl Checks for typedefs, structures, and compiler characteristics.
 AC_C_CONST
 AC_TYPE_SIGNAL
index cfe7aa2b31144ee303c8660da0459ab4aa5a7749..24917ac394c3262f288920c5c1d00708508e0031 100644 (file)
 #include <libaudit.h>
 #endif
 
+#ifdef HAVE_FCNTL_H    /* fcntl(2) */
+# include <fcntl.h>
+#endif
+#ifdef HAVE_UNISTD_H   /* lockf(3) */
+# include <unistd.h>
+#endif
+#ifdef HAVE_FLOCK      /* flock(2) */
+# include <sys/file.h>
+#endif
+#include <stdio.h>
+
 #if defined(SYSLOG) && defined(LOG_FILE)
 # undef LOG_FILE
 #endif
@@ -46,6 +57,25 @@ static int LogFD = ERR;
 static int syslog_open = FALSE;
 #endif
 
+#if defined(HAVE_FCNTL) && defined(F_SETLK)
+static int trylock_file(int fd)
+{
+       struct flock fl;
+
+       memset(&fl, '\0', sizeof(fl));
+       fl.l_type = F_WRLCK;
+       fl.l_whence = SEEK_SET;
+       fl.l_start = 0;
+       fl.l_len = 0;
+
+       return fcntl(fd, F_SETLK, &fl);
+}
+#elif defined(HAVE_LOCKF)
+# define trylock_file(fd)      lockf((fd), F_TLOCK, 0)
+#elif defined(HAVE_FLOCK)
+# define trylock_file(fd)      flock((fd), LOCK_EX|LOCK_NB)
+#endif
+
 /*
  * glue_strings is the overflow-safe equivalent of
  *             sprintf(buffer, "%s%c%s", a, separator, b);
@@ -305,7 +335,7 @@ acquire_daemonlock(int closeflag) {
                        exit(ERROR_EXIT);
                }
 
-               if (flock(fd, LOCK_EX|LOCK_NB) < OK) {
+               if (trylock_file(fd) < OK) {
                        int save_errno = errno;
 
                        bzero(buf, sizeof(buf));