Debian Codegraph @

source4/torture/smb2/lease_break_handler.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3
4 test suite for SMB2 leases
5
6 Copyright (C) Zachary Loafman 2009
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include <tevent.h>
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "torture/torture.h"
27 #include "torture/smb2/proto.h"
28 #include "torture/util.h"
29 #include "libcli/smb/smbXcli_base.h"
30 #include "lease_break_handler.h"
31
32 struct lease_break_info lease_break_info;
33
34 static void torture_lease_break_callback(struct smb2_request *req)
35 {
36 NTSTATUS status;
37
38 status = smb2_lease_break_ack_recv(req, &lease_break_info.lease_break_ack);
39 if (!NT_STATUS_IS_OK(status))
40 lease_break_info.failures++;
41
42 return;
43 }
44
45 static void torture_lease_break_close_callback(struct smb2_request *req)
46 {
47 NTSTATUS status;
48
49 status = smb2_close_recv(req, &lease_break_info.close);
50 if (!NT_STATUS_IS_OK(status)) {
51 lease_break_info.failures++;
52 }
53 return;
54 }
55
56 /* a lease break request handler */
57 bool torture_lease_handler(struct smb2_transport *transport,
58 const struct smb2_lease_break *lb,
59 void *private_data)
60 {
61 struct smb2_tree *tree = private_data;
62 struct smb2_lease_break_ack io;
63 struct smb2_request *req;
64 const char *action = NULL;
65 char *ls = smb2_util_lease_state_string(lease_break_info.tctx,
66 lb->new_lease_state);
67
68 if (lb->break_flags & SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED) {
69 action = "acking";
70 } else {
71 action = "received";
72 }
73
74 lease_break_info.lease_transport = transport;
75 lease_break_info.lease_break = *lb;
76 lease_break_info.count++;
77
78 if (!smb2_util_handle_empty(lease_break_info.lease_handle) &&
79 (lb->current_lease.lease_state & SMB2_LEASE_HANDLE) &&
80 !(lb->new_lease_state & SMB2_LEASE_HANDLE))
81 {
82 torture_comment(lease_break_info.tctx,
83 "transport[%p] closing handle\n",
84 transport);
85
86 ZERO_STRUCT(lease_break_info.close);
87 lease_break_info.close.in.file.handle =
88 lease_break_info.lease_handle;
89 ZERO_STRUCT(lease_break_info.lease_handle);
90
91 req = smb2_close_send(tree, &lease_break_info.close);
92 req->async.fn = torture_lease_break_close_callback;
93 req->async.private_data = NULL;
94 return true;
95 }
96
97 if (lease_break_info.lease_skip_ack) {
98 torture_comment(lease_break_info.tctx,
99 "transport[%p] Skip %s to %s in lease handler\n",
100 transport, action, ls);
101 return true;
102 }
103
104 torture_comment(lease_break_info.tctx,
105 "transport[%p] %s to %s in lease handler\n",
106 transport, action, ls);
107
108 if (lb->break_flags & SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED) {
109 ZERO_STRUCT(io);
110 io.in.lease.lease_key = lb->current_lease.lease_key;
111 io.in.lease.lease_state = lb->new_lease_state;
112
113 req = smb2_lease_break_ack_send(tree, &io);
114 req->async.fn = torture_lease_break_callback;
115 req->async.private_data = NULL;
116 }
117
118 return true;
119 }
120
121 /*
122 Timer handler function notifies the registering function that time is up
123 */
124 static void timeout_cb(struct tevent_context *ev,
125 struct tevent_timer *te,
126 struct timeval current_time,
127 void *private_data)
128 {
129 bool *timesup = (bool *)private_data;
130 *timesup = true;
131 return;
132 }
133
134 /*
135 Wait a short period of time to receive a single oplock break request
136 */
137 void torture_wait_for_lease_break(struct torture_context *tctx)
138 {
139 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
140 struct tevent_timer *te = NULL;
141 struct timeval ne;
142 bool timesup = false;
143 int old_count = lease_break_info.count;
144
145 /* Wait 1 second for an lease break */
146 ne = tevent_timeval_current_ofs(0, 1000000);
147
148 te = tevent_add_timer(tctx->ev, tmp_ctx, ne, timeout_cb, &timesup);
149 if (te == NULL) {
150 torture_comment(tctx, "Failed to wait for an lease break. "
151 "test results may not be accurate.\n");
152 goto done;
153 }
154
155 torture_comment(tctx, "Waiting for a potential lease break...\n");
156 while (!timesup && lease_break_info.count < old_count + 1) {
157 if (tevent_loop_once(tctx->ev) != 0) {
158 torture_comment(tctx, "Failed to wait for a lease "
159 "break. test results may not be "
160 "accurate.\n");
161 goto done;
162 }
163 }
164 if (timesup) {
165 torture_comment(tctx, "... waiting for a lease break timed out\n");
166 } else {
167 torture_comment(tctx, "Got %u lease breaks\n",
168 lease_break_info.count - old_count);
169 }
170
171 done:
172 /* We don't know if the timed event fired and was freed, we received
173 * our oplock break, or some other event triggered the loop. Thus,
174 * we create a tmp_ctx to be able to safely free/remove the timed
175 * event in all 3 cases. */
176 talloc_free(tmp_ctx);
177
178 return;
179 }