- /*
- * Get the name of the eventual lock file, as well
- * as a name for a temporary lock file.
- */
- lockname (file, &lkinfo, 1);
-
- for (i = 0;;) {
- /* attempt to create lock file */
- if (lockit (&lkinfo) == 0) {
- /* if successful, turn on timer and return */
- timerON (lkinfo.curlock, fd);
- return fd;
+ /*
+ ** Get the name of the eventual lock file, as well
+ ** as a name for a temporary lock file.
+ */
+ lockname(file, &lkinfo, 1);
+
+#if !defined(HAVE_LIBLOCKFILE)
+ {
+ int i;
+ for (i = 0;;) {
+ /* attempt to create lock file */
+ if (lockit(&lkinfo) == 0) {
+ /* if successful, turn on timer and return */
+ timerON(lkinfo.curlock, fd);
+ return fd;
+ } else {
+ /*
+ ** Abort locking, if we fail to lock after 5
+ ** attempts and are never able to stat the
+ ** lock file.
+ */
+ struct stat st;
+ if (stat(lkinfo.curlock, &st) == -1) {
+ if (i++ > 5)
+ return -1;
+ sleep(1);
+ } else {
+ time_t curtime;
+ i = 0;
+ time(&curtime);
+
+ /*
+ ** check for stale lockfile,
+ ** else sleep
+ */
+ if (curtime > st.st_ctime + RSECS)
+ unlink(lkinfo.curlock);
+ else
+ sleep(1);
+ }
+ lockname(file, &lkinfo, 1);
+ }
+ }
+ }
+#else
+ if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
+ timerON(lkinfo.curlock, fd);
+ return fd;