898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/***
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright 2011 Lennart Poettering
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer systemd is free software; you can redistribute it and/or modify it
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin under the terms of the GNU Lesser General Public License as published by
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer the Free Software Foundation; either version 2.1 of the License, or
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer (at your option) any later version.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt systemd is distributed in the hope that it will be useful, but
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt WITHOUT ANY WARRANTY; without even the implied warranty of
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt Lesser General Public License for more details.
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt
4be4833ece2856e0cacc09f8f8b2c02b320751faMartin Pitt You should have received a copy of the GNU Lesser General Public License
c50a4525aa8151b180d5a325e88c5f3812e66c36Martin Pitt along with systemd; If not, see <http://www.gnu.org/licenses/>.
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier***/
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <errno.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <string.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include <unistd.h>
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "sd-id128.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "alloc-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "btrfs-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "bus-common-errors.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "bus-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "cgroup-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "fd-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "formats-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "hostname-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "image-dbus.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "io-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "machine-dbus.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "machine-image.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "machine-pool.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "machined.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "path-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "process-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "stdio-util.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "strv.h"
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier#include "unit-name.h"
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier#include "user-util.h"
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyerstatic int property_get_pool_path(
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer sd_bus *bus,
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer const char *path,
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer const char *interface,
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer const char *property,
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer sd_bus_message *reply,
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer void *userdata,
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin sd_bus_error *error) {
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier assert(bus);
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin assert(reply);
61fea35e14d84144e6e2122f5cd247f9c7e6245eEvgeny Vereshchagin
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier return sd_bus_message_append(reply, "s", "/var/lib/machines");
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier}
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalierstatic int property_get_pool_usage(
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier sd_bus *bus,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier const char *path,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier const char *interface,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier const char *property,
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt sd_bus_message *reply,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier void *userdata,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier sd_bus_error *error) {
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier _cleanup_close_ int fd = -1;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier uint64_t usage = (uint64_t) -1;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier struct stat st;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier assert(bus);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier assert(reply);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier /* We try to read the quota info from /var/lib/machines, as
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier * well as the usage of the loopback file
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier * /var/lib/machines.raw, and pick the larger value. */
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier if (fd >= 0) {
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier BtrfsQuotaInfo q;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0)
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek usage = q.referenced;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (stat("/var/lib/machines.raw", &st) >= 0) {
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek if (usage == (uint64_t) -1 || st.st_blocks * 512ULL > usage)
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt usage = st.st_blocks * 512ULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_message_append(reply, "t", usage);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int property_get_pool_limit(
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier sd_bus *bus,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *path,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *interface,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *property,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier sd_bus_message *reply,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier void *userdata,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier sd_bus_error *error) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_close_ int fd = -1;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier uint64_t size = (uint64_t) -1;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier struct stat st;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(bus);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(reply);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* We try to read the quota limit from /var/lib/machines, as
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier * well as the size of the loopback file
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier * /var/lib/machines.raw, and pick the smaller value. */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (fd >= 0) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier BtrfsQuotaInfo q;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier size = q.referenced_max;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (stat("/var/lib/machines.raw", &st) >= 0) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (size == (uint64_t) -1 || (uint64_t) st.st_size < size)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier size = st.st_size;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_message_append(reply, "t", size);
2375607039517c88df51ef16ddbb624ec1c10654Kay Sievers}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int method_get_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_free_ char *p = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Manager *m = userdata;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Machine *machine;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *name;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(message);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(m);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "s", &name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
b8667ee4162cd2510363602b417cecede9fd2ccaZbigniew Jędrzejewski-Szmek machine = hashmap_get(m->machines, name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!machine)
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
edbced8a151c1b7ded685e2ec644950d2adec5f5Harald Hoyer p = machine_bus_path(machine);
edbced8a151c1b7ded685e2ec644950d2adec5f5Harald Hoyer if (!p)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return -ENOMEM;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_reply_method_return(message, "o", p);
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int method_get_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_free_ char *p = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Manager *m = userdata;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *name;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(message);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(m);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "s", &name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = image_find(name, NULL);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r == 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier p = image_bus_path(name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!p)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return -ENOMEM;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_reply_method_return(message, "o", p);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int method_get_machine_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin _cleanup_free_ char *p = NULL;
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin Manager *m = userdata;
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin Machine *machine = NULL;
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin pid_t pid;
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin int r;
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin assert(message);
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin assert(m);
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin assert_cc(sizeof(pid_t) == sizeof(uint32_t));
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "u", &pid);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (pid < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return -EINVAL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (pid == 0) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_creds_get_pid(creds, &pid);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = manager_get_machine_by_pid(m, pid, &machine);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!machine)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, BUS_ERROR_NO_MACHINE_FOR_PID, "PID "PID_FMT" does not belong to any known machine", pid);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier p = machine_bus_path(machine);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!p)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return -ENOMEM;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_reply_method_return(message, "o", p);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int method_list_machines(sd_bus_message *message, void *userdata, sd_bus_error *error) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Manager *m = userdata;
bf3a947cb44f31359bba313e0252cbcc0dc95b03Evgeny Vereshchagin Machine *machine;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Iterator i;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(message);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(m);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_new_method_return(message, &reply);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_set_errno(error, r);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_open_container(reply, 'a', "(ssso)");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_set_errno(error, r);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier HASHMAP_FOREACH(machine, m->machines, i) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_free_ char *p = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier p = machine_bus_path(machine);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!p)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return -ENOMEM;
4be4833ece2856e0cacc09f8f8b2c02b320751faMartin Pitt
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt r = sd_bus_message_append(reply, "(ssso)",
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt machine->name,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier strempty(machine_class_to_string(machine->class)),
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier machine->service,
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier p);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_set_errno(error, r);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_close_container(reply);
cffae62bcb6912fbaf1b7b282d9d170c9d308897Martin Pitt if (r < 0)
99877b7e3782a51b31bf191825f0335500f52fe5Harald Hoyer return sd_bus_error_set_errno(error, r);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_send(NULL, reply, NULL);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pittstatic int method_create_or_register_machine(Manager *manager, sd_bus_message *message, bool read_network, Machine **_m, sd_bus_error *error) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const char *name, *service, *class, *root_directory;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const int32_t *netif = NULL;
e63b61be5350dbe92ea12e1eeb96dde251ed9292Evgeny Vereshchagin MachineClass c;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier uint32_t leader;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier sd_id128_t id;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier const void *v;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Machine *m;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier size_t n, n_netif = 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier int r;
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin assert(manager);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(message);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier assert(_m);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "s", &name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
0fe15dc8ddddeb39a5cad1f4f4afa25fa074a5d1Evgeny Vereshchagin return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!machine_name_is_valid(name))
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name");
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin r = sd_bus_message_read_array(message, 'y', &v, &n);
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (n == 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier id = SD_ID128_NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else if (n == 16)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier memcpy(&id, v, n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine ID parameter");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (read_network) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier size_t i;
25b47f96d9601ff566257b2a31bfb5f4bd25d661Marko Myllynen
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier n_netif /= sizeof(int32_t);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier for (i = 0; i < n_netif; i++) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (netif[i] <= 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (isempty(class))
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier c = _MACHINE_CLASS_INVALID;
53d90f9582f96208b3674da823ad1a3d2c3b1aa4Martin Pitt else {
5c404f1ab8e96efedb983806443ca982a1b2a372Evgeny Vereshchagin c = machine_class_from_string(class);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (c < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier }
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (leader == 1)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (!isempty(root_directory) && !path_is_absolute(root_directory))
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (leader == 0) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer assert_cc(sizeof(uint32_t) == sizeof(pid_t));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (r < 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (hashmap_get(manager->machines, name))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_MACHINE_EXISTS, "Machine '%s' already exists", name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = manager_add_machine(manager, name, &m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->leader = leader;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->class = c;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->id = id;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!isempty(service)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->service = strdup(service);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!m->service) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!isempty(root_directory)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->root_directory = strdup(root_directory);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!m->root_directory) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n_netif > 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert_cc(sizeof(int32_t) == sizeof(int));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->netif = memdup(netif, sizeof(int32_t) * n_netif);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!m->netif) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->n_netif = n_netif;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer *_m = m;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 1;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfail:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine_add_to_gc_queue(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier}
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int method_create_machine_internal(sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Manager *manager = userdata;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier Machine *m = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(manager);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = method_create_or_register_machine(manager, message, read_network, &m, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_enter_container(message, 'a', "(sv)");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = machine_start(m, message, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->create_message = sd_bus_message_ref(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 1;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfail:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine_add_to_gc_queue(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_create_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return method_create_machine_internal(message, true, userdata, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_create_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return method_create_machine_internal(message, false, userdata, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_register_machine_internal(sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *manager = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_free_ char *p = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *m = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(manager);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = method_create_or_register_machine(manager, message, read_network, &m, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = cg_pid_get_unit(m->leader, &m->unit);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_error_set_errnof(error, r, "Failed to determine unit of process "PID_FMT" : %s", m->leader, strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = machine_start(m, NULL, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer p = machine_bus_path(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!p) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer goto fail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_reply_method_return(message, "o", p);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerfail:
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine_add_to_gc_queue(m);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_register_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return method_register_machine_internal(message, true, userdata, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_register_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return method_register_machine_internal(message, false, userdata, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_set_errno(error, r);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine = hashmap_get(m->machines, name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!machine)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_terminate(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_kill_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_set_errno(error, r);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine = hashmap_get(m->machines, name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!machine)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_kill(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_get_machine_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_set_errno(error, r);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine = hashmap_get(m->machines, name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!machine)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_get_addresses(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_get_machine_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_set_errno(error, r);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine = hashmap_get(m->machines, name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!machine)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_get_os_release(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(image_hashmap_freep) Hashmap *images = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Image *image;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Iterator i;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer images = hashmap_new(&string_hash_ops);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!images)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = image_discover(images);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_new_method_return(message, &reply);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_open_container(reply, 'a', "(ssbttto)");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer HASHMAP_FOREACH(image, images, i) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_free_ char *p = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer p = image_bus_path(image->name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!p)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_append(reply, "(ssbttto)",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer image->name,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer image_type_to_string(image->type),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer image->read_only,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer image->crtime,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer image->mtime,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer image->usage,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer p);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_close_container(reply);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_send(NULL, reply, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_open_machine_pty(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_set_errno(error, r);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine = hashmap_get(m->machines, name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!machine)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_open_pty(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_open_machine_login(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine = hashmap_get(m->machines, name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!machine)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_open_login(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_open_machine_shell(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine = hashmap_get(m->machines, name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!machine)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_open_shell(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_bind_mount_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine = hashmap_get(m->machines, name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!machine)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_bind_mount(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_copy_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine = hashmap_get(m->machines, name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!machine)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_copy(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(image_unrefp) Image* i = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!image_name_is_valid(name))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = image_find(name, &i);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer i->userdata = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_image_method_remove(message, i, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(image_unrefp) Image* i = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *old_name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!image_name_is_valid(old_name))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = image_find(old_name, &i);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer i->userdata = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_image_method_rename(message, i, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(image_unrefp) Image *i = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *old_name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!image_name_is_valid(old_name))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = image_find(old_name, &i);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer i->userdata = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_image_method_clone(message, i, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_mark_image_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(image_unrefp) Image *i = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!image_name_is_valid(name))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = image_find(name, &i);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer i->userdata = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_image_method_mark_read_only(message, i, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer uint64_t limit;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "t", &limit);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!FILE_SIZE_VALID_OR_INFINITY(limit))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = bus_verify_polkit_async(
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer message,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer CAP_SYS_ADMIN,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "org.freedesktop.machine1.manage-machines",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer NULL,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer false,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer UID_INVALID,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer &m->polkit_registry,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 1; /* Will call us back */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Set up the machine directory if necessary */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = setup_machine_directory(limit, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Resize the backing loopback device, if there is one, except if we asked to drop any limit */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (limit != (uint64_t) -1) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = btrfs_resize_loopback("/var/lib/machines", limit, false);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == -ENOTTY)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs.");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0 && r != -ENODEV) /* ignore ENODEV, as that's what is returned if the file system is not on loopback */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_set_errnof(error, r, "Failed to adjust loopback limit: %m");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == -ENOTTY)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs.");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_reply_method_return(message, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(image_unrefp) Image *i = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!image_name_is_valid(name))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = image_find(name, &i);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer i->userdata = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_image_method_set_limit(message, i, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_fclose_ FILE *f = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer const char *name, *p;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Machine *machine;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer uint32_t uid;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = sd_bus_message_read(message, "su", &name, &uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!uid_is_valid(uid))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer machine = hashmap_get(m->machines, name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!machine)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (machine->class != MACHINE_CONTAINER)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer p = procfs_file_alloca(machine->leader, "uid_map");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer f = fopen(p, "re");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!f)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -errno;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer for (;;) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer uid_t uid_base, uid_shift, uid_range, converted;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int k;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer errno = 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (k < 0 && feof(f))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer break;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (k != 3) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (ferror(f) && errno > 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -errno;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -EIO;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (uid < uid_base || uid >= uid_base + uid_range)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer continue;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer converted = uid - uid_base + uid_shift;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!uid_is_valid(converted))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Manager *m = userdata;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Machine *machine;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer uid_t uid;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Iterator i;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = sd_bus_message_read(message, "u", &uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!uid_is_valid(uid))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (uid < 0x10000)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "User " UID_FMT " belongs to host UID range", uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer HASHMAP_FOREACH(machine, m->machines, i) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer _cleanup_fclose_ FILE *f = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer char p[strlen("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1];
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (machine->class != MACHINE_CONTAINER)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer continue;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer xsprintf(p, "/proc/" UID_FMT "/uid_map", machine->leader);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer f = fopen(p, "re");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!f) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_warning_errno(errno, "Failed top open %s, ignoring,", p);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer continue;
c53158818d8cdaf46b3f1b5299b9bda118a1043fThomas Hindoe Paaboel Andersen }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer for (;;) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer _cleanup_free_ char *o = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer uid_t uid_base, uid_shift, uid_range, converted;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int k;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer errno = 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (k < 0 && feof(f))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer break;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (k != 3) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (ferror(f) && errno > 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -errno;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -EIO;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (uid < uid_shift || uid >= uid_shift + uid_range)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer continue;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer converted = (uid - uid_shift + uid_base);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!uid_is_valid(converted))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer o = machine_bus_path(machine);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!o)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -ENOMEM;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int method_map_from_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer _cleanup_fclose_ FILE *f = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Manager *m = groupdata;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer const char *name, *p;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Machine *machine;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer uint32_t gid;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = sd_bus_message_read(message, "su", &name, &gid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!gid_is_valid(gid))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer machine = hashmap_get(m->machines, name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!machine)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (machine->class != MACHINE_CONTAINER)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer p = procfs_file_alloca(machine->leader, "gid_map");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer f = fopen(p, "re");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!f)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -errno;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer for (;;) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer gid_t gid_base, gid_shift, gid_range, converted;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int k;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer errno = 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT, &gid_base, &gid_shift, &gid_range);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (k < 0 && feof(f))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer break;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (k != 3) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (ferror(f) && errno > 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -errno;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -EIO;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (gid < gid_base || gid >= gid_base + gid_range)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer continue;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer converted = gid - gid_base + gid_shift;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!gid_is_valid(converted))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Machine '%s' has no matching group mappings.", name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer}
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int method_map_to_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Manager *m = groupdata;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Machine *machine;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer gid_t gid;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer Iterator i;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = sd_bus_message_read(message, "u", &gid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!gid_is_valid(gid))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (gid < 0x10000)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Group " GID_FMT " belongs to host GID range", gid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer HASHMAP_FOREACH(machine, m->machines, i) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer _cleanup_fclose_ FILE *f = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer char p[strlen("/proc//gid_map") + DECIMAL_STR_MAX(pid_t) + 1];
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (machine->class != MACHINE_CONTAINER)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer continue;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer xsprintf(p, "/proc/" GID_FMT "/gid_map", machine->leader);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer f = fopen(p, "re");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!f) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_warning_errno(errno, "Failed top open %s, ignoring,", p);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer continue;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer for (;;) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer _cleanup_free_ char *o = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer gid_t gid_base, gid_shift, gid_range, converted;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer int k;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer errno = 0;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT, &gid_base, &gid_shift, &gid_range);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (k < 0 && feof(f))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer break;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (k != 3) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (ferror(f) && errno > 0)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -errno;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return -EIO;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (gid < gid_shift || gid >= gid_shift + gid_range)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer continue;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer converted = (gid - gid_shift + gid_base);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!gid_is_valid(converted))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer o = machine_bus_path(machine);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!o)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerconst sd_bus_vtable manager_vtable[] = {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_VTABLE_START(0),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("GetImage", "s", "o", method_get_image, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("ListMachines", NULL, "a(ssso)", method_list_machines, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("ListImages", NULL, "a(ssbttto)", method_list_images, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine, 0),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network, 0),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("TerminateMachine", "s", NULL, method_terminate_machine, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("KillMachine", "ssi", NULL, method_kill_machine, SD_BUS_VTABLE_UNPRIVILEGED),
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses, SD_BUS_VTABLE_UNPRIVILEGED),
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release, SD_BUS_VTABLE_UNPRIVILEGED),
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty, 0),
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login, SD_BUS_VTABLE_UNPRIVILEGED),
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("BindMountMachine", "sssbb", NULL, method_bind_mount_machine, SD_BUS_VTABLE_UNPRIVILEGED),
33a5e20ffaa2cbb2853f14265566bac66a7f9026Harald Hoyer SD_BUS_METHOD("CopyFromMachine", "sss", NULL, method_copy_machine, SD_BUS_VTABLE_UNPRIVILEGED),
33a5e20ffaa2cbb2853f14265566bac66a7f9026Harald Hoyer SD_BUS_METHOD("CopyToMachine", "sss", NULL, method_copy_machine, SD_BUS_VTABLE_UNPRIVILEGED),
33a5e20ffaa2cbb2853f14265566bac66a7f9026Harald Hoyer SD_BUS_METHOD("RemoveImage", "s", NULL, method_remove_image, SD_BUS_VTABLE_UNPRIVILEGED),
33a5e20ffaa2cbb2853f14265566bac66a7f9026Harald Hoyer SD_BUS_METHOD("RenameImage", "ss", NULL, method_rename_image, SD_BUS_VTABLE_UNPRIVILEGED),
33a5e20ffaa2cbb2853f14265566bac66a7f9026Harald Hoyer SD_BUS_METHOD("CloneImage", "ssb", NULL, method_clone_image, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL, method_mark_image_read_only, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("SetPoolLimit", "t", NULL, method_set_pool_limit, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("SetImageLimit", "st", NULL, method_set_image_limit, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group, SD_BUS_VTABLE_UNPRIVILEGED),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_SIGNAL("MachineNew", "so", 0),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_SIGNAL("MachineRemoved", "so", 0),
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_BUS_VTABLE_END
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier};
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *path, *result, *unit;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Machine *machine;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer uint32_t id;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(message);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer bus_log_parse_error(r);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine = hashmap_get(m->machine_units, unit);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!machine)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (streq_ptr(path, machine->scope_job)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine->scope_job = mfree(machine->scope_job);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (machine->started) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (streq(result, "done"))
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine_send_create_reply(machine, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer else {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine_send_create_reply(machine, &e);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine_save(machine);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine_add_to_gc_queue(machine);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_free_ char *unit = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *path;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
Machine *machine;
int r;
assert(message);
assert(m);
path = sd_bus_message_get_path(message);
if (!path)
return 0;
r = unit_name_from_dbus_path(path, &unit);
if (r == -EINVAL) /* not for a unit */
return 0;
if (r < 0){
log_oom();
return 0;
}
machine = hashmap_get(m->machine_units, unit);
if (!machine)
return 0;
machine_add_to_gc_queue(machine);
return 0;
}
int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *path, *unit;
Manager *m = userdata;
Machine *machine;
int r;
assert(message);
assert(m);
r = sd_bus_message_read(message, "so", &unit, &path);
if (r < 0) {
bus_log_parse_error(r);
return 0;
}
machine = hashmap_get(m->machine_units, unit);
if (!machine)
return 0;
machine_add_to_gc_queue(machine);
return 0;
}
int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
Machine *machine;
Iterator i;
int b, r;
assert(message);
assert(m);
r = sd_bus_message_read(message, "b", &b);
if (r < 0) {
bus_log_parse_error(r);
return 0;
}
if (b)
return 0;
/* systemd finished reloading, let's recheck all our machines */
log_debug("System manager has been reloaded, rechecking machines...");
HASHMAP_FOREACH(machine, m->machines, i)
machine_add_to_gc_queue(machine);
return 0;
}
int manager_start_scope(
Manager *manager,
const char *scope,
pid_t pid,
const char *slice,
const char *description,
sd_bus_message *more_properties,
sd_bus_error *error,
char **job) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
int r;
assert(manager);
assert(scope);
assert(pid > 1);
r = sd_bus_message_new_method_call(
manager->bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartTransientUnit");
if (r < 0)
return r;
r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
if (r < 0)
return r;
r = sd_bus_message_open_container(m, 'a', "(sv)");
if (r < 0)
return r;
if (!isempty(slice)) {
r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
if (r < 0)
return r;
}
if (!isempty(description)) {
r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
if (r < 0)
return r;
}
r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
if (r < 0)
return r;
r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1);
if (r < 0)
return r;
r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", UINT64_C(16384));
if (r < 0)
return bus_log_create_error(r);
if (more_properties) {
r = sd_bus_message_copy(m, more_properties, true);
if (r < 0)
return r;
}
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "a(sa(sv))", 0);
if (r < 0)
return r;
r = sd_bus_call(manager->bus, m, 0, error, &reply);
if (r < 0)
return r;
if (job) {
const char *j;
char *copy;
r = sd_bus_message_read(reply, "o", &j);
if (r < 0)
return r;
copy = strdup(j);
if (!copy)
return -ENOMEM;
*job = copy;
}
return 1;
}
int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
int r;
assert(manager);
assert(unit);
r = sd_bus_call_method(
manager->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StopUnit",
error,
&reply,
"ss", unit, "fail");
if (r < 0) {
if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
if (job)
*job = NULL;
sd_bus_error_free(error);
return 0;
}
return r;
}
if (job) {
const char *j;
char *copy;
r = sd_bus_message_read(reply, "o", &j);
if (r < 0)
return r;
copy = strdup(j);
if (!copy)
return -ENOMEM;
*job = copy;
}
return 1;
}
int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) {
assert(manager);
assert(unit);
return sd_bus_call_method(
manager->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"KillUnit",
error,
NULL,
"ssi", unit, "all", signo);
}
int manager_unit_is_active(Manager *manager, const char *unit) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_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;
r = sd_bus_get_property(
manager->bus,
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"ActiveState",
&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;
if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
return false;
return r;
}
r = sd_bus_message_read(reply, "s", &state);
if (r < 0)
return -EINVAL;
return !STR_IN_SET(state, "inactive", "failed");
}
int manager_job_is_active(Manager *manager, const char *path) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
int r;
assert(manager);
assert(path);
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;
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
return false;
return r;
}
/* We don't actually care about the state really. The fact
* that we could read the job state is enough for us */
return true;
}
int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) {
Machine *mm;
int r;
assert(m);
assert(pid >= 1);
assert(machine);
mm = hashmap_get(m->machine_leaders, PID_TO_PTR(pid));
if (!mm) {
_cleanup_free_ char *unit = NULL;
r = cg_pid_get_unit(pid, &unit);
if (r >= 0)
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, _MACHINE_CLASS_INVALID, name);
if (!machine)
return -ENOMEM;
}
if (_machine)
*_machine = machine;
return 0;
}