notifyd: Factor out notify_walk() into its own file
authorVolker Lendecke <vl@samba.org>
Wed, 21 Oct 2020 15:28:14 +0000 (17:28 +0200)
committerJeremy Allison <jra@samba.org>
Sat, 24 Oct 2020 05:57:31 +0000 (05:57 +0000)
To be used in smbtorture, avoid having to include almost all of smbd
just for this

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/smbd/notify_msg.c
source3/smbd/notifyd/notifyd.c
source3/smbd/notifyd/notifyd.h
source3/smbd/notifyd/notifyd_db.c [new file with mode: 0644]
source3/smbd/notifyd/notifyd_db.h [new file with mode: 0644]
source3/smbd/notifyd/wscript_build
source3/smbd/proto.h
source3/utils/status.c

index 7238cff68283ed2554823a5c107a7a1ed3248e2a..0ea992c4929317d6df125d861e05c5150564ff51 100644 (file)
@@ -245,71 +245,3 @@ void notify_trigger(struct notify_context *ctx,
                ctx->msg_ctx, ctx->notifyd, MSG_SMB_NOTIFY_TRIGGER,
                iov, ARRAY_SIZE(iov), NULL, 0);
 }
-
-NTSTATUS notify_walk(struct notify_context *notify,
-                    bool (*fn)(const char *path, struct server_id server,
-                               const struct notify_instance *instance,
-                               void *private_data),
-                    void *private_data)
-{
-       struct tevent_context *ev;
-       struct tevent_req *req;
-       struct messaging_rec *rec;
-       uint64_t log_idx;
-       NTSTATUS status;
-       int ret;
-       bool ok;
-
-       ev = samba_tevent_context_init(notify);
-       if (ev == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       req = messaging_read_send(ev, ev, notify->msg_ctx, MSG_SMB_NOTIFY_DB);
-       if (req == NULL) {
-               TALLOC_FREE(ev);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       ok = tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0));
-       if (!ok) {
-               TALLOC_FREE(ev);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       status = messaging_send_buf(notify->msg_ctx, notify->notifyd,
-                                   MSG_SMB_NOTIFY_GET_DB, NULL, 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               DBG_DEBUG("messaging_send_buf failed: %s\n",
-                         nt_errstr(status));
-               TALLOC_FREE(ev);
-               return status;
-       }
-
-       ok = tevent_req_poll(req, ev);
-       if (!ok) {
-               DBG_DEBUG("tevent_req_poll failed\n");
-               TALLOC_FREE(ev);
-               return NT_STATUS_INTERNAL_ERROR;
-       }
-
-       ret = messaging_read_recv(req, ev, &rec);
-       if (ret != 0) {
-               DBG_DEBUG("messaging_read_recv failed: %s\n",
-                         strerror(ret));
-               TALLOC_FREE(ev);
-               return map_nt_error_from_unix(ret);
-       }
-
-       ret = notifyd_parse_db(rec->buf.data, rec->buf.length, &log_idx,
-                              fn, private_data);
-       if (ret != 0) {
-               DBG_DEBUG("notifyd_parse_db failed: %s\n",
-                         strerror(ret));
-               TALLOC_FREE(ev);
-               return map_nt_error_from_unix(ret);
-       }
-
-       TALLOC_FREE(ev);
-       return NT_STATUS_OK;
-}
index 9029e88dc575b9d889f1b67bf2afdf49f8ad101b..601bd168cd85c68142ead632844da6582e5ef0d7 100644 (file)
@@ -1426,73 +1426,3 @@ static int notifyd_snoop_broadcast(struct tevent_context *ev,
        return 0;
 }
 #endif
-
-struct notifyd_parse_db_state {
-       bool (*fn)(const char *path,
-                  struct server_id server,
-                  const struct notify_instance *instance,
-                  void *private_data);
-       void *private_data;
-};
-
-static bool notifyd_parse_db_parser(TDB_DATA key, TDB_DATA value,
-                                   void *private_data)
-{
-       struct notifyd_parse_db_state *state = private_data;
-       char path[key.dsize+1];
-       struct notifyd_instance *instances = NULL;
-       size_t num_instances = 0;
-       size_t i;
-       bool ok;
-
-       memcpy(path, key.dptr, key.dsize);
-       path[key.dsize] = 0;
-
-       ok = notifyd_parse_entry(value.dptr, value.dsize, &instances,
-                                &num_instances);
-       if (!ok) {
-               DBG_DEBUG("Could not parse entry for path %s\n", path);
-               return true;
-       }
-
-       for (i=0; i<num_instances; i++) {
-               ok = state->fn(path, instances[i].client,
-                              &instances[i].instance,
-                              state->private_data);
-               if (!ok) {
-                       return false;
-               }
-       }
-
-       return true;
-}
-
-int notifyd_parse_db(const uint8_t *buf, size_t buflen,
-                    uint64_t *log_index,
-                    bool (*fn)(const char *path,
-                               struct server_id server,
-                               const struct notify_instance *instance,
-                               void *private_data),
-                    void *private_data)
-{
-       struct notifyd_parse_db_state state = {
-               .fn = fn, .private_data = private_data
-       };
-       NTSTATUS status;
-
-       if (buflen < 8) {
-               return EINVAL;
-       }
-       *log_index = BVAL(buf, 0);
-
-       buf += 8;
-       buflen -= 8;
-
-       status = dbwrap_parse_marshall_buf(
-               buf, buflen, notifyd_parse_db_parser, &state);
-       if (!NT_STATUS_IS_OK(status)) {
-               return map_errno_from_nt_status(status);
-       }
-
-       return 0;
-}
index 6904761af8f9f9d7dff143d88d968331203fd1f8..5ef8c4c8d09de5e23dc50e39784d1c0ed3ceb484 100644 (file)
@@ -142,17 +142,4 @@ struct tevent_req *notifyd_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
                                struct sys_notify_context *sys_notify_ctx);
 int notifyd_recv(struct tevent_req *req);
 
