1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
|
|
|
/* under the terms of the GNU Library General Public License. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-08 08:18:32 -07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1998-10-20 05:50:25 -07:00
|
|
|
#include <mlvalues.h>
|
|
|
|
#include "unixsupport.h"
|
1995-05-08 08:18:32 -07:00
|
|
|
|
1998-10-20 05:50:25 -07:00
|
|
|
#if defined(F_GETLK) && defined(F_SETLK) && defined(F_SETLKW)
|
1995-05-08 08:18:32 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_lockf(value fd, value cmd, value span)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
struct flock l;
|
|
|
|
int ret;
|
|
|
|
int fildes;
|
|
|
|
long size;
|
|
|
|
|
|
|
|
fildes = Int_val(fd);
|
|
|
|
size = Long_val(span);
|
|
|
|
l.l_whence = 1;
|
|
|
|
if (size < 0) {
|
|
|
|
l.l_start = size;
|
|
|
|
l.l_len = -size;
|
|
|
|
} else {
|
|
|
|
l.l_start = 0L;
|
|
|
|
l.l_len = size;
|
|
|
|
}
|
1995-06-18 07:59:32 -07:00
|
|
|
switch (Int_val(cmd)) {
|
1995-05-08 08:18:32 -07:00
|
|
|
case 0: /* F_ULOCK */
|
|
|
|
l.l_type = F_UNLCK;
|
|
|
|
ret = fcntl(fildes, F_SETLK, &l);
|
|
|
|
break;
|
|
|
|
case 1: /* F_LOCK */
|
|
|
|
l.l_type = F_WRLCK;
|
|
|
|
ret = fcntl(fildes, F_SETLKW, &l);
|
|
|
|
break;
|
|
|
|
case 2: /* F_TLOCK */
|
|
|
|
l.l_type = F_WRLCK;
|
|
|
|
ret = fcntl(fildes, F_SETLK, &l);
|
|
|
|
break;
|
|
|
|
case 3: /* F_TEST */
|
|
|
|
l.l_type = F_WRLCK;
|
|
|
|
ret = fcntl(fildes, F_GETLK, &l);
|
|
|
|
if (ret != -1) {
|
|
|
|
if (l.l_type == F_UNLCK)
|
|
|
|
ret = 0;
|
|
|
|
else {
|
|
|
|
errno = EACCES;
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
1998-10-20 05:50:25 -07:00
|
|
|
case 4: /* F_RLOCK */
|
|
|
|
l.l_type = F_RDLCK;
|
|
|
|
ret = fcntl(fildes, F_SETLKW, &l);
|
|
|
|
break;
|
|
|
|
case 5: /* F_TRLOCK */
|
|
|
|
l.l_type = F_RDLCK;
|
|
|
|
ret = fcntl(fildes, F_SETLK, &l);
|
|
|
|
break;
|
1995-05-08 08:18:32 -07:00
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
if (ret == -1) uerror("lockf", Nothing);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
1998-10-20 05:50:25 -07:00
|
|
|
#ifdef HAS_LOCKF
|
|
|
|
#ifdef HAS_UNISTD
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#define F_ULOCK 0
|
|
|
|
#define F_LOCK 1
|
|
|
|
#define F_TLOCK 2
|
|
|
|
#define F_TEST 3
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int lock_command_table[] = {
|
|
|
|
F_ULOCK, F_LOCK, F_TLOCK, F_TEST, F_LOCK, F_TLOCK
|
|
|
|
};
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_lockf(value fd, value cmd, value span)
|
1998-10-20 05:50:25 -07:00
|
|
|
{
|
|
|
|
if (lockf(Int_val(fd), lock_command_table[Int_val(cmd)], Long_val(span))
|
|
|
|
== -1) uerror("lockf", Nothing);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_lockf(value fd, value cmd, value span)
|
1997-09-02 05:55:01 -07:00
|
|
|
{ invalid_argument("lockf not implemented"); }
|
1995-05-08 08:18:32 -07:00
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|