chiark / gitweb /
execute: allow configuration of SCHED_RESET_ON_FORK
[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         "  <property name=\"Id\" type=\"u\" access=\"read\"/>"
13         "  <property name=\"Unit\" type=\"(so)\" access=\"read\"/>"
14         "  <property name=\"Type\" type=\"s\" access=\"read\"/>"
15         "  <property name=\"State\" type=\"s\" access=\"read\"/>"
16         " </interface>"
17         BUS_PROPERTIES_INTERFACE
18         BUS_INTROSPECTABLE_INTERFACE
19         "</node>";
20
21 static int bus_job_append_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
22         Job *j = data;
23         const char *state;
24
25         assert(m);
26         assert(i);
27         assert(property);
28         assert(j);
29
30         state = job_state_to_string(j->state);
31
32         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
33                 return -ENOMEM;
34
35         return 0;
36 }
37
38 static int bus_job_append_type(Manager *m, DBusMessageIter *i, const char *property, void *data) {
39         Job *j = data;
40         const char *type;
41
42         assert(m);
43         assert(i);
44         assert(property);
45         assert(j);
46
47         type = job_type_to_string(j->type);
48
49         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &type))
50                 return -ENOMEM;
51
52         return 0;
53 }
54
55 static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
56         Job *j = data;
57         DBusMessageIter sub;
58         char *p;
59         const char *id;
60
61         assert(m);
62         assert(i);
63         assert(property);
64         assert(j);
65
66         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
67                 return -ENOMEM;
68
69         if (!(p = unit_dbus_path(j->unit)))
70                 return -ENOMEM;
71
72         id = unit_id(j->unit);
73
74         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
75             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
76                 free(p);
77                 return -ENOMEM;
78         }
79
80         free(p);
81
82         if (!dbus_message_iter_close_container(i, &sub))
83                 return -ENOMEM;
84
85         return 0;
86 }
87
88 static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusMessage *message) {
89
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", "Type",  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         return bus_default_message_handler(j->manager, message, introspection, properties);
99 }
100
101 DBusHandlerResult bus_job_message_handler(DBusConnection  *connection, DBusMessage  *message, void *data) {
102         Manager *m = data;
103         Job *j;
104         int r;
105
106         assert(connection);
107         assert(message);
108         assert(m);
109
110         log_debug("Got D-Bus request: %s.%s() on %s",
111                   dbus_message_get_interface(message),
112                   dbus_message_get_member(message),
113                   dbus_message_get_path(message));
114
115         if ((r = manager_get_job_from_dbus_path(m, dbus_message_get_path(message), &j)) < 0) {
116
117                 if (r == -ENOMEM)
118                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
119
120                 if (r == -ENOENT)
121                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
122
123                 return bus_send_error_reply(m, message, NULL, r);
124         }
125
126         return bus_job_message_dispatch(j, message);
127 }
128
129 const DBusObjectPathVTable bus_job_vtable = {
130         .message_function = bus_job_message_handler
131 };