2007-06-09 23:06:15 -04:00
|
|
|
#include "ruby/config.h"
|
2012-05-16 22:48:59 -04:00
|
|
|
#include "ruby/ruby.h"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2007-02-28 07:18:48 -05:00
|
|
|
#if defined _WIN32
|
2012-05-16 22:48:59 -04:00
|
|
|
#elif defined HAVE_FCNTL && defined HAVE_FCNTL_H && !defined(__native_client__)
|
2001-02-18 03:18:47 -05:00
|
|
|
|
2013-05-18 23:10:21 -04:00
|
|
|
/* These are the flock() constants. Since this systems doesn't have
|
2001-02-18 03:18:47 -05:00
|
|
|
flock(), the values of the constants are probably not available.
|
|
|
|
*/
|
|
|
|
# ifndef LOCK_SH
|
|
|
|
# define LOCK_SH 1
|
|
|
|
# endif
|
|
|
|
# ifndef LOCK_EX
|
|
|
|
# define LOCK_EX 2
|
|
|
|
# endif
|
|
|
|
# ifndef LOCK_NB
|
|
|
|
# define LOCK_NB 4
|
|
|
|
# endif
|
|
|
|
# ifndef LOCK_UN
|
|
|
|
# define LOCK_UN 8
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
int
|
2006-01-05 00:29:48 -05:00
|
|
|
flock(int fd, int operation)
|
2001-02-18 03:18:47 -05:00
|
|
|
{
|
|
|
|
struct flock lock;
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2001-02-18 03:18:47 -05:00
|
|
|
switch (operation & ~LOCK_NB) {
|
|
|
|
case LOCK_SH:
|
|
|
|
lock.l_type = F_RDLCK;
|
|
|
|
break;
|
|
|
|
case LOCK_EX:
|
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
break;
|
|
|
|
case LOCK_UN:
|
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = lock.l_len = 0L;
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2001-02-18 03:18:47 -05:00
|
|
|
return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_LOCKF)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
/* Emulate flock() with lockf() or fcntl(). This is just to increase
|
|
|
|
portability of scripts. The calls might not be completely
|
|
|
|
interchangeable. What's really needed is a good file
|
|
|
|
locking module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
# ifndef F_ULOCK
|
|
|
|
# define F_ULOCK 0 /* Unlock a previously locked region */
|
|
|
|
# endif
|
|
|
|
# ifndef F_LOCK
|
|
|
|
# define F_LOCK 1 /* Lock a region for exclusive use */
|
|
|
|
# endif
|
|
|
|
# ifndef F_TLOCK
|
|
|
|
# define F_TLOCK 2 /* Test and lock a region for exclusive use */
|
|
|
|
# endif
|
|
|
|
# ifndef F_TEST
|
|
|
|
# define F_TEST 3 /* Test a region for other processes locks */
|
|
|
|
# endif
|
|
|
|
|
2013-05-18 23:10:21 -04:00
|
|
|
/* These are the flock() constants. Since this systems doesn't have
|
1998-01-16 07:13:05 -05:00
|
|
|
flock(), the values of the constants are probably not available.
|
|
|
|
*/
|
|
|
|
# ifndef LOCK_SH
|
|
|
|
# define LOCK_SH 1
|
|
|
|
# endif
|
|
|
|
# ifndef LOCK_EX
|
|
|
|
# define LOCK_EX 2
|
|
|
|
# endif
|
|
|
|
# ifndef LOCK_NB
|
|
|
|
# define LOCK_NB 4
|
|
|
|
# endif
|
|
|
|
# ifndef LOCK_UN
|
|
|
|
# define LOCK_UN 8
|
|
|
|
# endif
|
|
|
|
|
|
|
|
int
|
2006-01-05 00:29:48 -05:00
|
|
|
flock(int fd, int operation)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
switch (operation) {
|
|
|
|
|
|
|
|
/* LOCK_SH - get a shared lock */
|
|
|
|
case LOCK_SH:
|
2001-02-26 00:29:06 -05:00
|
|
|
rb_notimplement();
|
|
|
|
return -1;
|
1998-01-16 07:13:05 -05:00
|
|
|
/* LOCK_EX - get an exclusive lock */
|
|
|
|
case LOCK_EX:
|
2001-03-26 03:57:16 -05:00
|
|
|
return lockf (fd, F_LOCK, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
/* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
|
|
|
|
case LOCK_SH|LOCK_NB:
|
2001-02-26 00:29:06 -05:00
|
|
|
rb_notimplement();
|
|
|
|
return -1;
|
1998-01-16 07:13:05 -05:00
|
|
|
/* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
|
|
|
|
case LOCK_EX|LOCK_NB:
|
2001-03-26 03:57:16 -05:00
|
|
|
return lockf (fd, F_TLOCK, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
/* LOCK_UN - unlock */
|
|
|
|
case LOCK_UN:
|
2001-03-26 03:57:16 -05:00
|
|
|
return lockf (fd, F_ULOCK, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
/* Default - can't decipher operation */
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
2001-03-26 03:57:16 -05:00
|
|
|
return -1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2007-02-28 07:18:48 -05:00
|
|
|
#else
|
1998-01-16 07:13:05 -05:00
|
|
|
int
|
2006-01-05 00:29:48 -05:00
|
|
|
flock(int fd, int operation)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
rb_notimplement();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|