1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2016 Joyent, Inc.
*/
/*
* Acquire the specified kind of lock with the specified parameters. After
* acquiring the lock, a byte will be written to stdout. The program will
* then wait for a byte to be written to stdin before exiting.
*
* Usage: <posix|ofd|flock> <shared|exclusive> <path>
*/
#include "util.h"
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/file.h>
#include <unistd.h>
static void acq_fcntl(int, int, int);
static void acq_flock(int fd, int mode);
static void acq_run(int, lock_style_t, boolean_t);
static void
acq_fcntl(int fd, int cmd, int mode)
{
struct flock fl;
int ret, i;
/*
* Acquire the lock, and then try reacquiring it several times. Once we
* have acquired the lock, trying to acquire it again should succeed,
* and shouldn't upgrade, downgrade or free the lock.
*/
for (i = 0; i < 3; i++) {
flock_reinit(&fl, mode);
flock_log("Acquiring lock (fcntl)...\n");
ret = fcntl(fd, cmd, &fl);
if (ret == -1) {
err(EXIT_FAILURE, "fcntl failed");
}
}
/* Let the parent know we have the lock and wait */
flock_log("Waiting (fcntl)...\n");
flock_alert(1);
flock_block(0);
/* Now unlock */
flock_reinit(&fl, F_UNLCK);
flock_log("Releasing lock (fcntl)...\n");
ret = fcntl(fd, cmd, &fl);
if (ret == -1) {
err(EXIT_FAILURE, "fcntl failed");
}
}
static void
acq_flock(int fd, int mode)
{
int ret, i;
/*
* Acquire the lock, and then try reacquiring it several times. Once we
* have acquired the lock, trying to acquire it again should succeed,
* and shouldn't upgrade, downgrade or free the lock.
*/
for (i = 0; i < 3; i++) {
flock_log("Acquiring lock (flock)...\n");
ret = flock(fd, mode);
if (ret == -1) {
err(EXIT_FAILURE, "flock failed");
}
}
/* Wait to be okayed to unlock */
flock_log("Waiting (flock)...\n");
flock_alert(1);
flock_block(0);
/* Release lock */
flock_log("Releasing lock (flock)...\n");
ret = flock(fd, LOCK_UN);
if (ret == -1) {
err(EXIT_FAILURE, "flock failed");
}
}
static void
acq_run(int fd, lock_style_t style, boolean_t is_exclusive)
{
switch (style) {
case LSTYLE_POSIX:
acq_fcntl(fd, F_SETLKW, is_exclusive ? F_WRLCK : F_RDLCK);
break;
case LSTYLE_OFD:
acq_fcntl(fd, F_OFD_SETLKW, is_exclusive ? F_WRLCK : F_RDLCK);
break;
case LSTYLE_FLOCK:
acq_flock(fd, is_exclusive ? LOCK_EX : LOCK_SH);
break;
default:
abort();
}
}
int
main(int argc, char *argv[])
{
char *modestr, *path;
lock_style_t style;
boolean_t is_exclusive;
int fd;
if (argc < 4) {
errx(EXIT_FAILURE, BAD_ARGS_MESSAGE, argc - 1);
}
modestr = argv[2];
path = argv[3];
style = flock_styleenum(argv[1]);
if (strcmp(modestr, "shared") == 0) {
is_exclusive = B_FALSE;
} else if (strcmp(modestr, "exclusive") == 0) {
is_exclusive = B_TRUE;
} else {
errx(EXIT_FAILURE, BAD_MODE_MESSAGE);
}
boolean_t rdonly = style == LSTYLE_FLOCK || !is_exclusive;
fd = open(path, rdonly ? O_RDONLY : O_WRONLY);
if (fd == -1) {
err(EXIT_FAILURE, "Failed to open %s", path);
}
acq_run(fd, style, is_exclusive);
return (0);
}
|