chiark / gitweb /
systemctl: suppress a gcc sign assignemnt warning
[elogind.git] / dbus-job.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23
24 #include "dbus.h"
25 #include "log.h"
26
27 static const char introspection[] =
28         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
29         "<node>"
30         " <interface name=\"org.freedesktop.systemd1.Job\">"
31         "  <method name=\"Cancel\"/>"
32         "  <property name=\"Id\" type=\"u\" access=\"read\"/>"
33         "  <property name=\"Unit\" type=\"(so)\" access=\"read\"/>"
34         "  <property name=\"JobType\" type=\"s\" access=\"read\"/>"
35         "  <property name=\"State\" type=\"s\" access=\"read\"/>"
36         " </interface>"
37         BUS_PROPERTIES_INTERFACE
38         BUS_INTROSPECTABLE_INTERFACE
39         "</node>";
40
41 static int bus_job_append_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
42         Job *j = data;
43         const char *state;
44
45         assert(m);
46         assert(i);
47         assert(property);
48         assert(j);
49
50         state = job_state_to_string(j->state);
51
52         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
53                 return -ENOMEM;
54
55         return 0;
56 }
57
58 static int bus_job_append_type(Manager *m, DBusMessageIter *i, const char *property, void *data) {
59         Job *j = data;
60         const char *type;
61
62         assert(m);
63         assert(i);
64         assert(property);
65         assert(j);
66
67         type = job_type_to_string(j->type);
68
69         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &type))
70                 return -ENOMEM;
71
72         return 0;
73 }
74
75 static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
76         Job *j = data;
77         DBusMessageIter sub;
78         char *p;
79         const char *id;
80
81         assert(m);
82         assert(i);
83         assert(property);
84         assert(j);
85
86         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
87                 return -ENOMEM;
88
89         if (!(p = unit_dbus_path(j->unit)))
90                 return -ENOMEM;
91
92         id = unit_id(j->unit);
93
94         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
95             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
96                 free(p);
97                 return -ENOMEM;
98         }
99
100         free(p);
101
102         if (!dbus_message_iter_close_container(i, &sub))
103                 return -ENOMEM;
104
105         return 0;
106 }
107
108 static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusMessage *message) {
109         const BusProperty properties[] = {
110                 { "org.freedesktop.systemd1.Job", "Id",      bus_property_append_uint32, "u",    &j->id },
111                 { "org.freedesktop.systemd1.Job", "State",   bus_job_append_state,       "s",    j      },
112                 { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type,        "s",    j      },
113                 { "org.freedesktop.systemd1.Job", "Unit",    bus_job_append_unit,        "(so)", j      },
114                 { NULL, NULL, NULL, NULL, NULL }
115         };
116
117         DBusMessage *reply = NULL;
118         Manager *m = j->manager;
119
120         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Job", "Cancel")) {
121                 if (!(reply = dbus_message_new_method_return(message)))
122                         goto oom;
123
124                 job_free(j);
125
126         } else
127                 return bus_default_message_handler(j->manager, message, introspection, properties);
128
129         if (reply) {
130                 if (!dbus_connection_send(m->bus, reply, NULL))
131                         goto oom;
132
133                 dbus_message_unref(reply);
134         }
135
136         return DBUS_HANDLER_RESULT_HANDLED;
137
138 oom:
139         if (reply)
140                 dbus_message_unref(reply);
141
142         return DBUS_HANDLER_RESULT_NEED_MEMORY;
143 }
144
145 static DBusHandlerResult bus_job_message_handler(DBusConnection  *connection, DBusMessage  *message, void *data) {
146         Manager *m = data;
147         Job *j;
148         int r;
149
150         assert(connection);
151         assert(message);
152         assert(m);
153
154         log_debug("Got D-Bus request: %s.%s() on %s",
155                   dbus_message_get_interface(message),
156                   dbus_message_get_member(message),
157                   dbus_message_get_path(message));
158
159         if ((r = manager_get_job_from_dbus_path(m, dbus_message_get_path(message), &j)) < 0) {
160
161                 if (r == -ENOMEM)
162                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
163
164                 if (r == -ENOENT)
165                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
166
167                 return bus_send_error_reply(m, message, NULL, r);
168         }
169
170         return bus_job_message_dispatch(j, message);
171 }
172
173 const DBusObjectPathVTable bus_job_vtable = {
174         .message_function = bus_job_message_handler
175 };