chiark / gitweb /
dbus: implement start/stop/restart/reload/cancel D-Bus calls
[elogind.git] / dbus-job.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #include <errno.h>
4
5 #include "dbus.h"
6 #include "log.h"
7
8 static const char introspection[] =
9         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
10         "<node>"
11         " <interface name=\"org.freedesktop.systemd1.Job\">"
12         "  <method name=\"Cancel\"/>"
13         "  <property name=\"Id\" type=\"u\" access=\"read\"/>"
14         "  <property name=\"Unit\" type=\"(so)\" access=\"read\"/>"
15         "  <property name=\"JobType\" type=\"s\" access=\"read\"/>"
16         "  <property name=\"State\" type=\"s\" access=\"read\"/>"
17         " </interface>"
18         BUS_PROPERTIES_INTERFACE
19         BUS_INTROSPECTABLE_INTERFACE
20         "</node>";
21
22 static int bus_job_append_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
23         Job *j = data;
24         const char *state;
25
26         assert(m);
27         assert(i);
28         assert(property);
29         assert(j);
30
31         state = job_state_to_string(j->state);
32
33         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
34                 return -ENOMEM;
35
36         return 0;
37 }
38
39 static int bus_job_append_type(Manager *m, DBusMessageIter *i, const char *property, void *data) {
40         Job *j = data;
41         const char *type;
42
43         assert(m);
44         assert(i);
45         assert(property);
46         assert(j);
47
48         type = job_type_to_string(j->type);
49
50         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &type))
51                 return -ENOMEM;
52
53         return 0;
54 }
55
56 static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
57         Job *j = data;
58         DBusMessageIter sub;
59         char *p;
60         const char *id;
61
62         assert(m);
63         assert(i);
64         assert(property);
65         assert(j);
66
67         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
68                 return -ENOMEM;
69
70         if (!(p = unit_dbus_path(j->unit)))
71                 return -ENOMEM;
72
73         id = unit_id(j->unit);
74
75         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
76             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
77                 free(p);
78                 return -ENOMEM;
79         }
80
81         free(p);
82
83         if (!dbus_message_iter_close_container(i, &sub))
84                 return -ENOMEM;
85
86         return 0;
87 }
88
89 static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusMessage *message) {
90         const BusProperty properties[] = {
91                 { "org.freedesktop.systemd1.Job", "Id",      bus_property_append_uint32, "u",    &j->id },
92                 { "org.freedesktop.systemd1.Job", "State",   bus_job_append_state,       "s",    j      },
93                 { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type,        "s",    j      },
94                 { "org.freedesktop.systemd1.Job", "Unit",    bus_job_append_unit,        "(so)", j      },
95                 { NULL, NULL, NULL, NULL, NULL }
96         };
97
98         DBusMessage *reply = NULL;
99         Manager *m = j->manager;
100
101         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Job", "Cancel")) {
102                 if (!(reply = dbus_message_new_method_return(message)))
103                         goto oom;
104
105                 job_free(j);
106
107         } else
108                 return bus_default_message_handler(j->manager, message, introspection, properties);
109
110         if (reply) {
111                 if (!dbus_connection_send(m->bus, reply, NULL))
112                         goto oom;
113
114                 dbus_message_unref(reply);
115         }
116
117         return DBUS_HANDLER_RESULT_HANDLED;
118
119 oom:
120         if (reply)
121                 dbus_message_unref(reply);
122
123         return DBUS_HANDLER_RESULT_NEED_MEMORY;
124 }
125
126 DBusHandlerResult bus_job_message_handler(DBusConnection  *connection, DBusMessage  *message, void *data) {
127         Manager *m = data;
128         Job *j;
129         int r;
130
131         assert(connection);
132         assert(message);
133         assert(m);
134
135         log_debug("Got D-Bus request: %s.%s() on %s",
136                   dbus_message_get_interface(message),
137                   dbus_message_get_member(message),
138                   dbus_message_get_path(message));
139
140         if ((r = manager_get_job_from_dbus_path(m, dbus_message_get_path(message), &j)) < 0) {
141
142                 if (r == -ENOMEM)
143                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
144
145                 if (r == -ENOENT)
146                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
147
148                 return bus_send_error_reply(m, message, NULL, r);
149         }
150
151         return bus_job_message_dispatch(j, message);
152 }
153
154 const DBusObjectPathVTable bus_job_vtable = {
155         .message_function = bus_job_message_handler
156 };