Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp730073ybh; Wed, 18 Mar 2020 08:07:09 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtMLDTGcrHUu9OVlQ/y6aVpEEPfLibot20fA8XiRe7CKaKU/7B3fConZw7bALZn66DV29WU X-Received: by 2002:a9d:5f7:: with SMTP id 110mr3879942otd.73.1584544029081; Wed, 18 Mar 2020 08:07:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584544029; cv=none; d=google.com; s=arc-20160816; b=nyzw+uVFIKHoTTUh7Ba/KWAS8wCm+YCBAqmsdYeRanCHKHC6rA7GFjMeU7JxQ5qHm4 qWhIV3BI+7v3hX4QImCONsSz6iuHPD2QSUdmwKYuDgwfWA679JLcrYjEdtGluTMp/PsN wsd6+I1vFbnqVGiBsBTIL1Sgsamyvl7jBSre9BDy6rIQNc+lBxmeR9vzS4HKTwylqP86 F3+2hIiVGWtPHWOvdWq6LrUZ6mcBTp/qTr0wWlOPp6iF8zcwbpe1/lSMJ8xGDbwBo/T8 ckLg5bAND8gc0AYmnwD5x6j2BwN3vfasyPxFcrkH/FbAZAIWRWfyTtyuZBsd9Qs7+oBZ qk1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:organization:dkim-signature; bh=dI8IlWBYCaNWXpDkzgWnEx2eTwMg9g0MUeU/TkEB0Zk=; b=NYKCicwKjHBvZEgt+1E6T8b8gTl+aiCp9XqYLfG/yO0VLV4Seu2dknYHftcY50k8xF xlPfg403CDqiUIHGYYyu7buktJzzBvGc9iQIL1owJ35IlTK5mN7ywDlujGKPRNATVZyM A96n82dRhjKvXghTBHErUvlqvWxAV5ENYyAo5f4imb7o5B2z8RIYrpmUxREYG+l3Kuem MXsilsomKPPazJGYCrDJAtXJAGCQkL5qKGCI+p6LaSoEFpnxpCYQqOs15tRBpESP+k9R RNsFRMZfIpkPOPQ6L4t9mxRmqD9oJD8CtzFRHVYF//dojk7nQIYu0lASMAcDvZof/XMI GtLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Jo7p2vLq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k3si3534536oib.82.2020.03.18.08.06.43; Wed, 18 Mar 2020 08:07:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Jo7p2vLq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727383AbgCRPFf (ORCPT + 99 others); Wed, 18 Mar 2020 11:05:35 -0400 Received: from us-smtp-delivery-74.mimecast.com ([216.205.24.74]:45428 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727351AbgCRPFe (ORCPT ); Wed, 18 Mar 2020 11:05:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584543933; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dI8IlWBYCaNWXpDkzgWnEx2eTwMg9g0MUeU/TkEB0Zk=; b=Jo7p2vLq5wLRJmzLOaSqma0tLl3L9e09228Cyx+VVOIODran0v5geO84XlicK0Etxl+GYS 1PsSp9vOwMbptCniplff/bQ/CIe5fz8/dpwcORejKu83aIlrR0vs0uN5ghFmNMbnkInFFm 9GRR6AN+2Bs1q0xDbBv8QHHJpYZ4Uvk= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-249-LjYnuJUpO5G4_Wy66GHzsQ-1; Wed, 18 Mar 2020 11:05:28 -0400 X-MC-Unique: LjYnuJUpO5G4_Wy66GHzsQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EF8538010D9; Wed, 18 Mar 2020 15:05:25 +0000 (UTC) Received: from warthog.procyon.org.uk (ovpn-113-126.rdu2.redhat.com [10.10.113.126]) by smtp.corp.redhat.com (Postfix) with ESMTP id 244591001B07; Wed, 18 Mar 2020 15:05:22 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH 14/17] watch_queue: sample: Display mount tree change notifications [ver #5] From: David Howells To: torvalds@linux-foundation.org, viro@zeniv.linux.org.uk Cc: dhowells@redhat.com, casey@schaufler-ca.com, sds@tycho.nsa.gov, nicolas.dichtel@6wind.com, raven@themaw.net, christian@brauner.io, andres@anarazel.de, jlayton@redhat.com, dray@redhat.com, kzak@redhat.com, keyrings@vger.kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Date: Wed, 18 Mar 2020 15:05:22 +0000 Message-ID: <158454392238.2863966.16418724162547711363.stgit@warthog.procyon.org.uk> In-Reply-To: <158454378820.2863966.10496767254293183123.stgit@warthog.procyon.org.uk> References: <158454378820.2863966.10496767254293183123.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.21 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is run like: ./watch_test and watches "/" for changes to the mount topology and the attributes of individual mount objects. # mount -t tmpfs none /mnt # mount -o remount,ro /mnt # mount -o remount,rw /mnt producing: # ./watch_test read() = 16 NOTIFY[000]: ty=000002 sy=00 i=02000010 MOUNT 00000060 change=0[new_mount] aux=416 read() = 16 NOTIFY[000]: ty=000002 sy=04 i=02010010 MOUNT 000001a0 change=4[setattr] aux=0 read() = 16 NOTIFY[000]: ty=000002 sy=04 i=02010010 MOUNT 000001a0 change=4[setattr] aux=0 Signed-off-by: David Howells --- samples/watch_queue/watch_test.c | 44 +++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/samples/watch_queue/watch_test.c b/samples/watch_queue/watch_test.c index 46e618a897fe..36caaea5261f 100644 --- a/samples/watch_queue/watch_test.c +++ b/samples/watch_queue/watch_test.c @@ -26,6 +26,9 @@ #ifndef __NR_keyctl #define __NR_keyctl -1 #endif +#ifndef __NR_watch_mount +#define __NR_watch_mount -1 +#endif #define BUF_SIZE 256 @@ -58,6 +61,32 @@ static void saw_key_change(struct watch_notification *n, size_t len) k->key_id, n->subtype, key_subtypes[n->subtype], k->aux); } +static const char *mount_subtypes[256] = { + [NOTIFY_MOUNT_NEW_MOUNT] = "new_mount", + [NOTIFY_MOUNT_UNMOUNT] = "unmount", + [NOTIFY_MOUNT_EXPIRY] = "expiry", + [NOTIFY_MOUNT_READONLY] = "readonly", + [NOTIFY_MOUNT_SETATTR] = "setattr", + [NOTIFY_MOUNT_MOVE_FROM] = "move_from", + [NOTIFY_MOUNT_MOVE_TO] = "move_to", +}; + +static void saw_mount_change(struct watch_notification *n, size_t len) +{ + struct mount_notification *m = (struct mount_notification *)n; + + if (len != sizeof(struct mount_notification)) + return; + + printf("MOUNT %08x change=%u[%s] aux=%u ctr=%x,%x actr=%x\n", + m->triggered_on, n->subtype, mount_subtypes[n->subtype], + m->auxiliary_mount, + m->topology_changes, + m->attr_changes, + m->aux_topology_changes); + +} + /* * Consume and display events. */ @@ -134,6 +163,9 @@ static void consumer(int fd) default: printf("other type\n"); break; + case WATCH_TYPE_MOUNT_NOTIFY: + saw_mount_change(&n.n, len); + break; } p += len; @@ -142,12 +174,17 @@ static void consumer(int fd) } static struct watch_notification_filter filter = { - .nr_filters = 1, + .nr_filters = 2, .filters = { [0] = { .type = WATCH_TYPE_KEY_NOTIFY, .subtype_filter[0] = UINT_MAX, }, + [1] = { + .type = WATCH_TYPE_MOUNT_NOTIFY, + // Reject move-from notifications + .subtype_filter[0] = UINT_MAX & ~(1 << NOTIFY_MOUNT_MOVE_FROM), + }, }, }; @@ -181,6 +218,11 @@ int main(int argc, char **argv) exit(1); } + if (syscall(__NR_watch_mount, AT_FDCWD, "/", 0, fd, 0x02) == -1) { + perror("watch_mount"); + exit(1); + } + consumer(fd); exit(0); }