Beispiel #1
0
static int
alock_test_lock ( int fd, int slot )
{
	int res;

#if defined( HAVE_LOCKF )
	res = lseek (fd, (off_t) (ALOCK_SLOT_SIZE * slot), SEEK_SET);
	if (res == -1) return -1;

	res = lockf (fd, F_TEST, (off_t) ALOCK_SLOT_SIZE);
	if (res == -1) {
		if (errno == EACCES || errno == EAGAIN) { 
			return ALOCK_LOCKED;
		} else {
			return -1;
		}
	}
#elif defined( HAVE_FCNTL )
	struct flock lock_info;
	(void) memset ((void *) &lock_info, 0, sizeof (struct flock));

	lock_info.l_type = F_WRLCK;
	lock_info.l_whence = SEEK_SET;
	lock_info.l_start = (off_t) (ALOCK_SLOT_SIZE * slot);
	lock_info.l_len = (off_t) ALOCK_SLOT_SIZE;

	res = fcntl (fd, F_GETLK, &lock_info);
	if (res == -1) return -1;

	if (lock_info.l_type != F_UNLCK) return ALOCK_LOCKED;
#elif defined( _WIN32 )
	res = _lseek (fd, (ALOCK_SLOT_SIZE * slot), SEEK_SET);
	if (res == -1) return -1;
	res = _locking( fd, _LK_NBLCK, ALOCK_SLOT_SIZE );
	_locking( fd, _LK_UNLCK, ALOCK_SLOT_SIZE );
	if (res == -1) {
	   if( errno == EACCES ) {
		   return ALOCK_LOCKED;
	   } else {
		   return -1;
	   }
	}
#else
#   error alock needs lockf, fcntl, or _locking
#endif
	
	return 0;
}
Beispiel #2
0
	bool iiDisk_unlock(SThisCode* thisCode, SBuilder* lockRoot, SDiskLock* dl)
	{
		bool llResult;


		// If it's a valid lock, unlock it
		llResult = false;
		if (dl->isValid)
		{
			// Seek
			if (_lseeki64(dl->nFile, dl->nOffset, SEEK_SET) == dl->nOffset)
			{
				// Unlock
				llResult = (_locking(dl->nFile, _LK_UNLCK, dl->nLength) == 0);

			} else {
				// If we get here, the file is likely no longer open, so all locks are closed
				// But, we just silently fall through
			}

			// If cleared, indicate the lock is no longer valid
			dl->isValid = !llResult;
		}

		// Indicate success or failure
		return(llResult);
	}
