Ruby
1.9.3p484(2013-11-22revision43786)
Main Page
Modules
Data Structures
Files
File List
Globals
missing
flock.c
Go to the documentation of this file.
1
#include "ruby/config.h"
2
3
#if defined _WIN32
4
#elif defined HAVE_FCNTL && defined HAVE_FCNTL_H
5
6
/* These are the flock() constants. Since this sytems doesn't have
7
flock(), the values of the constants are probably not available.
8
*/
9
# ifndef LOCK_SH
10
# define LOCK_SH 1
11
# endif
12
# ifndef LOCK_EX
13
# define LOCK_EX 2
14
# endif
15
# ifndef LOCK_NB
16
# define LOCK_NB 4
17
# endif
18
# ifndef LOCK_UN
19
# define LOCK_UN 8
20
# endif
21
22
#include <fcntl.h>
23
#include <unistd.h>
24
#include <errno.h>
25
26
int
27
flock
(
int
fd,
int
operation)
28
{
29
struct
flock
lock;
30
31
switch
(operation & ~
LOCK_NB
) {
32
case
LOCK_SH
:
33
lock.l_type = F_RDLCK;
34
break
;
35
case
LOCK_EX
:
36
lock.l_type = F_WRLCK;
37
break
;
38
case
LOCK_UN
:
39
lock.l_type = F_UNLCK;
40
break
;
41
default
:
42
errno
= EINVAL;
43
return
-1;
44
}
45
lock.l_whence =
SEEK_SET
;
46
lock.l_start = lock.l_len = 0L;
47
48
return
fcntl
(fd, (operation &
LOCK_NB
) ? F_SETLK : F_SETLKW, &lock);
49
}
50
51
#elif defined(HAVE_LOCKF)
52
53
#include <unistd.h>
54
#include <errno.h>
55
56
/* Emulate flock() with lockf() or fcntl(). This is just to increase
57
portability of scripts. The calls might not be completely
58
interchangeable. What's really needed is a good file
59
locking module.
60
*/
61
62
# ifndef F_ULOCK
63
# define F_ULOCK 0
/* Unlock a previously locked region */
64
# endif
65
# ifndef F_LOCK
66
# define F_LOCK 1
/* Lock a region for exclusive use */
67
# endif
68
# ifndef F_TLOCK
69
# define F_TLOCK 2
/* Test and lock a region for exclusive use */
70
# endif
71
# ifndef F_TEST
72
# define F_TEST 3
/* Test a region for other processes locks */
73
# endif
74
75
/* These are the flock() constants. Since this sytems doesn't have
76
flock(), the values of the constants are probably not available.
77
*/
78
# ifndef LOCK_SH
79
# define LOCK_SH 1
80
# endif
81
# ifndef LOCK_EX
82
# define LOCK_EX 2
83
# endif
84
# ifndef LOCK_NB
85
# define LOCK_NB 4
86
# endif
87
# ifndef LOCK_UN
88
# define LOCK_UN 8
89
# endif
90
91
int
92
flock
(
int
fd,
int
operation)
93
{
94
switch
(operation) {
95
96
/* LOCK_SH - get a shared lock */
97
case
LOCK_SH
:
98
rb_notimplement
();
99
return
-1;
100
/* LOCK_EX - get an exclusive lock */
101
case
LOCK_EX
:
102
return
lockf (fd, F_LOCK, 0);
103
104
/* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
105
case
LOCK_SH
|
LOCK_NB
:
106
rb_notimplement
();
107
return
-1;
108
/* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
109
case
LOCK_EX
|
LOCK_NB
:
110
return
lockf (fd, F_TLOCK, 0);
111
112
/* LOCK_UN - unlock */
113
case
LOCK_UN
:
114
return
lockf (fd, F_ULOCK, 0);
115
116
/* Default - can't decipher operation */
117
default
:
118
errno
= EINVAL;
119
return
-1;
120
}
121
}
122
#else
123
int
124
flock
(
int
fd,
int
operation)
125
{
126
rb_notimplement
();
127
return
-1;
128
}
129
#endif
130
Generated on Fri Nov 22 2013 07:04:13 for Ruby by
1.8.3