From patchwork Thu Mar 14 15:15:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= X-Patchwork-Id: 10853095 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 60AD91390 for ; Thu, 14 Mar 2019 15:30:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 479352A56F for ; Thu, 14 Mar 2019 15:30:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3BD042A578; Thu, 14 Mar 2019 15:30:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6B52B2A56F for ; Thu, 14 Mar 2019 15:30:22 +0000 (UTC) Received: from localhost ([127.0.0.1]:39624 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4SJJ-0005Y3-NB for patchwork-qemu-devel@patchwork.kernel.org; Thu, 14 Mar 2019 11:30:21 -0400 Received: from eggs.gnu.org ([209.51.188.92]:44910) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4SBT-0007Dg-CJ for qemu-devel@nongnu.org; Thu, 14 Mar 2019 11:22:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4S54-0000TV-Vk for qemu-devel@nongnu.org; Thu, 14 Mar 2019 11:15:42 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38284) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h4S54-0000TN-LQ for qemu-devel@nongnu.org; Thu, 14 Mar 2019 11:15:38 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 09EB5308CF9D for ; Thu, 14 Mar 2019 15:15:38 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-35.ams2.redhat.com [10.36.112.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 80EC660F89; Thu, 14 Mar 2019 15:15:36 +0000 (UTC) From: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Thu, 14 Mar 2019 15:15:27 +0000 Message-Id: <20190314151527.25533-3-berrange@redhat.com> In-Reply-To: <20190314151527.25533-1-berrange@redhat.com> References: <20190314151527.25533-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Thu, 14 Mar 2019 15:15:38 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/2] filemon: ensure watch IDs are unique to QFileMonitor scope X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Bandan Das Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The watch IDs are mistakenly only unique within the scope of the directory being monitored. This is not useful for clients which are monitoring multiple directories. They require watch IDs to be unique globally within the QFileMonitor scope. Signed-off-by: Daniel P. Berrangé Reviewed-by: Marc-André Lureau --- tests/test-util-filemonitor.c | 116 +++++++++++++++++++++++++++++++--- util/filemonitor-inotify.c | 5 +- 2 files changed, 110 insertions(+), 11 deletions(-) diff --git a/tests/test-util-filemonitor.c b/tests/test-util-filemonitor.c index ea3715a8f4..71a7cf5de0 100644 --- a/tests/test-util-filemonitor.c +++ b/tests/test-util-filemonitor.c @@ -35,6 +35,8 @@ enum { QFILE_MONITOR_TEST_OP_RENAME, QFILE_MONITOR_TEST_OP_TOUCH, QFILE_MONITOR_TEST_OP_UNLINK, + QFILE_MONITOR_TEST_OP_MKDIR, + QFILE_MONITOR_TEST_OP_RMDIR, }; typedef struct { @@ -298,6 +300,54 @@ test_file_monitor_events(void) .eventid = QFILE_MONITOR_EVENT_DELETED }, + { .type = QFILE_MONITOR_TEST_OP_MKDIR, + .filesrc = "fish", }, + { .type = QFILE_MONITOR_TEST_OP_EVENT, + .filesrc = "fish", .watchid = 0, + .eventid = QFILE_MONITOR_EVENT_CREATED }, + + + { .type = QFILE_MONITOR_TEST_OP_ADD_WATCH, + .filesrc = "fish/", .watchid = 4 }, + { .type = QFILE_MONITOR_TEST_OP_ADD_WATCH, + .filesrc = "fish/one.txt", .watchid = 5 }, + { .type = QFILE_MONITOR_TEST_OP_CREATE, + .filesrc = "fish/one.txt", }, + { .type = QFILE_MONITOR_TEST_OP_EVENT, + .filesrc = "one.txt", .watchid = 4, + .eventid = QFILE_MONITOR_EVENT_CREATED }, + { .type = QFILE_MONITOR_TEST_OP_EVENT, + .filesrc = "one.txt", .watchid = 5, + .eventid = QFILE_MONITOR_EVENT_CREATED }, + + + { .type = QFILE_MONITOR_TEST_OP_DEL_WATCH, + .filesrc = "fish/one.txt", .watchid = 5 }, + { .type = QFILE_MONITOR_TEST_OP_RENAME, + .filesrc = "fish/one.txt", .filedst = "two.txt", }, + { .type = QFILE_MONITOR_TEST_OP_EVENT, + .filesrc = "one.txt", .watchid = 4, + .eventid = QFILE_MONITOR_EVENT_DELETED }, + { .type = QFILE_MONITOR_TEST_OP_EVENT, + .filesrc = "two.txt", .watchid = 0, + .eventid = QFILE_MONITOR_EVENT_CREATED }, + { .type = QFILE_MONITOR_TEST_OP_EVENT, + .filesrc = "two.txt", .watchid = 2, + .eventid = QFILE_MONITOR_EVENT_CREATED }, + + + { .type = QFILE_MONITOR_TEST_OP_RMDIR, + .filesrc = "fish", }, + { .type = QFILE_MONITOR_TEST_OP_EVENT, + .filesrc = "", .watchid = 4, + .eventid = QFILE_MONITOR_EVENT_IGNORED }, + { .type = QFILE_MONITOR_TEST_OP_EVENT, + .filesrc = "fish", .watchid = 0, + .eventid = QFILE_MONITOR_EVENT_DELETED }, + { .type = QFILE_MONITOR_TEST_OP_DEL_WATCH, + .filesrc = "fish", .watchid = 4 }, + + { .type = QFILE_MONITOR_TEST_OP_UNLINK, .filesrc = "two.txt", }, { .type = QFILE_MONITOR_TEST_OP_EVENT, @@ -366,6 +416,8 @@ test_file_monitor_events(void) int fd; int watchid; struct utimbuf ubuf; + char *watchdir; + const char *watchfile; pathsrc = g_strdup_printf("%s/%s", dir, op->filesrc); if (op->filedst) { @@ -378,13 +430,26 @@ test_file_monitor_events(void) g_printerr("Add watch %s %s %d\n", dir, op->filesrc, op->watchid); } + if (op->filesrc && strchr(op->filesrc, '/')) { + watchdir = g_strdup_printf("%s/%s", dir, op->filesrc); + watchfile = strrchr(watchdir, '/'); + *(char *)watchfile = '\0'; + watchfile++; + if (*watchfile == '\0') { + watchfile = NULL; + } + } else { + watchdir = g_strdup(dir); + watchfile = op->filesrc; + } watchid = qemu_file_monitor_add_watch(mon, - dir, - op->filesrc, + watchdir, + watchfile, qemu_file_monitor_test_handler, &data, &local_err); + g_free(watchdir); if (watchid < 0) { g_printerr("Unable to add watch %s", error_get_pretty(local_err)); @@ -400,9 +465,17 @@ test_file_monitor_events(void) if (debug) { g_printerr("Del watch %s %d\n", dir, op->watchid); } + if (op->filesrc && strchr(op->filesrc, '/')) { + watchdir = g_strdup_printf("%s/%s", dir, op->filesrc); + watchfile = strrchr(watchdir, '/'); + *(char *)watchfile = '\0'; + } else { + watchdir = g_strdup(dir); + } qemu_file_monitor_remove_watch(mon, - dir, + watchdir, op->watchid); + g_free(watchdir); break; case QFILE_MONITOR_TEST_OP_EVENT: if (debug) { @@ -492,6 +565,28 @@ test_file_monitor_events(void) } break; + case QFILE_MONITOR_TEST_OP_MKDIR: + if (debug) { + g_printerr("Mkdir %s\n", pathsrc); + } + if (mkdir(pathsrc, 0700) < 0) { + g_printerr("Unable to mkdir %s: %s", + pathsrc, strerror(errno)); + goto cleanup; + } + break; + + case QFILE_MONITOR_TEST_OP_RMDIR: + if (debug) { + g_printerr("Rmdir %s\n", pathsrc); + } + if (rmdir(pathsrc) < 0) { + g_printerr("Unable to rmdir %s: %s", + pathsrc, strerror(errno)); + goto cleanup; + } + break; + default: g_assert_not_reached(); } @@ -532,13 +627,18 @@ test_file_monitor_events(void) const QFileMonitorTestOp *op = &(ops[i]); char *path = g_strdup_printf("%s/%s", dir, op->filesrc); - unlink(path); - g_free(path); - if (op->filedst) { - path = g_strdup_printf("%s/%s", - dir, op->filedst); + if (op->type == QFILE_MONITOR_TEST_OP_MKDIR) { + rmdir(path); + g_free(path); + } else { unlink(path); g_free(path); + if (op->filedst) { + path = g_strdup_printf("%s/%s", + dir, op->filedst); + unlink(path); + g_free(path); + } } } if (rmdir(dir) < 0) { diff --git a/util/filemonitor-inotify.c b/util/filemonitor-inotify.c index 3a72be037f..3eb29f860b 100644 --- a/util/filemonitor-inotify.c +++ b/util/filemonitor-inotify.c @@ -29,7 +29,7 @@ struct QFileMonitor { int fd; - + int nextid; /* watch ID counter */ QemuMutex lock; /* protects dirs & idmap */ GHashTable *dirs; /* dirname => QFileMonitorDir */ GHashTable *idmap; /* inotify ID => dirname */ @@ -47,7 +47,6 @@ typedef struct { typedef struct { char *path; int id; /* inotify ID */ - int nextid; /* watch ID counter */ GArray *watches; /* QFileMonitorWatch elements */ } QFileMonitorDir; @@ -277,7 +276,7 @@ qemu_file_monitor_add_watch(QFileMonitor *mon, } } - watch.id = dir->nextid++; + watch.id = mon->nextid++; watch.filename = g_strdup(filename); watch.cb = cb; watch.opaque = opaque;