Fix bug 5334
[Samba.git] / source / lib / async_req.c
blob2e85d9a38d7ec50c19783bc934494121980103a4
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async requests
4 Copyright (C) Volker Lendecke 2008
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.
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.
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/>.
20 #include "includes.h"
22 char *async_req_print(TALLOC_CTX *mem_ctx, struct async_req *req)
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));
29 struct async_req *async_req_new(TALLOC_CTX *mem_ctx, struct event_context *ev)
31 struct async_req *result;
33 result = TALLOC_ZERO_P(mem_ctx, struct async_req);
34 if (result == NULL) {
35 return NULL;
37 result->state = ASYNC_REQ_IN_PROGRESS;
38 result->event_ctx = ev;
39 result->print = async_req_print;
40 return result;
43 void async_req_done(struct async_req *req)
45 req->status = NT_STATUS_OK;
46 req->state = ASYNC_REQ_DONE;
47 if (req->async.fn != NULL) {
48 req->async.fn(req);
52 void async_req_error(struct async_req *req, NTSTATUS status)
54 req->status = status;
55 req->state = ASYNC_REQ_ERROR;
56 if (req->async.fn != NULL) {
57 req->async.fn(req);
61 static void async_trigger(struct event_context *ev, struct timed_event *te,
62 const struct timeval *now, void *priv)
64 struct async_req *req = talloc_get_type_abort(priv, struct async_req);
66 TALLOC_FREE(te);
67 if (NT_STATUS_IS_OK(req->status)) {
68 async_req_done(req);
70 else {
71 async_req_error(req, req->status);
75 bool async_post_status(struct async_req *req, NTSTATUS status)
78 * Used if a request is finished before it even started
81 req->status = status;
83 if (event_add_timed(req->event_ctx, req, timeval_zero(),
84 "async_trigger",
85 async_trigger, req) == NULL) {
86 return false;
88 return true;
91 bool async_req_nomem(const void *p, struct async_req *req)
93 if (p != NULL) {
94 return false;
96 async_req_error(req, NT_STATUS_NO_MEMORY);
97 return true;