1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Infrastructure for async requests
|
---|
4 | Copyright (C) Volker Lendecke 2008
|
---|
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 |
|
---|
22 | char *async_req_print(TALLOC_CTX *mem_ctx, struct async_req *req)
|
---|
23 | {
|
---|
24 | return talloc_asprintf(mem_ctx, "async_req: state=%d, status=%s, "
|
---|
25 | "priv=%s", req->state, nt_errstr(req->status),
|
---|
26 | talloc_get_name(req->private_data));
|
---|
27 | }
|
---|
28 |
|
---|
29 | struct async_req *async_req_new(TALLOC_CTX *mem_ctx, struct event_context *ev)
|
---|
30 | {
|
---|
31 | struct async_req *result;
|
---|
32 |
|
---|
33 | result = TALLOC_ZERO_P(mem_ctx, struct async_req);
|
---|
34 | if (result == NULL) {
|
---|
35 | return NULL;
|
---|
36 | }
|
---|
37 | result->state = ASYNC_REQ_IN_PROGRESS;
|
---|
38 | result->event_ctx = ev;
|
---|
39 | result->print = async_req_print;
|
---|
40 | return result;
|
---|
41 | }
|
---|
42 |
|
---|
43 | void async_req_done(struct async_req *req)
|
---|
44 | {
|
---|
45 | req->status = NT_STATUS_OK;
|
---|
46 | req->state = ASYNC_REQ_DONE;
|
---|
47 | if (req->async.fn != NULL) {
|
---|
48 | req->async.fn(req);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | void async_req_error(struct async_req *req, NTSTATUS status)
|
---|
53 | {
|
---|
54 | req->status = status;
|
---|
55 | req->state = ASYNC_REQ_ERROR;
|
---|
56 | if (req->async.fn != NULL) {
|
---|
57 | req->async.fn(req);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | static void async_trigger(struct event_context *ev, struct timed_event *te,
|
---|
62 | const struct timeval *now, void *priv)
|
---|
63 | {
|
---|
64 | struct async_req *req = talloc_get_type_abort(priv, struct async_req);
|
---|
65 |
|
---|
66 | TALLOC_FREE(te);
|
---|
67 | if (NT_STATUS_IS_OK(req->status)) {
|
---|
68 | async_req_done(req);
|
---|
69 | }
|
---|
70 | else {
|
---|
71 | async_req_error(req, req->status);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | bool async_post_status(struct async_req *req, NTSTATUS status)
|
---|
76 | {
|
---|
77 | /*
|
---|
78 | * Used if a request is finished before it even started
|
---|
79 | */
|
---|
80 |
|
---|
81 | req->status = status;
|
---|
82 |
|
---|
83 | if (event_add_timed(req->event_ctx, req, timeval_zero(),
|
---|
84 | "async_trigger",
|
---|
85 | async_trigger, req) == NULL) {
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 |
|
---|
91 | bool async_req_nomem(const void *p, struct async_req *req)
|
---|
92 | {
|
---|
93 | if (p != NULL) {
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 | async_req_error(req, NT_STATUS_NO_MEMORY);
|
---|
97 | return true;
|
---|
98 | }
|
---|