Exemplo n.º 1
0
bool __fastcall TMainForm::ToggleChat(bool on)
{
    int         n=node_num-1;
    static int  org_act;

    if(nodedab==-1)
        return(false);

    lseek(nodedab, n*sizeof(node_t), SEEK_SET);
    int i=locking(nodedab, LK_LOCK, sizeof(node_t));
    if(i) {
        Remote->Lines->Add("!Error "+AnsiString(i)+" reading record for"
            "node "+AnsiString(node_num));
        return(false);
    }
    read(nodedab, &node, sizeof(node_t));
    if(on) {
        org_act=node.action;
        if(org_act==NODE_PCHT)
            org_act=NODE_MAIN;
        node.misc|=NODE_LCHAT;
    } else {
        node.action=org_act;
        node.misc&=~NODE_LCHAT;
    }

    lseek(nodedab, n*sizeof(node_t), SEEK_SET);
    write(nodedab, &node, sizeof(node_t));
    lseek(nodedab, n*sizeof(node_t), SEEK_SET);
    locking(nodedab, LK_UNLCK, sizeof(node_t));

    utime(node_path,NULL);

    return(true);
}
Exemplo n.º 2
0
int _fast unlock(int fh, long offset, long len)
{
    if(-1L != lseek(fh, offset, SEEK_SET))
        return locking(fh, LK_UNLCK, len);
    else
        return(-1);
}
Exemplo n.º 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);
}
Exemplo n.º 4
0
xbShort xbXBase::LockFile( int fn, xbShort LockType, xbOffT lockLen)
{

  int mode;
  int rc;
  int tries = 0;

  /* convert the xbase locking command into a windows locking command */
  if( LockType == XB_UNLOCK )
    mode = LK_UNLCK;
  else if( LockType == XB_LOCK || LockType == XB_LOCK_HOLD )
    mode = LK_NBLCK;
  else
    return XB_INVALID_LOCK_OPTION;

  do{
    rc = locking( fn, mode, lockLen );
    if( rc )
      _sleep( 1 );
  } while( rc == -1 && tries++ < GetLockRetryCount());
  
  if( rc )
    return XB_LOCK_FAILED;
  
  return 0;
}
Exemplo n.º 5
0
void test_asioStrandWrap() {
	std::mutex lockable;
	IOServicePtr iosp = std::make_shared<IOService>();
	IOServiceWorkPtr iosWorkp = std::make_shared<IOServiceWork>(*iosp);
	IOService::strand iosStrand (*iosp);
	{std::lock_guard<std::mutex> locking(lockable);
	std::cout << "program exits once all work isfinished" << std::endl;
	}

	std::vector<std::thread> threadList;
	IOServiceWorker IOServiceWorkerObj(&lockable);
	for (int i = 0; i < 5; i++)
		threadList.push_back(std::thread(IOServiceWorkerObj, iosp, i));

	std::this_thread::sleep_for(sleeptime);
	for (int i = 10; i < 15; i++)
		iosp->post(iosStrand.wrap(boost::bind(&myPrint, i))); 
		// iosStrand.post(boost::bind(&myPrint, i)); 

	iosWorkp.reset ();

	for (auto& t : threadList) {
		if (t.joinable())
			t.join();
		else
			continue;
	}

}
Exemplo n.º 6
0
docstring LyXVC::vcstatus() const
{
	if (!vcs)
		return docstring();
	if (locking())
		return bformat(_("%1$s lock"), from_ascii(vcs->vcname()));
	else
		return from_ascii(vcs->vcname());
}
Exemplo n.º 7
0
/***
	PRIVATE
	RealLock

	Realiza um lock em uma regiao do arquivo, utilizando rotinas
	do Sistema Operacional
***/
int
C_File::RealLock( LockStruct *ls, BOOL bWait )
{
	C_FileCritSect	cCS0( this, CRITSECT0 );
	if( !bRealLock ){
		return( OK );
	}
	int	iRet = !OK;
	int	iFlag = FALSE;
	if( (_bIs32s && iFile == -1) || (!_bIs32s && hFile == INVALID_HANDLE_VALUE) ){
		if( ReOpen() != OK ){
			if( !_xFile ){
				return( !OK );
			}
			_xFile->PseudoClose();
			ReOpen();
			iFlag = TRUE;
		}
	}

	if( ls ){
		if( _bIs32s ){
			long lPos = CurPos();
			if( Seek( ls->iPos, SEEK_SET ) == OK ){
				iRet = locking( iFile, _LK_LOCK, ls->iSize );
			}
			Seek( lPos, SEEK_SET );
		} else {
			if( Seek( ls->iPos, SEEK_SET ) == OK ){
				DWORD	dwErr;

				SetLastError( 0 );
				iRet = LockFile( hFile, ls->iPos, 0, ls->iSize, 0 ) ? OK : !OK;
				dwErr = GetLastError();
				while( bWait && ((dwErr == ERROR_LOCK_FAILED) || (dwErr == ERROR_LOCK_VIOLATION)) ){
					// esperar e tentar novamente.
					cCS0.LeaveCriticalSection();
					Sleep( 1000 );	// 1 segundo
					cCS0.EnterCriticalSection();
					SetLastError( 0 );
					iRet = LockFile( hFile, ls->iPos, 0, ls->iSize, 0 ) ? OK : !OK;
					dwErr = GetLastError();
				}
			}
		}
	}
	if( iFlag ){
		PseudoClose();
		_xFile->ReOpen();
	}
	return( iRet );
}
Exemplo n.º 8
0
/***
	PRIVATE
	RealRelease

	Realiza um unlock em uma regiao do arquivo, utilizando rotinas
	do Sistema Operacional
***/
int
C_File::RealRelease( LockStruct *ls )
{
	C_FileCritSect	cCS0( this, CRITSECT0 );
	if( !bRealLock ){
		return( OK );
	}
	int	iRet = !OK;
	int	iFlag = FALSE;
	if( (_bIs32s && iFile == -1) || (!_bIs32s && hFile == INVALID_HANDLE_VALUE) ){
		if( ReOpen() != OK ){
			if( !_xFile ){
				return( !OK );
			}
			_xFile->PseudoClose();
			ReOpen();
			iFlag = TRUE;
		}
	}

	if( ls ){
		if( _bIs32s ){
			long lPos = CurPos();
			if( Seek( ls->iPos, SEEK_SET ) == OK ){
				iRet = locking( iFile, _LK_UNLCK, ls->iSize );
			}
			Seek( lPos, SEEK_SET );
		} else {
			if( Seek( ls->iPos, SEEK_SET ) == OK ){
				iRet = UnlockFile( hFile, ls->iPos, 0, ls->iSize, 0 ) ? OK : !OK;
			}
		}
	}
	if( iFlag ){
		PseudoClose();
		_xFile->ReOpen();
	}
	return( iRet );
}
Exemplo n.º 9
0
static BOOL OpenTeeFile(ISI_DL *dl, char *path)
{
BOOL result;
int handle;

    if (strcmp(path, dl->tee.path) == 0) return TRUE; /* already open */

    isidlCloseTee(dl);

    strlcpy(dl->tee.path, path, MAXPATHLEN+1);
    result = (dl->tee.fp = fopen(dl->tee.path, "a+b")) != NULL ? TRUE : FALSE;
    if (result == TRUE) {
        chmod(dl->tee.path, 0755);
        logioMsg(dl->lp, LOG_DEBUG, "tee file %s opened", dl->tee.path);
#ifdef WIN32
        handle = fileno(dl->tee.fp);
        locking(handle, LK_LOCK, 1);  // protect opened tee file from copying under Windows
#endif
    }

    return result;
}
Exemplo n.º 10
0
int
main (int argc, char **argv)
{
  char *inname, *outname;
  int indesc, outdesc;
  ssize_t nread;
  int wait_status;
  int c, preserve_mail = 0;

#ifndef MAIL_USE_SYSTEM_LOCK
  struct stat st;
  int tem;
  char *lockname;
  char *tempname;
  size_t inname_len, inname_dirlen;
  int desc;
#endif /* not MAIL_USE_SYSTEM_LOCK */

#ifdef MAIL_USE_MAILLOCK
  char *spool_name;
#endif

#ifdef MAIL_USE_POP
  int pop_reverse_order = 0;
# define ARGSTR "pr"
#else /* ! MAIL_USE_POP */
# define ARGSTR "p"
#endif /* MAIL_USE_POP */

  uid_t real_gid = getgid ();
  uid_t priv_gid = getegid ();

#ifdef WINDOWSNT
  /* Ensure all file i/o is in binary mode. */
  _fmode = _O_BINARY;
#endif

  delete_lockname = 0;

  while ((c = getopt (argc, argv, ARGSTR)) != EOF)
    {
      switch (c) {
#ifdef MAIL_USE_POP
      case 'r':
	pop_reverse_order = 1;
	break;
#endif
      case 'p':
	preserve_mail++;
	break;
      default:
	exit (EXIT_FAILURE);
      }
    }

  if (
#ifdef MAIL_USE_POP
      (argc - optind < 2) || (argc - optind > 3)
#else
      (argc - optind != 2)
#endif
      )
    {
#ifdef MAIL_USE_POP
      fprintf (stderr, "Usage: movemail [-p] [-r] inbox destfile%s\n",
	       " [POP-password]");
#else
      fprintf (stderr, "Usage: movemail [-p] inbox destfile%s\n", "");
#endif
      exit (EXIT_FAILURE);
    }

  inname = argv[optind];
  outname = argv[optind+1];

#ifdef MAIL_USE_MMDF
  mmdf_init (argv[0]);
#endif

  if (*outname == 0)
    fatal ("Destination file name is empty", 0, 0);

#ifdef MAIL_USE_POP
  if (!strncmp (inname, "po:", 3))
    {
      int status;

      status = popmail (inname + 3, outname, preserve_mail,
			(argc - optind == 3) ? argv[optind+2] : NULL,
			pop_reverse_order);
      exit (status);
    }

  if (setuid (getuid ()) < 0)
    fatal ("Failed to drop privileges", 0, 0);

#endif /* MAIL_USE_POP */

#ifndef DISABLE_DIRECT_ACCESS
#ifndef MAIL_USE_MMDF
#ifndef MAIL_USE_SYSTEM_LOCK
#ifdef MAIL_USE_MAILLOCK
  spool_name = mail_spool_name (inname);
  if (spool_name)
    {
#ifdef lint
      lockname = 0;
#endif
    }
  else
#endif
    {
      /* Use a lock file named after our first argument with .lock appended:
	 If it exists, the mail file is locked.  */
      /* Note: this locking mechanism is *required* by the mailer
	 (on systems which use it) to prevent loss of mail.

	 On systems that use a lock file, extracting the mail without locking
	 WILL occasionally cause loss of mail due to timing errors!

	 So, if creation of the lock file fails due to access
	 permission on the mail spool directory, you simply MUST
	 change the permission and/or make movemail a setgid program
	 so it can create lock files properly.

	 You might also wish to verify that your system is one which
	 uses lock files for this purpose.  Some systems use other methods.  */

      inname_len = strlen (inname);
      lockname = xmalloc (inname_len + sizeof ".lock");
      strcpy (lockname, inname);
      strcpy (lockname + inname_len, ".lock");
      for (inname_dirlen = inname_len;
	   inname_dirlen && !IS_DIRECTORY_SEP (inname[inname_dirlen - 1]);
	   inname_dirlen--)
	continue;
      tempname = xmalloc (inname_dirlen + sizeof "EXXXXXX");

      while (1)
	{
	  /* Create the lock file, but not under the lock file name.  */
	  /* Give up if cannot do that.  */

	  memcpy (tempname, inname, inname_dirlen);
	  strcpy (tempname + inname_dirlen, "EXXXXXX");
#ifdef HAVE_MKSTEMP
	  desc = mkstemp (tempname);
#else
	  mktemp (tempname);
	  if (!*tempname)
	    desc = -1;
	  else
	    {
	      unlink (tempname);
	      desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0600);
	    }
#endif
	  if (desc < 0)
	    {
	      int mkstemp_errno = errno;
	      error ("error while creating what would become the lock file",
		     0, 0);
	      errno = mkstemp_errno;
	      pfatal_with_name (tempname);
	    }
	  close (desc);

	  tem = link (tempname, lockname);

#ifdef EPERM
	  if (tem < 0 && errno == EPERM)
	    fatal ("Unable to create hard link between %s and %s",
		   tempname, lockname);
#endif

	  unlink (tempname);
	  if (tem >= 0)
	    break;
	  sleep (1);

	  /* If lock file is five minutes old, unlock it.
	     Five minutes should be good enough to cope with crashes
	     and wedgitude, and long enough to avoid being fooled
	     by time differences between machines.  */
	  if (stat (lockname, &st) >= 0)
	    {
	      time_t now = time (0);
	      if (st.st_ctime < now - 300)
		unlink (lockname);
	    }
	}

      delete_lockname = lockname;
    }
