1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
23 #include <systemd/sd-bus.h>
24 #include <systemd/sd-event.h>
28 #include "daemon-bus.h"
31 static int nw_bus_on_connect(sd_bus_message
* m
, void* data
, sd_bus_error
* error
) {
32 nw_daemon
* daemon
= (nw_daemon
*)data
;
34 DEBUG("Connected to D-Bus\n");
39 int nw_bus_connect(sd_bus
** bus
, sd_event
* loop
, nw_daemon
* daemon
) {
43 // Create a bus object
46 ERROR("Could not allocate a bus object: %s\n", strerror(-r
));
51 r
= sd_bus_set_description(b
, NETWORKD_BUS_DESCRIPTION
);
53 ERROR("Could not set bus description: %s\n", strerror(-r
));
57 const char* address
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
59 address
= DEFAULT_SYSTEM_BUS_ADDRESS
;
62 r
= sd_bus_set_address(b
, address
);
64 ERROR("Could not set bus address: %s\n", strerror(-r
));
69 r
= sd_bus_set_bus_client(b
, 1);
71 ERROR("Could not set bus client: %s\n", strerror(-r
));
75 // Request some credentials for all messages
76 r
= sd_bus_negotiate_creds(b
, 1,
77 SD_BUS_CREDS_UID
|SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
);
79 ERROR("Could not negotiate creds: %s\n", strerror(-r
));
83 // Automatically bind when the socket is available
84 r
= sd_bus_set_watch_bind(b
, 1);
86 ERROR("Could not watch socket: %s\n", strerror(-r
));
90 // Emit a connected signal when we are connected
91 r
= sd_bus_set_connected_signal(b
, 1);
93 ERROR("Could not enable sending a connect signal: %s\n", strerror(-r
));
100 ERROR("Could not connect to bus: %s\n", strerror(-r
));
104 // Register the implementation
105 r
= nw_bus_register_implementation(b
, &daemon_bus_impl
, daemon
);
109 // Request interface name
110 r
= sd_bus_request_name_async(b
, NULL
, "org.ipfire.network1", 0, NULL
, NULL
);
112 ERROR("Could not request bus name: %s\n", strerror(-r
));
116 // Attach the event loop
117 r
= sd_bus_attach_event(b
, loop
, 0);
119 ERROR("Could not attach bus to event loop: %s\n", strerror(-r
));
123 // Request receiving a connect signal
124 r
= sd_bus_match_signal_async(b
, NULL
, "org.freedesktop.DBus.Local",
125 NULL
, "org.freedesktop.DBus.Local", "Connected", nw_bus_on_connect
, NULL
, NULL
);
127 ERROR("Could not request match on Connected signal: %s\n", strerror(-r
));
137 int nw_bus_register_implementation(sd_bus
* bus
,
138 const struct nw_bus_implementation
* impl
, void* data
) {
141 DEBUG("Registering bus object implementation for path=%s iface=%s\n",
142 impl
->path
, impl
->interface
);
145 for (const sd_bus_vtable
** vtable
= impl
->vtables
; vtable
&& *vtable
; vtable
++) {
146 r
= sd_bus_add_object_vtable(bus
, NULL
, impl
->path
, impl
->interface
, *vtable
, data
);
148 ERROR("Could not register bus path %s with interface %s: %m\n",
149 impl
->path
, impl
->interface
);
154 // Register fallback vtables
155 for (const struct nw_bus_vtable_pair
* p
= impl
->fallback_vtables
; p
&& p
->vtable
; p
++) {
156 r
= sd_bus_add_fallback_vtable(bus
, NULL
, impl
->path
, impl
->interface
,
157 p
->vtable
, p
->object_find
, data
);
159 ERROR("Could not register bus path %s with interface %s: %m\n",
160 impl
->path
, impl
->interface
);
165 // Register the node enumerator
166 if (impl
->node_enumerator
) {
167 r
= sd_bus_add_node_enumerator(bus
, NULL
, impl
->path
, impl
->node_enumerator
, data
);
169 ERROR("Could not add the node enumerator for %s: %m\n", impl
->path
);
174 // Register any child implementations
175 for (int i
= 0; impl
->children
&& impl
->children
[i
]; i
++) {
176 r
= nw_bus_register_implementation(bus
, impl
->children
[i
], data
);