whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 11 | * |
| 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. |
| 18 | * |
| 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. |
| 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | */ |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/file.h> |
| 35 | #include <sys/param.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <unistd.h> |
| 38 | #include <stdio.h> |
| 39 | #include <errno.h> |
| 40 | #include <sys/sysmacros.h> |
| 41 | #include <string.h> /* memset, strerror */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame^] | 42 | #include "file_lock.h" |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 43 | |
| 44 | extern int errno; |
| 45 | |
| 46 | #ifndef EFSEXCLWR |
| 47 | #define EFSEXCLWR 503 |
| 48 | #endif |
| 49 | |
| 50 | /* |
| 51 | * String containing the last system call. |
| 52 | * |
| 53 | */ |
| 54 | char Fl_syscall_str[128]; |
| 55 | |
| 56 | static char errmsg[256]; |
| 57 | |
| 58 | /*********************************************************************** |
| 59 | * |
| 60 | * Test interface to the fcntl system call. |
| 61 | * It will loop if the LOCK_NB flags is NOT set. |
| 62 | ***********************************************************************/ |
| 63 | int |
| 64 | file_lock(fd, flags, errormsg) |
| 65 | int fd; |
| 66 | int flags; |
| 67 | char **errormsg; |
| 68 | { |
| 69 | register int cmd, ret; |
| 70 | struct flock flocks; |
| 71 | |
| 72 | memset(&flocks, 0, sizeof(struct flock)); |
| 73 | |
| 74 | if (flags&LOCK_NB) |
| 75 | cmd = F_SETLK; |
| 76 | else |
| 77 | cmd = F_SETLKW; |
| 78 | |
| 79 | flocks.l_whence = 0; |
| 80 | flocks.l_start = 0; |
| 81 | flocks.l_len = 0; |
| 82 | |
| 83 | if (flags&LOCK_UN) |
| 84 | flocks.l_type = F_UNLCK; |
| 85 | else if (flags&LOCK_EX) |
| 86 | flocks.l_type = F_WRLCK; |
| 87 | else if (flags&LOCK_SH) |
| 88 | flocks.l_type = F_RDLCK; |
| 89 | else { |
| 90 | errno = EINVAL; |
| 91 | if ( errormsg != NULL ) { |
| 92 | sprintf(errmsg, |
| 93 | "Programmer error, called file_lock with in valid flags\n"); |
| 94 | *errormsg = errmsg; |
| 95 | } |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | sprintf(Fl_syscall_str, |
| 100 | "fcntl(%d, %d, &flocks): type:%d whence:%d, start:%lld len:%lld\n", |
| 101 | fd, cmd, flocks.l_type, flocks.l_whence, |
| 102 | (long long)flocks.l_start, (long long)flocks.l_len); |
| 103 | |
| 104 | while (1) { |
| 105 | ret = fcntl(fd, cmd, &flocks); |
| 106 | |
| 107 | if ( ret < 0 ) { |
| 108 | if ( cmd == F_SETLK ) |
| 109 | switch (errno) { |
| 110 | /* these errors are okay */ |
| 111 | case EACCES: /* Permission denied */ |
| 112 | case EINTR: /* interrupted system call */ |
| 113 | #ifdef EFILESH |
| 114 | case EFILESH: /* file shared */ |
| 115 | #endif |
| 116 | case EFSEXCLWR: /* File is write protected */ |
| 117 | continue; /* retry getting lock */ |
| 118 | } |
| 119 | if ( errormsg != NULL ) { |
| 120 | sprintf(errmsg, "fcntl(%d, %d, &flocks): errno:%d %s\n", |
| 121 | fd, cmd, errno, strerror(errno)); |
| 122 | *errormsg = errmsg; |
| 123 | } |
| 124 | return -1; |
| 125 | } |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | return ret; |
| 130 | |
| 131 | } /* end of file_lock */ |
| 132 | |
| 133 | /*********************************************************************** |
| 134 | * |
| 135 | * Test interface to the fcntl system call. |
| 136 | * It will loop if the LOCK_NB flags is NOT set. |
| 137 | ***********************************************************************/ |
| 138 | int |
| 139 | record_lock(fd, flags, start, len, errormsg) |
| 140 | int fd; |
| 141 | int flags; |
| 142 | int start; |
| 143 | int len; |
| 144 | char **errormsg; |
| 145 | { |
| 146 | register int cmd, ret; |
| 147 | struct flock flocks; |
| 148 | |
| 149 | memset(&flocks, 0, sizeof(struct flock)); |
| 150 | |
| 151 | if (flags&LOCK_NB) |
| 152 | cmd = F_SETLK; |
| 153 | else |
| 154 | cmd = F_SETLKW; |
| 155 | |
| 156 | flocks.l_whence = 0; |
| 157 | flocks.l_start = start; |
| 158 | flocks.l_len = len; |
| 159 | |
| 160 | if (flags&LOCK_UN) |
| 161 | flocks.l_type = F_UNLCK; |
| 162 | else if (flags&LOCK_EX) |
| 163 | flocks.l_type = F_WRLCK; |
| 164 | else if (flags&LOCK_SH) |
| 165 | flocks.l_type = F_RDLCK; |
| 166 | else { |
| 167 | errno = EINVAL; |
| 168 | if ( errormsg != NULL ) { |
| 169 | sprintf(errmsg, |
| 170 | "Programmer error, called record_lock with in valid flags\n"); |
| 171 | *errormsg = errmsg; |
| 172 | } |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | sprintf(Fl_syscall_str, |
| 177 | "fcntl(%d, %d, &flocks): type:%d whence:%d, start:%lld len:%lld\n", |
| 178 | fd, cmd, flocks.l_type, flocks.l_whence, |
| 179 | (long long)flocks.l_start, (long long)flocks.l_len); |
| 180 | |
| 181 | while (1) { |
| 182 | ret = fcntl(fd, cmd, &flocks); |
| 183 | |
| 184 | if ( ret < 0 ) { |
| 185 | if ( cmd == F_SETLK ) |
| 186 | switch (errno) { |
| 187 | /* these errors are okay */ |
| 188 | case EACCES: /* Permission denied */ |
| 189 | case EINTR: /* interrupted system call */ |
| 190 | #ifdef EFILESH |
| 191 | case EFILESH: /* file shared */ |
| 192 | #endif |
| 193 | case EFSEXCLWR: /* File is write protected */ |
| 194 | continue; /* retry getting lock */ |
| 195 | } |
| 196 | if ( errormsg != NULL ) { |
| 197 | sprintf(errmsg, "fcntl(%d, %d, &flocks): errno:%d %s\n", |
| 198 | fd, cmd, errno, strerror(errno)); |
| 199 | *errormsg = errmsg; |
| 200 | } |
| 201 | return -1; |
| 202 | } |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | return ret; |
| 207 | |
| 208 | } /* end of record_lock */ |
| 209 | |
| 210 | |