]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/core/dbus-job.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / core / dbus-job.c
index bdb9096e42877023ced3a597e137943fd1100583..5551c56d0e9fd0d638c10068125b6d2402658924 100644 (file)
@@ -1,30 +1,14 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
-/***
-  This file is part of systemd.
-
-  Copyright 2010 Lennart Poettering
-
-  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 "sd-bus.h"
 
+#include "alloc-util.h"
+#include "dbus-job.h"
+#include "dbus.h"
+#include "job.h"
 #include "log.h"
-#include "sd-bus.h"
 #include "selinux-access.h"
-#include "job.h"
-#include "dbus-job.h"
-#include "dbus-client-track.h"
+#include "string-util.h"
 
 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, job_type, JobType);
 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_state, job_state, JobState);
@@ -35,8 +19,8 @@ static int property_get_unit(
                 const char *interface,
                 const char *property,
                 sd_bus_message *reply,
-                sd_bus_error *error,
-                void *userdata) {
+                void *userdata,
+                sd_bus_error *error) {
 
         _cleanup_free_ char *p = NULL;
         Job *j = userdata;
@@ -52,71 +36,99 @@ static int property_get_unit(
         return sd_bus_message_append(reply, "(so)", j->unit->id, p);
 }
 
-static int method_cancel(sd_bus *bus, sd_bus_message *message, void *userdata) {
+int bus_job_method_cancel(sd_bus_message *message, void *userdata, sd_bus_error *error) {
         Job *j = userdata;
+        int r;
 
-        assert(bus);
         assert(message);
         assert(j);
 
-        SELINUX_UNIT_ACCESS_CHECK(j->unit, bus, message, "stop");
-        job_finish_and_invalidate(j, JOB_CANCELED, true);
+        r = mac_selinux_unit_access_check(j->unit, message, "stop", error);
+        if (r < 0)
+                return r;
+
+        /* Access is granted to the job owner */
+        if (!sd_bus_track_contains(j->bus_track, sd_bus_message_get_sender(message))) {
+
+                /* And for everybody else consult PolicyKit */
+                r = bus_verify_manage_units_async(j->unit->manager, message, error);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
+        }
+
+        job_finish_and_invalidate(j, JOB_CANCELED, true, false);
 
         return sd_bus_reply_method_return(message, NULL);
 }
 
