Skip to contentSkip to conversation

smbclient: cancel pending notify on SIGINT

PATCH Superseded [email protected] 2 participants 12 messages May 19 – May 20

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

[email protected]

author reviewer
#0 lore raw

PATCH 1/1 smbclient: cancel pending notify on SIGINT

From: ChenXiaoSong <[email protected]>

When client stops querying a directory for change notifications, it should send a cancel request to the server.

---
 source3/client/client.c | 57 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 54 insertions(+), 3 deletions(-)
source3/client/client.c +54 -3
index 1d3656aab34..7fa850a6dd9 100644
@@ -45,6 +45,7 @@
 #include "lib/cmdline/cmdline.h"
 #include "libcli/smb/reparse.h"
 #include "lib/param/param.h"
+#include "lib/util/tevent_ntstatus.h"
 
 #ifndef REGISTER
 #define REGISTER 0
@@ -4653,12 +4654,40 @@ static int cmd_newer(TALLOC_CTX *mem_ctx)
  Watch directory changes
 ****************************************************************************/
 
+struct notify_state {
+	struct tevent_req *notify_req;
+	bool cancelled;
+};
+
+static void
+notify_sig_int_handler(
+	struct tevent_context *ev,
+	struct tevent_signal *se,
+	int signum,
+	int count,
+	void *siginfo,
+	void *private_data)
+{
+	struct notify_state *state = (struct notify_state *)private_data;
+
+	if (state->notify_req) {
+		state->cancelled = tevent_req_cancel(state->notify_req);
+	}
+}
+
 static int cmd_notify(TALLOC_CTX *mem_ctx)
 {
 	TALLOC_CTX *frame = talloc_stackframe();
 	char *name, *buf;
 	NTSTATUS status;
 	uint16_t fnum;
+	struct tevent_req *req;
+	struct tevent_context *ev;
+	struct tevent_signal *se;
+	struct notify_state sig_state = {
+		.notify_req = NULL,
+		.cancelled = false,
+	};
 
 	name = talloc_strdup(talloc_tos(), client_get_cur_dir());
 	if (name == NULL) {
@@ -4684,14 +4713,36 @@ static int cmd_notify(TALLOC_CTX *mem_ctx)
 		goto fail;
 	}
 
+	ev = samba_tevent_context_init(frame);
+	if (!ev)
+		goto fail;
+
+	se = tevent_add_signal(ev, frame, SIGINT, 0, notify_sig_int_handler, &sig_state);
+	if (!se)
+		goto fail;
+
 	while (1) {
 		uint32_t i;
 		uint32_t num_changes = 0;
 		struct notify_change *changes = NULL;
 
-		status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
-				    true,
-				    talloc_tos(), &num_changes, &changes);
+		req = cli_notify_send(frame, ev, cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL, true);
+		if (!req) {
+			goto fail;
+		}
+		sig_state.notify_req = req;
+
+		if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+			goto fail;
+		}
+
+		status = cli_notify_recv(req, talloc_tos(), &num_changes, &changes);
+
+		if (sig_state.cancelled) {
+			printf("notify cancelled\n");
+			goto fail;
+		}
+
 		if (NT_STATUS_EQUAL(status, NT_STATUS_NOTIFY_ENUM_DIR)) {
 			printf("NOTIFY_ENUM_DIR\n");
 			status = NT_STATUS_OK;
Signature · 1 line
2.54.0
author reviewer
#1 lore raw

Are there any other Samba maintainers I should send this patch to? It seems that [email protected] did not receive my email.

I added cancel notify support to smbclient of Samba to make it easier to debug the ksmbd notify I am currently developing.

Quoted 9 lines · On 2026/5/19 17:51, [email protected] wrote:

From: ChenXiaoSong<[email protected]>

When client stops querying a directory for change notifications, it should send a cancel request to the server.

---
  source3/client/client.c | 57 ++++++++++++++++++++++++++++++++++++++---
  1 file changed, 54 insertions(+), 3 deletions(-)
#2 lore raw
Quoted 2 lines

I added cancel notify support to smbclient of Samba to make it easier to debug the ksmbd notify I am currently developing.

Have you tried to run the smb2.notify test of smbtorture?

metze

author reviewer
#3 lore raw

Once the implementation is finished, I will test it with smbtorture. But right now I'm still developing it, and some issues require an open-source client that supports cancel notify to debug.

Quoted 1 line · Stefan Metzmacher

Have you tried to run the smb2.notify test of smbtorture?

Signature · 3 lines
ChenXiaoSong <[email protected]>
Chinese Homepage: chenxiaosong.com
English Homepage: chenxiaosong.com/en
#4 lore raw

Am 20.05.26 um 11:21 schrieb ChenXiaoSong:

Quoted 1 line

Once the implementation is finished, I will test it with smbtorture. But right now I'm still developing it, and some issues require an open-source client that supports cancel notify to debug.

That open-source client *is* smbtorture

torture_smb2_notify_tcp_disconnect is a very simple test for this.

metze

author reviewer
#5 lore raw

Got it, I will try to use smbtorture to debug, thanks.

Quoted 9 lines · Stefan Metzmacher

Am 20.05.26 um 11:21 schrieb ChenXiaoSong:

Quoted 1 line

Once the implementation is finished, I will test it with smbtorture. But right now I'm still developing it, and some issues require an open-source client that supports cancel notify to debug.

That open-source client *is* smbtorture

torture_smb2_notify_tcp_disconnect is a very simple test for this.

metze

author reviewer
#6 lore raw

But this patch is helpful for improving smbclient. When we press Ctrl+C, it should exit notify instead of terminating smbclient.

Quoted 12 lines · ChenXiaoSong

Got it, I will try to use smbtorture to debug, thanks.

Quoted 9 lines · Stefan Metzmacher

Am 20.05.26 um 11:21 schrieb ChenXiaoSong:

Quoted 1 line

Once the implementation is finished, I will test it with smbtorture. But right now I'm still developing it, and some issues require an open-source client that supports cancel notify to debug.

That open-source client *is* smbtorture

torture_smb2_notify_tcp_disconnect is a very simple test for this.

metze

#9 lore raw
In reply to [email protected]

Hi ChenXiaoSong,

I just realized that the initial mail of this thread actually has the patch... And I fetched it from lore.kernel.org...

A few comments below...

Am 19.05.26 um 11:51 schrieb [email protected]:

Quoted 86 lines

From: ChenXiaoSong <[email protected]>

When client stops querying a directory for change notifications, it should send a cancel request to the server.

---
  source3/client/client.c | 57 ++++++++++++++++++++++++++++++++++++++---
  1 file changed, 54 insertions(+), 3 deletions(-)
source3/client/client.c +40 -3
index 1d3656aab34..7fa850a6dd9 100644
@@ -45,6 +45,7 @@
  #include "lib/cmdline/cmdline.h"
  #include "libcli/smb/reparse.h"
  #include "lib/param/param.h"
+#include "lib/util/tevent_ntstatus.h"
 
  #ifndef REGISTER
  #define REGISTER 0
@@ -4653,12 +4654,40 @@ static int cmd_newer(TALLOC_CTX *mem_ctx)
   Watch directory changes
  ****************************************************************************/
 
+struct notify_state {
+	struct tevent_req *notify_req;
+	bool cancelled;
+};
+
+static void
+notify_sig_int_handler(
+	struct tevent_context *ev,
+	struct tevent_signal *se,
+	int signum,
+	int count,
+	void *siginfo,
+	void *private_data)
+{
+	struct notify_state *state = (struct notify_state *)private_data;
+
+	if (state->notify_req) {
+		state->cancelled = tevent_req_cancel(state->notify_req);
+	}
+}
+
  static int cmd_notify(TALLOC_CTX *mem_ctx)
  {
  	TALLOC_CTX *frame = talloc_stackframe();
  	char *name, *buf;
  	NTSTATUS status;
  	uint16_t fnum;
+	struct tevent_req *req;
+	struct tevent_context *ev;
+	struct tevent_signal *se;
+	struct notify_state sig_state = {
+		.notify_req = NULL,
+		.cancelled = false,
+	};
 
  	name = talloc_strdup(talloc_tos(), client_get_cur_dir());
  	if (name == NULL) {
@@ -4684,14 +4713,36 @@ static int cmd_notify(TALLOC_CTX *mem_ctx)
  		goto fail;
  	}
 
+	ev = samba_tevent_context_init(frame);
+	if (!ev)
+		goto fail;
+
+	se = tevent_add_signal(ev, frame, SIGINT, 0, notify_sig_int_handler, &sig_state);
+	if (!se)
+		goto fail;
+
  	while (1) {
  		uint32_t i;
  		uint32_t num_changes = 0;
  		struct notify_change *changes = NULL;
 
-		status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
-				    true,
-				    talloc_tos(), &num_changes, &changes);
+		req = cli_notify_send(frame, ev, cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL, true);
+		if (!req) {
+			goto fail;

source3/client/client.c static int cmd_notify(TALLOC_CTX *mem_ctx) · line 4741

Quoted 10 lines
source3/client/client.c +10 -0
+		}
+		sig_state.notify_req = req;
+
+		if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+			goto fail;
+		}
+
+		status = cli_notify_recv(req, talloc_tos(), &num_changes, &changes);
+
+		if (sig_state.cancelled) {

I think here we should check for NT_STATUS_CANCELLED.

And sig_state.cancelled should be checked before the next cli_notify_send

Thanks!
metze

author reviewer
#10 lore raw

Thanks for your review. After cancel notify succeeds, cli_notify_recv() seems to return NT_STATUS_OK.

Quoted 3 lines · Stefan Metzmacher

I think here we should check for NT_STATUS_CANCELLED.

And sig_state.cancelled should be checked before the next cli_notify_send

author reviewer
#11 lore raw
In reply to Stefan Metzmacher

Hi Stefan,

Yes, you are correct. sig_state.cancelled should be checked before cli_notify_send(), because changes needs to be freed.

I have already made the changes and created a merge request on GitLab: https://gitlab.com/samba-team/samba/-/merge_requests/4530

But I cannot run pipelines because I cannot verify my account, as GitLab does not support phone number verification in China. Should I send the v2 to the mailing list?

Quoted 1 line · Stefan Metzmacher

And sig_state.cancelled should be checked before the next cli_notify_send