]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/networkd/bus.c
networkd: daemon: Correctly store reference to bus
[people/ms/network.git] / src / networkd / bus.c
index 1daa0352604c7af5aecb8bee32ed2f70dd77cf44..8158c84f89edf8f591b553c7e9b6818fca66ecd6 100644 (file)
@@ -36,18 +36,19 @@ static int nw_bus_on_connect(sd_bus_message* m, void* data, sd_bus_error* error)
        return 0;
 }
 
-int nw_bus_connect(sd_bus* bus, sd_event* loop, nw_daemon* daemon) {
+int nw_bus_connect(sd_bus** bus, sd_event* loop, nw_daemon* daemon) {
+       sd_bus* b = NULL;
        int r;
 
        // Create a bus object
-       r = sd_bus_new(&bus);
+       r = sd_bus_new(&b);
        if (r < 0) {
                ERROR("Could not allocate a bus object: %s\n", strerror(-r));
                return 1;
        }
 
        // Set description
-       r = sd_bus_set_description(bus, NETWORKD_BUS_DESCRIPTION);
+       r = sd_bus_set_description(b, NETWORKD_BUS_DESCRIPTION);
        if (r < 0) {
                ERROR("Could not set bus description: %s\n", strerror(-r));
                return 1;
@@ -58,21 +59,21 @@ int nw_bus_connect(sd_bus* bus, sd_event* loop, nw_daemon* daemon) {
                address = DEFAULT_SYSTEM_BUS_ADDRESS;
 
        // Set bus address
-       r = sd_bus_set_address(bus, address);
+       r = sd_bus_set_address(b, address);
        if (r < 0) {
                ERROR("Could not set bus address: %s\n", strerror(-r));
                return 1;
        }
 
        // Set bus client
-       r = sd_bus_set_bus_client(bus, 1);
+       r = sd_bus_set_bus_client(b, 1);
        if (r < 0) {
                ERROR("Could not set bus client: %s\n", strerror(-r));
                return 1;
        }
 
        // Request some credentials for all messages
-       r = sd_bus_negotiate_creds(bus, 1,
+       r = sd_bus_negotiate_creds(b, 1,
                        SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
        if (r < 0) {
                ERROR("Could not negotiate creds: %s\n", strerror(-r));
@@ -80,53 +81,56 @@ int nw_bus_connect(sd_bus* bus, sd_event* loop, nw_daemon* daemon) {
        }
 
        // Automatically bind when the socket is available
-       r = sd_bus_set_watch_bind(bus, 1);
+       r = sd_bus_set_watch_bind(b, 1);
        if (r < 0) {
                ERROR("Could not watch socket: %s\n", strerror(-r));
                return 1;
        }
 
        // Emit a connected signal when we are connected
-       r = sd_bus_set_connected_signal(bus, 1);
+       r = sd_bus_set_connected_signal(b, 1);
        if (r < 0) {
                ERROR("Could not enable sending a connect signal: %s\n", strerror(-r));
                return 1;
        }
 
        // Connect to the bus
-       r = sd_bus_start(bus);
+       r = sd_bus_start(b);
        if (r < 0) {
                ERROR("Could not connect to bus: %s\n", strerror(-r));
                return 1;
        }
 
        // Register the implementation
-       r = nw_bus_register_implementation(bus, &daemon_bus_impl, daemon);
+       r = nw_bus_register_implementation(b, &daemon_bus_impl, daemon);
        if (r)
                return r;
 
        // Request interface name
-       r = sd_bus_request_name_async(bus, NULL, "org.ipfire.network1", 0, NULL, NULL);
+       r = sd_bus_request_name_async(b, NULL, "org.ipfire.network1", 0, NULL, NULL);
        if (r < 0) {
                ERROR("Could not request bus name: %s\n", strerror(-r));
                return 1;
        }
 
        // Attach the event loop
-       r = sd_bus_attach_event(bus, loop, 0);
+       r = sd_bus_attach_event(b, loop, 0);
        if (r < 0) {
                ERROR("Could not attach bus to event loop: %s\n", strerror(-r));
                return 1;
        }
 
        // Request receiving a connect signal
-       r = sd_bus_match_signal_async(bus, NULL, "org.freedesktop.DBus.Local",
+       r = sd_bus_match_signal_async(b, NULL, "org.freedesktop.DBus.Local",
                NULL, "org.freedesktop.DBus.Local", "Connected", nw_bus_on_connect, NULL, NULL);
        if (r < 0) {
                ERROR("Could not request match on Connected signal: %s\n", strerror(-r));
                return 1;
        }
 
+       // Return reference
+       *bus = b;
+
        return 0;
 }