From: Michael Ellerman <mpe@ellerman.id.au>
To: David Herrmann <dh.herrmann@gmail.com>
Cc: linux-kernel@vger.kernel.org,
John McCutchan <john@johnmccutchan.com>,
Robert Love <rlove@rlove.org>, Eric Paris <eparis@parisplace.org>,
Andrew Morton <akpm@linux-foundation.org>,
Al Viro <viro@zeniv.linux.org.uk>,
linux-api@vger.kernel.org, Shuah Khan <shuahkh@osg.samsung.com>
Subject: Re: [PATCH 3/3] kselftest/inotify: add inotify_update_watch(2) test-cases
Date: Fri, 04 Sep 2015 16:42:17 +1000 [thread overview]
Message-ID: <1441348937.13721.5.camel@ellerman.id.au> (raw)
In-Reply-To: <1441293027-3363-4-git-send-email-dh.herrmann@gmail.com>
On Thu, 2015-09-03 at 17:10 +0200, David Herrmann wrote:
> diff --git a/tools/testing/selftests/inotify/Makefile b/tools/testing/selftests/inotify/Makefile
> new file mode 100644
> index 0000000..c205abb
> --- /dev/null
> +++ b/tools/testing/selftests/inotify/Makefile
> @@ -0,0 +1,14 @@
> +CC = $(CROSS_COMPILE)gcc
You don't need this, it's in lib.mk already.
> +CFLAGS += -I../../../../include/uapi/
> +CFLAGS += -I../../../../include/
Please don't include the unexported kernel headers in userspace programs.
It will sometimes work, but often not.
> +CFLAGS += -I../../../../usr/include/
This is good, it allows the test to build against the exported kernel headers, in
their default install location (usr/include). To install them run 'make
headers_install' in the top-level of the kernel tree.
It is sufficient to get the new syscall numbers you need, and is usually safe.
> +all:
> + $(CC) $(CFLAGS) test_inotify.c -o test_inotify
> +
> +TEST_PROGS := test_inotify
You don't need to spell out the all rule, this is sufficient:
TEST_PROGS := test_inotify
all: $(TEST_PROGS)
> +
> +include ../lib.mk
> +
> +clean:
> + $(RM) test_inotify test_file
You should either add a leading "-", or make this rm -f, so that when the files
don't exist rm doesn't give you an error.
> diff --git a/tools/testing/selftests/inotify/test_inotify.c b/tools/testing/selftests/inotify/test_inotify.c
> new file mode 100644
> index 0000000..094420a
> --- /dev/null
> +++ b/tools/testing/selftests/inotify/test_inotify.c
> @@ -0,0 +1,105 @@
> +/*
> + * inotify tests
> + */
> +
> +#define _GNU_SOURCE
> +#define __EXPORTED_HEADERS__
Please don't do that.
> +
> +#include <assert.h>
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <inttypes.h>
> +#include <limits.h>
> +#include <linux/unistd.h>
> +#include <signal.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <sys/inotify.h>
> +#include <sys/syscall.h>
> +#include <unistd.h>
> +
> +#define TEST_FILE "./test_file"
> +
> +static int sys_inotify_update_watch(int fd, uint32_t wd, uint32_t mask)
> +{
> + return syscall(__NR_inotify_update_watch, fd, wd, mask);
> +}
> +
> +static int read_inotify(int fd, struct inotify_event **out)
> +{
> + union {
> + struct inotify_event event;
> + char buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
> + } buf;
> + int r;
> +
> + r = read(fd, &buf, sizeof(buf));
> + if (r < 0)
> + return r;
> +
> + assert(r >= sizeof(struct inotify_event) && r <= sizeof(buf));
> + *out = &buf.event;
> + return r;
> +}
> +
> +/* test inotify_update_watch(2) */
> +static void test_update(void)
> +{
> + struct inotify_event *event;
> + int ifd, wd, fd, r;
> +
> + ifd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
> + assert(ifd >= 0);
> +
> + unlink(TEST_FILE);
> +
> + /* try adding watch on non-existant file */
> + wd = inotify_add_watch(ifd, TEST_FILE, IN_IGNORED);
> + assert(wd < 0 && errno == ENOENT);
> +
> + /* create file */
> + fd = open(TEST_FILE, O_CREAT | O_CLOEXEC | O_EXCL, S_IRUSR);
> + assert(fd >= 0);
> +
> + /* add watch descriptor without IN_CLOSE */
> + wd = inotify_add_watch(ifd, TEST_FILE, IN_IGNORED);
> + assert(wd > 0);
> +
> + /* close file */
> + close(fd);
> +
> + /* should not have gotten any event */
> + r = read_inotify(ifd, &event);
> + assert(r < 0 && errno == EAGAIN);
> +
> + /* reopen file */
> + fd = open(TEST_FILE, O_CLOEXEC | O_EXCL);
> + assert(fd >= 0);
> +
> + /* invalid flags must result in EINVAL */
> + r = sys_inotify_update_watch(ifd, wd, -1);
> + assert(r < 0 && errno == EINVAL);
> +
> + /* update watch descriptor with IN_CLOSE */
> + r = sys_inotify_update_watch(ifd, wd, IN_MASK_ADD | IN_CLOSE);
> + assert(r >= 0);
> +
> + /* close file */
> + close(fd);
> +
> + /* now we should have seen the event */
> + r = read_inotify(ifd, &event);
> + assert(r >= 0);
> + assert(event->wd == wd);
> + assert(event->mask & IN_CLOSE);
> +
> + unlink(TEST_FILE);
> + close(ifd);
> +}
> +
> +int main(int argc, char **argv)
> +{
> + test_update();
> + return 0;
> +}
You may as well just drop test_update() and put the whole test in main() ?
cheers
prev parent reply other threads:[~2015-09-04 6:42 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-03 15:10 [PATCH 0/3] Introduce inotify_update_watch(2) David Herrmann
2015-09-03 15:10 ` [PATCH 1/3] inotify: move wd lookup out of update_existing_watch() David Herrmann
2015-09-03 15:10 ` [PATCH 2/3] inotify: add inotify_update_watch() syscall David Herrmann
2015-09-03 15:10 ` [PATCH 3/3] kselftest/inotify: add inotify_update_watch(2) test-cases David Herrmann
2015-09-04 6:42 ` Michael Ellerman [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1441348937.13721.5.camel@ellerman.id.au \
--to=mpe@ellerman.id.au \
--cc=akpm@linux-foundation.org \
--cc=dh.herrmann@gmail.com \
--cc=eparis@parisplace.org \
--cc=john@johnmccutchan.com \
--cc=linux-api@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rlove@rlove.org \
--cc=shuahkh@osg.samsung.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®