1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
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.
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.
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/>.
28 static int property_get_id(
31 const char *interface,
33 sd_bus_message *reply,
37 Machine *m = userdata;
44 r = sd_bus_message_append_array(reply, 'y', &m->id, 16);
51 static int property_get_state(
54 const char *interface,
56 sd_bus_message *reply,
60 Machine *m = userdata;
68 state = machine_state_to_string(machine_get_state(m));
70 r = sd_bus_message_append_basic(reply, 's', state);
77 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class, machine_class, MachineClass);
79 static int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata) {
80 Machine *m = userdata;
89 return sd_bus_reply_method_errno(bus, message, r, NULL);
91 return sd_bus_reply_method_return(bus, message, NULL);
94 static int method_kill(sd_bus *bus, sd_bus_message *message, void *userdata) {
95 Machine *m = userdata;
105 r = sd_bus_message_read(message, "si", &swho, &signo);
107 return sd_bus_reply_method_errno(bus, message, r, NULL);
112 who = kill_who_from_string(swho);
114 return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
117 if (signo <= 0 || signo >= _NSIG)
118 return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
120 r = machine_kill(m, who, signo);
122 return sd_bus_reply_method_errno(bus, message, r, NULL);
124 return sd_bus_reply_method_return(bus, message, NULL);
127 const 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),
144 int 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;
156 p = startswith(path, "/org/freedesktop/machine1/machine/");
160 e = bus_path_unescape(p);
164 machine = hashmap_get(m->machines, e);
172 char *machine_bus_path(Machine *m) {
173 _cleanup_free_ char *e = NULL;
177 e = bus_path_escape(m->name);
181 return strappend("/org/freedesktop/machine1/machine/", e);
184 int machine_send_signal(Machine *m, bool new_machine) {
185 _cleanup_free_ char *p = NULL;
189 p = machine_bus_path(m);
193 return sd_bus_emit_signal(
195 "/org/freedesktop/machine1",
196 "org.freedesktop.machine1.Manager",
197 new_machine ? "MachineNew" : "MachineRemoved",
201 int machine_send_create_reply(Machine *m, sd_bus_error *error) {
202 _cleanup_bus_message_unref_ sd_bus_message *c = NULL;
203 _cleanup_free_ char *p = NULL;
207 if (!m->create_message)
210 c = m->create_message;
211 m->create_message = NULL;
214 return sd_bus_reply_method_error(m->manager->bus, c, error);
216 /* Update the machine state file before we notify the client
217 * about the result. */
220 p = machine_bus_path(m);
224 return sd_bus_reply_method_return(m->manager->bus, c, "o", p);