Skip to contentSkip to conversation

smbinfo: add options to notify subcommand

PATCH Applied ChenXiaoSong 2 participants 5 messages Nov 11 – Nov 17

Subsystems (2)
lore
Share & tools
lore

ChenXiaoSong

author reviewer
#0 lore raw

cover smbinfo: add options to notify subcommand

From: ChenXiaoSong <[email protected]>

Add optional watch_tree and completion_filter arguments to make debugging easier.

Example:

./smbinfo notify -h
usage: smbinfo notify [-h] directory [completion_filter] [watch_tree]

positional arguments:

    directory          Directory to watch for changes
    completion_filter  Filter mask from 0 to 0xFFF; supports '|' (quote expressions in the shell; default: 0xFFF)
    watch_tree         Watch subdirectories: True/1 or False/0 (default: True)

options:
-h, --help show this help message and exit

completion_filter flags:

    0x00000001  FILE_NOTIFY_CHANGE_FILE_NAME
    0x00000002  FILE_NOTIFY_CHANGE_DIR_NAME
    0x00000003  FILE_NOTIFY_CHANGE_NAME
    0x00000004  FILE_NOTIFY_CHANGE_ATTRIBUTES
    0x00000008  FILE_NOTIFY_CHANGE_SIZE
    0x00000010  FILE_NOTIFY_CHANGE_LAST_WRITE
    0x00000020  FILE_NOTIFY_CHANGE_LAST_ACCESS
    0x00000040  FILE_NOTIFY_CHANGE_CREATION
    0x00000080  FILE_NOTIFY_CHANGE_EA
    0x00000100  FILE_NOTIFY_CHANGE_SECURITY
    0x00000200  FILE_NOTIFY_CHANGE_STREAM_NAME
    0x00000400  FILE_NOTIFY_CHANGE_STREAM_SIZE
    0x00000800  FILE_NOTIFY_CHANGE_STREAM_WRITE

Example: smbinfo notify /mnt '0x1|0x2' 0

ChenXiaoSong (3):
smbinfo: show directory in notify help
smbinfo: make the notify completion filter configurable
smbinfo: make notify subtree watching configurable

 smbinfo     | 68 +++++++++++++++++++++++++++++++++++++++++++++++------
 smbinfo.rst | 20 +++++++++++++++-
 2 files changed, 80 insertions(+), 8 deletions(-)
Signature · 1 line
2.53.0
author reviewer
#1 lore raw

PATCH 1/3 smbinfo: show directory in notify help

From: ChenXiaoSong <[email protected]>

Change the notify argument name from file to directory in the help and manual.

Example:

./smbinfo notify -h
usage: smbinfo notify [-h] directory

positional arguments:
directory Directory to watch for changes

---
 smbinfo     | 4 ++--
 smbinfo.rst | 6 +++++-
 2 files changed, 7 insertions(+), 3 deletions(-)
smbinfo +2 -2
index f9a8c4d..1ed1b6c 100755
@@ -317,7 +317,7 @@ def main():
     sap.set_defaults(func=cmd_gettconinfo)
 
     sap = subp.add_parser("notify", help="Query a directory for change notifications. Press Ctrl+C to exit.")
-    sap.add_argument("file")
+    sap.add_argument("directory", help="Directory to watch for changes")
     sap.set_defaults(func=cmd_notify)
 
     # parse arguments
@@ -982,7 +982,7 @@ def cmd_gettconinfo(args):
 
 def cmd_notify(args):
     try:
-        fd = os.open(args.file, os.O_RDONLY)
+        fd = os.open(args.directory, os.O_RDONLY)
     except Exception as e:
         print("syscall failed: %s"%e)
         return False
smbinfo.rst +5 -1
index 7aa3ecd..a74ce57 100644
@@ -104,7 +104,11 @@ the SMB3 traffic of this mount can be decryped e.g. via wireshark
 
 `gettconinfo`: Prints both the TCON Id and Session Id for a cifs file.
 