Beispiel #3
0
static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long start, long len, const char *funcname) {
    int code;
#ifdef _WIN32
    /* lkmode valid values are:
       LK_LOCK    Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error.
       LK_NBLCK   Locks the specified bytes. If the bytes cannot be locked, the constant returns an error.
       LK_NBRLCK  Same as _LK_NBLCK.
       LK_RLCK    Same as _LK_LOCK.
       LK_UNLCK   Unlocks the specified bytes, which must have been previously locked.

       Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.

       http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp
    */
    int lkmode;
    switch (*mode) {
    case 'r':
        lkmode = LK_NBLCK;
        break;
    case 'w':
        lkmode = LK_NBLCK;
        break;
    case 'u':
        lkmode = LK_UNLCK;
        break;
    default :
        return luaL_error (L, "%s: invalid mode", funcname);
    }
    if (!len) {
        fseek (fh, 0L, SEEK_END);
        len = ftell (fh);
    }
    fseek (fh, start, SEEK_SET);
#ifdef __BORLANDC__
    code = locking (fileno(fh), lkmode, len);
#else
    code = _locking (fileno(fh), lkmode, len);
#endif
#else
    struct flock f;
    switch (*mode) {
    case 'w':
        f.l_type = F_WRLCK;
        break;
    case 'r':
        f.l_type = F_RDLCK;
        break;
    case 'u':
        f.l_type = F_UNLCK;
        break;
    default :
        return luaL_error (L, "%s: invalid mode", funcname);
    }
    f.l_whence = SEEK_SET;
    f.l_start = (off_t)start;
    f.l_len = (off_t)len;
    code = fcntl (fileno(fh), F_SETLK, &f);
#endif
    return (code != -1);
}
Beispiel #4
0
int sdbm_fd_unlock(int fd)
{
    int rc;

#ifdef USE_FCNTL
    unlock_it.l_whence = SEEK_SET; /* from current point */
    unlock_it.l_start  = 0;        /* -"- */
    unlock_it.l_len    = 0;        /* until end of file */
    unlock_it.l_type   = F_UNLCK;  /* unlock */
    unlock_it.l_pid    = 0;        /* pid not actually interesting */

    rc = fcntl(fd, F_SETLKW, &unlock_it);
#endif
#ifdef USE_FLOCK
    rc = flock(fd, LOCK_UN);
#endif
#ifdef USE_LOCKING
    lseek(fd, 0, SEEK_SET);
    rc = _locking(fd, _LK_UNLCK, 1);
#endif
#ifdef USE_SEM_LOCKING
	if (locking_sem)
		SignalLocalSemaphore (locking_sem);
	rc = 1;
#endif

    return rc;
}
Beispiel #5
0
	void iiDisk_unlock_all(SThisCode* thisCode, SBuilder* lockRoot)
	{
		u32			lnI;
		SDiskLock*	dl;


		// If it's a valid lock, unlock it
		for (lnI = 0, dl = (SDiskLock*)lockRoot->buffer; lnI < lockRoot->populatedLength; lnI += sizeof(SDiskLock), dl++)
		{
			// Is this one locked?
			if (dl->isValid)
			{
				// Seek
				if (_lseeki64(dl->nFile, dl->nOffset, SEEK_SET) == dl->nOffset)
				{
					// Unlock
					_locking(dl->nFile, _LK_UNLCK, dl->nLength);

				} else {
					// If we get here, the file is likely no longer open, so all locks are closed
					// But, we just silently fall through
				}

				// Indicate the lock is no longer valid
				dl->isValid = false;
			}
		}
	}
Beispiel #6
0
/*
 * get exclusive lock to whole file.
 * lock gets removed when we close the file
 *
 * returns 0 on success
 */
