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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/* $NetBSD: h_tools.c,v 1.7 2006/11/09 16:20:06 jmmv Exp $ */
/*-
* Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Julio M. Merino Vidal, developed as part of Google's Summer of Code
* 2005 program.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*/
/*
* Helper tools for several tests. These are kept in a single file due
* to the limitations of bsd.prog.mk to build a single program in a
* given directory.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/mount.h>
#include <sys/statvfs.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* --------------------------------------------------------------------- */
static int getfh_main(int, char **);
static int kqueue_main(int, char **);
static int rename_main(int, char **);
static int sockets_main(int, char **);
static int statvfs_main(int, char **);
/* --------------------------------------------------------------------- */
int
getfh_main(int argc, char **argv)
{
int error;
fhandle_t fh;
if (argc < 2)
return EXIT_FAILURE;
error = getfh(argv[1], &fh);
if (error == 0)
err(EXIT_FAILURE, "can not getfh");
error = write(STDOUT_FILENO, &fh, sizeof(fh));
if (error == -1) {
perror("write");
return EXIT_FAILURE;
}
return 0;
}
/* --------------------------------------------------------------------- */
int
kqueue_main(int argc, char **argv)
{
char *line;
int i, kq;
size_t len;
struct kevent *changes, event;
if (argc < 2)
return EXIT_FAILURE;
argc--;
argv++;
changes = malloc(sizeof(struct kevent) * (argc - 1));
if (changes == NULL)
errx(EXIT_FAILURE, "not enough memory");
for (i = 0; i < argc; i++) {
int fd;
fd = open(argv[i], O_RDONLY);
if (fd == -1)
err(EXIT_FAILURE, "cannot open %s", argv[i]);
EV_SET(&changes[i], fd, EVFILT_VNODE,
EV_ADD | EV_ENABLE | EV_ONESHOT,
NOTE_ATTRIB | NOTE_DELETE | NOTE_EXTEND | NOTE_LINK |
NOTE_RENAME | NOTE_REVOKE | NOTE_WRITE,
0, 0);
}
kq = kqueue();
if (kq == -1)
err(EXIT_FAILURE, "kqueue");
while ((line = fgetln(stdin, &len)) != NULL) {
int ec, nev;
struct timespec to;
to.tv_sec = 0;
to.tv_nsec = 100000;
(void)kevent(kq, changes, argc, &event, 1, &to);
assert(len > 0);
assert(line[len - 1] == '\n');
line[len - 1] = '\0';
ec = system(line);
if (ec != EXIT_SUCCESS)
errx(ec, "%s returned %d", line, ec);
do {
nev = kevent(kq, changes, argc, &event, 1, &to);
if (nev == -1)
err(EXIT_FAILURE, "kevent");
else if (nev > 0) {
for (i = 0; i < argc; i++)
if (event.ident == changes[i].ident)
break;
if (event.fflags & NOTE_ATTRIB)
printf("%s - NOTE_ATTRIB\n", argv[i]);
if (event.fflags & NOTE_DELETE)
printf("%s - NOTE_DELETE\n", argv[i]);
if (event.fflags & NOTE_EXTEND)
printf("%s - NOTE_EXTEND\n", argv[i]);
if (event.fflags & NOTE_LINK)
printf("%s - NOTE_LINK\n", argv[i]);
if (event.fflags & NOTE_RENAME)
printf("%s - NOTE_RENAME\n", argv[i]);
if (event.fflags & NOTE_REVOKE)
printf("%s - NOTE_REVOKE\n", argv[i]);
if (event.fflags & NOTE_WRITE)
printf("%s - NOTE_WRITE\n", argv[i]);
}
} while (nev > 0);
}
for (i = 0; i < argc; i++)
close(changes[i].ident);
free(changes);
return EXIT_SUCCESS;
}
/* --------------------------------------------------------------------- */
int
rename_main(int argc, char **argv)
{
if (argc < 3)
return EXIT_FAILURE;
return rename(argv[1], argv[2]);
}
/* --------------------------------------------------------------------- */
int
sockets_main(int argc, char **argv)
{
int error, fd;
struct sockaddr_un addr;
if (argc < 2)
return EXIT_FAILURE;
fd = socket(PF_LOCAL, SOCK_STREAM, 0);
if (fd == -1) {
perror("socket");
return EXIT_FAILURE;
}
(void)strlcpy(addr.sun_path, argv[1], sizeof(addr.sun_path));
addr.sun_family = PF_UNIX;
error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
if (error == -1) {
perror("connect");
return EXIT_FAILURE;
}
close(fd);
return EXIT_SUCCESS;
}
/* --------------------------------------------------------------------- */
int
statvfs_main(int argc, char **argv)
{
int error;
struct statfs buf;
if (argc < 2)
return EXIT_FAILURE;
memset(&buf, 0, sizeof(buf));
buf.f_version = STATFS_VERSION;
error = statfs(argv[1], &buf);
if (error != 0) {
perror("statvfs");
return EXIT_FAILURE;
}
(void)printf("f_bsize=%llu\n", buf.f_bsize);
(void)printf("f_blocks=%llu\n", buf.f_blocks);
(void)printf("f_bfree=%llu\n", buf.f_bfree);
(void)printf("f_files=%llu\n", buf.f_files);
return EXIT_SUCCESS;
}
/* --------------------------------------------------------------------- */
int
main(int argc, char **argv)
{
int error;
if (argc < 2)
return EXIT_FAILURE;
argc -= 1;
argv += 1;
if (strcmp(argv[0], "getfh") == 0)
error = getfh_main(argc, argv);
else if (strcmp(argv[0], "kqueue") == 0)
error = kqueue_main(argc, argv);
else if (strcmp(argv[0], "rename") == 0)
error = rename_main(argc, argv);
else if (strcmp(argv[0], "sockets") == 0)
error = sockets_main(argc, argv);
else if (strcmp(argv[0], "statvfs") == 0)
error = statvfs_main(argc, argv);
else
error = EXIT_FAILURE;
return error;
}
|