From: hlu@eecs.wsu.edu (H.J. Lu) Subject: Re: C code porting question [?] Date: Sat, 14 Nov 1992 21:51:04 GMT
In article <1992Nov10.155619.4245@telex.mn.org>, brian@telex.mn.org (Brian K. Teravskis) writes:
|> Hello fellow Linuxers:
|>
|> I am trying to port over a BSD program to Linux (SLS 0.98), and
|> I ran into a couple of modules that use the function flock and
|> the LOCK_NB, LOCK_EX constants. After perusing through the
|> include files I could not find any reference to flock or
|> the constants. I want to know how file locking (as it is) is
|> done under Linux. I would guess that it follows the POSIX lock
|> file creation with the O_CREAT and O_EXCL, but I want to make
|> sure that I'm following the 'correct' way of doing it.
|>
|> Thanks in advance...
|>
|> Brian
|>
I suggest anyone who wants to porting code to Linux join the GCC channel.
You will use the cutting edge gcc and c library. The public release
of the C library is 4.1. Gcc is 2.2.2d7 (GNU 2.2.2). I am going to
start the alpha testing of gcc 2.3.2. Another testing release of
C library 4.2 will be out soon. FYI, flock () is in 4.2.
Here are the relevant files.
H.J.
=======<sys/file.h>=====
#ifndef _SYS_FILE_H
#define _SYS_FILE_H
#include <features.h>
#include <fcntl.h>
#define L_SET 0 /* absolute offset */
#define L_INCR 1 /* relative to current offset */
#define L_XTND 2 /* relative to end of file */
/* Operations for the `flock' call. */
#define LOCK_SH 1 /* Shared lock. */
#define LOCK_EX 2 /* Exclusive lock. */
#define LOCK_UN 8 /* Unlock. */
/* Can be OR'd in to one of the above. */
#define LOCK_NB 4 /* Don't block when locking. */
__BEGIN_DECLS
/* Apply or remove an advisory lock, according to OPERATION,
on the file FD refers to. */
extern int __flock __P ((int __fd, int __operation));
extern int flock __P ((int __fd, int __operation));
__END_DECLS
#endif
=========__flock.c=======
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h>
/* Apply or remove an advisory lock, according to OPERATION,
on the file FD refers to. */
int
DEFUN(__flock, (fd, operation), int fd AND int operation)
{
struct flock flock;
int cmd;
switch (operation & ~LOCK_NB) {
case LOCK_SH:
flock.l_type = F_RDLCK;
break;
case LOCK_EX:
flock.l_type = F_WRLCK;
break;
case LOCK_UN:
flock.l_type = F_UNLCK;
break;
default:
errno = EINVAL;
return -1;
}
flock.l_whence = SEEK_SET;
flock.l_start = flock.l_len = 0L;
cmd = (operation & LOCK_NB) ? F_SETLK : F_SETLKW;
return (__fcntl (fd, cmd, &flock));
}