int rrd_lock(
    rrd_file_t *rrd_file)
{
#ifdef DISABLE_FLOCK
    (void)rrd_file;
    return 0;
#else
    int       rcstat;
    rrd_simple_file_t *rrd_simple_file;
    rrd_simple_file = (rrd_simple_file_t *)rrd_file->pvt;
    {
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
        struct _stat st;

        if (_fstat(rrd_simple_file->fd, &st) == 0) {
            rcstat = _locking(rrd_simple_file->fd, _LK_NBLCK, st.st_size);
        } else {
            rcstat = -1;
        }
#else
        struct flock lock;

        lock.l_type = F_WRLCK;  /* exclusive write lock */
        lock.l_len = 0; /* whole file */
        lock.l_start = 0;   /* start of file */
        lock.l_whence = SEEK_SET;   /* end of file */

        rcstat = fcntl(rrd_simple_file->fd, F_SETLK, &lock);
#endif
    }

    return (rcstat);
#endif
}
Beispiel #7
0
bool CFile::LockRegion(ulong lStart, ulong lNumBytes, bool bWait)
{
	int 	iMode;
	long	lOldPos;
	int		iResult;
		
	// Save old position and 
	// move to start of region.
	lOldPos = Pos();
	Pos(lStart);
		
	// Wait if already locked?
	if (bWait)
		iMode = _LK_LOCK;
	else
		iMode = _LK_NBLCK;
		
	// Lock region and restore position.
	iResult = _locking(m_hFile, iMode, lNumBytes);
	Pos(lOldPos);
    
    // Success?
    if (iResult == 0)
		return TRUE;
		 	
	return FALSE;
}
Beispiel #8
0
static int
alock_release_lock ( int fd, int slot )
{
	int res;
	
#if defined( HAVE_LOCKF )
	res = lseek (fd, (off_t) (ALOCK_SLOT_SIZE * slot), SEEK_SET);
	if (res == -1) return -1;
	res = lockf (fd, F_ULOCK, (off_t) ALOCK_SLOT_SIZE);
	if (res == -1) return -1;
#elif defined ( HAVE_FCNTL )
	struct flock lock_info;
	(void) memset ((void *) &lock_info, 0, sizeof (struct flock));

	lock_info.l_type = F_UNLCK;
	lock_info.l_whence = SEEK_SET;
	lock_info.l_start = (off_t) (ALOCK_SLOT_SIZE * slot);
	lock_info.l_len = (off_t) ALOCK_SLOT_SIZE;

	res = fcntl (fd, F_SETLKW, &lock_info);
	if (res == -1) return -1;
#elif defined( _WIN32 )
	res = _lseek (fd, (ALOCK_SLOT_SIZE * slot), SEEK_SET);
	if (res == -1) return -1;
	res = _locking( fd, _LK_UNLCK, ALOCK_SLOT_SIZE );
	if (res == -1) return -1;
#else
#   error alock needs lockf, fcntl, or _locking
#endif

	return 0;
}
Beispiel #9
0
static void unlock_logfile (STATE *state) {
#ifdef WIN32
	// Need to lock the file. NEED TO FINISH
	_locking(state->logFd, _LK_UNLCK, *state->length);
#else
    flock(state->logFd, LOCK_UN);
#endif
}
inline bool lock_locking_file(int fd)
{
   int ret = 0;
   while(ret != 0 && errno == EDEADLK){
      ret = _locking(fd, _LK_LOCK, 1/*lock_file_contents_length()*/);
   }
   return 0 == ret;
}
Beispiel #11
0
/**
 * @brief
 * 		lock out the lockfile for this daemon
 * 			$PBS_HOME/server_priv/comm.lock
 *
 * @param[in] fds - lockfile fd to lock
 * @param[in] op  - F_WRLCK to lock, F_UNLCK to unlock
 *
 * @return	void
 */
static void
lock_out(int fds, int op)
{
	int i;
	int j;
#ifndef WIN32
	struct flock flock;
#endif
	char buf[100];
#ifdef WIN32
	/* seek to start in case of win32 as _locking locks/unlock
	 * from current file pointer position
	 */
	(void) lseek(fds, (off_t) 0, SEEK_SET);
#endif

	j = 1; /* not fail over, try lock one time */

#ifdef WIN32
	for (i = 0; i < j; i++) {
		/*
		 * just lock/unlock the first 10 bytes of the file
		 * so that we lock/unlock that same bytes each time
		 * instead of based on the size, which would fail
		 */
		if (_locking(fds, op, 10) != -1) {
			if (op == F_WRLCK) {
				/* if write-lock, record pid in file */
				(void)sprintf(buf, "%d\n", getpid());
				(void)write(fds, buf, strlen(buf));
			}
			return;
		}
		sleep(2);
	}
#else
	(void) lseek(fds, (off_t) 0, SEEK_SET);
	flock.l_type = op;
	flock.l_whence = SEEK_SET;
	flock.l_start = 0;
	flock.l_len = 0;
	for (i = 0; i < j; i++) {
		if (fcntl(fds, F_SETLK, &flock) != -1) {
			if (op == F_WRLCK) {
				/* if write-lock, record pid in file */
				(void) ftruncate(fds, (off_t) 0);
				(void) sprintf(buf, "%d\n", getpid());
				(void) write(fds, buf, strlen(buf));
			}
			return;
		}
		sleep(2);
	}
#endif
	(void) strcpy(log_buffer, "another PBS comm router running at the same port");
	fprintf(stderr, "pbs_comm: %s\n", log_buffer);
	exit(1);
}
Beispiel #12
0
static void lock_logfile (STATE *state) {
    open_logfile(state);
#ifdef WIN32
	// Need to lock the file. NEED TO FINISH
	_locking(state->logFd, _LK_LOCK, *state->length);
#else
    flock(state->logFd, LOCK_EX);
#endif
}
Beispiel #13
0
	SDiskLock* iiDisk_lock_range(SThisCode* thisCode, SBuilder* lockRoot, s32 tnFile, s64 tnOffset, s32 tnLength, uptr tnExtra)
	{
		u32			lnI;
		SDiskLock*	dl;


		//////////
		// Iterate to find an empty slot
		//////
			for (lnI = 0, dl = (SDiskLock*)lockRoot->buffer; lnI < lockRoot->populatedLength; lnI += sizeof(SDiskLock), dl++)
			{
				// Is this slot empty?
				if (!dl->isValid)
					break;	// Yes
			}


		//////////
		// When we get here, we have a slot or not
		//////
			if (lnI >= lockRoot->populatedLength)
			{
				// Create a new one
				dl = (SDiskLock*)iBuilder_appendData(lockRoot, (cs8*)NULL, sizeof(SDiskLock));
				// Note:  disk members are all initialized to 0s
			}


		//////////
		// Physically try the lock
		//////
			dl->isValid = true;
			dl->nFile	= tnFile;
			dl->nOffset	= tnOffset;
			dl->nLength	= 0;

			// Seek to the offset
			if (_lseeki64(tnFile, tnOffset, SEEK_SET) == tnOffset)
			{
				// Lock the bytes
				if (_locking(tnFile, _LK_NBLCK, tnLength) == 0)
				{
					// Indicate a successful lock
					dl->nLength = tnLength;
				}

			} else {
				// The length being 0 will indicate the lock failed
				// So we just let it fall through
			}


		//////////
		// Indicate success or failure
		//////
			return(dl);
	}
