898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright 2011 Lennart Poettering
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.
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.
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/>.
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer const char *path,
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier return sd_bus_message_append(reply, "s", "/var/lib/machines");
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 fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (stat("/var/lib/machines.raw", &st) >= 0) {
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek if (usage == (uint64_t) -1 || st.st_blocks * 512ULL > usage)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_message_append(reply, "t", usage);
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 fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0)
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 return sd_bus_message_append(reply, "t", size);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int method_get_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "s", &name);
b8667ee4162cd2510363602b417cecede9fd2ccaZbigniew Jędrzejewski-Szmek machine = hashmap_get(m->machines, name);
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_reply_method_return(message, "o", p);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int method_get_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "s", &name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_reply_method_return(message, "o", p);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int method_get_machine_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin assert_cc(sizeof(pid_t) == sizeof(uint32_t));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "u", &pid);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = manager_get_machine_by_pid(m, pid, &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 return sd_bus_reply_method_return(message, "o", p);
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 r = sd_bus_message_new_method_return(message, &reply);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_open_container(reply, 'a', "(ssso)");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier strempty(machine_class_to_string(machine->class)),
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 void *v;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "s", &name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name");
417491f122b346a31cf8dc406c4f9195a5900cecEvgeny Vereshchagin r = sd_bus_message_read_array(message, 'y', &v, &n);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier else if (n == 16)
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine ID parameter");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier for (i = 0; i < n_netif; i++) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
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 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_MACHINE_EXISTS, "Machine '%s' already exists", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->netif = memdup(netif, sizeof(int32_t) * n_netif);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int method_create_machine_internal(sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = method_create_or_register_machine(manager, message, read_network, &m, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_enter_container(message, 'a', "(sv)");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->create_message = sd_bus_message_ref(message);
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 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 Hoyerstatic int method_register_machine_internal(sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = method_create_or_register_machine(manager, message, read_network, &m, error);
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 return sd_bus_reply_method_return(message, "o", p);
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 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 Hoyerstatic int method_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_terminate(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_kill_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_kill(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_get_machine_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_get_addresses(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_get_machine_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_get_os_release(message, machine, error);
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 r = sd_bus_message_new_method_return(message, &reply);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_open_container(reply, 'a', "(ssbttto)");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_open_machine_pty(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_open_pty(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_open_machine_login(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_open_login(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_open_machine_shell(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_open_shell(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_bind_mount_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_bind_mount(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_copy_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_machine_method_copy(message, machine, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_image_method_remove(message, i, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_image_method_rename(message, i, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "s", &old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", old_name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_image_method_clone(message, i, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_mark_image_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_image_method_mark_read_only(message, i, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "org.freedesktop.machine1.manage-machines",
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Set up the machine directory if necessary */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Resize the backing loopback device, if there is one, except if we asked to drop any limit */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = btrfs_resize_loopback("/var/lib/machines", limit, false);
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 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs.");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_reply_method_return(message, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return bus_image_method_set_limit(message, i, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = sd_bus_message_read(message, "su", &name, &uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer p = procfs_file_alloca(machine->leader, "uid_map");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer uid_t uid_base, uid_shift, uid_range, converted;
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 if (k != 3) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (uid < uid_base || uid >= uid_base + uid_range)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
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 char p[strlen("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1];
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer xsprintf(p, "/proc/" UID_FMT "/uid_map", machine->leader);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_warning_errno(errno, "Failed top open %s, ignoring,", p);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer uid_t uid_base, uid_shift, uid_range, converted;
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 if (k != 3) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (uid < uid_shift || uid >= uid_shift + uid_range)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int method_map_from_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = sd_bus_message_read(message, "su", &name, &gid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer p = procfs_file_alloca(machine->leader, "gid_map");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer gid_t gid_base, gid_shift, gid_range, converted;
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 if (k != 3) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (gid < gid_base || gid >= gid_base + gid_range)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Machine '%s' has no matching group mappings.", name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int method_map_to_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
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 char p[strlen("/proc//gid_map") + DECIMAL_STR_MAX(pid_t) + 1];
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer xsprintf(p, "/proc/" GID_FMT "/gid_map", machine->leader);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_warning_errno(errno, "Failed top open %s, ignoring,", p);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer gid_t gid_base, gid_shift, gid_range, converted;
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 if (k != 3) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (gid < gid_shift || gid >= gid_shift + gid_range)
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid);
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 Hoyerint match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer machine->scope_job = mfree(machine->scope_job);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
assert(m);
if (!path)
log_oom();
if (!machine)
assert(m);
if (!machine)
Iterator i;
assert(m);
const char *scope,
const char *slice,
const char *description,
char **job) {
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
return bus_log_create_error(r);
if (more_properties) {
r = sd_bus_message_close_container(m);
if (job) {
if (!copy)
return -ENOMEM;
r = sd_bus_call_method(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
&reply,
if (job)
if (job) {
if (!copy)
return -ENOMEM;
return sd_bus_call_method(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
NULL,
if (!path)
return -ENOMEM;
r = sd_bus_get_property(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
&error,
&reply,
return -EINVAL;
r = sd_bus_get_property(
"org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Job",
&error,
&reply,
assert(m);
if (!mm) {
if (!mm)
assert(m);
if (!machine) {
if (!machine)
return -ENOMEM;
if (_machine)