[systemd-devel] [RFC 5/6] proxy-discoveryd: Add the basic parts for handling DBus methods

Tomasz Bursztyka tomasz.bursztyka at linux.intel.com
Fri Apr 10 05:17:42 PDT 2015


It currently exposes a simple method for requesting a proxy from an url
and its host.
---
 Makefile.am                                        |  1 +
 .../org.freedesktop.proxydiscovery1.conf           | 46 +++++++++++++++++++++
 .../org.freedesktop.proxydiscovery1.service        | 12 ++++++
 src/proxy-discovery/proxy-discoveryd-bus.c         | 48 ++++++++++++++++++++++
 src/proxy-discovery/proxy-discoveryd-manager.c     | 45 ++++++++++++++++++++
 src/proxy-discovery/proxy-discoveryd.c             |  6 +++
 src/proxy-discovery/proxy-discoveryd.h             |  4 ++
 units/org.freedesktop.proxydiscovery1.busname      | 15 +++++++
 8 files changed, 177 insertions(+)
 create mode 100644 src/proxy-discovery/org.freedesktop.proxydiscovery1.conf
 create mode 100644 src/proxy-discovery/org.freedesktop.proxydiscovery1.service
 create mode 100644 src/proxy-discovery/proxy-discoveryd-bus.c
 create mode 100644 units/org.freedesktop.proxydiscovery1.busname

diff --git a/Makefile.am b/Makefile.am
index 25ea0dd..385c92c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5899,6 +5899,7 @@ systemd_proxy_discoveryd_SOURCES = \
 	src/proxy-discovery/duktape.c \
 	src/proxy-discovery/proxy-discoveryd.c \
 	src/proxy-discovery/proxy-discoveryd.h \
+	src/proxy-discovery/proxy-discoveryd-bus.c \
 	src/proxy-discovery/proxy-discoveryd-manager.c \
 	src/proxy-discovery/proxy-discoveryd-pac.c \
 	src/proxy-discovery/proxy-discoveryd-proxy.c \
diff --git a/src/proxy-discovery/org.freedesktop.proxydiscovery1.conf b/src/proxy-discovery/org.freedesktop.proxydiscovery1.conf
new file mode 100644
index 0000000..110c114
--- /dev/null
+++ b/src/proxy-discovery/org.freedesktop.proxydiscovery1.conf
@@ -0,0 +1,46 @@
+<?xml version="1.0"?> <!--*-nxml-*-->
+<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+        "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+
+<!--
+  This file is part of systemd.
+
+  systemd 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.
+-->
+
+<busconfig>
+
+        <policy user="root">
+                <allow own="org.freedesktop.proxydiscovery1"/>
+                <allow send_destination="org.freedesktop.proxydiscovery1"/>
+                <allow receive_sender="org.freedesktop.proxydiscovery1"/>
+        </policy>
+
+        <policy context="default">
+		<deny send_destination="org.freedesktop.proxydiscovery1"/>
+
+                <allow send_destination="org.freedesktop.proxydiscovery1"
+                       send_interface="org.freedesktop.DBus.Introspectable"/>
+
+                <allow send_destination="org.freedesktop.proxydiscovery1"
+                       send_interface="org.freedesktop.DBus.Peer"/>
+
+                <allow send_destination="org.freedesktop.proxydiscovery1"
+                       send_interface="org.freedesktop.DBus.Properties"
+                       send_member="Get"/>
+
+                <allow send_destination="org.freedesktop.proxydiscovery1"
+                       send_interface="org.freedesktop.DBus.Properties"
+                       send_member="GetAll"/>
+
+                <allow send_destination="org.freedesktop.proxydiscovery1"
+                       send_interface="org.freedesktop.proxydiscovery1.Manager"
+                       send_member="FindProxy"/>
+
+                <allow receive_sender="org.freedesktop.proxydiscovery1"/>
+        </policy>
+
+</busconfig>
diff --git a/src/proxy-discovery/org.freedesktop.proxydiscovery1.service b/src/proxy-discovery/org.freedesktop.proxydiscovery1.service
new file mode 100644
index 0000000..d438381
--- /dev/null
+++ b/src/proxy-discovery/org.freedesktop.proxydiscovery1.service
@@ -0,0 +1,12 @@
+#  This file is part of systemd.
+#
+#  systemd 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.
+
+[D-BUS Service]
+Name=org.freedesktop.proxydiscovery1
+Exec=/bin/false
+User=root
+SystemdService=dbus-org.freedesktop.proxydiscovery1.service
diff --git a/src/proxy-discovery/proxy-discoveryd-bus.c b/src/proxy-discovery/proxy-discoveryd-bus.c
new file mode 100644
index 0000000..b917f38
--- /dev/null
+++ b/src/proxy-discovery/proxy-discoveryd-bus.c
@@ -0,0 +1,48 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright (C) 2015 Intel Corporation. All rights reserved.
+
+ systemd 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.
+
+ systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "bus-util.h"
+
+#include "proxy-discoveryd.h"
+
+static int method_find_proxy(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+        _cleanup_free_ char *p = strdup("DIRECT");
+        Manager *m = userdata;
+        int r;
+
+        assert(bus);
+        assert(message);
+        assert(m);
+
+        r = proxy_execute(m->default_proxies, message);
+        if (r < 0)
+                sd_bus_reply_method_return(message, "s", p);
+
+        return 1;
+}
+
+const sd_bus_vtable manager_vtable[] = {
+        SD_BUS_VTABLE_START(0),
+
+        SD_BUS_METHOD("FindProxy", "ss", "s", method_find_proxy, SD_BUS_VTABLE_UNPRIVILEGED),
+
+        SD_BUS_VTABLE_END
+};
diff --git a/src/proxy-discovery/proxy-discoveryd-manager.c b/src/proxy-discovery/proxy-discoveryd-manager.c
index 62fcc23..e5aef38 100644
--- a/src/proxy-discovery/proxy-discoveryd-manager.c
+++ b/src/proxy-discovery/proxy-discoveryd-manager.c
@@ -19,6 +19,8 @@
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include "bus-util.h"
+#include "bus-error.h"
 #include "conf-parser.h"
 #include "conf-files.h"
 #include "strv.h"
