ctdb-scripts: further untangle logic for success of interface monitoring
[obnox/samba/samba-obnox.git] / source3 / torture / test_oplock_cancel.c
1 /*
2    Unix SMB/CIFS implementation.
3    Test cleanup behaviour
4    Copyright (C) Volker Lendecke 2011
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "locking/proto.h"
22 #include "torture/proto.h"
23 #include "system/filesys.h"
24 #include "system/select.h"
25 #include "libsmb/libsmb.h"
26 #include "libcli/smb/smbXcli_base.h"
27 #include "libcli/security/security.h"
28 #include "lib/util/tevent_ntstatus.h"
29
30 struct create_cancel_state {
31         uint8_t dummy;
32 };
33
34 static void create_cancel_done(struct tevent_req *subreq);
35
36 static struct tevent_req *create_cancel_send(
37         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
38         struct cli_state *cli, const char *fname)
39 {
40         struct tevent_req *req, *subreq;
41         struct create_cancel_state *state;
42
43         req = tevent_req_create(mem_ctx, &state, struct create_cancel_state);
44         if (req == NULL) {
45                 return NULL;
46         }
47
48         subreq = cli_ntcreate_send(
49                 mem_ctx, ev, cli, fname, 0, FILE_GENERIC_READ,
50                 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE,
51                 FILE_OPEN_IF, 0, 0);
52         if (tevent_req_nomem(subreq, req)) {
53                 return tevent_req_post(req, ev);
54         }
55         if (!tevent_req_cancel(subreq)) {
56                 tevent_req_oom(req);
57                 return tevent_req_post(req, ev);
58         }
59         tevent_req_set_callback(subreq, create_cancel_done, req);
60         return req;
61 }
62
63 static void create_cancel_done(struct tevent_req *subreq)
64 {
65         struct tevent_req *req = tevent_req_callback_data(
66                 subreq, struct tevent_req);
67         NTSTATUS status;
68
69         status = cli_ntcreate_recv(subreq, NULL, NULL);
70         TALLOC_FREE(subreq);
71         if (!NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) {
72                 if (NT_STATUS_IS_OK(status)) {
73                         status = NT_STATUS_UNSUCCESSFUL;
74                 }
75                 tevent_req_nterror(req, status);
76                 return;
77         }
78         tevent_req_done(req);
79 }
80
81 static NTSTATUS create_cancel_recv(struct tevent_req *req)
82 {
83         return tevent_req_simple_recv_ntstatus(req);
84 }
85
86 static NTSTATUS create_cancel(struct cli_state *cli, const char *fname)
87 {
88         TALLOC_CTX *frame = talloc_stackframe();
89         struct tevent_context *ev;
90         struct tevent_req *req;
91         NTSTATUS status = NT_STATUS_NO_MEMORY;
92
93         ev = samba_tevent_context_init(frame);
94         if (ev == NULL) {
95                 goto fail;
96         }
97         req = create_cancel_send(frame, ev, cli, fname);
98         if (req == NULL) {
99                 goto fail;
100         }
101         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
102                 goto fail;
103         }
104         status = create_cancel_recv(req);
105  fail:
106         TALLOC_FREE(frame);
107         return status;
108 }
109
110 bool run_oplock_cancel(int dummy)
111 {
112         struct cli_state *cli1, *cli2;
113         const char *fname = "oplock-cancel";
114         uint16_t fnum1;
115         NTSTATUS status;
116
117         lp_set_cmdline("client max protocol", "smb3");
118
119         if (!torture_open_connection(&cli1, 0)) {
120                 return false;
121         }
122         cli1->use_oplocks = true;
123
124         if (!torture_open_connection(&cli2, 0)) {
125                 return false;
126         }
127         cli2->use_oplocks = true;
128
129         status = cli_ntcreate(
130                 cli1, fname, 0, FILE_GENERIC_READ, FILE_ATTRIBUTE_NORMAL,
131                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF, 0, 0,
132                 &fnum1, NULL);
133         if (!NT_STATUS_IS_OK(status)) {
134                 d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
135                 return false;
136         }
137
138         status = create_cancel(cli2, fname);
139         if (!NT_STATUS_IS_OK(status)) {
140                 d_printf("create_cancel failed: %s\n", nt_errstr(status));
141                 return false;
142         }
143
144         TALLOC_FREE(cli1);
145
146         /*
147          * Give cli1's smbd time to inform cli2's smbd
148          */
149         smb_msleep(5000);
150
151         status = cli_unlink(cli2, fname,
152                             FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
153         if (!NT_STATUS_IS_OK(status)) {
154                 d_printf("cli_unlink failed: %s\n", nt_errstr(status));
155                 return false;
156         }
157
158         return true;
159 }