-const sd_bus_vtable bus_job_vtable[] = {
-        SD_BUS_VTABLE_START(0),
-        SD_BUS_METHOD("Cancel", NULL, NULL, method_cancel, 0),
-        SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Job, id), 0),
-        SD_BUS_PROPERTY("Unit", "(so)", property_get_unit, 0, 0),
-        SD_BUS_PROPERTY("JobType", "s", property_get_type, offsetof(Job, type), 0),
-        SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
-        SD_BUS_VTABLE_END
-};
+int bus_job_method_get_waiting_jobs(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
+        _cleanup_free_ Job **list = NULL;
+        Job *j = userdata;
+        int r, i, n;
 
-static int foreach_client(Job *j, int (*send_message)(sd_bus *bus, const char *name, Job *j)) {
-        BusTrackedClient *one_destination = NULL;
-        Iterator i;
-        sd_bus *b;
-        unsigned n, m;
-        int r;
+        if (strstr(sd_bus_message_get_member(message), "After"))
+                n = job_get_after(j, &list);
+        else
+                n = job_get_before(j, &list);
+        if (n < 0)
+                return n;
 
-        assert(j);
-        assert(send_message);
+        r = sd_bus_message_new_method_return(message, &reply);
+        if (r < 0)
+                return r;
 
-        n = set_size(j->manager->subscribed);
-        m = set_size(j->subscribed);
+        r = sd_bus_message_open_container(reply, 'a', "(usssoo)");
+        if (r < 0)
+                return r;
 
-        if (n <= 0 && m <= 0)
-                return 0;
+        for (i = 0; i < n; i ++) {
+                _cleanup_free_ char *unit_path = NULL, *job_path = NULL;
 
-        if (n == 1 && m == 0)
-                one_destination = set_first(j->manager->subscribed);
-        else if (n == 0 && m == 1)
-                one_destination = set_first(j->subscribed);
-        else
-                one_destination = NULL;
+                job_path = job_dbus_path(list[i]);
+                if (!job_path)
+                        return -ENOMEM;
 
-        if (one_destination)
-                return send_message(one_destination->bus, isempty(one_destination->name) ? NULL : one_destination->name, j);
+                unit_path = unit_dbus_path(list[i]->unit);
+                if (!unit_path)
+                        return -ENOMEM;
 
-        /* Send to everybody */
-        SET_FOREACH(b, j->manager->private_buses, i) {
-                r = send_message(b, NULL, j);
+                r = sd_bus_message_append(reply, "(usssoo)",
+                                          list[i]->id,
+                                          list[i]->unit->id,
+                                          job_type_to_string(list[i]->type),
+                                          job_state_to_string(list[i]->state),
+                                          job_path,
+                                          unit_path);
                 if (r < 0)
                         return r;
         }
 
-        if (j->manager->api_bus)
-                return send_message(j->manager->api_bus, NULL, j);
+        r = sd_bus_message_close_container(reply);
+        if (r < 0)
+                return r;
 
-        return 0;
+        return sd_bus_send(NULL, reply, NULL);
 }
 
-static int send_new_signal(sd_bus *bus, const char *destination, Job *j) {
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+const sd_bus_vtable bus_job_vtable[] = {
+        SD_BUS_VTABLE_START(0),
+        SD_BUS_METHOD("Cancel", NULL, NULL, bus_job_method_cancel, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("GetAfter", NULL, "a(usssoo)", bus_job_method_get_waiting_jobs, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("GetBefore", NULL, "a(usssoo)", bus_job_method_get_waiting_jobs, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Job, id), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("Unit", "(so)", property_get_unit, 0, SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("JobType", "s", property_get_type, offsetof(Job, type), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("State", "s", property_get_state, offsetof(Job, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_VTABLE_END
+};
+
+static int send_new_signal(sd_bus *bus, void *userdata) {
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
         _cleanup_free_ char *p = NULL;
+        Job *j = userdata;
         int r;
 
         assert(bus);
@@ -128,10 +140,10 @@ static int send_new_signal(sd_bus *bus, const char *destination, Job *j) {
 
         r = sd_bus_message_new_signal(
                         bus,
+                        &m,
                         "/org/freedesktop/systemd1",
                         "org.freedesktop.systemd1.Manager",
-                        "JobNew",
-                        &m);
+                        "JobNew");
         if (r < 0)
                 return r;
 
@@ -139,11 +151,12 @@ static int send_new_signal(sd_bus *bus, const char *destination, Job *j) {
         if (r < 0)
                 return r;
 
-        return sd_bus_send_to(bus, m, destination, NULL);
+        return sd_bus_send(bus, m, NULL);
 }
 
-static int send_changed_signal(sd_bus *bus, const char *destination, Job *j) {
+static int send_changed_signal(sd_bus *bus, void *userdata) {
         _cleanup_free_ char *p = NULL;
+        Job *j = userdata;
 
         assert(bus);
         assert(j);
@@ -165,16 +178,17 @@ void bus_job_send_change_signal(Job *j) {
                 j->in_dbus_queue = false;
         }
 
-        r = foreach_client(j, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal);
+        r = bus_foreach_bus(j->manager, j->bus_track, j->sent_dbus_new_signal ? send_changed_signal : send_new_signal, j);
         if (r < 0)
-                log_warning("Failed to send job change signal for %u/%s: %s", j->id, j->unit->id, strerror(-r));
+                log_debug_errno(r, "Failed to send job change signal for %u: %m", j->id);
 
         j->sent_dbus_new_signal = true;
 }
 
-static int send_removed_signal(sd_bus *bus, const char *destination, Job *j) {
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+static int send_removed_signal(sd_bus *bus, void *userdata) {
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
         _cleanup_free_ char *p = NULL;
+        Job *j = userdata;
         int r;
 
         assert(bus);
@@ -184,13 +198,12 @@ static int send_removed_signal(sd_bus *bus, const char *destination, Job *j) {
         if (!p)
                 return -ENOMEM;
 
-
         r = sd_bus_message_new_signal(
                         bus,
+                        &m,
                         "/org/freedesktop/systemd1",
                         "org.freedesktop.systemd1.Manager",
-                        "JobRemoved",
-                        &m);
+                        "JobRemoved");
         if (r < 0)
                 return r;
 
@@ -198,7 +211,7 @@ static int send_removed_signal(sd_bus *bus, const char *destination, Job *j) {
         if (r < 0)
                 return r;
 
-        return sd_bus_send_to(bus, m, destination, NULL);
+        return sd_bus_send(bus, m, NULL);
 }
 
 void bus_job_send_removed_signal(Job *j) {
@@ -209,8 +222,69 @@ void bus_job_send_removed_signal(Job *j) {
         if (!j->sent_dbus_new_signal)
                 bus_job_send_change_signal(j);
 
-        r = foreach_client(j, send_removed_signal);
+        r = bus_foreach_bus(j->manager, j->bus_track, send_removed_signal, j);
+        if (r < 0)
+                log_debug_errno(r, "Failed to send job remove signal for %u: %m", j->id);
+}
+
+static int bus_job_track_handler(sd_bus_track *t, void *userdata) {
+        Job *j = userdata;
+
+        assert(t);
+        assert(j);
+
+        j->bus_track = sd_bus_track_unref(j->bus_track); /* make sure we aren't called again */
+
+        /* Last client dropped off the bus, maybe we should GC this now? */
+        job_add_to_gc_queue(j);
+        return 0;
+}
+
+static int bus_job_allocate_bus_track(Job *j) {
+
+        assert(j);
+
+        if (j->bus_track)
+                return 0;
+
+        return sd_bus_track_new(j->unit->manager->api_bus, &j->bus_track, bus_job_track_handler, j);
+}
+
+int bus_job_coldplug_bus_track(Job *j) {
+        int r = 0;
+        _cleanup_strv_free_ char **deserialized_clients = NULL;
+
+        assert(j);
+
+        deserialized_clients = TAKE_PTR(j->deserialized_clients);
+
+        if (strv_isempty(deserialized_clients))
+                return 0;
+
+        if (!j->manager->api_bus)
+                return 0;
+
+        r = bus_job_allocate_bus_track(j);
+        if (r < 0)
+                return r;
+
+        return bus_track_add_name_many(j->bus_track, deserialized_clients);
+}
+
+int bus_job_track_sender(Job *j, sd_bus_message *m) {
+        int r;
+
+        assert(j);
+        assert(m);
+
+        if (sd_bus_message_get_bus(m) != j->unit->manager->api_bus) {
+                j->ref_by_private_bus = true;
+                return 0;
+        }
+
+        r = bus_job_allocate_bus_track(j);
         if (r < 0)
-                log_warning("Failed to send job removal signal for %u/%s: %s", j->id, j->unit->id, strerror(-r));
-                return;
+                return r;
+
+        return sd_bus_track_add_sender(j->bus_track, m);
 }