@@ -53,6 +55,10 @@ int manager_new(Manager **ret) {
         if (r < 0)
                 return r;
 
+        r = sd_bus_default_system(&m->bus);
+        if (r < 0 && r != -ENOENT) /* TODO: drop when we can rely on kdbus */
+                return r;
+
         LIST_HEAD_INIT(m->default_proxies);
 
         *ret = m;
@@ -68,6 +74,7 @@ Manager *manager_free(Manager *m) {
                 return NULL;
 
         sd_event_unref(m->event);
+        sd_bus_unref(m->bus);
 
         while((proxy = m->default_proxies))
                 proxy_free(proxy);
@@ -77,6 +84,44 @@ Manager *manager_free(Manager *m) {
         return NULL;
 }
 
+int manager_bus_listen(Manager *m) {
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        int r;
+
+        assert(m);
+        assert(m->event);
+
+        if (!m->bus) /* TODO: drop when we can rely on kdbus */
+                return 0;
+
+        r = sd_bus_add_object_vtable(m->bus, NULL,
+                                     "/org/freedesktop/proxydiscovery1",
+                                     "org.freedesktop.proxydiscovery1.Manager",
+                                     manager_vtable, m);
+        if (r < 0)
+                return log_error_errno(r, "Failed to add manager object vtable: %m");
+
+        r = sd_bus_call_method(m->bus,
+                               "org.freedesktop.systemd1",
+                               "/org/freedesktop/systemd1",
+                               "org.freedesktop.systemd1.Manager",
+                               "Subscribe", &error, NULL, NULL);
+        if (r < 0) {
+                log_error("Failed to enable subscription: %s", bus_error_message(&error, r));
+                return r;
+        }
+
+        r = sd_bus_request_name(m->bus, "org.freedesktop.proxydiscovery1", 0);
+        if (r < 0)
+                return log_error_errno(r, "Failed to register name: %m");
+
+        r = sd_bus_attach_event(m->bus, m->event, 0);
+        if (r < 0)
+                return log_error_errno(r, "Failed to attach bus to event loop: %m");
+
+        return 0;
+}
+
 int manager_load_config(Manager *m) {
         _cleanup_strv_free_ char **files = NULL;
         Proxy *proxy;
diff --git a/src/proxy-discovery/proxy-discoveryd.c b/src/proxy-discovery/proxy-discoveryd.c
index b4789d1..10af22f 100644
--- a/src/proxy-discovery/proxy-discoveryd.c
+++ b/src/proxy-discovery/proxy-discoveryd.c
@@ -44,6 +44,12 @@ int main(int argc, char *argv[]) {
                 goto out;
         }
 
+        r = manager_bus_listen(m);
+        if (r < 0) {
+                log_error_errno(r, "Could not listed to system bus: %m");
+                goto out;
+        }
+
         r = manager_load_config(m);
         if (r < 0) {
                 log_error_errno(r, "Could not load configuration files: %m");
diff --git a/src/proxy-discovery/proxy-discoveryd.h b/src/proxy-discovery/proxy-discoveryd.h
index d8d9482..c97afad 100644
--- a/src/proxy-discovery/proxy-discoveryd.h
+++ b/src/proxy-discovery/proxy-discoveryd.h
@@ -32,6 +32,7 @@ typedef struct Proxy Proxy;
 
 struct Manager {
         sd_event *event;
+        sd_bus *bus;
 
         LIST_HEAD(Proxy, default_proxies);
 };
@@ -52,8 +53,11 @@ struct Proxy {
 int manager_new(Manager **ret);
 Manager *manager_free(Manager *m);
 
+int manager_bus_listen(Manager *m);
 int manager_load_config(Manager *m);
 
+extern const sd_bus_vtable manager_vtable[];
+
 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
 #define _cleanup_manager_free_ _cleanup_(manager_freep)
 
diff --git a/units/org.freedesktop.proxydiscovery1.busname b/units/org.freedesktop.proxydiscovery1.busname
new file mode 100644
index 0000000..c6cfc8a
--- /dev/null
+++ b/units/org.freedesktop.proxydiscovery1.busname
@@ -0,0 +1,15 @@
+#  This file is part of systemd.
+#
+#  systemd 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.
+
+[Unit]
+Description=Proxy Discovery Service Bus Name
+#Documentation=man:systemd-proxy-discoveryd.service(8)
+#Documentation=http://www.freedesktop.org/wiki/Software/systemd/proxy-discoveryd
+
+[BusName]
+Service=systemd-proxy-discoveryd.service
+AllowWorld=talk
-- 
2.0.5



More information about the systemd-devel mailing list