-/*
- * Parse a database received via the MSG_SMB_NOTIFY_[GET_]DB messages to the
- * notify daemon
- */
-int notifyd_parse_db(const uint8_t *buf, size_t buflen,
-                    uint64_t *log_index,
-                    bool (*fn)(const char *path,
-                               struct server_id server,
-                               const struct notify_instance *instance,
-                               void *private_data),
-                    void *private_data);
-
-
 #endif
diff --git a/source3/smbd/notifyd/notifyd_db.c b/source3/smbd/notifyd/notifyd_db.c
new file mode 100644 (file)
index 0000000..1822861
--- /dev/null
@@ -0,0 +1,165 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "replace.h"
+#include "lib/util/debug.h"
+#include "lib/util/server_id_db.h"
+#include "notifyd_private.h"
+#include "notifyd_db.h"
+
+struct notifyd_parse_db_state {
+       bool (*fn)(const char *path,
+                  struct server_id server,
+                  const struct notify_instance *instance,
+                  void *private_data);
+       void *private_data;
+};
+
+static bool notifyd_parse_db_parser(TDB_DATA key, TDB_DATA value,
+                                   void *private_data)
+{
+       struct notifyd_parse_db_state *state = private_data;
+       char path[key.dsize+1];
+       struct notifyd_instance *instances = NULL;
+       size_t num_instances = 0;
+       size_t i;
+       bool ok;
+
+       memcpy(path, key.dptr, key.dsize);
+       path[key.dsize] = 0;
+
+       ok = notifyd_parse_entry(value.dptr, value.dsize, &instances,
+                                &num_instances);
+       if (!ok) {
+               DBG_DEBUG("Could not parse entry for path %s\n", path);
+               return true;
+       }
+
+       for (i=0; i<num_instances; i++) {
+               ok = state->fn(path, instances[i].client,
+                              &instances[i].instance,
+                              state->private_data);
+               if (!ok) {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+static NTSTATUS notifyd_parse_db(
+       const uint8_t *buf,
+       size_t buflen,
+       uint64_t *log_index,
+       bool (*fn)(const char *path,
+                  struct server_id server,
+                  const struct notify_instance *instance,
+                  void *private_data),
+       void *private_data)
+{
+       struct notifyd_parse_db_state state = {
+               .fn = fn, .private_data = private_data
+       };
+       NTSTATUS status;
+
+       if (buflen < 8) {
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+       *log_index = BVAL(buf, 0);
+
+       buf += 8;
+       buflen -= 8;
+
+       status = dbwrap_parse_marshall_buf(
+               buf, buflen, notifyd_parse_db_parser, &state);
+       return status;
+}
+
+NTSTATUS notify_walk(struct messaging_context *msg_ctx,
+                    bool (*fn)(const char *path, struct server_id server,
+                               const struct notify_instance *instance,
+                               void *private_data),
+                    void *private_data)
+{
+       struct server_id_db *names_db = NULL;
+       struct server_id notifyd = { .pid = 0, };
+       struct tevent_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       struct messaging_rec *rec = NULL;
+       uint64_t log_idx;
+       NTSTATUS status;
+       int ret;
+       bool ok;
+
+       names_db = messaging_names_db(msg_ctx);
+       ok = server_id_db_lookup_one(names_db, "notify-daemon", &notifyd);
+       if (!ok) {
+               DBG_WARNING("No notify daemon around\n");
+               return NT_STATUS_SERVER_UNAVAILABLE;
+       }
+
+       ev = samba_tevent_context_init(msg_ctx);
+       if (ev == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       req = messaging_read_send(ev, ev, msg_ctx, MSG_SMB_NOTIFY_DB);
+       if (req == NULL) {
+               TALLOC_FREE(ev);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       ok = tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0));
+       if (!ok) {
+               TALLOC_FREE(ev);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = messaging_send_buf(
+               msg_ctx, notifyd, MSG_SMB_NOTIFY_GET_DB, NULL, 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_DEBUG("messaging_send_buf failed: %s\n",
+                         nt_errstr(status));
+               TALLOC_FREE(ev);
+               return status;
+       }
+
+       ok = tevent_req_poll(req, ev);
+       if (!ok) {
+               DBG_DEBUG("tevent_req_poll failed\n");
+               TALLOC_FREE(ev);
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       ret = messaging_read_recv(req, ev, &rec);
+       if (ret != 0) {
+               DBG_DEBUG("messaging_read_recv failed: %s\n",
+                         strerror(ret));
+               TALLOC_FREE(ev);
+               return map_nt_error_from_unix(ret);
+       }
+
+       status = notifyd_parse_db(
+               rec->buf.data, rec->buf.length, &log_idx, fn, private_data);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_DEBUG("notifyd_parse_db failed: %s\n",
+                         nt_errstr(status));
+               TALLOC_FREE(ev);
+               return status;
+       }
+
+       TALLOC_FREE(ev);
+       return NT_STATUS_OK;
+}
diff --git a/source3/smbd/notifyd/notifyd_db.h b/source3/smbd/notifyd/notifyd_db.h
new file mode 100644 (file)
index 0000000..bd9e226
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __NOTIFYD_NOTIFYD_DB_H__
+#define __NOTIFYD_NOTIFYD_DB_H__
+#include "replace.h"
+#include "notifyd.h"
+
+NTSTATUS notify_walk(struct messaging_context *msg_ctx,
+                    bool (*fn)(const char *path, struct server_id server,
+                               const struct notify_instance *instance,
+                               void *private_data),
+                    void *private_data);
+
+#endif
index e87421d4202b2243cc2b4bb16f78ea74130116ea..8d549bd0d25f9e72d9ec78b6749ba627a8e888bb 100644 (file)
@@ -1,8 +1,8 @@
 #!/usr/bin/env python
 
 bld.SAMBA3_SUBSYSTEM('notifyd_db',
-                    source='notifyd_entry.c',
-                     deps='samba-debug')
+                    source='notifyd_entry.c notifyd_db.c',
+                     deps='samba-debug dbwrap errors3')
 
 bld.SAMBA3_SUBSYSTEM('notifyd',
                     source='notifyd.c',
index 40e7c3b04e7edbc9b7060c884025ff6627b8c8fd..b768e81afe9c6b660e9c32852825ff7343b8a6f9 100644 (file)
@@ -660,13 +660,6 @@ void notify_trigger(struct notify_context *notify,
                    uint32_t action, uint32_t filter,
                    const char *dir, const char *path);
 
-struct notify_instance;
-NTSTATUS notify_walk(struct notify_context *notify,
-                    bool (*fn)(const char *path, struct server_id server,
-                               const struct notify_instance *instance,
-                               void *private_data),
-                    void *private_data);
-
 /* The following definitions come from smbd/ntquotas.c  */
 
 NTSTATUS vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype,
index 1c1cc4e9e52cc6e2da20711f3eb24a68410f7c4a..05cc46086fe327f93e4c2c85b86da29927ea7c29 100644 (file)
@@ -47,7 +47,7 @@
 #include "conn_tdb.h"
 #include "serverid.h"
 #include "status_profile.h"
-#include "smbd/notifyd/notifyd.h"
+#include "smbd/notifyd/notifyd_db.h"
 #include "cmdline_contexts.h"
 #include "locking/leases_db.h"
 #include "lib/util/string_wrappers.h"
@@ -837,15 +837,7 @@ int main(int argc, const char *argv[])
        }
 
        if (show_notify) {
-               struct notify_context *n;
-
-               n = notify_init(talloc_tos(), msg_ctx,
-                               NULL, NULL);
-               if (n == NULL) {
-                       goto done;
-               }
-               notify_walk(n, print_notify_rec, NULL);
-               TALLOC_FREE(n);
+               notify_walk(msg_ctx, print_notify_rec, NULL);
        }
 
 done: