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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/*
* 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 2019 Joyent, Inc.
*/
/*
* Utility functions for use in both acquire-lock and runtests.
*/
#include "util.h"
#include <err.h>
#include <errno.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
boolean_t LOG = B_FALSE;
void
flock_log(const char *format, ...)
{
va_list ap;
if (!LOG) {
return;
}
va_start(ap, format);
(void) vfprintf(stderr, format, ap);
va_end(ap);
}
boolean_t
flock_nodata(int fd)
{
struct pollfd pfd = { fd, POLLIN, 0 };
int ret = poll(&pfd, 1, 1000);
if (ret == -1) {
err(EXIT_FAILURE, "poll failed");
}
return (ret == 0);
}
void
flock_block(int fd)
{
char buf[1];
int ret = 0;
while (ret < 1) {
ret = read(fd, buf, 1);
if (ret == -1) {
if (errno == EINTR)
continue;
err(EXIT_FAILURE, "read failed");
}
}
}
void
flock_alert(int fd)
{
int ret = 0;
while (ret < 1) {
ret = write(fd, "1", 1);
if (ret == -1) {
if (errno == EINTR)
continue;
err(EXIT_FAILURE, "write failed");
}
}
}
lock_style_t
flock_styleenum(char *stylestr)
{
if (strcmp(stylestr, "posix") == 0) {
return (LSTYLE_POSIX);
} else if (strcmp(stylestr, "ofd") == 0) {
return (LSTYLE_OFD);
} else if (strcmp(stylestr, "flock") == 0) {
return (LSTYLE_FLOCK);
} else {
errx(EXIT_FAILURE, BAD_LOCK_MESSAGE);
}
}
char *
flock_stylestr(lock_style_t style)
{
switch (style) {
case LSTYLE_POSIX:
return ("posix");
case LSTYLE_OFD:
return ("ofd");
case LSTYLE_FLOCK:
return ("flock");
default:
abort();
}
}
char *
flock_stylename(lock_style_t style)
{
switch (style) {
case LSTYLE_POSIX:
return ("fcntl(2) POSIX");
case LSTYLE_OFD:
return ("fcntl(2) OFD");
case LSTYLE_FLOCK:
return ("flock(3C)");
default:
abort();
}
}
void
flock_reinit(struct flock *flp, int ltype)
{
bzero(flp, sizeof (*flp));
flp->l_type = ltype;
}
char *
flock_cmdname(int cmd)
{
switch (cmd) {
case F_SETLK:
return ("F_SETLK");
case F_OFD_SETLK:
return ("F_OFD_SETLK");
case F_SETLKW:
return ("F_SETLKW");
case F_OFD_SETLKW:
return ("F_OFD_SETLKW");
case F_GETLK:
return ("F_GETLK");
case F_OFD_GETLK:
return ("F_OFD_GETLK");
case F_FLOCK:
return ("F_FLOCK");
case F_FLOCKW:
return ("F_FLOCKW");
#if !defined(_LP64)
case F_OFD_SETLK64:
return ("F_OFD_SETLK64");
case F_OFD_SETLKW64:
return ("F_OFD_SETLKW64");
case F_OFD_GETLK64:
return ("F_OFD_GETLK64");
#endif
default:
abort();
}
}
|