smbclient: cancel pending notify on SIGINT
Subsystems (2)
[email protected]
reviewerPATCH 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
@@ -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
ChenXiaoSong
reviewerAre 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.
Signed-off-by ChenXiaoSong<[email protected]>--- source3/client/client.c | 57 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-)
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
ChenXiaoSong
reviewerOnce 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
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
ChenXiaoSong
reviewerGot 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_disconnectis a very simple test for this.metze
ChenXiaoSong
reviewerBut 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_disconnectis a very simple test for this.metze
Am 20.05.26 um 12:07 schrieb ChenXiaoSong:
Quoted 1 line
But this patch is helpful for improving smbclient. When we press Ctrl+C, it should exit notify instead of terminating smbclient.
See https://wiki.samba.org/index.php/Contribute and https://wiki.samba.org/index.php/Samba_on_GitLab
and create a merged request :-)
metze
ChenXiaoSong
reviewerGot it, thanks for your guidance.
Quoted 2 lines · Stefan Metzmacher
See https://wiki.samba.org/index.php/Contribute and https://wiki.samba.org/index.php/Samba_on_GitLab
and create a merged request 🙂
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.
Signed-off-by ChenXiaoSong <[email protected]>--- source3/client/client.c | 57 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-)source3/client/client.c +40
-3@@ -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
ChenXiaoSong
reviewerThanks 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
ChenXiaoSong
reviewerHi 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