2013-07-02 03:47:23 +02:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2011 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
#include "sd-id128.h"
|
|
|
|
#include "sd-messages.h"
|
2013-07-02 03:47:23 +02:00
|
|
|
#include "strv.h"
|
|
|
|
#include "mkdir.h"
|
|
|
|
#include "path-util.h"
|
|
|
|
#include "special.h"
|
|
|
|
#include "fileio-label.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "utf8.h"
|
|
|
|
#include "unit-name.h"
|
2013-10-30 02:06:55 +01:00
|
|
|
#include "bus-util.h"
|
2014-12-10 19:00:46 +01:00
|
|
|
#include "bus-common-errors.h"
|
2013-10-30 02:06:55 +01:00
|
|
|
#include "time-util.h"
|
2013-11-07 15:19:39 +01:00
|
|
|
#include "cgroup-util.h"
|
2014-12-27 01:57:03 +01:00
|
|
|
#include "machine-image.h"
|
|
|
|
#include "image-dbus.h"
|
2013-10-30 02:06:55 +01:00
|
|
|
#include "machined.h"
|
2014-12-27 01:57:03 +01:00
|
|
|
#include "machine-dbus.h"
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
static int method_get_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
2013-10-30 02:06:55 +01:00
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Machine *machine;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return r;
|
2013-10-30 02:06:55 +01:00
|
|
|
|
|
|
|
machine = hashmap_get(m->machines, name);
|
|
|
|
if (!machine)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
|
2013-10-30 02:06:55 +01:00
|
|
|
|
|
|
|
p = machine_bus_path(machine);
|
|
|
|
if (!p)
|
2013-11-21 19:34:37 +01:00
|
|
|
return -ENOMEM;
|
2013-10-30 02:06:55 +01:00
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
return sd_bus_reply_method_return(message, "o", p);
|
2013-10-30 02:06:55 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 20:07:23 +01:00
|
|
|
static int method_get_image(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = image_find(name, NULL);
|
|
|
|
if (r == 0)
|
|
|
|
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
p = image_bus_path(name);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(message, "o", p);
|
|
|
|
}
|
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
static int method_get_machine_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
2013-10-30 02:06:55 +01:00
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Machine *machine = NULL;
|
2013-11-05 20:44:27 +01:00
|
|
|
pid_t pid;
|
2013-10-30 02:06:55 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
2013-11-05 20:44:27 +01:00
|
|
|
assert_cc(sizeof(pid_t) == sizeof(uint32_t));
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read(message, "u", &pid);
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return r;
|
2013-10-30 02:06:55 +01:00
|
|
|
|
2013-11-05 20:44:27 +01:00
|
|
|
if (pid == 0) {
|
2013-11-28 17:50:02 +01:00
|
|
|
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
|
|
|
|
|
|
|
|
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_creds_get_pid(creds, &pid);
|
2013-11-05 20:44:27 +01:00
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return r;
|
2013-11-05 20:44:27 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = manager_get_machine_by_pid(m, pid, &machine);
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return r;
|
2013-10-30 02:06:55 +01:00
|
|
|
if (!machine)
|
2014-04-25 13:45:15 +02:00
|
|
|
return sd_bus_error_setf(error, BUS_ERROR_NO_MACHINE_FOR_PID, "PID "PID_FMT" does not belong to any known machine", pid);
|
2013-10-30 02:06:55 +01:00
|
|
|
|
|
|
|
p = machine_bus_path(machine);
|
|
|
|
if (!p)
|
2013-11-21 19:34:37 +01:00
|
|
|
return -ENOMEM;
|
2013-10-30 02:06:55 +01:00
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
return sd_bus_reply_method_return(message, "o", p);
|
2013-10-30 02:06:55 +01:00
|
|
|
}
|
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
static int method_list_machines(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
2013-10-30 02:06:55 +01:00
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Machine *machine;
|
|
|
|
Iterator i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
2013-11-21 01:51:16 +01:00
|
|
|
r = sd_bus_message_new_method_return(message, &reply);
|
2013-10-30 02:06:55 +01:00
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-10-30 02:06:55 +01:00
|
|
|
|
|
|
|
r = sd_bus_message_open_container(reply, 'a', "(ssso)");
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-10-30 02:06:55 +01:00
|
|
|
|
|
|
|
HASHMAP_FOREACH(machine, m->machines, i) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
|
|
|
p = machine_bus_path(machine);
|
|
|
|
if (!p)
|
2013-11-21 19:34:37 +01:00
|
|
|
return -ENOMEM;
|
2013-10-30 02:06:55 +01:00
|
|
|
|
|
|
|
r = sd_bus_message_append(reply, "(ssso)",
|
|
|
|
machine->name,
|
|
|
|
strempty(machine_class_to_string(machine->class)),
|
|
|
|
machine->service,
|
|
|
|
p);
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-10-30 02:06:55 +01:00
|
|
|
}
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_close_container(reply);
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-10-30 02:06:55 +01:00
|
|
|
|
|
|
|
return sd_bus_send(bus, reply, NULL);
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:47:55 +02:00
|
|
|
static int method_create_or_register_machine(Manager *manager, sd_bus_message *message, bool read_network, Machine **_m, sd_bus_error *error) {
|
2013-07-02 15:49:27 +02:00
|
|
|
const char *name, *service, *class, *root_directory;
|
2014-07-10 22:47:55 +02:00
|
|
|
const int32_t *netif = NULL;
|
2013-07-02 03:47:23 +02:00
|
|
|
MachineClass c;
|
|
|
|
uint32_t leader;
|
|
|
|
sd_id128_t id;
|
2013-10-30 02:06:55 +01:00
|
|
|
const void *v;
|
2013-07-02 03:47:23 +02:00
|
|
|
Machine *m;
|
2014-07-10 22:47:55 +02:00
|
|
|
size_t n, n_netif = 0;
|
2013-10-30 02:06:55 +01:00
|
|
|
int r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
assert(manager);
|
2014-02-11 17:15:38 +01:00
|
|
|
assert(message);
|
|
|
|
assert(_m);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return r;
|
2014-07-03 01:17:26 +02:00
|
|
|
if (!machine_name_is_valid(name))
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read_array(message, 'y', &v, &n);
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
if (n == 0)
|
|
|
|
id = SD_ID128_NULL;
|
|
|
|
else if (n == 16)
|
|
|
|
memcpy(&id, v, n);
|
|
|
|
else
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine ID parameter");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory);
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2014-07-10 22:47:55 +02:00
|
|
|
if (read_network) {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
n_netif /= sizeof(int32_t);
|
|
|
|
|
|
|
|
for (i = 0; i < n_netif; i++) {
|
|
|
|
if (netif[i] <= 0)
|
|
|
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 03:47:23 +02:00
|
|
|
if (isempty(class))
|
|
|
|
c = _MACHINE_CLASS_INVALID;
|
|
|
|
else {
|
|
|
|
c = machine_class_from_string(class);
|
|
|
|
if (c < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter");
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
if (leader == 1)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
if (!isempty(root_directory) && !path_is_absolute(root_directory))
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path");
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
if (leader == 0) {
|
2013-11-28 17:50:02 +01:00
|
|
|
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
|
|
|
|
|
|
|
|
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
assert_cc(sizeof(uint32_t) == sizeof(pid_t));
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
|
2013-10-30 02:06:55 +01:00
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return r;
|
2013-10-30 02:06:55 +01:00
|
|
|
}
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-07-02 03:47:23 +02:00
|
|
|
if (hashmap_get(manager->machines, name))
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_setf(error, BUS_ERROR_MACHINE_EXISTS, "Machine '%s' already exists", name);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
r = manager_add_machine(manager, name, &m);
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
m->leader = leader;
|
|
|
|
m->class = c;
|
|
|
|
m->id = id;
|
|
|
|
|
|
|
|
if (!isempty(service)) {
|
|
|
|
m->service = strdup(service);
|
|
|
|
if (!m->service) {
|
2013-11-21 19:34:37 +01:00
|
|
|
r = -ENOMEM;
|
2013-07-02 03:47:23 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isempty(root_directory)) {
|
|
|
|
m->root_directory = strdup(root_directory);
|
|
|
|
if (!m->root_directory) {
|
2013-11-21 19:34:37 +01:00
|
|
|
r = -ENOMEM;
|
2013-07-02 03:47:23 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:47:55 +02:00
|
|
|
if (n_netif > 0) {
|
|
|
|
assert_cc(sizeof(int32_t) == sizeof(int));
|
|
|
|
m->netif = memdup(netif, sizeof(int32_t) * n_netif);
|
|
|
|
if (!m->netif) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
m->n_netif = n_netif;
|
|
|
|
}
|
|
|
|
|
2014-02-11 17:15:38 +01:00
|
|
|
*_m = m;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
machine_add_to_gc_queue(m);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:47:55 +02:00
|
|
|
static int method_create_machine_internal(sd_bus *bus, sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
|
2014-02-11 17:15:38 +01:00
|
|
|
Manager *manager = userdata;
|
|
|
|
Machine *m = NULL;
|
|
|
|
int r;
|
|
|
|
|
2014-07-10 22:47:55 +02:00
|
|
|
r = method_create_or_register_machine(manager, message, read_network, &m, error);
|
2014-02-11 17:15:38 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_enter_container(message, 'a', "(sv)");
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
r = machine_start(m, message, error);
|
|
|
|
if (r < 0)
|
2013-07-02 03:47:23 +02:00
|
|
|
goto fail;
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
m->create_message = sd_bus_message_ref(message);
|
|
|
|
return 1;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
fail:
|
2013-10-12 01:33:43 +02:00
|
|
|
machine_add_to_gc_queue(m);
|
2014-02-11 17:15:38 +01:00
|
|
|
return r;
|
|
|
|
}
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2014-07-10 22:47:55 +02:00
|
|
|
static int method_create_machine_with_network(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
return method_create_machine_internal(bus, message, true, userdata, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_create_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
return method_create_machine_internal(bus, message, false, userdata, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_register_machine_internal(sd_bus *bus, sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
|
2014-02-11 17:15:38 +01:00
|
|
|
Manager *manager = userdata;
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
Machine *m = NULL;
|
|
|
|
int r;
|
|
|
|
|
2014-07-10 22:47:55 +02:00
|
|
|
r = method_create_or_register_machine(manager, message, read_network, &m, error);
|
2014-02-11 17:15:38 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = cg_pid_get_unit(m->leader, &m->unit);
|
|
|
|
if (r < 0) {
|
|
|
|
r = sd_bus_error_set_errnof(error, r, "Failed to determine unit of process "PID_FMT" : %s", m->leader, strerror(-r));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-07-03 20:19:58 +02:00
|
|
|
m->registered = true;
|
|
|
|
|
2014-02-11 17:15:38 +01:00
|
|
|
r = machine_start(m, NULL, error);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
p = machine_bus_path(m);
|
|
|
|
if (!p) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sd_bus_reply_method_return(message, "o", p);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
machine_add_to_gc_queue(m);
|
2013-07-02 03:47:23 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:47:55 +02:00
|
|
|
static int method_register_machine_with_network(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
return method_register_machine_internal(bus, message, true, userdata, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_register_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
return method_register_machine_internal(bus, message, false, userdata, error);
|
|
|
|
}
|
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
static int method_terminate_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
2013-07-02 03:47:23 +02:00
|
|
|
Manager *m = userdata;
|
2013-10-30 02:06:55 +01:00
|
|
|
Machine *machine;
|
|
|
|
const char *name;
|
2013-07-02 03:47:23 +02:00
|
|
|
int r;
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
assert(bus);
|
2013-07-02 03:47:23 +02:00
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
machine = hashmap_get(m->machines, name);
|
|
|
|
if (!machine)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2014-05-18 13:48:53 +02:00
|
|
|
return bus_machine_method_terminate(bus, message, machine, error);
|
2013-10-30 02:06:55 +01:00
|
|
|
}
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
static int method_kill_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
2013-10-30 02:06:55 +01:00
|
|
|
Manager *m = userdata;
|
|
|
|
Machine *machine;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2014-05-18 13:48:53 +02:00
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
2013-10-30 02:06:55 +01:00
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
machine = hashmap_get(m->machines, name);
|
|
|
|
if (!machine)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2014-05-18 13:48:53 +02:00
|
|
|
return bus_machine_method_kill(bus, message, machine, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int method_get_machine_addresses(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
Machine *machine;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
2013-10-30 02:06:55 +01:00
|
|
|
if (r < 0)
|
2013-11-21 19:34:37 +01:00
|
|
|
return sd_bus_error_set_errno(error, r);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2014-05-18 13:48:53 +02:00
|
|
|
machine = hashmap_get(m->machines, name);
|
|
|
|
if (!machine)
|
|
|
|
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
|
|
|
|
|
|
|
|
return bus_machine_method_get_addresses(bus, message, machine, error);
|
2013-10-30 02:06:55 +01:00
|
|
|
}
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2014-07-03 17:50:55 +02:00
|
|
|
static int method_get_machine_os_release(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
Machine *machine;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_error_set_errno(error, r);
|
|
|
|
|
|
|
|
machine = hashmap_get(m->machines, name);
|
|
|
|
if (!machine)
|
|
|
|
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
|
|
|
|
|
|
|
|
return bus_machine_method_get_os_release(bus, message, machine, error);
|
|
|
|
}
|
|
|
|
|
2014-12-19 18:42:50 +01:00
|
|
|
static int method_list_images(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
|
|
|
_cleanup_(image_hashmap_freep) Hashmap *images = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Image *image;
|
|
|
|
Iterator i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
images = hashmap_new(&string_hash_ops);
|
|
|
|
if (!images)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
r = image_discover(images);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_new_method_return(message, &reply);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-12-25 03:19:19 +01:00
|
|
|
r = sd_bus_message_open_container(reply, 'a', "(ssbtto)");
|
2014-12-19 18:42:50 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
HASHMAP_FOREACH(image, images, i) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
|
|
|
p = image_bus_path(image->name);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-12-25 03:19:19 +01:00
|
|
|
r = sd_bus_message_append(reply, "(ssbtto)",
|
2014-12-19 18:42:50 +01:00
|
|
|
image->name,
|
|
|
|
image_type_to_string(image->type),
|
|
|
|
image->read_only,
|
2014-12-25 03:19:19 +01:00
|
|
|
image->crtime,
|
|
|
|
image->mtime,
|
2014-12-19 18:42:50 +01:00
|
|
|
p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(reply);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return sd_bus_send(bus, reply, NULL);
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:17:29 +01:00
|
|
|
static int method_open_machine_pty(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
Machine *machine;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_error_set_errno(error, r);
|
|
|
|
|
|
|
|
machine = hashmap_get(m->machines, name);
|
|
|
|
if (!machine)
|
|
|
|
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
|
|
|
|
|
|
|
|
return bus_machine_method_open_pty(bus, message, machine, error);
|
|
|
|
}
|
|
|
|
|
2014-12-23 19:12:49 +01:00
|
|
|
static int method_open_machine_login(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
|
|
|
Manager *m = userdata;
|
|
|
|
Machine *machine;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
r = sd_bus_message_read(message, "s", &name);
|
|
|
|
if (r < 0)
|
|
|
|
return sd_bus_error_set_errno(error, r);
|
|
|
|
|
|
|
|
machine = hashmap_get(m->machines, name);
|
|
|
|
if (!machine)
|
|
|
|
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
|
|
|
|
|
|
|
|
return bus_machine_method_open_login(bus, message, machine, error);
|
|
|
|
}
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
const sd_bus_vtable manager_vtable[] = {
|
|
|
|
SD_BUS_VTABLE_START(0),
|
2013-12-10 17:41:39 +01:00
|
|
|
SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine, SD_BUS_VTABLE_UNPRIVILEGED),
|
2014-12-19 20:07:23 +01:00
|
|
|
SD_BUS_METHOD("GetImage", "s", "o", method_get_image, SD_BUS_VTABLE_UNPRIVILEGED),
|
2013-12-10 17:41:39 +01:00
|
|
|
SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
|
|
|
|
SD_BUS_METHOD("ListMachines", NULL, "a(ssso)", method_list_machines, SD_BUS_VTABLE_UNPRIVILEGED),
|
2014-12-25 03:19:19 +01:00
|
|
|
SD_BUS_METHOD("ListImages", NULL, "a(ssbtto)", method_list_images, SD_BUS_VTABLE_UNPRIVILEGED),
|
2013-10-30 02:06:55 +01:00
|
|
|
SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine, 0),
|
2014-07-10 22:47:55 +02:00
|
|
|
SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0),
|
2014-11-06 20:00:14 +01:00
|
|
|
SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0),
|
2014-07-10 22:47:55 +02:00
|
|
|
SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network, 0),
|
2013-12-10 17:41:39 +01:00
|
|
|
SD_BUS_METHOD("KillMachine", "ssi", NULL, method_kill_machine, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
|
|
|
|
SD_BUS_METHOD("TerminateMachine", "s", NULL, method_terminate_machine, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
|
2014-08-04 19:05:06 +02:00
|
|
|
SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses, SD_BUS_VTABLE_UNPRIVILEGED),
|
2014-07-03 17:50:55 +02:00
|
|
|
SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release, SD_BUS_VTABLE_UNPRIVILEGED),
|
2014-12-22 21:17:29 +01:00
|
|
|
SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty, 0),
|
2014-12-23 21:28:22 +01:00
|
|
|
SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login, SD_BUS_VTABLE_UNPRIVILEGED),
|
2013-10-30 02:06:55 +01:00
|
|
|
SD_BUS_SIGNAL("MachineNew", "so", 0),
|
|
|
|
SD_BUS_SIGNAL("MachineRemoved", "so", 0),
|
|
|
|
SD_BUS_VTABLE_END
|
|
|
|
};
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
int match_job_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
2013-10-30 02:06:55 +01:00
|
|
|
const char *path, *result, *unit;
|
2013-07-02 03:47:23 +02:00
|
|
|
Manager *m = userdata;
|
2013-10-30 02:06:55 +01:00
|
|
|
Machine *machine;
|
|
|
|
uint32_t id;
|
|
|
|
int r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
assert(bus);
|
2013-07-02 03:47:23 +02:00
|
|
|
assert(message);
|
2013-10-30 02:06:55 +01:00
|
|
|
assert(m);
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
|
|
|
|
if (r < 0) {
|
2013-11-21 19:34:37 +01:00
|
|
|
bus_log_parse_error(r);
|
|
|
|
return r;
|
2013-10-30 02:06:55 +01:00
|
|
|
}
|
2013-07-10 23:31:40 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
machine = hashmap_get(m->machine_units, unit);
|
|
|
|
if (!machine)
|
|
|
|
return 0;
|
2013-07-10 23:31:40 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
if (streq_ptr(path, machine->scope_job)) {
|
|
|
|
free(machine->scope_job);
|
|
|
|
machine->scope_job = NULL;
|
2013-07-10 23:31:40 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
if (machine->started) {
|
|
|
|
if (streq(result, "done"))
|
|
|
|
machine_send_create_reply(machine, NULL);
|
|
|
|
else {
|
2013-11-21 19:34:37 +01:00
|
|
|
_cleanup_bus_error_free_ sd_bus_error e = SD_BUS_ERROR_NULL;
|
2013-07-10 23:31:40 +02:00
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
|
2013-07-10 23:31:40 +02:00
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
machine_send_create_reply(machine, &e);
|
2013-10-30 02:06:55 +01:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
machine_save(machine);
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
machine_add_to_gc_queue(machine);
|
|
|
|
return 0;
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
2013-10-30 02:06:55 +01:00
|
|
|
_cleanup_free_ char *unit = NULL;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Machine *machine;
|
|
|
|
const char *path;
|
2013-11-21 19:34:37 +01:00
|
|
|
int r;
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
path = sd_bus_message_get_path(message);
|
|
|
|
if (!path)
|
2013-07-11 19:53:46 +02:00
|
|
|
return 0;
|
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
r = unit_name_from_dbus_path(path, &unit);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
machine = hashmap_get(m->machine_units, unit);
|
|
|
|
if (machine)
|
|
|
|
machine_add_to_gc_queue(machine);
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
int match_unit_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
2013-10-30 02:06:55 +01:00
|
|
|
const char *path, *unit;
|
|
|
|
Manager *m = userdata;
|
|
|
|
Machine *machine;
|
|
|
|
int r;
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
assert(bus);
|
|
|
|
assert(message);
|
|
|
|
assert(m);
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read(message, "so", &unit, &path);
|
|
|
|
if (r < 0) {
|
2013-11-21 19:34:37 +01:00
|
|
|
bus_log_parse_error(r);
|
|
|
|
return r;
|
2013-07-11 19:53:46 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
machine = hashmap_get(m->machine_units, unit);
|
|
|
|
if (machine)
|
|
|
|
machine_add_to_gc_queue(machine);
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-11-21 19:34:37 +01:00
|
|
|
int match_reloading(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
2013-10-30 02:06:55 +01:00
|
|
|
Manager *m = userdata;
|
2013-11-05 00:52:46 +01:00
|
|
|
Machine *machine;
|
|
|
|
Iterator i;
|
2013-10-30 02:06:55 +01:00
|
|
|
int b, r;
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
assert(bus);
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read(message, "b", &b);
|
|
|
|
if (r < 0) {
|
2013-11-21 19:34:37 +01:00
|
|
|
bus_log_parse_error(r);
|
|
|
|
return r;
|
2013-07-11 19:53:46 +02:00
|
|
|
}
|
2013-11-05 00:52:46 +01:00
|
|
|
if (b)
|
|
|
|
return 0;
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-11-05 00:52:46 +01:00
|
|
|
/* systemd finished reloading, let's recheck all our machines */
|
|
|
|
log_debug("System manager has been reloaded, rechecking machines...");
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2013-11-05 00:52:46 +01:00
|
|
|
HASHMAP_FOREACH(machine, m->machines, i)
|
|
|
|
machine_add_to_gc_queue(machine);
|
2013-07-11 19:53:46 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-02 03:47:23 +02:00
|
|
|
int manager_start_scope(
|
|
|
|
Manager *manager,
|
|
|
|
const char *scope,
|
|
|
|
pid_t pid,
|
|
|
|
const char *slice,
|
|
|
|
const char *description,
|
2013-10-30 02:06:55 +01:00
|
|
|
sd_bus_message *more_properties,
|
|
|
|
sd_bus_error *error,
|
2013-07-02 03:47:23 +02:00
|
|
|
char **job) {
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
|
2013-07-11 19:53:46 +02:00
|
|
|
int r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
assert(scope);
|
|
|
|
assert(pid > 1);
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_new_method_call(
|
|
|
|
manager->bus,
|
2014-02-19 23:54:58 +01:00
|
|
|
&m,
|
2013-07-02 03:47:23 +02:00
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
2014-02-19 23:54:58 +01:00
|
|
|
"StartTransientUnit");
|
2013-10-30 02:06:55 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-11-05 00:52:46 +01:00
|
|
|
r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
|
2013-10-30 02:06:55 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_open_container(m, 'a', "(sv)");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
if (!isempty(slice)) {
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isempty(description)) {
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-11 19:53:46 +02:00
|
|
|
|
2014-11-05 17:57:23 +01:00
|
|
|
r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-07-11 19:53:46 +02:00
|
|
|
if (more_properties) {
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_copy(m, more_properties, true);
|
2013-07-11 19:53:46 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_close_container(m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
2013-11-21 20:05:49 +01:00
|
|
|
r = sd_bus_message_append(m, "a(sa(sv))", 0);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-11-11 18:55:34 +01:00
|
|
|
r = sd_bus_call(manager->bus, m, 0, error, &reply);
|
2013-10-30 02:06:55 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
if (job) {
|
|
|
|
const char *j;
|
|
|
|
char *copy;
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read(reply, "o", &j);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
copy = strdup(j);
|
|
|
|
if (!copy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*job = copy;
|
|
|
|
}
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
return 1;
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
2013-07-02 03:47:23 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
assert(unit);
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_call_method(
|
2013-07-02 03:47:23 +02:00
|
|
|
manager->bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
|
|
|
"StopUnit",
|
|
|
|
error,
|
2013-10-30 02:06:55 +01:00
|
|
|
&reply,
|
|
|
|
"ss", unit, "fail");
|
2013-07-02 03:47:23 +02:00
|
|
|
if (r < 0) {
|
2013-10-30 02:06:55 +01:00
|
|
|
if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
|
|
|
|
sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
|
2013-07-10 23:31:40 +02:00
|
|
|
|
|
|
|
if (job)
|
|
|
|
*job = NULL;
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
sd_bus_error_free(error);
|
2013-07-10 23:31:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-02 03:47:23 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (job) {
|
|
|
|
const char *j;
|
|
|
|
char *copy;
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read(reply, "o", &j);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-07-02 03:47:23 +02:00
|
|
|
|
|
|
|
copy = strdup(j);
|
|
|
|
if (!copy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*job = copy;
|
|
|
|
}
|
|
|
|
|
2013-07-10 23:31:40 +02:00
|
|
|
return 1;
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
2014-03-18 04:43:08 +01:00
|
|
|
int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) {
|
2013-07-02 03:47:23 +02:00
|
|
|
assert(manager);
|
|
|
|
assert(unit);
|
|
|
|
|
2013-11-05 00:52:46 +01:00
|
|
|
return sd_bus_call_method(
|
2013-07-02 03:47:23 +02:00
|
|
|
manager->bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager",
|
|
|
|
"KillUnit",
|
|
|
|
error,
|
2013-11-05 00:52:46 +01:00
|
|
|
NULL,
|
2014-03-18 04:43:08 +01:00
|
|
|
"ssi", unit, "all", signo);
|
2013-07-02 03:47:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int manager_unit_is_active(Manager *manager, const char *unit) {
|
2013-10-30 02:06:55 +01:00
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
2013-07-02 03:47:23 +02:00
|
|
|
_cleanup_free_ char *path = NULL;
|
|
|
|
const char *state;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
assert(unit);
|
|
|
|
|
|
|
|
path = unit_dbus_path_from_name(unit);
|
|
|
|
if (!path)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_get_property(
|
2013-07-02 03:47:23 +02:00
|
|
|
manager->bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
path,
|
2013-10-30 02:06:55 +01:00
|
|
|
"org.freedesktop.systemd1.Unit",
|
|
|
|
"ActiveState",
|
2013-07-02 03:47:23 +02:00
|
|
|
&error,
|
2013-10-30 02:06:55 +01:00
|
|
|
&reply,
|
|
|
|
"s");
|
2013-07-02 03:47:23 +02:00
|
|
|
if (r < 0) {
|
2013-10-30 02:06:55 +01:00
|
|
|
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
|
|
|
|
sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
|
2013-07-10 23:31:40 +02:00
|
|
|
return true;
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
|
|
|
|
sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
|
2013-07-10 23:31:40 +02:00
|
|
|
return false;
|
|
|
|
|
2013-07-02 03:47:23 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_message_read(reply, "s", &state);
|
|
|
|
if (r < 0)
|
2013-07-02 03:47:23 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return !streq(state, "inactive") && !streq(state, "failed");
|
|
|
|
}
|
2013-09-25 13:26:08 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
int manager_job_is_active(Manager *manager, const char *path) {
|
|
|
|
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
|
|
|
|
int r;
|
2013-09-25 13:26:08 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
assert(manager);
|
|
|
|
assert(path);
|
2013-09-25 13:26:08 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
r = sd_bus_get_property(
|
|
|
|
manager->bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
path,
|
|
|
|
"org.freedesktop.systemd1.Job",
|
|
|
|
"State",
|
|
|
|
&error,
|
|
|
|
&reply,
|
|
|
|
"s");
|
|
|
|
if (r < 0) {
|
|
|
|
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
|
|
|
|
sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
|
|
|
|
return true;
|
2013-09-25 13:26:08 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
|
|
|
|
return false;
|
2013-09-25 13:26:08 +02:00
|
|
|
|
|
|
|
return r;
|
2013-10-30 02:06:55 +01:00
|
|
|
}
|
2013-09-25 13:26:08 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
/* We don't actually care about the state really. The fact
|
|
|
|
* that we could read the job state is enough for us */
|
2013-09-25 13:26:08 +02:00
|
|
|
|
2013-10-30 02:06:55 +01:00
|
|
|
return true;
|
2013-09-25 13:26:08 +02:00
|
|
|
}
|
2013-11-07 14:56:31 +01:00
|
|
|
|
|
|
|
int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) {
|
|
|
|
_cleanup_free_ char *unit = NULL;
|
|
|
|
Machine *mm;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(pid >= 1);
|
|
|
|
assert(machine);
|
|
|
|
|
|
|
|
r = cg_pid_get_unit(pid, &unit);
|
|
|
|
if (r < 0)
|
|
|
|
mm = hashmap_get(m->machine_leaders, UINT_TO_PTR(pid));
|
|
|
|
else
|
|
|
|
mm = hashmap_get(m->machine_units, unit);
|
|
|
|
|
|
|
|
if (!mm)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*machine = mm;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int manager_add_machine(Manager *m, const char *name, Machine **_machine) {
|
|
|
|
Machine *machine;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(name);
|
|
|
|
|
|
|
|
machine = hashmap_get(m->machines, name);
|
|
|
|
if (!machine) {
|
|
|
|
machine = machine_new(m, name);
|
|
|
|
if (!machine)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_machine)
|
|
|
|
*_machine = machine;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|