#endif /* not MAIL_USE_SYSTEM_LOCK */
#endif /* not MAIL_USE_MMDF */

  if (fork () == 0)
    {
      int lockcount = 0;
      int status = 0;
#if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
      time_t touched_lock;
# ifdef lint
      touched_lock = 0;
# endif
#endif

      if (setuid (getuid ()) < 0 || setregid (-1, real_gid) < 0)
	fatal ("Failed to drop privileges", 0, 0);

#ifndef MAIL_USE_MMDF
#ifdef MAIL_USE_SYSTEM_LOCK
      indesc = open (inname, O_RDWR);
#else  /* if not MAIL_USE_SYSTEM_LOCK */
      indesc = open (inname, O_RDONLY);
#endif /* not MAIL_USE_SYSTEM_LOCK */
#else  /* MAIL_USE_MMDF */
      indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
#endif /* MAIL_USE_MMDF */

      if (indesc < 0)
	pfatal_with_name (inname);

#ifdef BSD_SYSTEM
      /* In case movemail is setuid to root, make sure the user can
	 read the output file.  */
      /* This is desirable for all systems
	 but I don't want to assume all have the umask system call */
      umask (umask (0) & 0333);
#endif /* BSD_SYSTEM */
      outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
      if (outdesc < 0)
	pfatal_with_name (outname);

      if (setregid (-1, priv_gid) < 0)
	fatal ("Failed to regain privileges", 0, 0);

      /* This label exists so we can retry locking
	 after a delay, if it got EAGAIN or EBUSY.  */
    retry_lock:

      /* Try to lock it.  */
#ifdef MAIL_USE_MAILLOCK
      if (spool_name)
	{
	  /* The "0 - " is to make it a negative number if maillock returns
	     non-zero. */
	  status = 0 - maillock (spool_name, 1);
#ifdef HAVE_TOUCHLOCK
	  touched_lock = time (0);
#endif
	  lockcount = 5;
	}
      else
#endif /* MAIL_USE_MAILLOCK */
	{
#ifdef MAIL_USE_SYSTEM_LOCK
#ifdef MAIL_USE_LOCKF
	  status = lockf (indesc, F_LOCK, 0);
#else /* not MAIL_USE_LOCKF */
#ifdef WINDOWSNT
	  status = locking (indesc, LK_RLCK, -1L);
#else
	  status = flock (indesc, LOCK_EX);
#endif
#endif /* not MAIL_USE_LOCKF */
#endif /* MAIL_USE_SYSTEM_LOCK */
	}

      /* If it fails, retry up to 5 times
	 for certain failure codes.  */
      if (status < 0)
	{
	  if (++lockcount <= 5)
	    {
#ifdef EAGAIN
	      if (errno == EAGAIN)
		{
		  sleep (1);
		  goto retry_lock;
		}
#endif
#ifdef EBUSY
	      if (errno == EBUSY)
		{
		  sleep (1);
		  goto retry_lock;
		}
#endif
	    }

	  pfatal_with_name (inname);
	}

      {
	char buf[1024];

	while (1)
	  {
	    nread = read (indesc, buf, sizeof buf);
	    if (nread < 0)
	      pfatal_with_name (inname);
	    if (nread != write (outdesc, buf, nread))
	      {
		int saved_errno = errno;
		unlink (outname);
		errno = saved_errno;
		pfatal_with_name (outname);
	      }
	    if (nread < sizeof buf)
	      break;
#if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
	    if (spool_name)
	      {
		time_t now = time (0);
		if (now - touched_lock > 60)
		  {
		    touchlock ();
		    touched_lock = now;
		  }
	      }
#endif /* MAIL_USE_MAILLOCK */
	  }
      }

#ifdef BSD_SYSTEM
      if (fsync (outdesc) < 0)
	pfatal_and_delete (outname);
#endif

      /* Prevent symlink attacks truncating other users' mailboxes */
      if (setregid (-1, real_gid) < 0)
	fatal ("Failed to drop privileges", 0, 0);

      /* Check to make sure no errors before we zap the inbox.  */
      if (close (outdesc) != 0)
	pfatal_and_delete (outname);

#ifdef MAIL_USE_SYSTEM_LOCK
      if (! preserve_mail)
	{
	  if (ftruncate (indesc, 0L) != 0)
	    pfatal_with_name (inname);
	}
#endif /* MAIL_USE_SYSTEM_LOCK */

#ifdef MAIL_USE_MMDF
      lk_close (indesc, 0, 0, 0);
#else
      close (indesc);
#endif

#ifndef MAIL_USE_SYSTEM_LOCK
      if (! preserve_mail)
	{
	  /* Delete the input file; if we can't, at least get rid of its
	     contents.  */
#ifdef MAIL_UNLINK_SPOOL
	  /* This is generally bad to do, because it destroys the permissions
	     that were set on the file.  Better to just empty the file.  */
	  if (unlink (inname) < 0 && errno != ENOENT)
#endif /* MAIL_UNLINK_SPOOL */
	    creat (inname, 0600);
	}
#endif /* not MAIL_USE_SYSTEM_LOCK */

      /* End of mailbox truncation */
      if (setregid (-1, priv_gid) < 0)
	fatal ("Failed to regain privileges", 0, 0);

#ifdef MAIL_USE_MAILLOCK
      /* This has to occur in the child, i.e., in the process that
         acquired the lock! */
      if (spool_name)
	mailunlock ();
#endif
      exit (EXIT_SUCCESS);
    }

  wait (&wait_status);
  if (!WIFEXITED (wait_status))
    exit (EXIT_FAILURE);
  else if (WEXITSTATUS (wait_status) != 0)
    exit (WEXITSTATUS (wait_status));

