1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000 Silicon Graphics, Inc.
14 #include <sys/sysmacros.h>
15 #include <string.h> /* memset, strerror */
16 #include "file_lock.h"
24 * String containing the last system call.
27 char Fl_syscall_str[128];
29 static char errmsg[256];
31 /***********************************************************************
33 * Test interface to the fcntl system call.
34 * It will loop if the LOCK_NB flags is NOT set.
35 ***********************************************************************/
37 file_lock(fd, flags, errormsg)
42 register int cmd, ret;
45 memset(&flocks, 0, sizeof(struct flock));
57 flocks.l_type = F_UNLCK;
58 else if (flags&LOCK_EX)
59 flocks.l_type = F_WRLCK;
60 else if (flags&LOCK_SH)
61 flocks.l_type = F_RDLCK;
64 if ( errormsg != NULL ) {
66 "Programmer error, called file_lock with in valid flags\n");
72 sprintf(Fl_syscall_str,
73 "fcntl(%d, %d, &flocks): type:%d whence:%d, start:%lld len:%lld\n",
74 fd, cmd, flocks.l_type, flocks.l_whence,
75 (long long)flocks.l_start, (long long)flocks.l_len);
78 ret = fcntl(fd, cmd, &flocks);
83 /* these errors are okay */
84 case EACCES: /* Permission denied */
85 case EINTR: /* interrupted system call */
87 case EFILESH: /* file shared */
89 case EFSEXCLWR: /* File is write protected */
90 continue; /* retry getting lock */
92 if ( errormsg != NULL ) {
93 sprintf(errmsg, "fcntl(%d, %d, &flocks): errno:%d %s\n",
94 fd, cmd, errno, strerror(errno));
104 } /* end of file_lock */
106 /***********************************************************************
108 * Test interface to the fcntl system call.
109 * It will loop if the LOCK_NB flags is NOT set.
110 ***********************************************************************/
112 record_lock(fd, flags, start, len, errormsg)
119 register int cmd, ret;
122 memset(&flocks, 0, sizeof(struct flock));
130 flocks.l_start = start;
134 flocks.l_type = F_UNLCK;
135 else if (flags&LOCK_EX)
136 flocks.l_type = F_WRLCK;
137 else if (flags&LOCK_SH)
138 flocks.l_type = F_RDLCK;
141 if ( errormsg != NULL ) {
143 "Programmer error, called record_lock with in valid flags\n");
149 sprintf(Fl_syscall_str,
150 "fcntl(%d, %d, &flocks): type:%d whence:%d, start:%lld len:%lld\n",
151 fd, cmd, flocks.l_type, flocks.l_whence,
152 (long long)flocks.l_start, (long long)flocks.l_len);
155 ret = fcntl(fd, cmd, &flocks);
158 if ( cmd == F_SETLK )
160 /* these errors are okay */
161 case EACCES: /* Permission denied */
162 case EINTR: /* interrupted system call */
164 case EFILESH: /* file shared */
166 case EFSEXCLWR: /* File is write protected */
167 continue; /* retry getting lock */
169 if ( errormsg != NULL ) {
170 sprintf(errmsg, "fcntl(%d, %d, &flocks): errno:%d %s\n",
171 fd, cmd, errno, strerror(errno));
181 } /* end of record_lock */