]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine/machine-dbus.c
event: add some well-defined priority values of event sources
[thirdparty/systemd.git] / src / machine / machine-dbus.c
CommitLineData
9444b1f2
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <errno.h>
23#include <string.h>
24
c3350683 25#include "bus-util.h"
1ee306e1 26#include "machine.h"
9444b1f2 27
c3350683
LP
28static int property_get_id(
29 sd_bus *bus,
30 const char *path,
31 const char *interface,
32 const char *property,
33 sd_bus_message *reply,
34 sd_bus_error *error,
35 void *userdata) {
9444b1f2 36
c3350683
LP
37 Machine *m = userdata;
38 int r;
9444b1f2 39
c3350683
LP
40 assert(bus);
41 assert(reply);
42 assert(m);
43
44 r = sd_bus_message_append_array(reply, 'y', &m->id, 16);
45 if (r < 0)
46 return r;
9444b1f2 47
c3350683 48 return 1;
9444b1f2
LP
49}
50
c3350683
LP
51static int property_get_state(
52 sd_bus *bus,
53 const char *path,
54 const char *interface,
55 const char *property,
56 sd_bus_message *reply,
57 sd_bus_error *error,
58 void *userdata) {
59
60 Machine *m = userdata;
fb6becb4 61 const char *state;
c3350683 62 int r;
fb6becb4 63
c3350683
LP
64 assert(bus);
65 assert(reply);
fb6becb4
LP
66 assert(m);
67
68 state = machine_state_to_string(machine_get_state(m));
69
c3350683
LP
70 r = sd_bus_message_append_basic(reply, 's', state);
71 if (r < 0)
72 return r;
fb6becb4 73
c3350683 74 return 1;
fb6becb4
LP
75}
76
c3350683 77static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class, machine_class, MachineClass);
9444b1f2 78
c3350683
LP
79static int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata) {
80 Machine *m = userdata;
81 int r;
9444b1f2 82
c3350683
LP
83 assert(bus);
84 assert(message);
85 assert(m);
9444b1f2 86
c3350683
LP
87 r = machine_stop(m);
88 if (r < 0)
89 return sd_bus_reply_method_errno(bus, message, r, NULL);
9444b1f2 90
c3350683 91 return sd_bus_reply_method_return(bus, message, NULL);
9444b1f2
LP
92}
93
c3350683
LP
94static int method_kill(sd_bus *bus, sd_bus_message *message, void *userdata) {
95 Machine *m = userdata;
96 const char *swho;
97 int32_t signo;
98 KillWho who;
9444b1f2
LP
99 int r;
100
c3350683 101 assert(bus);
9444b1f2 102 assert(message);
c3350683 103 assert(m);
9444b1f2 104
c3350683
LP
105 r = sd_bus_message_read(message, "si", &swho, &signo);
106 if (r < 0)
107 return sd_bus_reply_method_errno(bus, message, r, NULL);
9444b1f2 108
c3350683
LP
109 if (isempty(swho))
110 who = KILL_ALL;
111 else {
112 who = kill_who_from_string(swho);
113 if (who < 0)
114 return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
9444b1f2
LP
115 }
116
c3350683
LP
117 if (signo <= 0 || signo >= _NSIG)
118 return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
9444b1f2 119
c3350683
LP
120 r = machine_kill(m, who, signo);
121 if (r < 0)
122 return sd_bus_reply_method_errno(bus, message, r, NULL);
9444b1f2 123
c3350683 124 return sd_bus_reply_method_return(bus, message, NULL);
9444b1f2
LP
125}
126
c3350683
LP
127const sd_bus_vtable machine_vtable[] = {
128 SD_BUS_VTABLE_START(0),
129 SD_BUS_PROPERTY("Name", "s", NULL, offsetof(Machine, name), 0),
130 SD_BUS_PROPERTY("Id", "ay", property_get_id, 0, 0),
131 SD_BUS_PROPERTY("Timestamp", "t", NULL, offsetof(Machine, timestamp.realtime), 0),
132 SD_BUS_PROPERTY("TimestampMonotonic", "t", NULL, offsetof(Machine, timestamp.monotonic), 0),
133 SD_BUS_PROPERTY("Service", "s", NULL, offsetof(Machine, service), 0),
134 SD_BUS_PROPERTY("Scope", "s", NULL, offsetof(Machine, scope), 0),
135 SD_BUS_PROPERTY("Leader", "u", NULL, offsetof(Machine, leader), 0),
136 SD_BUS_PROPERTY("Class", "s", property_get_class, offsetof(Machine, class), 0),
137 SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
138 SD_BUS_PROPERTY("RootDirectory", "s", NULL, offsetof(Machine, root_directory), 0),
139 SD_BUS_METHOD("Terminate", NULL, NULL, method_terminate, 0),
140 SD_BUS_METHOD("Kill", "si", NULL, method_kill, 0),
141 SD_BUS_VTABLE_END
142};
9444b1f2 143
c3350683
LP
144int machine_object_find(sd_bus *bus, const char *path, const char *interface, void **found, void *userdata) {
145 _cleanup_free_ char *e = NULL;
146 Manager *m = userdata;
147 Machine *machine;
148 const char *p;
9444b1f2 149
c3350683
LP
150 assert(bus);
151 assert(path);
152 assert(interface);
153 assert(found);
154 assert(m);
9444b1f2 155
c3350683
LP
156 p = startswith(path, "/org/freedesktop/machine1/machine/");
157 if (!p)
158 return 0;
9444b1f2 159
c3350683
LP
160 e = bus_path_unescape(p);
161 if (!e)
162 return -ENOMEM;
9444b1f2 163
c3350683
LP
164 machine = hashmap_get(m->machines, e);
165 if (!machine)
166 return 0;
9444b1f2 167
c3350683
LP
168 *found = machine;
169 return 1;
9444b1f2
LP
170}
171
9444b1f2
LP
172char *machine_bus_path(Machine *m) {
173 _cleanup_free_ char *e = NULL;
174
175 assert(m);
176
177 e = bus_path_escape(m->name);
178 if (!e)
179 return NULL;
180
1ee306e1 181 return strappend("/org/freedesktop/machine1/machine/", e);
9444b1f2
LP
182}
183
184int machine_send_signal(Machine *m, bool new_machine) {
9444b1f2
LP
185 _cleanup_free_ char *p = NULL;
186
187 assert(m);
188
9444b1f2
LP
189 p = machine_bus_path(m);
190 if (!p)
191 return -ENOMEM;
192
c3350683
LP
193 return sd_bus_emit_signal(
194 m->manager->bus,
195 "/org/freedesktop/machine1",
196 "org.freedesktop.machine1.Manager",
197 new_machine ? "MachineNew" : "MachineRemoved",
198 "so", m->name, p);
9444b1f2
LP
199}
200
c3350683
LP
201int machine_send_create_reply(Machine *m, sd_bus_error *error) {
202 _cleanup_bus_message_unref_ sd_bus_message *c = NULL;
9444b1f2
LP
203 _cleanup_free_ char *p = NULL;
204
205 assert(m);
206
fb6becb4
LP
207 if (!m->create_message)
208 return 0;
209
c3350683
LP
210 c = m->create_message;
211 m->create_message = NULL;
fb6becb4 212
76e66585
LP
213 /* Update the machine state file before we notify the client
214 * about the result. */
215 machine_save(m);
216
c3350683
LP
217 if (error)
218 return sd_bus_reply_method_error(m->manager->bus, c, error);
fb6becb4 219
c3350683
LP
220 p = machine_bus_path(m);
221 if (!p)
222 return -ENOMEM;
fb6becb4 223
c3350683 224 return sd_bus_reply_method_return(m->manager->bus, c, "o", p);
fb6becb4 225}