Skip to contentSkip to conversation

client: Extend client proxy object's API

PATCH Applied Tim Kourt 2 participants 7 messages Nov 8, 2019 – Nov 9, 2019 Kbuild

lore
Share & tools
lore

Tim Kourt

author
#0 lore raw

PATCH 1/5 client: Extend client proxy object's API

Add Start/Stop operations to the client proxy objects. The framework invokes these operations right after the proxy objects are created or just before the are destroyed, so proxy objects can perform the needed actions like start/stop a service, etc. if they choose to implement these ops.

---
 client/dbus-proxy.c | 16 ++++++++++++++++
 client/dbus-proxy.h |  2 ++
 2 files changed, 18 insertions(+)
client/dbus-proxy.c +16 -0
index 26c0511d..eb9812f7 100644
@@ -555,6 +555,19 @@ static void proxy_interface_create(const char *path,
 		}
 
 		l_queue_push_tail(proxy_interfaces, proxy);
+
+		if (!interface_type->ops || !interface_type->ops->start)
+			continue;
+
+		if (interface_type->ops->start(proxy))
+			continue;
+
+		if (!command_is_interactive_mode())
+			command_set_exit_status(EXIT_FAILURE);
+
+		l_main_quit();
+
+		return;
 	}
 }
 
@@ -562,6 +575,9 @@ static void proxy_interface_destroy(void *data)
 {
 	struct proxy_interface *proxy = data;
 
+	if (proxy->type->ops && proxy->type->ops->stop)
+		proxy->type->ops->stop(proxy);
+
 	l_free(proxy->path);
 
 	if (proxy->type->ops && proxy->type->ops->destroy)
client/dbus-proxy.h +2 -0
index a3304848..e6b6f8c0 100644
@@ -50,6 +50,8 @@ struct proxy_interface_property {
 struct proxy_interface_type_ops {
 	void *(*create)(void);
 	void (*destroy)(void *data);
+	bool (*start)(const struct proxy_interface *proxy);
+	void (*stop)(const struct proxy_interface *proxy);
 	const char *(*identity)(void *data);
 	void (*display)(const char *margin, const void *data);
 };
Signature · 1 line
2.13.6
author
#1 lore raw

PATCH 2/5 client: Add start/stop ops to agent manager proxy

This guarantees that the agent gets registered only when the agent manager interface is available on dbus.

---
 client/agent-manager.c | 51 +++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 44 insertions(+), 7 deletions(-)
client/agent-manager.c +44 -7
index b7d4271b..fb371ea2 100644
@@ -30,6 +30,7 @@
 #include "agent.h"
 #include "dbus-proxy.h"
 #include "agent-manager.h"
+#include "command.h"
 
 #define IWD_AGENT_MANAGER_PATH		"/net/connman/iwd"
 
@@ -39,6 +40,46 @@ static void check_errors_method_callback(struct l_dbus_message *message,
 	dbus_message_has_error(message);
 }
 
+static bool agent_manager_start(const struct proxy_interface *proxy)
+{
+	const char *path;
+
+	if (command_needs_no_agent())
+		return true;
+
+	path = proxy_interface_get_data(proxy);
+	if (!path)
+		return false;
+
+	if (!agent_init(path))
+		return false;
+
+	proxy_interface_method_call(proxy, "RegisterAgent", "o",
+					check_errors_method_callback, path);
+
+	return true;
+}
+
+static void agent_manager_stop(const struct proxy_interface *proxy)
+{
+	const char *path;
+
+	if (command_needs_no_agent())
+		return;
+
+	if (!proxy)
+		return;
+
+	path = proxy_interface_get_data(proxy);
+	if (!path)
+		return;
+
+	proxy_interface_method_call(proxy, "UnregisterAgent", "o",
+					check_errors_method_callback, path);
+
+	agent_exit(path);
+}
+
 bool agent_manager_register_agent(void)
 {
 	const char *path;
@@ -81,25 +122,21 @@ bool agent_manager_unregister_agent(void)
 
 static void *agent_manager_create(void)
 {
-	char *path = l_strdup_printf("/agent/%i", getpid());
-
-	agent_init(path);
-
-	return path;
+	return l_strdup_printf("/agent/%i", getpid());
 }
 
 static void agent_manager_destroy(void *data)
 {
 	char *path = data;
 
-	agent_exit(path);
-
 	l_free(path);
 }
 
 static const struct proxy_interface_type_ops agent_manager_ops = {
 	.create = agent_manager_create,
 	.destroy = agent_manager_destroy,
+	.start = agent_manager_start,
+	.stop = agent_manager_stop,
 };
 
 static struct proxy_interface_type agent_manager_interface_type = {
Signature · 1 line
2.13.6
#2 lore raw

Re: [PATCH 2/5] client: Add start/stop ops to agent manager proxy

Hi Tim,

Quoted 49 lines · Tim Kourt

This guarantees that the agent gets registered only when the agent manager interface is available on dbus.

---
  client/agent-manager.c | 51 +++++++++++++++++++++++++++++++++++++++++++-------
  1 file changed, 44 insertions(+), 7 deletions(-)
client/agent-manager.c +28 -0
index b7d4271b..fb371ea2 100644
@@ -30,6 +30,7 @@
  #include "agent.h"
  #include "dbus-proxy.h"
  #include "agent-manager.h"
+#include "command.h"
 
  #define IWD_AGENT_MANAGER_PATH		"/net/connman/iwd"
 
@@ -39,6 +40,46 @@ static void check_errors_method_callback(struct l_dbus_message *message,
  	dbus_message_has_error(message);
  }
 
+static bool agent_manager_start(const struct proxy_interface *proxy)
+{
+	const char *path;
+
+	if (command_needs_no_agent())
+		return true;
+
+	path = proxy_interface_get_data(proxy);
+	if (!path)
+		return false;
+
+	if (!agent_init(path))
+		return false;
+
+	proxy_interface_method_call(proxy, "RegisterAgent", "o",
+					check_errors_method_callback, path);
+
+	return true;
+}
+
+static void agent_manager_stop(const struct proxy_interface *proxy)
+{
+	const char *path;
+
+	if (command_needs_no_agent())
+		return;
+

client/agent-manager.c static void check_errors_method_callback(struct l_dbus_message *message, · line 79

Quoted 10 lines
client/agent-manager.c +10 -0
+	if (!proxy)
+		return;
+
+	path = proxy_interface_get_data(proxy);
+	if (!path)
+		return;
+
+	proxy_interface_method_call(proxy, "UnregisterAgent", "o",
+					check_errors_method_callback, path);
+

Trying to unregister the agent when the AgentManager interface goes away is rather pointless, no? Besides, you already have .Release that should have been called by now and no agent should be registered anyway.

Quoted 7 lines
client/agent-manager.c +3 -0
+	agent_exit(path);
+}
+
  bool agent_manager_register_agent(void)
  {
  	const char *path;

client/agent-manager.c bool agent_manager_unregister_agent(void) · line 125

Quoted 9 lines
client/agent-manager.c +1 -5
@@ -81,25 +122,21 @@ bool agent_manager_unregister_agent(void)
 
  static void *agent_manager_create(void)
  {
-	char *path = l_strdup_printf("/agent/%i", getpid());
-
-	agent_init(path);
-
-	return path;
+	return l_strdup_printf("/agent/%i", getpid());

So why do you need start/stop at all? Can't you just do the ops as part of the create/destroy?

Quoted 10 lines
client/agent-manager.c +0 -2
  }
 
  static void agent_manager_destroy(void *data)
  {
  	char *path = data;
 
-	agent_exit(path);
-
  	l_free(path);

client/agent-manager.c bool agent_manager_unregister_agent(void) · line 142

Quoted 10 lines
client/agent-manager.c +2 -0
  }
 
  static const struct proxy_interface_type_ops agent_manager_ops = {
  	.create = agent_manager_create,
  	.destroy = agent_manager_destroy,
+	.start = agent_manager_start,
+	.stop = agent_manager_stop,
  };
 
  static struct proxy_interface_type agent_manager_interface_type = {

Regards,
-Denis

author
#3 lore raw

PATCH 3/5 client: Remove explicit agent registartion from framework

In reply to Tim Kourt

This removes the no longer needed APIs from agent-manager.

---
 Makefile.am            |  4 ++--
 client/agent-manager.c | 41 -----------------------------------------
 client/agent-manager.h | 24 ------------------------
 client/dbus-proxy.c    | 19 -------------------
 4 files changed, 2 insertions(+), 86 deletions(-)
 delete mode 100644 client/agent-manager.h
Makefile.am +2 -2
index d6c6ce29..eda16b67 100644
@@ -267,7 +267,7 @@ bin_PROGRAMS += client/iwctl
 client_iwctl_SOURCES = client/main.c \
 			client/adapter.c \
 			client/agent.h client/agent.c \
-			client/agent-manager.h client/agent-manager.c \
+			client/agent-manager.c \
 			client/ad-hoc.c \
 			client/ap.c \
 			client/command.h client/command.c \
@@ -486,7 +486,7 @@ if CLIENT
 unit_test_client_SOURCES = unit/test-client.c \
 				client/adapter.c \
 				client/agent.h client/agent.c \
-				client/agent-manager.h client/agent-manager.c \
+				client/agent-manager.c \
 				client/command.h client/command.c \
 				client/dbus-proxy.h client/dbus-proxy.c \
 				client/display.h client/display.c \
client/agent-manager.c +0 -41

53 lines not on this page · raw message

client/agent-manager.h deleted +0 -24
deleted file mode 100644
index c0433f53..00000000
@@ -1,24 +0,0 @@
-/*
- *
- *  Wireless daemon for Linux
- *
- *  Copyright (C) 2017-2019  Intel Corporation. All rights reserved.
- *
- *  This library is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU Lesser General Public
- *  License as published by the Free Software Foundation; either
- *  version 2.1 of the License, or (at your option) any later version.
- *
- *  This library 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
- *  Lesser General Public License for more details.
- *
- *  You should have received a copy of the GNU Lesser General Public
- *  License along with this library; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- */
-
-bool agent_manager_register_agent(void);
-bool agent_manager_unregister_agent(void);
client/dbus-proxy.c +0 -19
index eb9812f7..dae5f336 100644
@@ -27,7 +27,6 @@
 #include <stdio.h>
 #include <ell/ell.h>
 
-#include "agent-manager.h"
 #include "dbus-proxy.h"
 #include "display.h"
 #include "command.h"
@@ -732,21 +731,6 @@ static void get_managed_objects_callback(struct l_dbus_message *message,
 	while (l_dbus_message_iter_next_entry(&objects, &path, &object))
 		proxy_interface_create(path, &object);
 
-	if (command_needs_no_agent())
-		goto no_agent;
-
-	if (!agent_manager_register_agent()) {
-		display_error("Failed to register Agent.\n");
-
-		if (!command_is_interactive_mode())
-			command_set_exit_status(EXIT_FAILURE);
-
-		l_main_quit();
-
-		return;
-	}
-
-no_agent:
 	if (!command_is_interactive_mode()) {
 		command_noninteractive_trigger();
 
@@ -861,9 +845,6 @@ bool dbus_proxy_exit(void)
 {
 	struct interface_type_desc *desc;
 
-	if (!command_needs_no_agent())
-		agent_manager_unregister_agent();
-
 	for (desc = __start___interface; desc < __stop___interface; desc++) {
 		if (!desc->exit)
 			continue;
Signature · 1 line
2.13.6
author
#4 lore raw

PATCH 4/5 client: Use full include path for local includes

In reply to Tim Kourt
---
 client/ad-hoc.c         |  8 ++++----
 client/adapter.c        |  8 ++++----
 client/agent-manager.c  |  6 +++---
 client/agent.c          |  8 ++++----
 client/ap.c             |  8 ++++----
 client/command.c        |  4 ++--
 client/dbus-proxy.c     |  8 ++++----
 client/device.c         | 12 ++++++------
 client/display.c        |  6 +++---
 client/known-networks.c | 10 +++++-----
 client/main.c           |  6 +++---
 client/network.c        |  6 +++---
 client/properties.c     |  2 +-
 client/station.c        | 10 +++++-----
 client/wsc.c            |  8 ++++----
 15 files changed, 55 insertions(+), 55 deletions(-)
client/ad-hoc.c +4 -4
index 9fba9c96..41d5e2aa 100644
@@ -26,10 +26,10 @@
 
 #include <ell/ell.h>
 
-#include "command.h"
-#include "dbus-proxy.h"
-#include "device.h"
-#include "display.h"
+#include "client/command.h"
+#include "client/dbus-proxy.h"
+#include "client/device.h"
+#include "client/display.h"
 
 struct ad_hoc {
 	bool started;
client/adapter.c +4 -4
index e815a39b..6a0d3455 100644
@@ -26,10 +26,10 @@
 
 #include <ell/ell.h>
 
-#include "command.h"
-#include "dbus-proxy.h"
-#include "display.h"
-#include "properties.h"
+#include "client/command.h"
+#include "client/dbus-proxy.h"
+#include "client/display.h"
+#include "client/properties.h"
 
 struct adapter {
 	bool powered;
client/agent-manager.c +3 -3
index 98a78436..91cde8c4 100644
@@ -27,9 +27,9 @@
 #include <ell/ell.h>
 #include <unistd.h>
 
-#include "agent.h"
-#include "dbus-proxy.h"
-#include "command.h"
+#include "client/agent.h"
+#include "client/dbus-proxy.h"
+#include "client/command.h"
 
 #define IWD_AGENT_MANAGER_PATH		"/net/connman/iwd"
 
client/agent.c +4 -4
index 16bc3c4f..b804b50d 100644
@@ -26,10 +26,10 @@
 
 #include <ell/ell.h>
 
-#include "agent.h"
-#include "dbus-proxy.h"
-#include "display.h"
-#include "command.h"
+#include "client/agent.h"
+#include "client/dbus-proxy.h"
+#include "client/display.h"
+#include "client/command.h"
 
 #define IWD_AGENT_INTERFACE "net.connman.iwd.Agent"
 
client/ap.c +4 -4
index bf55d07f..a6a2681b 100644
@@ -26,10 +26,10 @@
 
 #include <ell/ell.h>
 
-#include "command.h"
-#include "dbus-proxy.h"
-#include "device.h"
-#include "display.h"
+#include "client/command.h"
+#include "client/dbus-proxy.h"
+#include "client/device.h"
+#include "client/display.h"
 
 struct ap {
 	bool started;
client/command.c +2 -2
index 03a61de3..188596d1 100644
@@ -31,8 +31,8 @@
 #include <ell/ell.h>
 #include <readline/readline.h>
 
-#include "command.h"
-#include "display.h"
+#include "client/command.h"
+#include "client/display.h"
 
 static struct l_queue *command_families;
 static struct l_queue *command_options;
client/dbus-proxy.c +4 -4
index dae5f336..37f0e75c 100644
@@ -27,10 +27,10 @@
 #include <stdio.h>
 #include <ell/ell.h>
 
-#include "dbus-proxy.h"
-#include "display.h"
-#include "command.h"
-#include "properties.h"
+#include "client/dbus-proxy.h"
+#include "client/display.h"
+#include "client/command.h"
+#include "client/properties.h"
 
 #define IWD_SERVICE		"net.connman.iwd"
 #define IWD_ROOT_PATH		"/"
client/device.c +6 -6
index ef109c84..5b971883 100644
@@ -26,12 +26,12 @@
 
 #include <ell/ell.h>
 
-#include "command.h"
-#include "dbus-proxy.h"
-#include "device.h"
-#include "display.h"
-#include "network.h"
-#include "properties.h"
+#include "client/command.h"
+#include "client/dbus-proxy.h"
+#include "client/device.h"
+#include "client/display.h"
+#include "client/network.h"
+#include "client/properties.h"
 
 struct device {
 	bool powered;
client/display.c +3 -3
index 10c87b2a..349eb503 100644
@@ -32,9 +32,9 @@
 #include <readline/readline.h>
 #include <ell/ell.h>
 
-#include "agent.h"
-#include "command.h"
-#include "display.h"
+#include "client/agent.h"
+#include "client/command.h"
+#include "client/display.h"
 
 #define IWD_PROMPT COLOR_GREEN "[iwd]" COLOR_OFF "# "
 #define LINE_LEN 81
client/known-networks.c +5 -5
index 7cfc2ecf..45f60af2 100644
@@ -29,11 +29,11 @@
 #include <time.h>
 #include <ell/ell.h>
 
-#include "command.h"
-#include "dbus-proxy.h"
-#include "display.h"
-#include "properties.h"
-#include "network.h"
+#include "client/command.h"
+#include "client/dbus-proxy.h"
+#include "client/display.h"
+#include "client/properties.h"
+#include "client/network.h"
 
 struct known_network {
 	char *identity;
client/main.c +3 -3
index 54d4cb31..8ca45a15 100644
@@ -28,9 +28,9 @@
 #include <signal.h>
 #include <ell/ell.h>
 
-#include "command.h"
-#include "display.h"
-#include "dbus-proxy.h"
+#include "client/command.h"
+#include "client/display.h"
+#include "client/dbus-proxy.h"
 
 static void signal_handler(uint32_t signo, void *user_data)
 {
client/network.c +3 -3
index fdeeef67..6b79bcd1 100644
@@ -26,9 +26,9 @@
 
 #include <ell/ell.h>
 
-#include "dbus-proxy.h"
-#include "display.h"
-#include "network.h"
+#include "client/dbus-proxy.h"
+#include "client/display.h"
+#include "client/network.h"
 
 struct network {
 	bool connected;
client/properties.c +1 -1
index 8838f4f0..dadf660d 100644
@@ -26,7 +26,7 @@
 
 #include <ell/ell.h>
 
-#include "properties.h"
+#include "client/properties.h"
 
 const char *properties_on_off_opts[3] = { "on", "off", NULL };
 const char *properties_yes_no_opts[3] = { "yes", "no", NULL };
client/station.c +5 -5
index dbcfa88f..b6b59239 100644
@@ -26,11 +26,11 @@
 
 #include <ell/ell.h>
 
-#include "command.h"
-#include "dbus-proxy.h"
-#include "device.h"
-#include "network.h"
-#include "display.h"
+#include "client/command.h"
+#include "client/dbus-proxy.h"
+#include "client/device.h"
+#include "client/network.h"
+#include "client/display.h"
 
 struct station {
 	bool scanning;
client/wsc.c +4 -4
index ffbb54aa..39950453 100644
@@ -26,10 +26,10 @@
 
 #include <ell/ell.h>
 
-#include "command.h"
-#include "dbus-proxy.h"
-#include "device.h"
-#include "display.h"
+#include "client/command.h"
+#include "client/dbus-proxy.h"
+#include "client/device.h"
+#include "client/display.h"
 
 static struct proxy_interface_type wsc_interface_type = {
 	.interface = IWD_WSC_INTERFACE,
Signature · 1 line
2.13.6
author
#5 lore raw

PATCH 5/5 client: Fail in non-interactive mode if iwd service isn’t running

In reply to Tim Kourt

In non-interactive mode request the managed object right away and do not wait for the service to appear. This way client can fail right away instead of endlessly waiting in non-interactive mode.

---
 client/dbus-proxy.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)
client/dbus-proxy.c +16 -10
index 37f0e75c..313cd5a6 100644
@@ -740,11 +740,17 @@ static void get_managed_objects_callback(struct l_dbus_message *message,
 	display_enable_cmd_prompt();
 }
 
-static void service_appeared_callback(struct l_dbus *dbus, void *user_data)
+static void get_managed_objects(void)
 {
-	if (!command_is_interactive_mode())
-		goto get_objects;
+	l_dbus_method_call(dbus, IWD_SERVICE, IWD_ROOT_PATH,
+					L_DBUS_INTERFACE_OBJECT_MANAGER,
+					"GetManagedObjects", NULL,
+					get_managed_objects_callback,
+					NULL, NULL);
+}
 
+static void service_appeared_callback(struct l_dbus *dbus, void *user_data)
+{
 	l_dbus_add_signal_watch(dbus, IWD_SERVICE, IWD_ROOT_PATH,
 					L_DBUS_INTERFACE_OBJECT_MANAGER,
 					"InterfacesAdded", L_DBUS_MATCH_NONE,
@@ -759,12 +765,8 @@ static void service_appeared_callback(struct l_dbus *dbus, void *user_data)
 					L_DBUS_INTERFACE_PROPERTIES,
 					"PropertiesChanged", L_DBUS_MATCH_NONE,
 					properties_changed_callback, NULL);
-get_objects:
-	l_dbus_method_call(dbus, IWD_SERVICE, IWD_ROOT_PATH,
-					L_DBUS_INTERFACE_OBJECT_MANAGER,
-					"GetManagedObjects", NULL,
-					get_managed_objects_callback,
-					NULL, NULL);
+
+	get_managed_objects();
 }
 
 static void service_disappeared_callback(struct l_dbus *dbus,
@@ -834,9 +836,13 @@ bool dbus_proxy_init(void)
 	l_dbus_set_disconnect_handler(dbus, dbus_disconnect_callback, NULL,
 									NULL);
 
-	l_dbus_add_service_watch(dbus, IWD_SERVICE, service_appeared_callback,
+	if (command_is_interactive_mode())
+		l_dbus_add_service_watch(dbus, IWD_SERVICE,
+						service_appeared_callback,
 						service_disappeared_callback,
 						NULL, NULL);
+	else
+		get_managed_objects();
 
 	return true;
 }
Signature · 1 line
2.13.6
#6 lore raw

Re: [PATCH 5/5] client: Fail in non-interactive mode if iwd service isn’t running

Hi Tim,

Quoted 7 lines · Tim Kourt

In non-interactive mode request the managed object right away and do not wait for the service to appear. This way client can fail right away instead of endlessly waiting in non-interactive mode.

---
  client/dbus-proxy.c | 26 ++++++++++++++++----------
  1 file changed, 16 insertions(+), 10 deletions(-)

Applied, thanks.

Regards,
-Denis