[PATCHES] smbcontrol num-children

Christof Schmitt cs at samba.org
Thu Jan 2 12:14:07 MST 2014


This patch implements a new smbcontrol command. It returns the number of
child processes from the parent smbd process. We found that this is a
lightweight way of getting the current number of connections, especially
in large cluster environments.

Christof
-------------- next part --------------
>From a983f6af09742807381ef12f3d336a76fe34d5ce Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Mon, 6 May 2013 11:23:03 +0200
Subject: [PATCH 1/2] smbd: Make "num_children" available by smbcontrol

Reviewed-by: Christof Schmit <cs at samba.org>
---
 source3/librpc/idl/messaging.idl |    4 +++
 source3/smbd/process.c           |    2 +
 source3/smbd/server.c            |   16 ++++++++++++
 source3/utils/smbcontrol.c       |   49 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/source3/librpc/idl/messaging.idl b/source3/librpc/idl/messaging.idl
index 583eaf0..9405d53 100644
--- a/source3/librpc/idl/messaging.idl
+++ b/source3/librpc/idl/messaging.idl
@@ -92,6 +92,10 @@ interface messaging
 		/* shutdown connection for given client */
 		MSG_SMB_KILL_CLIENT_IP		= 0x0316,
 
+		/* Tell number of child processes */
+		MSG_SMB_TELL_NUM_CHILDREN       = 0x0317,
+		MSG_SMB_NUM_CHILDREN            = 0x0318,
+
 		/* winbind messages */
 		MSG_WINBIND_FINISHED		= 0x0401,
 		MSG_WINBIND_FORGET_STATE	= 0x0402,
diff --git a/source3/smbd/process.c b/source3/smbd/process.c
index 8bd1c2e..1f4cfe7 100644
--- a/source3/smbd/process.c
+++ b/source3/smbd/process.c
@@ -3562,6 +3562,8 @@ void smbd_process(struct tevent_context *ev_ctx,
 			   MSG_SMB_KILL_CLIENT_IP,
 			   msg_kill_client_ip);
 
+	messaging_deregister(sconn->msg_ctx, MSG_SMB_TELL_NUM_CHILDREN, NULL);
+
 	/*
 	 * Use the default MSG_DEBUG handler to avoid rebroadcasting
 	 * MSGs to all child processes
diff --git a/source3/smbd/server.c b/source3/smbd/server.c
index 99b0a10..feb47da 100644
--- a/source3/smbd/server.c
+++ b/source3/smbd/server.c
@@ -396,6 +396,20 @@ static void add_child_pid(struct smbd_parent_context *parent,
 	parent->num_children += 1;
 }
 
+static void smb_tell_num_children(struct messaging_context *ctx, void *data,
+				  uint32_t msg_type, struct server_id srv_id,
+				  DATA_BLOB *msg_data)
+{
+	uint8_t buf[sizeof(uint32_t)];
+
+	if (am_parent) {
+		SIVAL(buf, 0, am_parent->num_children);
+		messaging_send_buf(ctx, srv_id, MSG_SMB_NUM_CHILDREN,
+				   buf, sizeof(buf));
+	}
+}
+
+
 /*
   at most every smbd:cleanuptime seconds (default 20), we scan the BRL
   and locking database for entries to cleanup. As a side effect this
@@ -890,6 +904,8 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
 			   smb_parent_force_tdis);
 	messaging_register(msg_ctx, NULL, MSG_SMB_KILL_CLIENT_IP,
 			   smb_parent_kill_client_by_ip);
+	messaging_register(msg_ctx, NULL, MSG_SMB_TELL_NUM_CHILDREN,
+			   smb_tell_num_children);
 
 	messaging_register(msg_ctx, NULL,
 			   ID_CACHE_DELETE, smbd_parent_id_cache_delete);
diff --git a/source3/utils/smbcontrol.c b/source3/utils/smbcontrol.c
index ea1f609..579aa10 100644
--- a/source3/utils/smbcontrol.c
+++ b/source3/utils/smbcontrol.c
@@ -921,6 +921,53 @@ static bool do_dmalloc_changed(struct tevent_context *ev_ctx,
 			    NULL, 0);
 }
 
+static void print_uint32_cb(struct messaging_context *msg, void *private_data,
+			    uint32_t msg_type, struct server_id pid,
+			    DATA_BLOB *data)
+{
+	uint32_t num_children;
+
+	if (data->length != sizeof(uint32_t)) {
+		printf("Invalid response: %d bytes long\n",
+		       (int)data->length);
+		goto done;
+	}
+	num_children = IVAL(data->data, 0);
+	printf("%u children\n", (unsigned)num_children);
+done:
+	num_replies++;
+}
+
+static bool do_num_children(struct tevent_context *ev_ctx,
+			    struct messaging_context *msg_ctx,
+			    const struct server_id pid,
+			    const int argc, const char **argv)
+{
+	if (argc != 1) {
+		fprintf(stderr, "Usage: smbcontrol <dest> num-children\n");
+		return False;
+	}
+
+	messaging_register(msg_ctx, NULL, MSG_SMB_NUM_CHILDREN,
+			   print_uint32_cb);
+
+	/* Send a message and register our interest in a reply */
+
+	if (!send_message(msg_ctx, pid, MSG_SMB_TELL_NUM_CHILDREN, NULL, 0))
+		return false;
+
+	wait_replies(ev_ctx, msg_ctx, procid_to_pid(&pid) == 0);
+
+	/* No replies were received within the timeout period */
+
+	if (num_replies == 0)
+		printf("No replies received\n");
+
+	messaging_deregister(msg_ctx, MSG_SMB_TELL_NUM_CHILDREN, NULL);
+
+	return num_replies;
+}
+
 /* Shutdown a server process */
 
 static bool do_shutdown(struct tevent_context *ev_ctx,
@@ -1329,6 +1376,8 @@ static const struct {
 	  "Validate winbind's credential cache" },
 	{ "dump-domain-list", do_winbind_dump_domain_list, "Dump winbind domain list"},
 	{ "notify-cleanup", do_notify_cleanup },
+	{ "num-children", do_num_children,
+	  "Print number of smbd child processes" },
 	{ "noop", do_noop, "Do nothing" },
 	{ NULL }
 };
-- 
1.7.1


>From 97b98d5cb709dbdf34322724942b7d975b248c03 Mon Sep 17 00:00:00 2001
From: Christof Schmitt <cs at samba.org>
Date: Thu, 2 Jan 2014 10:49:26 -0700
Subject: [PATCH 2/2] docs: Add num-children to smbcontrol manpage

Signed-off-by: Christof Schmitt <cs at samba.org>
---
 docs-xml/manpages/smbcontrol.1.xml |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/docs-xml/manpages/smbcontrol.1.xml b/docs-xml/manpages/smbcontrol.1.xml
index 4c36d63..e8fef42 100644
--- a/docs-xml/manpages/smbcontrol.1.xml
+++ b/docs-xml/manpages/smbcontrol.1.xml
@@ -304,6 +304,13 @@
 	</variablelist>
 	</varlistentry>
 
+	<varlistentry>
+	<term>num-children</term>
+	<listitem><para>Query the number of smbd child processes. This
+	message can only be sent
+	to <constant>smbd</constant>.</para></listitem>
+	</varlistentry>
+
 </variablelist>
 </refsect1>
 
-- 
1.7.1



More information about the samba-technical mailing list