-`notify`: Query a directory for change notifications.
+`notify directory`
+  Query a directory for change notifications.
+
+  `directory`
+    Required. The directory to watch for changes.
 
 *****
 NOTES
Signature · 1 line
2.53.0
author reviewer
#2 lore raw

PATCH 2/3 smbinfo: make the notify completion filter configurable

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Add an optional completion_filter argument to make debugging easier.

Example:

./smbinfo notify -h
usage: smbinfo notify [-h] directory [completion_filter]

positional arguments:

    ...
    completion_filter  Filter mask from 0 to 0xFFF; supports '|' (quote expressions in the shell; default: 0xFFF)
  ...
  completion_filter flags:
    0x00000001  FILE_NOTIFY_CHANGE_FILE_NAME
    0x00000002  FILE_NOTIFY_CHANGE_DIR_NAME
    0x00000003  FILE_NOTIFY_CHANGE_NAME
    0x00000004  FILE_NOTIFY_CHANGE_ATTRIBUTES
    0x00000008  FILE_NOTIFY_CHANGE_SIZE
    0x00000010  FILE_NOTIFY_CHANGE_LAST_WRITE
    0x00000020  FILE_NOTIFY_CHANGE_LAST_ACCESS
    0x00000040  FILE_NOTIFY_CHANGE_CREATION
    0x00000080  FILE_NOTIFY_CHANGE_EA
    0x00000100  FILE_NOTIFY_CHANGE_SECURITY
    0x00000200  FILE_NOTIFY_CHANGE_STREAM_NAME
    0x00000400  FILE_NOTIFY_CHANGE_STREAM_SIZE
    0x00000800  FILE_NOTIFY_CHANGE_STREAM_WRITE

Example: smbinfo notify /mnt '0x1|0x2'

---
 smbinfo     | 52 ++++++++++++++++++++++++++++++++++++++++++++++++----
 smbinfo.rst | 11 ++++++++++-
 2 files changed, 58 insertions(+), 5 deletions(-)
smbinfo +48 -4
index 1ed1b6c..e2faeb3 100755
@@ -133,6 +133,27 @@ FILE_MODE_FLAGS = [
     (0x00001000, "DELETE_ON_CLOSE"),
 ]
 
+# See notify completion filter flags in linux kernel fs/smb/common/smb2pdu.h
+NOTIFY_CHANGE_FLAGS = [
+    (0x00000001, "FILE_NOTIFY_CHANGE_FILE_NAME"),
+    (0x00000002, "FILE_NOTIFY_CHANGE_DIR_NAME"),
+    (0x00000003, "FILE_NOTIFY_CHANGE_NAME"),
+    (0x00000004, "FILE_NOTIFY_CHANGE_ATTRIBUTES"),
+    (0x00000008, "FILE_NOTIFY_CHANGE_SIZE"),
+    (0x00000010, "FILE_NOTIFY_CHANGE_LAST_WRITE"),
+    (0x00000020, "FILE_NOTIFY_CHANGE_LAST_ACCESS"),
+    (0x00000040, "FILE_NOTIFY_CHANGE_CREATION"),
+    (0x00000080, "FILE_NOTIFY_CHANGE_EA"),
+    (0x00000100, "FILE_NOTIFY_CHANGE_SECURITY"),
+    (0x00000200, "FILE_NOTIFY_CHANGE_STREAM_NAME"),
+    (0x00000400, "FILE_NOTIFY_CHANGE_STREAM_SIZE"),
+    (0x00000800, "FILE_NOTIFY_CHANGE_STREAM_WRITE"),
+]
+
+NOTIFY_CHANGE_MASK = 0
+for flag, _ in NOTIFY_CHANGE_FLAGS:
+    NOTIFY_CHANGE_MASK |= flag
+
 ALIGN_TYPES = [
     (0, "BYTE_ALIGNMENT"),
     (1, "WORD_ALIGNMENT"),
@@ -316,8 +337,18 @@ def main():
     sap.add_argument("file")
     sap.set_defaults(func=cmd_gettconinfo)
 
-    sap = subp.add_parser("notify", help="Query a directory for change notifications. Press Ctrl+C to exit.")
+    sap = subp.add_parser(
+        "notify", help="Query a directory for change notifications.",
+        formatter_class=argparse.RawDescriptionHelpFormatter,
+        epilog="completion_filter flags:\n" + "\n".join(
+            "  0x%08x  %s"%(value, name) for value, name in NOTIFY_CHANGE_FLAGS
+        ) + "\n\nExample: smbinfo notify /mnt '0x1|0x2'")
     sap.add_argument("directory", help="Directory to watch for changes")
+    sap.add_argument(
+        "completion_filter", nargs="?", type=parse_notify_completion_filter,
+        default=NOTIFY_CHANGE_MASK,
+        help=f"Filter mask from 0 to 0x{NOTIFY_CHANGE_MASK:X}; supports '|' "
+             f"(quote expressions in the shell; default: 0x{NOTIFY_CHANGE_MASK:X})")
     sap.set_defaults(func=cmd_notify)
 
     # parse arguments
@@ -980,6 +1011,19 @@ def cmd_gettconinfo(args):
     print("TCON Id: 0x%x"%tcon.tid)
     print("Session Id: 0x%x"%tcon.session_id)
 
+def parse_notify_completion_filter(value):
+    try:
+        completion_filter = 0
+        for flag in value.split("|"):
+            completion_filter |= int(flag.strip(), 0)
+        if 0 <= completion_filter <= NOTIFY_CHANGE_MASK:
+            return completion_filter
+    except ValueError:
+        pass
+    raise argparse.ArgumentTypeError(
+        "completion_filter must contain integers between 0 and "
+        f"0x{NOTIFY_CHANGE_MASK:X} separated by '|'.")
+
 def cmd_notify(args):
     try:
         fd = os.open(args.directory, os.O_RDONLY)
@@ -987,7 +1031,8 @@ def cmd_notify(args):
         print("syscall failed: %s"%e)
         return False
 
-    thread = threading.Thread(target=notify_thread, args=(fd,))
+    thread = threading.Thread(target=notify_thread,
+                              args=(fd, args.completion_filter))
     thread.start()
 
     try:
@@ -997,10 +1042,9 @@ def cmd_notify(args):
     finally:
         os.close(fd)
 
-def notify_thread(fd):
+def notify_thread(fd, completion_filter):
     fmt = "<IBI"
     # See `struct smb3_notify_info` in linux kernel fs/smb/client/cifs_ioctl.h
-    completion_filter = 0xFFF
     watch_tree = True
     data_len = 1000
 
smbinfo.rst +10 -1
index a74ce57..924a37b 100644
@@ -104,12 +104,21 @@ the SMB3 traffic of this mount can be decryped e.g. via wireshark
 
 `gettconinfo`: Prints both the TCON Id and Session Id for a cifs file.
 
-`notify directory`
+`notify directory [completion_filter]`
   Query a directory for change notifications.
 
   `directory`
     Required. The directory to watch for changes.
 
+  `completion_filter`
+    Optional. Accepts integers in decimal or with a ``0x`` prefix for
+    hexadecimal, optionally joined by ``|``. Its default and upper limit are
+    the bitwise OR of all ``FILE_NOTIFY_CHANGE_*`` flags; the lower limit is
+    zero. Run ``smbinfo notify -h`` to list the flags and their hexadecimal
+    values. Quote bitwise OR expressions in the shell, for example::
+
+      smbinfo notify /mnt '0x1|0x2'
+
 *****
 NOTES
 *****
Signature · 1 line
2.53.0
author reviewer
#3 lore raw

PATCH 3/3 smbinfo: make notify subtree watching configurable

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Add an optional watch_tree argument to make debugging easier.

Example:

./smbinfo notify -h
usage: smbinfo notify [-h] directory [completion_filter] [watch_tree]

positional arguments:
...
watch_tree Watch subdirectories: True/1 or False/0 (default: True)
...
Example: smbinfo notify /mnt '0x1|0x2' 0

---
 smbinfo     | 18 ++++++++++++++----
 smbinfo.rst |  9 +++++++--
 2 files changed, 21 insertions(+), 6 deletions(-)
smbinfo +14 -4
index e2faeb3..2c966d7 100755
@@ -342,13 +342,16 @@ def main():
         formatter_class=argparse.RawDescriptionHelpFormatter,
         epilog="completion_filter flags:\n" + "\n".join(
             "  0x%08x  %s"%(value, name) for value, name in NOTIFY_CHANGE_FLAGS
-        ) + "\n\nExample: smbinfo notify /mnt '0x1|0x2'")
+        ) + "\n\nExample: smbinfo notify /mnt '0x1|0x2' 0")
     sap.add_argument("directory", help="Directory to watch for changes")
     sap.add_argument(
         "completion_filter", nargs="?", type=parse_notify_completion_filter,
         default=NOTIFY_CHANGE_MASK,
         help=f"Filter mask from 0 to 0x{NOTIFY_CHANGE_MASK:X}; supports '|' "
              f"(quote expressions in the shell; default: 0x{NOTIFY_CHANGE_MASK:X})")
