xfstests: resolve compiler warnings
[xfstests-dev.git] / lib / file_lock.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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.
8  *
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.
13  *
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
17  */
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/file.h>
21 #include <sys/param.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <sys/sysmacros.h>
27 #include <string.h> /* memset, strerror */
28 #include "file_lock.h"
29
30
31 #ifndef EFSEXCLWR
32 #define EFSEXCLWR       503
33 #endif
34
35 /*
36  * String containing the last system call.
37  *
38  */
39 char Fl_syscall_str[128];
40
41 static char errmsg[256];
42
43 /***********************************************************************
44  *
45  * Test interface to the fcntl system call.
46  * It will loop if the LOCK_NB flags is NOT set.
47  ***********************************************************************/
48 int
49 file_lock(fd, flags, errormsg)
50 int fd;
51 int flags;
52 char **errormsg;
53 {
54         register int cmd, ret;
55         struct flock flocks;
56
57         memset(&flocks, 0, sizeof(struct flock));
58
59         if (flags&LOCK_NB)
60                 cmd = F_SETLK;
61         else
62                 cmd = F_SETLKW;
63
64         flocks.l_whence = 0;
65         flocks.l_start = 0;
66         flocks.l_len = 0;
67
68         if (flags&LOCK_UN)
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;
74         else {
75                 errno = EINVAL;
76             if ( errormsg != NULL ) {
77                 sprintf(errmsg,
78                     "Programmer error, called file_lock with in valid flags\n");
79                 *errormsg = errmsg;
80             }
81             return -1;
82         }
83
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);
88
89         while (1) {
90             ret = fcntl(fd, cmd, &flocks);
91
92             if ( ret < 0 ) {
93                 if ( cmd == F_SETLK )
94                     switch (errno) {
95                        /* these errors are okay */
96                         case EACCES:    /* Permission denied */
97                         case EINTR:             /* interrupted system call */
98 #ifdef EFILESH
99                         case EFILESH:   /* file shared */
100 #endif
101                         case EFSEXCLWR: /* File is write protected */
102                             continue;   /* retry getting lock */
103                 }
104                 if ( errormsg != NULL ) {
105                     sprintf(errmsg, "fcntl(%d, %d, &flocks): errno:%d %s\n",
106                         fd, cmd, errno, strerror(errno));
107                     *errormsg = errmsg;
108                 }
109                 return -1;
110             }
111             break;
112         }
113
114         return ret;
115
116 }       /* end of file_lock */
117
118 /***********************************************************************
119  *
120  * Test interface to the fcntl system call.
121  * It will loop if the LOCK_NB flags is NOT set.
122  ***********************************************************************/
123 int
124 record_lock(fd, flags, start, len, errormsg)
125 int fd;
126 int flags;
127 int start;
128 int len;
129 char **errormsg;
130 {
131         register int cmd, ret;
132         struct flock flocks;
133
134         memset(&flocks, 0, sizeof(struct flock));
135
136         if (flags&LOCK_NB)
137                 cmd = F_SETLK;
138         else
139                 cmd = F_SETLKW;
140
141         flocks.l_whence = 0;
142         flocks.l_start = start;
143         flocks.l_len = len;
144
145         if (flags&LOCK_UN)
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;
151         else {
152                 errno = EINVAL;
153             if ( errormsg != NULL ) {
154                 sprintf(errmsg,
155                     "Programmer error, called record_lock with in valid flags\n");
156                 *errormsg = errmsg;
157             }
158             return -1;
159         }
160
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);
165
166         while (1) {
167             ret = fcntl(fd, cmd, &flocks);
168
169             if ( ret < 0 ) {
170                 if ( cmd == F_SETLK )
171                     switch (errno) {
172                        /* these errors are okay */
173                         case EACCES:    /* Permission denied */
174                         case EINTR:             /* interrupted system call */
175 #ifdef EFILESH
176                         case EFILESH:   /* file shared */
177 #endif
178                         case EFSEXCLWR: /* File is write protected */
179                             continue;   /* retry getting lock */
180                 }
181                 if ( errormsg != NULL ) {
182                     sprintf(errmsg, "fcntl(%d, %d, &flocks): errno:%d %s\n",
183                         fd, cmd, errno, strerror(errno));
184                     *errormsg = errmsg;
185                 }
186                 return -1;
187             }
188             break;
189         }
190
191         return ret;
192
193 }       /* end of record_lock */
194
195