#if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
#ifdef MAIL_USE_MAILLOCK
  if (! spool_name)
#endif /* MAIL_USE_MAILLOCK */
    unlink (lockname);
#endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */

#endif /* ! DISABLE_DIRECT_ACCESS */

  return EXIT_SUCCESS;
}
Exemplo n.º 11
0
void __fastcall TNodeForm::TimerTick(TObject *Sender)
{
	static int nodedab;
    char	str[256];
	char	status[128];
    int		i,n,rd,digits=1;
    node_t	node;

	if(!Visible)
		return;

    if(nodedab<1) {
    	char path[MAX_PATH+1];
        sprintf(path,"%sNODE.DAB",MainForm->global.ctrl_dir);
    	nodedab=_sopen(path,O_RDONLY|O_BINARY|O_CREAT, SH_DENYNONE, S_IREAD|S_IWRITE);
		if(nodedab==-1) {
		    ListBox->Items->Clear();
        	ListBox->Items->Add("Error "+AnsiString(errno)+" opening NODE.DAB");
            return;
        }
    }
	if(MainForm->cfg.sys_nodes>9)
		digits++;
	if(MainForm->cfg.sys_nodes>99)
		digits++;
	if(MainForm->cfg.sys_nodes>999)
		digits++;
    for(n=0;n<MainForm->cfg.sys_nodes;n++) {
	    lseek(nodedab, n*sizeof(node_t), SEEK_SET);
        if(eof(nodedab))
        	break;
#ifdef USE_LOCKING
        if(locking(nodedab, LK_NBLCK, sizeof(node_t))!=0)
        	continue;
#endif
		memset(&node,0,sizeof(node_t));
        rd=read(nodedab,&node, sizeof(node_t));
#ifdef USE_LOCKING
        lseek(nodedab, n*sizeof(node_t), SEEK_SET);
        locking(nodedab, LK_UNLCK, sizeof(node_t));
#endif

        if(rd!=sizeof(node_t))
        	continue;
            
		sprintf(str,"%*d %s"
			,digits
			,n+1
			,nodestatus(&MainForm->cfg,&node,status,sizeof(status)));
        AnsiString Str=AnsiString(str);
        if(ListBox->Items->Count<n+1)
        	ListBox->Items->Add(Str);
		else if(ListBox->Items->Strings[n]!=Str) {
            bool selected=ListBox->Selected[n]; // save selected state
        	ListBox->Items->Strings[n]=str;
            ListBox->Selected[n]=selected;      // restore
        }
    }
    if(n!=MainForm->cfg.sys_nodes) {    /* read error or something */
        close(nodedab);
        nodedab=-1;
    }
    Timer->Enabled=true;
}
Exemplo n.º 12
0
int my_lock(File fd, int locktype, my_off_t start, my_off_t length,
	    myf MyFlags)
{
#ifdef HAVE_FCNTL
  int value;
  ALARM_VARIABLES;
#endif
#ifdef __NETWARE__
  int nxErrno;
#endif
  DBUG_ENTER("my_lock");
  DBUG_PRINT("my",("Fd: %d  Op: %d  start: %ld  Length: %ld  MyFlags: %d",
		   fd,locktype,(long) start,(long) length,MyFlags));
#ifdef VMS
  DBUG_RETURN(0);
#else
  if (my_disable_locking)
    DBUG_RETURN(0);

#if defined(__NETWARE__)
  {
    NXSOffset_t nxLength = length;
    unsigned long nxLockFlags = 0;

    if (length == F_TO_EOF)
    {
      /* EOF is interpreted as a very large length. */
      nxLength = 0x7FFFFFFFFFFFFFFF;
    }

    if (locktype == F_UNLCK)
    {
      /* The lock flags are currently ignored by NKS. */
      if (!(nxErrno= NXFileRangeUnlock(fd, 0L, start, nxLength)))
        DBUG_RETURN(0);
    }
    else
    {
      if (locktype == F_RDLCK)
      {
        /* A read lock is mapped to a shared lock. */
        nxLockFlags = NX_RANGE_LOCK_SHARED;
      }
      else
      {
        /* A write lock is mapped to an exclusive lock. */
        nxLockFlags = NX_RANGE_LOCK_EXCL;
      }

      if (MyFlags & MY_DONT_WAIT)
      {
        /* Don't block on the lock. */
        nxLockFlags |= NX_RANGE_LOCK_TRYLOCK;
      }

      if (!(nxErrno= NXFileRangeLock(fd, nxLockFlags, start, nxLength)))
        DBUG_RETURN(0);
    }
  }
#elif defined(HAVE_LOCKING)
  /* Windows */
  {
    my_bool error= FALSE;
    pthread_mutex_lock(&my_file_info[fd].mutex);
    if (MyFlags & MY_SEEK_NOT_DONE) 
    {
      if( my_seek(fd,start,MY_SEEK_SET,MYF(MyFlags & ~MY_SEEK_NOT_DONE))
           == MY_FILEPOS_ERROR )
      {
        /*
          If my_seek fails my_errno will already contain an error code;
          just unlock and return error code.
         */
        DBUG_PRINT("error",("my_errno: %d (%d)",my_errno,errno));
        pthread_mutex_unlock(&my_file_info[fd].mutex);
        DBUG_RETURN(-1);
      }
    }
    error= locking(fd,locktype,(ulong) length) && errno != EINVAL;
    pthread_mutex_unlock(&my_file_info[fd].mutex);
    if (!error)
      DBUG_RETURN(0);
  }
#else
#if defined(HAVE_FCNTL)
  {
    struct flock lock;

    lock.l_type=   (short) locktype;
    lock.l_whence= SEEK_SET;
    lock.l_start=  (off_t) start;
    lock.l_len=    (off_t) length;

    if (MyFlags & MY_DONT_WAIT)
    {
      if (fcntl(fd,F_SETLK,&lock) != -1)	/* Check if we can lock */
	DBUG_RETURN(0);			/* Ok, file locked */
      DBUG_PRINT("info",("Was locked, trying with alarm"));
      ALARM_INIT;
      while ((value=fcntl(fd,F_SETLKW,&lock)) && ! ALARM_TEST &&
	     errno == EINTR)
      {			/* Setup again so we don`t miss it */
	ALARM_REINIT;
      }
      ALARM_END;
      if (value != -1)
	DBUG_RETURN(0);
      if (errno == EINTR)
	errno=EAGAIN;
    }
    else if (fcntl(fd,F_SETLKW,&lock) != -1) /* Wait until a lock */
      DBUG_RETURN(0);
  }
#else
  if (MyFlags & MY_SEEK_NOT_DONE)
  {
    if (my_seek(fd,start,MY_SEEK_SET,MYF(MyFlags & ~MY_SEEK_NOT_DONE))
        == MY_FILEPOS_ERROR)
    {
      /*
        If an error has occured in my_seek then we will already
        have an error code in my_errno; Just return error code.
      */
      DBUG_RETURN(-1);
    }
  }
  if (lockf(fd,locktype,length) != -1)
    DBUG_RETURN(0);
#endif /* HAVE_FCNTL */
#endif /* HAVE_LOCKING */

#ifdef __NETWARE__
  my_errno = nxErrno;
#else
	/* We got an error. We don't want EACCES errors */
  my_errno=(errno == EACCES) ? EAGAIN : errno ? errno : -1;
#endif
  if (MyFlags & MY_WME)
  {
    if (locktype == F_UNLCK)
      my_error(EE_CANTUNLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
    else
      my_error(EE_CANTLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
  }
  DBUG_PRINT("error",("my_errno: %d (%d)",my_errno,errno));
  DBUG_RETURN(-1);
#endif	/* ! VMS */
} /* my_lock */
Exemplo n.º 13
0
static void dyn_lock_function(int mode, struct CRYPTO_dynlock_value* ptr,const char *file, int line)
{
    locking(mode, (ErlNifRWLock*)ptr);
}
Exemplo n.º 14
0
static void locking_function(int mode, int n, const char *file, int line)
{
    locking(mode, lock_vec[n]);
}