1
by brian
clean slate |
1 |
/* Copyright (C) 2000-2003 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
#include "mysys_priv.h" |
|
17 |
#include "mysys_err.h" |
|
18 |
#include <errno.h> |
|
19 |
#undef MY_HOW_OFTEN_TO_ALARM
|
|
20 |
#define MY_HOW_OFTEN_TO_ALARM ((int) my_time_to_wait_for_lock)
|
|
21 |
#ifdef NO_ALARM_LOOP
|
|
22 |
#undef NO_ALARM_LOOP
|
|
23 |
#endif
|
|
24 |
#include <my_alarm.h> |
|
25 |
||
26 |
/*
|
|
27 |
Lock a part of a file
|
|
28 |
||
29 |
RETURN VALUE
|
|
30 |
0 Success
|
|
31 |
-1 An error has occured and 'my_errno' is set
|
|
32 |
to indicate the actual error code.
|
|
33 |
*/
|
|
34 |
||
35 |
int my_lock(File fd, int locktype, my_off_t start, my_off_t length, |
|
36 |
myf MyFlags) |
|
37 |
{
|
|
38 |
#ifdef HAVE_FCNTL
|
|
39 |
int value; |
|
40 |
ALARM_VARIABLES; |
|
41 |
#endif
|
|
42 |
DBUG_ENTER("my_lock"); |
|
43 |
DBUG_PRINT("my",("Fd: %d Op: %d start: %ld Length: %ld MyFlags: %d", |
|
44 |
fd,locktype,(long) start,(long) length,MyFlags)); |
|
45 |
if (my_disable_locking) |
|
46 |
DBUG_RETURN(0); |
|
47 |
||
48 |
#if defined(HAVE_FCNTL)
|
|
49 |
{
|
|
50 |
struct flock lock; |
|
51 |
||
52 |
lock.l_type= (short) locktype; |
|
53 |
lock.l_whence= SEEK_SET; |
|
54 |
lock.l_start= (off_t) start; |
|
55 |
lock.l_len= (off_t) length; |
|
56 |
||
57 |
if (MyFlags & MY_DONT_WAIT) |
|
58 |
{
|
|
59 |
if (fcntl(fd,F_SETLK,&lock) != -1) /* Check if we can lock */ |
|
60 |
DBUG_RETURN(0); /* Ok, file locked */ |
|
61 |
DBUG_PRINT("info",("Was locked, trying with alarm")); |
|
62 |
ALARM_INIT; |
|
63 |
while ((value=fcntl(fd,F_SETLKW,&lock)) && ! ALARM_TEST && |
|
64 |
errno == EINTR) |
|
65 |
{ /* Setup again so we don`t miss it */ |
|
66 |
ALARM_REINIT; |
|
67 |
}
|
|
68 |
ALARM_END; |
|
69 |
if (value != -1) |
|
70 |
DBUG_RETURN(0); |
|
71 |
if (errno == EINTR) |
|
72 |
errno=EAGAIN; |
|
73 |
}
|
|
74 |
else if (fcntl(fd,F_SETLKW,&lock) != -1) /* Wait until a lock */ |
|
75 |
DBUG_RETURN(0); |
|
76 |
}
|
|
77 |
#else
|
|
78 |
if (MyFlags & MY_SEEK_NOT_DONE) |
|
79 |
{
|
|
80 |
if (my_seek(fd,start,MY_SEEK_SET,MYF(MyFlags & ~MY_SEEK_NOT_DONE)) |
|
81 |
== MY_FILEPOS_ERROR) |
|
82 |
{
|
|
83 |
/*
|
|
84 |
If an error has occured in my_seek then we will already
|
|
85 |
have an error code in my_errno; Just return error code.
|
|
86 |
*/
|
|
87 |
DBUG_RETURN(-1); |
|
88 |
}
|
|
89 |
}
|
|
90 |
if (lockf(fd,locktype,length) != -1) |
|
91 |
DBUG_RETURN(0); |
|
92 |
#endif /* HAVE_FCNTL */ |
|
93 |
||
94 |
/* We got an error. We don't want EACCES errors */
|
|
95 |
my_errno=(errno == EACCES) ? EAGAIN : errno ? errno : -1; |
|
96 |
if (MyFlags & MY_WME) |
|
97 |
{
|
|
98 |
if (locktype == F_UNLCK) |
|
99 |
my_error(EE_CANTUNLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno); |
|
100 |
else
|
|
101 |
my_error(EE_CANTLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno); |
|
102 |
}
|
|
103 |
DBUG_PRINT("error",("my_errno: %d (%d)",my_errno,errno)); |
|
104 |
DBUG_RETURN(-1); |
|
105 |
} /* my_lock */ |