2 * Copyright (c) 2000 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <sys/types.h>
21 #include <sys/param.h>
26 #include <sys/sysmacros.h>
27 #include <string.h> /* memset, strerror */
28 #include "file_lock.h"
36 * String containing the last system call.
39 char Fl_syscall_str[128];
41 static char errmsg[256];
43 /***********************************************************************
45 * Test interface to the fcntl system call.
46 * It will loop if the LOCK_NB flags is NOT set.
47 ***********************************************************************/
49 file_lock(fd, flags, errormsg)
54 register int cmd, ret;
57 memset(&flocks, 0, sizeof(struct flock));
69 flocks.l_type = F_UNLCK;
70 else if (flags&LOCK_EX)
71 flocks.l_type = F_WRLCK;
72 else if (flags&LOCK_SH)
73 flocks.l_type = F_RDLCK;
76 if ( errormsg != NULL ) {
78 "Programmer error, called file_lock with in valid flags\n");
84 sprintf(Fl_syscall_str,
85 "fcntl(%d, %d, &flocks): type:%d whence:%d, start:%lld len:%lld\n",
86 fd, cmd, flocks.l_type, flocks.l_whence,
87 (long long)flocks.l_start, (long long)flocks.l_len);
90 ret = fcntl(fd, cmd, &flocks);
95 /* these errors are okay */
96 case EACCES: /* Permission denied */
97 case EINTR: /* interrupted system call */
99 case EFILESH: /* file shared */
101 case EFSEXCLWR: /* File is write protected */
102 continue; /* retry getting lock */
104 if ( errormsg != NULL ) {
105 sprintf(errmsg, "fcntl(%d, %d, &flocks): errno:%d %s\n",
106 fd, cmd, errno, strerror(errno));
116 } /* end of file_lock */
118 /***********************************************************************
120 * Test interface to the fcntl system call.
121 * It will loop if the LOCK_NB flags is NOT set.
122 ***********************************************************************/
124 record_lock(fd, flags, start, len, errormsg)
131 register int cmd, ret;
134 memset(&flocks, 0, sizeof(struct flock));
142 flocks.l_start = start;
146 flocks.l_type = F_UNLCK;
147 else if (flags&LOCK_EX)
148 flocks.l_type = F_WRLCK;
149 else if (flags&LOCK_SH)
150 flocks.l_type = F_RDLCK;
153 if ( errormsg != NULL ) {
155 "Programmer error, called record_lock with in valid flags\n");
161 sprintf(Fl_syscall_str,
162 "fcntl(%d, %d, &flocks): type:%d whence:%d, start:%lld len:%lld\n",
163 fd, cmd, flocks.l_type, flocks.l_whence,
164 (long long)flocks.l_start, (long long)flocks.l_len);
167 ret = fcntl(fd, cmd, &flocks);
170 if ( cmd == F_SETLK )
172 /* these errors are okay */
173 case EACCES: /* Permission denied */
174 case EINTR: /* interrupted system call */
176 case EFILESH: /* file shared */
178 case EFSEXCLWR: /* File is write protected */
179 continue; /* retry getting lock */
181 if ( errormsg != NULL ) {
182 sprintf(errmsg, "fcntl(%d, %d, &flocks): errno:%d %s\n",
183 fd, cmd, errno, strerror(errno));
193 } /* end of record_lock */