+    sap.add_argument(
+        "watch_tree", nargs="?", type=parse_notify_watch_tree, default=True,
+        help="Watch subdirectories: True/1 or False/0 (default: True)")
     sap.set_defaults(func=cmd_notify)
 
     # parse arguments
@@ -1024,6 +1027,14 @@ def parse_notify_completion_filter(value):
         "completion_filter must contain integers between 0 and "
         f"0x{NOTIFY_CHANGE_MASK:X} separated by '|'.")
 
+def parse_notify_watch_tree(value):
+    value = value.strip().lower()
+    if value in ("true", "1"):
+        return True
+    if value in ("false", "0"):
+        return False
+    raise argparse.ArgumentTypeError("watch_tree must be True, False, 1 or 0.")
+
 def cmd_notify(args):
     try:
         fd = os.open(args.directory, os.O_RDONLY)
@@ -1032,7 +1043,7 @@ def cmd_notify(args):
         return False
 
     thread = threading.Thread(target=notify_thread,
-                              args=(fd, args.completion_filter))
+                              args=(fd, args.completion_filter, args.watch_tree))
     thread.start()
 
     try:
@@ -1042,10 +1053,9 @@ def cmd_notify(args):
     finally:
         os.close(fd)
 
-def notify_thread(fd, completion_filter):
+def notify_thread(fd, completion_filter, watch_tree):
     fmt = "<IBI"
     # See `struct smb3_notify_info` in linux kernel fs/smb/client/cifs_ioctl.h
-    watch_tree = True
     data_len = 1000
 
     while True:
smbinfo.rst +7 -2
index 924a37b..9131d30 100644
@@ -104,7 +104,7 @@ the SMB3 traffic of this mount can be decryped e.g. via wireshark
 
 `gettconinfo`: Prints both the TCON Id and Session Id for a cifs file.
 
-`notify directory [completion_filter]`
+`notify directory [completion_filter] [watch_tree]`
   Query a directory for change notifications.
 
   `directory`
@@ -117,7 +117,12 @@ the SMB3 traffic of this mount can be decryped e.g. via wireshark
     zero. Run ``smbinfo notify -h`` to list the flags and their hexadecimal
     values. Quote bitwise OR expressions in the shell, for example::
 
-      smbinfo notify /mnt '0x1|0x2'
+      smbinfo notify /mnt '0x1|0x2' 0
+
+  `watch_tree`
+    Optional. Controls whether subdirectories are also watched. Accepts
+    `True` or `False` (case-insensitive), or ``1``/``0`` respectively, and
+    defaults to `True`. To specify `watch_tree`, also supply `completion_filter`.
 
 *****
 NOTES
Signature · 1 line
2.53.0