lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[nivanova/samba-autobuild/.git] / source4 / ntvfs / sysdep / sys_lease.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Stefan Metzmacher 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 /*
21   abstract the various kernel interfaces to leases (oplocks) into a
22   single Samba friendly interface
23 */
24
25 #include "includes.h"
26 #include "system/filesys.h"
27 #include "ntvfs/sysdep/sys_lease.h"
28 #include "../lib/util/dlinklist.h"
29 #include "param/param.h"
30 #include "lib/util/samba_modules.h"
31
32 /* list of registered backends */
33 static struct sys_lease_ops *backends;
34 static uint32_t num_backends;
35
36 #define LEASE_BACKEND   "lease:backend"
37
38 /*
39   initialise a system change notify backend
40 */
41 _PUBLIC_ struct sys_lease_context *sys_lease_context_create(struct share_config *scfg,
42                                                             TALLOC_CTX *mem_ctx,
43                                                             struct tevent_context *ev,
44                                                             struct imessaging_context *msg,
45                                                             sys_lease_send_break_fn break_send)
46 {
47         struct sys_lease_context *ctx;
48         const char *bname;
49         int i;
50         NTSTATUS status;
51         TALLOC_CTX * tmp_ctx;
52
53         if (num_backends == 0) {
54                 return NULL;
55         }
56
57         if (ev == NULL) {
58                 return NULL;
59         }
60
61         ctx = talloc_zero(mem_ctx, struct sys_lease_context);
62         if (ctx == NULL) {
63                 return NULL;
64         }
65
66         tmp_ctx = talloc_new(ctx);
67         if (tmp_ctx == NULL) {
68                 return NULL;
69         }
70
71         ctx->event_ctx = ev;
72         ctx->msg_ctx = msg;
73         ctx->break_send = break_send;
74
75         bname = share_string_option(tmp_ctx, scfg, LEASE_BACKEND, NULL);
76         if (!bname) {
77                 talloc_free(ctx);
78                 return NULL;
79         }
80
81         for (i=0;i<num_backends;i++) {
82                 if (strcasecmp(backends[i].name, bname) == 0) {
83                         ctx->ops = &backends[i];
84                         break;
85                 }
86         }
87
88         if (!ctx->ops) {
89                 talloc_free(ctx);
90                 return NULL;
91         }
92
93         status = ctx->ops->init(ctx);
94         if (!NT_STATUS_IS_OK(status)) {
95                 talloc_free(ctx);
96                 return NULL;
97         }
98
99         TALLOC_FREE(tmp_ctx);
100         return ctx;
101 }
102
103 /*
104   register a lease backend
105 */
106 _PUBLIC_ NTSTATUS sys_lease_register(const struct sys_lease_ops *backend)
107 {
108         struct sys_lease_ops *b;
109         b = talloc_realloc(talloc_autofree_context(), backends,
110                            struct sys_lease_ops, num_backends+1);
111         NT_STATUS_HAVE_NO_MEMORY(b);
112         backends = b;
113         backends[num_backends] = *backend;
114         num_backends++;
115         return NT_STATUS_OK;
116 }
117
118 _PUBLIC_ NTSTATUS sys_lease_init(void)
119 {
120         static bool initialized = false;
121 #define _MODULE_PROTO(init) extern NTSTATUS init(TALLOC_CTX *);
122         STATIC_sys_lease_MODULES_PROTO;
123         init_module_fn static_init[] = { STATIC_sys_lease_MODULES };
124
125         if (initialized) return NT_STATUS_OK;
126         initialized = true;
127
128         run_init_functions(NULL, static_init);
129
130         return NT_STATUS_OK;
131 }
132
133 NTSTATUS sys_lease_setup(struct sys_lease_context *ctx,
134                          struct opendb_entry *e)
135 {
136         return ctx->ops->setup(ctx, e);
137 }
138
139 NTSTATUS sys_lease_update(struct sys_lease_context *ctx,
140                           struct opendb_entry *e)
141 {
142         return ctx->ops->update(ctx, e);
143 }
144
145 NTSTATUS sys_lease_remove(struct sys_lease_context *ctx,
146                           struct opendb_entry *e)
147 {
148         return ctx->ops->remove(ctx, e);
149 }