ctdb-scripts: Move connection tracking to 10.interface
[samba4-gss.git] / source3 / smbd / notifyd / notifyd_db.c
blob7dc3cd580815abb9ae424705137a6a0c2c563bc5
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #include "replace.h"
17 #include "lib/util/debug.h"
18 #include "lib/util/server_id_db.h"
19 #include "notifyd_private.h"
20 #include "notifyd_db.h"
22 struct notifyd_parse_db_state {
23 bool (*fn)(const char *path,
24 struct server_id server,
25 const struct notify_instance *instance,
26 void *private_data);
27 void *private_data;
30 static bool notifyd_parse_db_parser(TDB_DATA key, TDB_DATA value,
31 void *private_data)
33 struct notifyd_parse_db_state *state = private_data;
34 char path[key.dsize+1];
35 struct notifyd_instance *instances = NULL;
36 size_t num_instances = 0;
37 size_t i;
38 bool ok;
40 memcpy(path, key.dptr, key.dsize);
41 path[key.dsize] = 0;
43 ok = notifyd_parse_entry(value.dptr,
44 value.dsize,
45 NULL,
46 &instances,
47 &num_instances);
48 if (!ok) {
49 DBG_DEBUG("Could not parse entry for path %s\n", path);
50 return true;
53 for (i=0; i<num_instances; i++) {
54 ok = state->fn(path, instances[i].client,
55 &instances[i].instance,
56 state->private_data);
57 if (!ok) {
58 return false;
62 return true;
65 static NTSTATUS notifyd_parse_db(
66 const uint8_t *buf,
67 size_t buflen,
68 uint64_t *log_index,
69 bool (*fn)(const char *path,
70 struct server_id server,
71 const struct notify_instance *instance,
72 void *private_data),
73 void *private_data)
75 struct notifyd_parse_db_state state = {
76 .fn = fn, .private_data = private_data
78 NTSTATUS status;
80 if (buflen < 8) {
81 return NT_STATUS_INTERNAL_DB_CORRUPTION;
83 *log_index = BVAL(buf, 0);
85 buf += 8;
86 buflen -= 8;
88 status = dbwrap_parse_marshall_buf(
89 buf, buflen, notifyd_parse_db_parser, &state);
90 return status;
93 NTSTATUS notify_walk(struct messaging_context *msg_ctx,
94 bool (*fn)(const char *path, struct server_id server,
95 const struct notify_instance *instance,
96 void *private_data),
97 void *private_data)
99 struct server_id_db *names_db = NULL;
100 struct server_id notifyd = { .pid = 0, };
101 struct tevent_context *ev = NULL;
102 struct tevent_req *req = NULL;
103 struct messaging_rec *rec = NULL;
104 uint64_t log_idx;
105 NTSTATUS status;
106 int ret;
107 bool ok;
109 names_db = messaging_names_db(msg_ctx);
110 ok = server_id_db_lookup_one(names_db, "notify-daemon", &notifyd);
111 if (!ok) {
112 DBG_WARNING("No notify daemon around\n");
113 return NT_STATUS_SERVER_UNAVAILABLE;
116 ev = samba_tevent_context_init(msg_ctx);
117 if (ev == NULL) {
118 return NT_STATUS_NO_MEMORY;
121 req = messaging_read_send(ev, ev, msg_ctx, MSG_SMB_NOTIFY_DB);
122 if (req == NULL) {
123 TALLOC_FREE(ev);
124 return NT_STATUS_NO_MEMORY;
127 ok = tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0));
128 if (!ok) {
129 TALLOC_FREE(ev);
130 return NT_STATUS_NO_MEMORY;
133 status = messaging_send_buf(
134 msg_ctx, notifyd, MSG_SMB_NOTIFY_GET_DB, NULL, 0);
135 if (!NT_STATUS_IS_OK(status)) {
136 DBG_DEBUG("messaging_send_buf failed: %s\n",
137 nt_errstr(status));
138 TALLOC_FREE(ev);
139 return status;
142 ok = tevent_req_poll(req, ev);
143 if (!ok) {
144 DBG_DEBUG("tevent_req_poll failed\n");
145 TALLOC_FREE(ev);
146 return NT_STATUS_INTERNAL_ERROR;
149 ret = messaging_read_recv(req, ev, &rec);
150 if (ret != 0) {
151 DBG_DEBUG("messaging_read_recv failed: %s\n",
152 strerror(ret));
153 TALLOC_FREE(ev);
154 return map_nt_error_from_unix(ret);
157 status = notifyd_parse_db(
158 rec->buf.data, rec->buf.length, &log_idx, fn, private_data);
159 if (!NT_STATUS_IS_OK(status)) {
160 DBG_DEBUG("notifyd_parse_db failed: %s\n",
161 nt_errstr(status));
162 TALLOC_FREE(ev);
163 return status;
166 TALLOC_FREE(ev);
167 return NT_STATUS_OK;