2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
32 #include <sys/types.h>
35 #include <sys/param.h>
40 #include <sys/sysmacros.h>
41 #include <string.h> /* memset, strerror */
42 #include "file_lock.h"
50 * String containing the last system call.
53 char Fl_syscall_str
[128];
55 static char errmsg
[256];
57 /***********************************************************************
59 * Test interface to the fcntl system call.
60 * It will loop if the LOCK_NB flags is NOT set.
61 ***********************************************************************/
63 file_lock(fd
, flags
, errormsg
)
68 register int cmd
, ret
;
71 memset(&flocks
, 0, sizeof(struct flock
));
83 flocks
.l_type
= F_UNLCK
;
84 else if (flags
&LOCK_EX
)
85 flocks
.l_type
= F_WRLCK
;
86 else if (flags
&LOCK_SH
)
87 flocks
.l_type
= F_RDLCK
;
90 if ( errormsg
!= NULL
) {
92 "Programmer error, called file_lock with in valid flags\n");
98 sprintf(Fl_syscall_str
,
99 "fcntl(%d, %d, &flocks): type:%d whence:%d, start:%lld len:%lld\n",
100 fd
, cmd
, flocks
.l_type
, flocks
.l_whence
,
101 (long long)flocks
.l_start
, (long long)flocks
.l_len
);
104 ret
= fcntl(fd
, cmd
, &flocks
);
107 if ( cmd
== F_SETLK
)
109 /* these errors are okay */
110 case EACCES
: /* Permission denied */
111 case EINTR
: /* interrupted system call */
113 case EFILESH
: /* file shared */
115 case EFSEXCLWR
: /* File is write protected */
116 continue; /* retry getting lock */
118 if ( errormsg
!= NULL
) {
119 sprintf(errmsg
, "fcntl(%d, %d, &flocks): errno:%d %s\n",
120 fd
, cmd
, errno
, strerror(errno
));
130 } /* end of file_lock */
132 /***********************************************************************
134 * Test interface to the fcntl system call.
135 * It will loop if the LOCK_NB flags is NOT set.
136 ***********************************************************************/
138 record_lock(fd
, flags
, start
, len
, errormsg
)
145 register int cmd
, ret
;
148 memset(&flocks
, 0, sizeof(struct flock
));
156 flocks
.l_start
= start
;
160 flocks
.l_type
= F_UNLCK
;
161 else if (flags
&LOCK_EX
)
162 flocks
.l_type
= F_WRLCK
;
163 else if (flags
&LOCK_SH
)
164 flocks
.l_type
= F_RDLCK
;
167 if ( errormsg
!= NULL
) {
169 "Programmer error, called record_lock with in valid flags\n");
175 sprintf(Fl_syscall_str
,
176 "fcntl(%d, %d, &flocks): type:%d whence:%d, start:%lld len:%lld\n",
177 fd
, cmd
, flocks
.l_type
, flocks
.l_whence
,
178 (long long)flocks
.l_start
, (long long)flocks
.l_len
);
181 ret
= fcntl(fd
, cmd
, &flocks
);
184 if ( cmd
== F_SETLK
)
186 /* these errors are okay */
187 case EACCES
: /* Permission denied */
188 case EINTR
: /* interrupted system call */
190 case EFILESH
: /* file shared */
192 case EFSEXCLWR
: /* File is write protected */
193 continue; /* retry getting lock */
195 if ( errormsg
!= NULL
) {
196 sprintf(errmsg
, "fcntl(%d, %d, &flocks): errno:%d %s\n",
197 fd
, cmd
, errno
, strerror(errno
));
207 } /* end of record_lock */