2009-11-05 02:16:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Mark Heily <mark@heily.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2010-01-31 22:05:44 +00:00
|
|
|
static int __thread kqfd;
|
|
|
|
static int __thread vnode_fd;
|
2010-02-03 02:52:44 +00:00
|
|
|
static char __thread testfile[1024];
|
|
|
|
|
|
|
|
|
|
|
|
/* Create an empty file */
|
|
|
|
static void
|
|
|
|
testfile_create(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((fd = open(testfile, O_CREAT | O_WRONLY, 0600)) < 0)
|
2010-02-07 05:03:21 +00:00
|
|
|
die("open");
|
2010-02-03 02:52:44 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
testfile_touch(void)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
2010-02-06 00:46:05 +00:00
|
|
|
snprintf(&buf[0], sizeof(buf), "touch %s", testfile);
|
2010-02-03 02:52:44 +00:00
|
|
|
if (system(buf) != 0)
|
2010-02-07 05:03:21 +00:00
|
|
|
die("system");
|
2010-02-03 02:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
testfile_write(void)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
2010-02-06 00:46:05 +00:00
|
|
|
snprintf(&buf[0], sizeof(buf), "echo hi >> %s", testfile);
|
2010-02-03 02:52:44 +00:00
|
|
|
if (system(buf) != 0)
|
2010-02-07 05:03:21 +00:00
|
|
|
die("system");
|
2010-02-03 02:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
testfile_rename(int step)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
2010-02-06 00:46:05 +00:00
|
|
|
snprintf(&buf[0], sizeof(buf), "%s.tmp", testfile);
|
2010-02-03 02:52:44 +00:00
|
|
|
/* XXX-FIXME use of 'step' conceals a major memory corruption
|
|
|
|
when the file is renamed twice.
|
|
|
|
To replicate, remove "if step" conditional so
|
|
|
|
two renames occur in this function.
|
|
|
|
*/
|
|
|
|
if (step == 0) {
|
|
|
|
if (rename(testfile,buf) != 0)
|
|
|
|
err(1,"rename");
|
|
|
|
} else {
|
|
|
|
if (rename(buf, testfile) != 0)
|
|
|
|
err(1,"rename");
|
|
|
|
}
|
|
|
|
}
|
2009-11-05 02:16:22 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
test_kevent_vnode_add(void)
|
|
|
|
{
|
|
|
|
struct kevent kev;
|
|
|
|
|
2010-02-03 02:52:44 +00:00
|
|
|
testfile_create();
|
|
|
|
|
|
|
|
vnode_fd = open(testfile, O_RDWR);
|
2009-11-05 02:16:22 +00:00
|
|
|
if (vnode_fd < 0)
|
|
|
|
err(1, "open of %s", testfile);
|
|
|
|
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_add(kqfd, &kev, vnode_fd, EVFILT_VNODE, EV_ADD,
|
2009-11-05 02:16:22 +00:00
|
|
|
NOTE_WRITE | NOTE_ATTRIB | NOTE_RENAME | NOTE_DELETE, 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_kevent_vnode_note_delete(void)
|
|
|
|
{
|
|
|
|
struct kevent kev;
|
|
|
|
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_add(kqfd, &kev, vnode_fd, EVFILT_VNODE, EV_ADD | EV_ONESHOT, NOTE_DELETE, 0, NULL);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
2010-02-03 02:52:44 +00:00
|
|
|
if (unlink(testfile) < 0)
|
2010-02-07 05:03:21 +00:00
|
|
|
die("unlink");
|
2009-11-05 02:16:22 +00:00
|
|
|
|
|
|
|
kevent_cmp(&kev, kevent_get(kqfd));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_kevent_vnode_note_write(void)
|
|
|
|
{
|
|
|
|
struct kevent kev;
|
|
|
|
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_add(kqfd, &kev, vnode_fd, EVFILT_VNODE, EV_ADD | EV_ONESHOT, NOTE_WRITE, 0, NULL);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
2010-02-03 02:52:44 +00:00
|
|
|
testfile_write();
|
2009-11-05 02:16:22 +00:00
|
|
|
|
|
|
|
/* BSD kqueue adds NOTE_EXTEND even though it was not requested */
|
|
|
|
/* BSD kqueue removes EV_ENABLE */
|
|
|
|
kev.flags &= ~EV_ENABLE; // XXX-FIXME compatibility issue
|
|
|
|
kev.fflags |= NOTE_EXTEND; // XXX-FIXME compatibility issue
|
|
|
|
kevent_cmp(&kev, kevent_get(kqfd));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_kevent_vnode_note_attrib(void)
|
|
|
|
{
|
|
|
|
struct kevent kev;
|
|
|
|
int nfds;
|
|
|
|
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_add(kqfd, &kev, vnode_fd, EVFILT_VNODE, EV_ADD | EV_ONESHOT, NOTE_ATTRIB, 0, NULL);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
2010-02-03 02:52:44 +00:00
|
|
|
testfile_touch();
|
2009-11-05 02:16:22 +00:00
|
|
|
|
|
|
|
nfds = kevent(kqfd, NULL, 0, &kev, 1, NULL);
|
|
|
|
if (nfds < 1)
|
2010-02-07 05:03:21 +00:00
|
|
|
die("kevent");
|
2009-11-05 02:16:22 +00:00
|
|
|
if (kev.ident != vnode_fd ||
|
|
|
|
kev.filter != EVFILT_VNODE ||
|
|
|
|
kev.fflags != NOTE_ATTRIB)
|
|
|
|
err(1, "%s - incorrect event (sig=%u; filt=%d; flags=%d)",
|
|
|
|
test_id, (unsigned int)kev.ident, kev.filter, kev.flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_kevent_vnode_note_rename(void)
|
|
|
|
{
|
|
|
|
struct kevent kev;
|
|
|
|
int nfds;
|
|
|
|
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_add(kqfd, &kev, vnode_fd, EVFILT_VNODE, EV_ADD | EV_ONESHOT, NOTE_RENAME, 0, NULL);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
2010-02-03 02:52:44 +00:00
|
|
|
testfile_rename(0);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
|
|
|
nfds = kevent(kqfd, NULL, 0, &kev, 1, NULL);
|
|
|
|
if (nfds < 1)
|
2010-02-07 05:03:21 +00:00
|
|
|
die("kevent");
|
2009-11-05 02:16:22 +00:00
|
|
|
if (kev.ident != vnode_fd ||
|
|
|
|
kev.filter != EVFILT_VNODE ||
|
|
|
|
kev.fflags != NOTE_RENAME)
|
|
|
|
err(1, "%s - incorrect event (sig=%u; filt=%d; flags=%d)",
|
|
|
|
test_id, (unsigned int)kev.ident, kev.filter, kev.flags);
|
|
|
|
|
2010-02-03 02:52:44 +00:00
|
|
|
testfile_rename(1);
|
|
|
|
|
|
|
|
test_no_kevents(kqfd);
|
2009-11-05 02:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_kevent_vnode_del(void)
|
|
|
|
{
|
|
|
|
struct kevent kev;
|
|
|
|
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_add(kqfd, &kev, vnode_fd, EVFILT_VNODE, EV_DELETE, 0, 0, NULL);
|
2009-11-05 02:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_kevent_vnode_disable_and_enable(void)
|
|
|
|
{
|
|
|
|
struct kevent kev;
|
|
|
|
int nfds;
|
|
|
|
|
2010-01-31 22:05:44 +00:00
|
|
|
test_no_kevents(kqfd);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
|
|
|
/* Add the watch and immediately disable it */
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_add(kqfd, &kev, vnode_fd, EVFILT_VNODE, EV_ADD | EV_ONESHOT, NOTE_ATTRIB, 0, NULL);
|
2009-11-05 02:16:22 +00:00
|
|
|
kev.flags = EV_DISABLE;
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_update(kqfd, &kev);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
|
|
|
/* Confirm that the watch is disabled */
|
2010-02-03 02:52:44 +00:00
|
|
|
testfile_touch();
|
2010-01-31 22:05:44 +00:00
|
|
|
test_no_kevents(kqfd);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
|
|
|
/* Re-enable and check again */
|
|
|
|
kev.flags = EV_ENABLE;
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_update(kqfd, &kev);
|
2010-02-03 02:52:44 +00:00
|
|
|
testfile_touch();
|
2009-11-05 02:16:22 +00:00
|
|
|
nfds = kevent(kqfd, NULL, 0, &kev, 1, NULL);
|
|
|
|
if (nfds < 1)
|
2010-02-07 05:03:21 +00:00
|
|
|
die("kevent");
|
2009-11-05 02:16:22 +00:00
|
|
|
if (kev.ident != vnode_fd ||
|
|
|
|
kev.filter != EVFILT_VNODE ||
|
|
|
|
kev.fflags != NOTE_ATTRIB)
|
|
|
|
err(1, "%s - incorrect event (sig=%u; filt=%d; flags=%d)",
|
|
|
|
test_id, (unsigned int)kev.ident, kev.filter, kev.flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_EV_DISPATCH
|
|
|
|
void
|
|
|
|
test_kevent_vnode_dispatch(void)
|
|
|
|
{
|
|
|
|
struct kevent kev;
|
|
|
|
int nfds;
|
|
|
|
|
2010-01-31 22:05:44 +00:00
|
|
|
test_no_kevents(kqfd);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_add(kqfd, &kev, vnode_fd, EVFILT_VNODE, EV_ADD | EV_DISPATCH, NOTE_ATTRIB, 0, NULL);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
2010-02-03 02:52:44 +00:00
|
|
|
testfile_touch();
|
2009-11-05 02:16:22 +00:00
|
|
|
|
|
|
|
nfds = kevent(kqfd, NULL, 0, &kev, 1, NULL);
|
|
|
|
if (nfds < 1)
|
2010-02-07 05:03:21 +00:00
|
|
|
die("kevent");
|
2009-11-05 02:16:22 +00:00
|
|
|
if (kev.ident != vnode_fd ||
|
|
|
|
kev.filter != EVFILT_VNODE ||
|
|
|
|
kev.fflags != NOTE_ATTRIB)
|
|
|
|
err(1, "%s - incorrect event (sig=%u; filt=%d; flags=%d)",
|
|
|
|
test_id, (unsigned int)kev.ident, kev.filter, kev.flags);
|
|
|
|
|
|
|
|
/* Confirm that the watch is disabled automatically */
|
2010-02-03 02:52:44 +00:00
|
|
|
testfile_touch();
|
2010-01-31 22:05:44 +00:00
|
|
|
test_no_kevents(kqfd);
|
2009-11-05 02:16:22 +00:00
|
|
|
|
2010-07-24 01:04:27 +00:00
|
|
|
/* Re-enable the kevent */
|
|
|
|
/* FIXME- is EV_DISPATCH needed when rearming ? */
|
|
|
|
kevent_add(kqfd, &kev, vnode_fd, EVFILT_VNODE, EV_ENABLE | EV_DISPATCH, 0, 0, NULL);
|
|
|
|
kev.flags = EV_ADD | EV_DISPATCH; /* FIXME: may not be portable */
|
|
|
|
kev.fflags = NOTE_ATTRIB;
|
|
|
|
testfile_touch();
|
|
|
|
kevent_cmp(&kev, kevent_get(kqfd));
|
|
|
|
test_no_kevents(kqfd);
|
|
|
|
|
2009-11-05 02:16:22 +00:00
|
|
|
/* Delete the watch */
|
2010-02-07 05:03:21 +00:00
|
|
|
kevent_add(kqfd, &kev, vnode_fd, EVFILT_VNODE, EV_DELETE, NOTE_ATTRIB, 0, NULL);
|
2009-11-05 02:16:22 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_EV_DISPATCH */
|
|
|
|
|
|
|
|
void
|
2010-01-31 22:05:44 +00:00
|
|
|
test_evfilt_vnode(int _kqfd)
|
2009-11-05 02:16:22 +00:00
|
|
|
{
|
2011-01-08 21:49:29 +00:00
|
|
|
|
|
|
|
#if (defined(__sun) && !defined(HAVE_PORT_SOURCE_FILE))
|
|
|
|
puts("**NOTE** EVFILT_VNODE is not supported on this version of Solaris");
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2010-02-03 02:52:44 +00:00
|
|
|
snprintf(testfile, sizeof(testfile), "/tmp/kqueue-test%d.tmp",
|
|
|
|
testing_make_uid());
|
|
|
|
|
2010-01-31 22:05:44 +00:00
|
|
|
kqfd = _kqfd;
|
2010-01-30 02:42:04 +00:00
|
|
|
test(kevent_vnode_add);
|
|
|
|
test(kevent_vnode_del);
|
|
|
|
test(kevent_vnode_disable_and_enable);
|
2009-11-05 02:16:22 +00:00
|
|
|
#if HAVE_EV_DISPATCH
|
2010-01-30 02:42:04 +00:00
|
|
|
test(kevent_vnode_dispatch);
|
2009-11-05 02:16:22 +00:00
|
|
|
#endif
|
2010-01-30 02:42:04 +00:00
|
|
|
test(kevent_vnode_note_write);
|
|
|
|
test(kevent_vnode_note_attrib);
|
|
|
|
test(kevent_vnode_note_rename);
|
|
|
|
test(kevent_vnode_note_delete);
|
2010-02-03 02:52:44 +00:00
|
|
|
unlink(testfile);
|
2009-11-05 02:16:22 +00:00
|
|
|
}
|