Beispiel #14
0
static B jtdolock(J jt,B lk,F f,I i,I n){I e;long c;fpos_t v; fpos_t q;
 c=fgetpos(f,(fpos_t*)&q);
 if(0!=c)R (B)jerrno();
 v=i;
 c=fsetpos(f,(fpos_t*)&v);
 if(0!=c)R (B)jerrno();
 e=_locking(_fileno(f),lk?_LK_NBLCK:_LK_UNLCK,(long)n);
 fsetpos(f,(fpos_t*)&q);
 R !e?1:errno==EACCES?0:(B)jerrno();
}
Beispiel #15
0
void CFile::UnLockRegion(ulong lStart, ulong lNumBytes)
{
	long lOldPos;
		
	// Save old position and 
	// move to start of region.
	lOldPos = Pos();
	Pos(lStart);
		
	// UnLock region and restore position.
	_locking(m_hFile, _LK_UNLCK, lNumBytes);
	Pos(lOldPos);
}
Beispiel #16
0
/*
 * lockf() - lockf() WINDOWS implementation
 *   return: 0 if success, -1 otherwise
 *   fd(in): file descriptor
 *   cmd(in): locking command to perform
 *   size(in): number of bytes
 */
int
lockf (int fd, int cmd, long size)
{
  switch (cmd)
    {
    case F_ULOCK:
      return (_locking (fd, _LK_UNLCK, (size ? size : LOCKING_SIZE)));

    case F_LOCK:
      return (_locking (fd, _LK_LOCK, (size ? size : LOCKING_SIZE)));

    case F_TLOCK:
      return (_locking (fd, _LK_NBLCK, (size ? size : LOCKING_SIZE)));

    case F_TEST:
      /* not implemented on WINDOWS */
      return (-1);

    default:
      errno = EINVAL;
      return (-1);
    }
}
Beispiel #17
0
static PyObject *
msvcrt_locking_impl(PyModuleDef *module, int fd, int mode, long nbytes)
/*[clinic end generated code: output=dff41e5e76d544de input=d9f13f0f6a713ba7]*/
{
    int err;

    Py_BEGIN_ALLOW_THREADS
    err = _locking(fd, mode, nbytes);
    Py_END_ALLOW_THREADS
    if (err != 0)
        return PyErr_SetFromErrno(PyExc_IOError);

    Py_RETURN_NONE;
}
Beispiel #18
0
void vsyslog(int priority, const char *fmt, va_list ap)
{
    #define WSMAND_WIN_LOGFILE "wsmand.log"
    enum { BUFSZ = 1024 };
    static FILE *df = NULL, *lock = NULL;
    char buf[BUFSZ];
    int i;

    /* first time open the log file and the lock file */
    if(df == NULL)
    {
        df = fopen(WSMAND_WIN_LOGFILE, "a+");
        lock = fopen(WSMAND_WIN_LOGFILE ".lock", "a+");
        if(df == NULL || lock == NULL)
            exit(1);
    }

    vsnprintf(buf, BUFSZ, fmt, ap);

    /* get the lock (i.e. lock the first byte of the lock file) */
    for(i = 0; 
        _locking(fileno(lock), _LK_NBLCK, 1) == EACCES && i < 10; ++i)
        Sleep(100);

    if(i < 10)
    {   /* we have the lock, write the log msg */
        fprintf(df, "%s\n", buf);
        fflush(df);
        /* unlock the file */
        _locking(fileno(lock), _LK_UNLCK, 1);
    } else {
        /* file is still locked after 10 tries, give up */
        ;
    }

    return;
}
Beispiel #19
0
bool CBUShm::locktype(int ltype, unsigned int offset, unsigned int len)
{
#ifdef WIN32
    _lseek(m_fileno,offset,SEEK_SET);
    return (_locking(m_fileno,ltype,len)==0);
#else
    struct flock lck;
    lck.l_type = ltype;
    lck.l_whence = SEEK_SET;
    lck.l_start = offset;
    lck.l_len = len;
    lck.l_pid = getpid();
    return (fcntl(m_fileno,F_SETLK,&lck)==0);
#endif
}
Beispiel #20
0
int
flock(int fd, int cmd)
{
	struct stat st;
	if (fstat(fd, &st) < 0)
		return EINVAL;

	off_t len = st.st_size;

	int stat;
	switch (cmd) {
	case F_LOCK:
		do     stat = _locking(fd, _LK_LOCK, len);
		while (stat == -1 && errno == EDEADLOCK);
		return stat;
	case F_TLOCK:
		return _locking(fd, _LK_NBLCK, len);
	case F_ULOCK:
		return _locking(fd, _LK_UNLCK, len);
	default:
		errno = EINVAL;
		return -1;
	}
}
Beispiel #21
0
static int
modifyFileLock (int file, int mode) {
  int ok = 0;
  off_t offset;

  if ((offset = lseek(file, 0, SEEK_CUR)) != -1) {
    if (lseek(file, 0, SEEK_SET) != -1) {
      int wait;

      if (mode == _LK_LOCK) {
        mode = _LK_NBLCK;
        wait = 1;
      } else if (mode == _LK_RLCK) {
        mode = _LK_NBRLCK;
        wait = 1;
      } else {
        wait = 0;
      }

      while (1) {
        if (_locking(file, mode, LONG_MAX) != -1) {
          ok = 1;
          break;
        }

        if (errno != EACCES) {
          logSystemError("_locking");
          break;
        }

        if (!wait) break;
        approximateDelay(1000);
      }

      if (lseek(file, offset, SEEK_SET) == -1) {
        logSystemError("lseek");
        ok = 0;
      }
    } else {
      logSystemError("lseek");
    }
  } else {
    logSystemError("lseek");
  }

  return ok;
}
// Perform locking operations on a C runtime file descriptor.
static PyObject *msvcrt_locking(PyObject *self, PyObject *args)
{
	int fd;
	int mode;
	long nbytes;
	int err;

	if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	err = _locking(fd, mode, nbytes);
	Py_END_ALLOW_THREADS
	if (err != 0)
		return PyErr_SetFromErrno(PyExc_IOError);

	Py_INCREF(Py_None);
	return Py_None;
}
Beispiel #23
0
int					/* O - 0 on success, -1 on error */
cupsFileLock(cups_file_t *fp,		/* I - CUPS file */
             int         block)		/* I - 1 to wait for the lock, 0 to fail right away */
{
 /*
  * Range check...
  */

  if (!fp || fp->mode == 's')
    return (-1);

 /*
  * Try the lock...
  */

#ifdef WIN32
  return (_locking(fp->fd, block ? _LK_LOCK : _LK_NBLCK, 0));
#else
  return (lockf(fp->fd, block ? F_LOCK : F_TLOCK, 0));
#endif /* WIN32 */
}
Beispiel #24
0
TBOOL File::LockRange(unsigned long dwPos, unsigned long dwCount)
{
    BASE_ASSERT_VALID(this);
    BASE_ASSERT(m_pStream != NULL);

    LONG lCurrentPos=GetPosition();
    if(Seek(dwPos,File::begin)==FALSE)
    {
        return(FALSE);
    }

#ifdef WIN32
    if(_locking(fileno(m_pStream),_LK_LOCK,dwCount))
#else
    if(lockf(fileno(m_pStream),F_LOCK,dwCount))
#endif
        return(FALSE);
    Seek(lCurrentPos,File::begin);
    
    return(TRUE);
}
Beispiel #25
0
/*
 * os_flock -- apply or remove an advisory lock on an open file
 */
