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