/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
-#include <string.h>
#include <unistd.h>
#include "sd-id128.h"
#include "alloc-util.h"
#include "btrfs-util.h"
#include "bus-common-errors.h"
+#include "bus-polkit.h"
#include "bus-util.h"
#include "cgroup-util.h"
+#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "machine-image.h"
#include "machine-pool.h"
#include "machined.h"
+#include "missing_capability.h"
#include "path-util.h"
#include "process-util.h"
#include "stdio-util.h"
#include "strv.h"
+#include "tmpfile-util.h"
#include "unit-name.h"
#include "user-util.h"
_cleanup_close_ int fd = -1;
uint64_t usage = (uint64_t) -1;
- struct stat st;
assert(bus);
assert(reply);
- /* We try to read the quota info from /var/lib/machines, as
- * well as the usage of the loopback file
- * /var/lib/machines.raw, and pick the larger value. */
-
fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
if (fd >= 0) {
BtrfsQuotaInfo q;
usage = q.referenced;
}
- if (stat("/var/lib/machines.raw", &st) >= 0) {
- if (usage == (uint64_t) -1 || st.st_blocks * 512ULL > usage)
- usage = st.st_blocks * 512ULL;
- }
-
return sd_bus_message_append(reply, "t", usage);
}
_cleanup_close_ int fd = -1;
uint64_t size = (uint64_t) -1;
- struct stat st;
assert(bus);
assert(reply);
- /* We try to read the quota limit from /var/lib/machines, as
- * well as the size of the loopback file
- * /var/lib/machines.raw, and pick the smaller value. */
-
fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
if (fd >= 0) {
BtrfsQuotaInfo q;
size = q.referenced_max;
}
- if (stat("/var/lib/machines.raw", &st) >= 0) {
- if (size == (uint64_t) -1 || (uint64_t) st.st_size < size)
- size = st.st_size;
- }
-
return sd_bus_message_append(reply, "t", size);
}
return method(message, machine, error);
}
+static int method_unregister_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return redirect_method_to_machine(message, userdata, error, bus_machine_method_unregister);
+}
+
static int method_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
return redirect_method_to_machine(message, userdata, error, bus_machine_method_terminate);
}
static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
- _cleanup_(image_hashmap_freep) Hashmap *images = NULL;
+ _cleanup_hashmap_free_ Hashmap *images = NULL;
Manager *m = userdata;
Image *image;
Iterator i;
assert(message);
assert(m);
- images = hashmap_new(&string_hash_ops);
+ images = hashmap_new(&image_hash_ops);
if (!images)
return -ENOMEM;
if (lseek(operation->extra_fd, 0, SEEK_SET) == (off_t) -1)
return -errno;
- f = fdopen(operation->extra_fd, "re");
+ f = fdopen(operation->extra_fd, "r");
if (!f)
return -errno;
errno = 0;
n = fread(&success, 1, sizeof(success), f);
if (n != sizeof(success))
- return ret < 0 ? ret : (errno != 0 ? -errno : -EIO);
+ return ret < 0 ? ret : errno_or_else(EIO);
if (ret < 0) {
_cleanup_free_ char *name = NULL;
if (success) /* The resulting temporary file could not be updated, ignore it. */
return ret;
- r = read_nul_string(f, &name);
- if (r < 0 || isempty(name)) /* Same here... */
+ r = read_nul_string(f, LONG_LINE_MAX, &name);
+ if (r <= 0) /* Same here... */
return ret;
return sd_bus_error_set_errnof(error, ret, "Failed to remove image %s: %m", name);
_cleanup_free_ char *name = NULL;
uint64_t size;
- r = read_nul_string(f, &name);
+ r = read_nul_string(f, LONG_LINE_MAX, &name);
if (r < 0)
return r;
- if (isempty(name)) /* reached the end */
+ if (r == 0) /* reached the end */
break;
errno = 0;
n = fread(&size, 1, sizeof(size), f);
if (n != sizeof(size))
- return errno != 0 ? -errno : -EIO;
+ return errno_or_else(EIO);
r = sd_bus_message_append(reply, "(st)", name, size);
if (r < 0)
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (r == 0) {
- _cleanup_(image_hashmap_freep) Hashmap *images = NULL;
+ _cleanup_hashmap_free_ Hashmap *images = NULL;
bool success = true;
Image *image;
Iterator i;
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
- images = hashmap_new(&string_hash_ops);
+ images = hashmap_new(&image_hash_ops);
if (!images) {
r = -ENOMEM;
goto child_fail;
return 1; /* Will call us back */
/* Set up the machine directory if necessary */
- r = setup_machine_directory(limit, error);
+ r = setup_machine_directory(error);
if (r < 0)
return r;
- /* Resize the backing loopback device, if there is one, except if we asked to drop any limit */
- if (limit != (uint64_t) -1) {
- r = btrfs_resize_loopback("/var/lib/machines", limit, false);
- if (r == -ENOTTY)
- return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs.");
- if (r < 0 && r != -ENODEV) /* ignore ENODEV, as that's what is returned if the file system is not on loopback */
- return sd_bus_error_set_errnof(error, r, "Failed to adjust loopback limit: %m");
- }
-
(void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit);
r = btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit);
if (k < 0 && feof(f))
break;
if (k != 3) {
- if (ferror(f) && errno > 0)
- return -errno;
+ if (ferror(f))
+ return errno_or_else(EIO);
return -EIO;
}
if (k < 0 && feof(f))
break;
if (k != 3) {
- if (ferror(f) && errno > 0)
- return -errno;
+ if (ferror(f))
+ return errno_or_else(EIO);
return -EIO;
}
if (k < 0 && feof(f))
break;
if (k != 3) {
- if (ferror(f) && errno > 0)
- return -errno;
+ if (ferror(f))
+ return errno_or_else(EIO);
return -EIO;
}
if (k < 0 && feof(f))
break;
if (k != 3) {
- if (ferror(f) && errno > 0)
- return -errno;
+ if (ferror(f))
+ return errno_or_else(EIO);
return -EIO;
}
SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0),
SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0),
SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network, 0),
+ SD_BUS_METHOD("UnregisterMachine", "s", NULL, method_unregister_machine, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("TerminateMachine", "s", NULL, method_terminate_machine, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("KillMachine", "ssi", NULL, method_kill_machine, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses, SD_BUS_VTABLE_UNPRIVILEGED),
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)(sv)(sv)(sv)(sv)",
- "PIDs", "au", 1, pid,
- "Delegate", "b", 1,
- "CollectMode", "s", "inactive-or-failed",
- "AddRef", "b", 1,
- "TasksMax", "t", UINT64_C(16384));
- if (r < 0)
- return 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_unref_unit(
Manager *m,
const char *unit,