int
os_flock(int fd, int operation)
{
	int flags = 0;
	SYSTEM_INFO  systemInfo;

	GetSystemInfo(&systemInfo);

	switch (operation & (OS_LOCK_EX | OS_LOCK_SH | OS_LOCK_UN)) {
		case OS_LOCK_EX:
		case OS_LOCK_SH:
			if (operation & OS_LOCK_NB)
				flags = _LK_NBLCK;
			else
				flags = _LK_LOCK;
			break;

		case OS_LOCK_UN:
			flags = _LK_UNLCK;
			break;

		default:
			errno = EINVAL;
			return -1;
	}

	os_off_t filelen = _filelengthi64(fd);
	if (filelen < 0)
		return -1;

	/* for our purpose it's enough to lock the first page of the file */
	long len = (filelen > systemInfo.dwPageSize) ?
				systemInfo.dwPageSize : (long)filelen;

	int res = _locking(fd, flags, len);
	if (res != 0 && errno == EACCES)
		errno = EWOULDBLOCK; /* for consistency with flock() */

	return res;
}
Beispiel #26
0
static int
alock_grab_lock ( int fd, int slot )
{
	int res;
	
#if defined( HAVE_LOCKF )
	res = lseek (fd, (off_t) (ALOCK_SLOT_SIZE * slot), SEEK_SET);
	if (res == -1) return -1;
	res = lockf (fd, F_LOCK, (off_t) ALOCK_SLOT_SIZE);
#elif defined( HAVE_FCNTL )
	struct flock lock_info;
	(void) memset ((void *) &lock_info, 0, sizeof (struct flock));

	lock_info.l_type = F_WRLCK;
	lock_info.l_whence = SEEK_SET;
	lock_info.l_start = (off_t) (ALOCK_SLOT_SIZE * slot);
	lock_info.l_len = (off_t) ALOCK_SLOT_SIZE;

	res = fcntl (fd, F_SETLKW, &lock_info);
#elif defined( _WIN32 )
	if( _lseek( fd, (ALOCK_SLOT_SIZE * slot), SEEK_SET ) < 0 )
		return -1;
	/*
	 * _lock will try for the lock once per second, returning EDEADLOCK
	 * after ten tries. We just loop until we either get the lock
	 * or some other error is returned.
	 */
	while((res = _locking( fd, _LK_LOCK, ALOCK_SLOT_SIZE )) < 0 ) {
		if( errno != EDEADLOCK )
			break;
	}
#else
#   error alock needs lockf, fcntl, or _locking
#endif
	if (res == -1) {
		assert (errno != EDEADLK);
		return -1;
	}
	return 0;
}
Beispiel #27
0
int
lock_file_plain(int fd, LOCK_TYPE type, bool do_block)
{
	int	mode, result;
	long original_pos, pos;

	original_pos = lseek(fd, SEEK_CUR, 0);
	if (original_pos < 0) return original_pos;
	pos = lseek(fd, SEEK_SET, 0);
	if (pos < 0) return pos;

	switch (type) {
	case READ_LOCK:
	case WRITE_LOCK:
		if (do_block)
			mode = _LK_LOCK;
		else
			mode = _LK_NBLCK;
		break;
	case UN_LOCK:
		mode = _LK_UNLCK;
		break;
	default:
		EXCEPT("Programmer error in use of lock_file_plain()");
		break;
	}

	// _locking() in blocking mode will try 10 times to get
	// the lock on the file, after which it gives up and fails
	// with errno==EDEADLOCK. Since we want to block indefinitely
	// for this lock, we loop until we get the lock.
	do {
		result = _locking(fd, mode, 4L);
	} while ((result < 0) && errno == EDEADLOCK); 

	lseek(fd, SEEK_SET, original_pos);

	return result;
}
Beispiel #28
0
/* NOTE: this function blocks until it acquires the lock */
int sdbm_fd_lock(int fd, int readonly)
{
    int rc;

#ifdef USE_FCNTL
    lock_it.l_whence = SEEK_SET; /* from current point */
    lock_it.l_start  = 0;        /* -"- */
    lock_it.l_len    = 0;        /* until end of file */
    lock_it.l_type   = readonly ? F_RDLCK : F_WRLCK;  /* set lock type */
    lock_it.l_pid    = 0;        /* pid not actually interesting */

    while (   ((rc = fcntl(fd, F_SETLKW, &lock_it)) < 0)
              && (errno == EINTR)                               ) {
        continue;
    }
#endif
#ifdef USE_FLOCK
    while (   ((rc = flock(fd, readonly ? LOCK_SH : LOCK_EX)) < 0)
              && (errno == EINTR)               ) {
        continue;
    }
#endif
#ifdef USE_LOCKING
    /* ### this doesn't allow simultaneous reads! */
    /* ### this doesn't block forever */
    /* Lock the first byte */
    lseek(fd, 0, SEEK_SET);
    rc = _locking(fd, _LK_LOCK, 1);
#endif
#ifdef USE_SEM_LOCKING
	if ((locking_sem != 0) && (TimedWaitOnLocalSemaphore (locking_sem, 10000) != 0))
		rc = -1;
	else
		rc = 1;
#endif

    return rc;
}
Beispiel #29
0
	s32 iiDisk_unlock_all_byCallback(SThisCode* thisCode, SBuilder* lockRoot, uptr tnFunction, uptr tnExtra)
	{
		u32					lnI, lnUnlockedCount;
		SDiskLock*			dl;
		SDiskLockCallback	dcb;


		// Initialize
		memset(&dcb, 0, sizeof(dcb));
		dcb._diskUnlockCallback	= tnFunction;
		dcb.extra				= tnExtra;

		// If it's a valid lock, unlock it
		for (lnI = 0, lnUnlockedCount = 0, dl = (SDiskLock*)lockRoot->buffer; lnI < lockRoot->populatedLength; lnI += sizeof(SDiskLock), dl++)
		{
			// Is this one locked?  And do they want to release it?
			if (dl->isValid && (!dcb._diskUnlockCallback || !dcb.diskUnlockCallback(thisCode, &dcb)))
			{
				// Seek
				if (_lseeki64(dl->nFile, dl->nOffset, SEEK_SET) == dl->nOffset)
				{
					// Unlock
					_locking(dl->nFile, _LK_UNLCK, dl->nLength);
					++lnUnlockedCount;

				} else {
					// If we get here, the file is likely no longer open, so all locks are closed
					// But, we just silently fall through
				}

				// Indicate the lock is no longer valid
				dl->isValid = false;
			}
		}

		// Indicate our count
		return(lnUnlockedCount);
	}
Beispiel #30
0
int lockf(int fd, int cmd, off_t len) { return _locking(fd, cmd, len); }