machinectl.c revision 1fe6fa167902199b6d0190697368bb1e80aeb465
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
8bdbb8d9cbe1d35708385573d70984ab4533812dLennart Poettering#include <sys/socket.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <unistd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <errno.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <string.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <getopt.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <locale.h>
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering#include <fcntl.h>
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#include <netinet/in.h>
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#include <arpa/inet.h>
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#include <net/if.h>
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#include <sys/mount.h>
eef46c372f64f40dd75415b2c504c73138719c8dLennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#include "sd-bus.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "log.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "macro.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "pager.h"
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering#include "spawn-polkit-agent.h"
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#include "bus-util.h"
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#include "bus-error.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "build.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "strv.h"
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering#include "unit-name.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "cgroup-show.h"
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering#include "logs-show.h"
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering#include "cgroup-util.h"
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering#include "ptyfwd.h"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering#include "event-util.h"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#include "path-util.h"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#include "mkdir.h"
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering#include "copy.h"
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering#include "verbs.h"
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering#include "import-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "process-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "terminal-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "signal-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic char **arg_property = NULL;
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersenstatic bool arg_all = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic bool arg_full = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic bool arg_no_pager = false;
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poetteringstatic bool arg_legend = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const char *arg_kill_who = NULL;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int arg_signal = SIGTERM;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poetteringstatic char *arg_host = NULL;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poetteringstatic bool arg_read_only = false;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poetteringstatic bool arg_mkdir = false;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poetteringstatic bool arg_quiet = false;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic bool arg_ask_password = true;
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poetteringstatic unsigned arg_lines = 10;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic OutputMode arg_output = OUTPUT_SHORT;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic bool arg_force = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const char* arg_dkr_index_url = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const char* arg_format = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void pager_open_if_enabled(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (arg_no_pager)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering pager_open(false);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering}
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poetteringstatic void polkit_agent_open_if_enabled(void) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering /* Open the polkit agent as a child process if necessary */
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (!arg_ask_password)
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (arg_transport != BUS_TRANSPORT_LOCAL)
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering polkit_agent_open();
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering}
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poetteringstatic OutputFlags get_output_flags(void) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering arg_all * OUTPUT_SHOW_ALL |
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering arg_full * OUTPUT_FULL_WIDTH |
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering on_tty() * OUTPUT_COLOR |
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering !arg_quiet * OUTPUT_WARN_CUTOFF;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering}
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringtypedef struct MachineInfo {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering const char *name;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering const char *class;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering const char *service;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering} MachineInfo;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic int compare_machine_info(const void *a, const void *b) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering const MachineInfo *x = a, *y = b;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return strcmp(x->name, y->name);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering}
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic int list_machines(int argc, char *argv[], void *userdata) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering size_t max_name = strlen("MACHINE"), max_class = strlen("CLASS"), max_service = strlen("SERVICE");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering _cleanup_free_ MachineInfo *machines = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *name, *class, *service, *object;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering size_t n_machines = 0, n_allocated = 0, j;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering sd_bus *bus = userdata;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen pager_open_if_enabled();
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = sd_bus_call_method(
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen bus,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "org.freedesktop.machine1",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "/org/freedesktop/machine1",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "org.freedesktop.machine1.Manager",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "ListMachines",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen &error,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen &reply,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Could not get machines: %s", bus_error_message(&error, -r));
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return r;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(ssso)");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r < 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return bus_log_parse_error(r);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering while ((r = sd_bus_message_read(reply, "(ssso)", &name, &class, &service, &object)) > 0) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering size_t l;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (!GREEDY_REALLOC(machines, n_allocated, n_machines + 1))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return log_oom();
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering machines[n_machines].name = name;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering machines[n_machines].class = class;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering machines[n_machines].service = service;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering l = strlen(name);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (l > max_name)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering max_name = l;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering l = strlen(class);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (l > max_class)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering max_class = l;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering l = strlen(service);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (l > max_service)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering max_service = l;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering n_machines ++;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r < 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return bus_log_parse_error(r);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_message_exit_container(reply);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (r < 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return bus_log_parse_error(r);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering qsort_safe(machines, n_machines, sizeof(MachineInfo), compare_machine_info);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (arg_legend)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering printf("%-*s %-*s %-*s\n",
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering (int) max_name, "MACHINE",
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering (int) max_class, "CLASS",
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering (int) max_service, "SERVICE");
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering for (j = 0; j < n_machines; j++)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering printf("%-*s %-*s %-*s\n",
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering (int) max_name, machines[j].name,
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen (int) max_class, machines[j].class,
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering (int) max_service, machines[j].service);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (arg_legend)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("\n%zu machines listed.\n", n_machines);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return 0;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringtypedef struct ImageInfo {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering const char *name;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering const char *type;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering bool read_only;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering usec_t crtime;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering usec_t mtime;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering uint64_t size;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering} ImageInfo;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic int compare_image_info(const void *a, const void *b) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering const ImageInfo *x = a, *y = b;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return strcmp(x->name, y->name);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
c19de71113f956809995fc68817e055e9f61f607Lennart Poetteringstatic int list_images(int argc, char *argv[], void *userdata) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering size_t max_name = strlen("NAME"), max_type = strlen("TYPE"), max_size = strlen("USAGE"), max_crtime = strlen("CREATED"), max_mtime = strlen("MODIFIED");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering _cleanup_free_ ImageInfo *images = NULL;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering size_t n_images = 0, n_allocated = 0, j;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering const char *name, *type, *object;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering sd_bus *bus = userdata;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering uint64_t crtime, mtime, size;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering int read_only, r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(bus);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering pager_open_if_enabled();
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = sd_bus_call_method(
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering bus,
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering "org.freedesktop.machine1",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering "/org/freedesktop/machine1",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering "org.freedesktop.machine1.Manager",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering "ListImages",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering &error,
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering &reply,
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering "");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (r < 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering log_error("Could not get images: %s", bus_error_message(&error, -r));
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering return r;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbttto)");
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (r < 0)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering return bus_log_parse_error(r);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering while ((r = sd_bus_message_read(reply, "(ssbttto)", &name, &type, &read_only, &crtime, &mtime, &size, &object)) > 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_BYTES_MAX)];
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering size_t l;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (name[0] == '.' && !arg_all)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering continue;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (!GREEDY_REALLOC(images, n_allocated, n_images + 1))
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return log_oom();
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering images[n_images].name = name;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering images[n_images].type = type;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering images[n_images].read_only = read_only;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering images[n_images].crtime = crtime;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering images[n_images].mtime = mtime;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering images[n_images].size = size;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering l = strlen(name);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (l > max_name)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering max_name = l;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering l = strlen(type);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (l > max_type)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering max_type = l;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (crtime != 0) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering l = strlen(strna(format_timestamp(buf, sizeof(buf), crtime)));
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (l > max_crtime)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering max_crtime = l;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering }
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (mtime != 0) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering l = strlen(strna(format_timestamp(buf, sizeof(buf), mtime)));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (l > max_mtime)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering max_mtime = l;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (size != (uint64_t) -1) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering l = strlen(strna(format_bytes(buf, sizeof(buf), size)));
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (l > max_size)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering max_size = l;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering n_images++;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (r < 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return bus_log_parse_error(r);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = sd_bus_message_exit_container(reply);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (r < 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return bus_log_parse_error(r);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering qsort_safe(images, n_images, sizeof(ImageInfo), compare_image_info);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (arg_legend)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering printf("%-*s %-*s %-3s %-*s %-*s %-*s\n",
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering (int) max_name, "NAME",
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering (int) max_type, "TYPE",
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering "RO",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (int) max_size, "USAGE",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (int) max_crtime, "CREATED",
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering (int) max_mtime, "MODIFIED");
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering for (j = 0; j < n_images; j++) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering char crtime_buf[FORMAT_TIMESTAMP_MAX], mtime_buf[FORMAT_TIMESTAMP_MAX], size_buf[FORMAT_BYTES_MAX];
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
8937e7b68940d0fa0d0aab90eb7425fa7dccebc9Lennart Poettering printf("%-*s %-*s %s%-3s%s %-*s %-*s %-*s\n",
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering (int) max_name, images[j].name,
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering (int) max_type, images[j].type,
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering images[j].read_only ? ansi_highlight_red() : "", yes_no(images[j].read_only), images[j].read_only ? ansi_highlight_off() : "",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (int) max_size, strna(format_bytes(size_buf, sizeof(size_buf), images[j].size)),
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (int) max_crtime, strna(format_timestamp(crtime_buf, sizeof(crtime_buf), images[j].crtime)),
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering (int) max_mtime, strna(format_timestamp(mtime_buf, sizeof(mtime_buf), images[j].mtime)));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (arg_legend)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering printf("\n%zu images listed.\n", n_images);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering return 0;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen}
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringstatic int show_unit_cgroup(sd_bus *bus, const char *unit, pid_t leader) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering _cleanup_free_ char *path = NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *cgroup;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering int r;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering unsigned c;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering assert(bus);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert(unit);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (arg_transport == BUS_TRANSPORT_REMOTE)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return 0;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering path = unit_dbus_path_from_name(unit);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (!path)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return log_oom();
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = sd_bus_get_property(
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering bus,
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering "org.freedesktop.systemd1",
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering path,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen endswith(unit, ".scope") ? "org.freedesktop.systemd1.Scope" : "org.freedesktop.systemd1.Service",
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering "ControlGroup",
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering &error,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen &reply,
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering "s");
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (r < 0) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering log_error("Failed to query ControlGroup: %s", bus_error_message(&error, -r));
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering return r;
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering }
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = sd_bus_message_read(reply, "s", &cgroup);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (r < 0)
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering return bus_log_parse_error(r);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (isempty(cgroup))
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering return 0;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup, false) != 0 && leader <= 0)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return 0;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering c = columns();
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (c > 18)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering c -= 18;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering else
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering c = 0;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, false, &leader, leader > 0, get_output_flags());
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return 0;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic int print_addresses(sd_bus *bus, const char *name, int ifi, const char *prefix, const char *prefix2) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering int r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(bus);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(name);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(prefix);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(prefix2);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_bus_call_method(bus,
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering "org.freedesktop.machine1",
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering "/org/freedesktop/machine1",
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering "org.freedesktop.machine1.Manager",
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering "GetMachineAddresses",
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering NULL,
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering &reply,
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering "s", name);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering if (r < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(iay)");
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering if (r < 0)
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering return bus_log_parse_error(r);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering int family;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering const void *a;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering size_t sz;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering char buffer[MAX(INET6_ADDRSTRLEN, INET_ADDRSTRLEN)];
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_bus_message_read(reply, "i", &family);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return bus_log_parse_error(r);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_bus_message_read_array(reply, 'y', &a, &sz);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (r < 0)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return bus_log_parse_error(r);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering fputs(prefix, stdout);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering fputs(inet_ntop(family, a, buffer, sizeof(buffer)), stdout);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (family == AF_INET6 && ifi > 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering printf("%%%i", ifi);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering fputc('\n', stdout);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_bus_message_exit_container(reply);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return bus_log_parse_error(r);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (prefix != prefix2)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering prefix = prefix2;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return bus_log_parse_error(r);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_bus_message_exit_container(reply);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return bus_log_parse_error(r);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return 0;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering}
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poetteringstatic int print_os_release(sd_bus *bus, const char *name, const char *prefix) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering const char *k, *v, *pretty = NULL;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering int r;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(bus);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(name);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(prefix);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = sd_bus_call_method(bus,
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering "org.freedesktop.machine1",
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering "/org/freedesktop/machine1",
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering "org.freedesktop.machine1.Manager",
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering "GetMachineOSRelease",
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering NULL,
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering &reply,
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering "s", name);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (r < 0)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return r;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = sd_bus_message_enter_container(reply, 'a', "{ss}");
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (r < 0)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return bus_log_parse_error(r);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering while ((r = sd_bus_message_read(reply, "{ss}", &k, &v)) > 0) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (streq(k, "PRETTY_NAME"))
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering pretty = v;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (r < 0)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return bus_log_parse_error(r);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = sd_bus_message_exit_container(reply);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (r < 0)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return bus_log_parse_error(r);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (pretty)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering printf("%s%s\n", prefix, pretty);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieverstypedef struct MachineStatusInfo {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers char *name;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering sd_id128_t id;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers char *class;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *service;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering char *unit;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering char *root_directory;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering pid_t leader;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct dual_timestamp timestamp;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int *netif;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen unsigned n_netif;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering} MachineStatusInfo;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic void machine_status_info_clear(MachineStatusInfo *info) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (info) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering free(info->name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(info->class);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(info->service);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(info->unit);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(info->root_directory);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(info->netif);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering zero(*info);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poetteringstatic void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char since2[FORMAT_TIMESTAMP_MAX], *s2;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int ifi = -1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(i);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fputs(strna(i->name), stdout);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!sd_id128_equal(i->id, SD_ID128_NULL))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("(" SD_ID128_FORMAT_STR ")\n", SD_ID128_FORMAT_VAL(i->id));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering putchar('\n');
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering s1 = format_timestamp_relative(since1, sizeof(since1), i->timestamp.realtime);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering s2 = format_timestamp(since2, sizeof(since2), i->timestamp.realtime);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (s1)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("\t Since: %s; %s\n", s2, s1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else if (s2)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("\t Since: %s\n", s2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (i->leader > 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_free_ char *t = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("\t Leader: %u", (unsigned) i->leader);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering get_process_comm(i->leader, &t);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (t)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf(" (%s)", t);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering putchar('\n');
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering }
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (i->service) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering printf("\t Service: %s", i->service);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (i->class)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering printf("; class %s", i->class);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering putchar('\n');
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering } else if (i->class)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering printf("\t Class: %s\n", i->class);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (i->root_directory)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering printf("\t Root: %s\n", i->root_directory);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (i->n_netif > 0) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering unsigned c;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering fputs("\t Iface:", stdout);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering for (c = 0; c < i->n_netif; c++) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering char name[IF_NAMESIZE+1] = "";
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (if_indextoname(i->netif[c], name)) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering fputc(' ', stdout);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering fputs(name, stdout);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (ifi < 0)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering ifi = i->netif[c];
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering else
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering ifi = 0;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering } else
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering printf(" %i", i->netif[c]);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering fputc('\n', stdout);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering print_addresses(bus, i->name, ifi,
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering "\t Address: ",
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering "\t ");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering print_os_release(bus, i->name, "\t OS: ");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (i->unit) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering printf("\t Unit: %s\n", i->unit);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering show_unit_cgroup(bus, i->unit, i->leader);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (arg_transport == BUS_TRANSPORT_LOCAL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering show_journal_by_unit(
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering stdout,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering i->unit,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering arg_output,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering 0,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering i->timestamp.monotonic,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering arg_lines,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering 0,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering get_output_flags() | OUTPUT_BEGIN_NEWLINE,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering SD_JOURNAL_LOCAL_ONLY,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering true,
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek NULL);
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek }
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering }
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering}
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic int map_netif(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering MachineStatusInfo *i = userdata;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering size_t l;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering const void *v;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering int r;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert_cc(sizeof(int32_t) == sizeof(int));
a6c616024db23fef34152c1432892824a07799ccLennart Poettering r = sd_bus_message_read_array(m, SD_BUS_TYPE_INT32, &v, &l);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (r < 0)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return r;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (r == 0)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return -EBADMSG;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering i->n_netif = l / sizeof(int32_t);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering i->netif = memdup(v, l);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (!i->netif)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return -ENOMEM;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return 0;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers}
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
a6c616024db23fef34152c1432892824a07799ccLennart Poetteringstatic int show_machine_info(const char *verb, sd_bus *bus, const char *path, bool *new_line) {
a6c616024db23fef34152c1432892824a07799ccLennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen static const struct bus_properties_map map[] = {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen { "Name", "s", NULL, offsetof(MachineStatusInfo, name) },
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering { "Class", "s", NULL, offsetof(MachineStatusInfo, class) },
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering { "Service", "s", NULL, offsetof(MachineStatusInfo, service) },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "Unit", "s", NULL, offsetof(MachineStatusInfo, unit) },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "RootDirectory", "s", NULL, offsetof(MachineStatusInfo, root_directory) },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "Leader", "u", NULL, offsetof(MachineStatusInfo, leader) },
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers { "Timestamp", "t", NULL, offsetof(MachineStatusInfo, timestamp.realtime) },
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers { "TimestampMonotonic", "t", NULL, offsetof(MachineStatusInfo, timestamp.monotonic) },
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers { "Id", "ay", bus_map_id128, offsetof(MachineStatusInfo, id) },
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers { "NetworkInterfaces", "ai", map_netif, 0 },
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers {}
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt };
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_(machine_status_info_clear) MachineStatusInfo info = {};
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(verb);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers assert(path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(new_line);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers r = bus_map_all_properties(bus,
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers "org.freedesktop.machine1",
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering path,
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers map,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering &info);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers return log_error_errno(r, "Could not get properties: %m");
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (*new_line)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering printf("\n");
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers *new_line = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering print_machine_status_info(bus, &info);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return r;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering}
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversstatic int show_machine_properties(sd_bus *bus, const char *path, bool *new_line) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert(bus);
27e72d6b22890ba4a8cbc05c49667cd1cccf1461Simon Peeters assert(path);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert(new_line);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (*new_line)
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering printf("\n");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *new_line = true;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_all);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r < 0)
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering log_error_errno(r, "Could not get properties: %m");
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return r;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int show_machine(int argc, char *argv[], void *userdata) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bool properties, new_line = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus *bus = userdata;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering int r = 0, i;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering properties = !strstr(argv[0], "status");
8c841f21f5042b11acc91cc1b039cb162cbbe8f4Djalal Harouni
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers pager_open_if_enabled();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (properties && argc <= 1) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* If no argument is specified, inspect the manager
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * itself */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = show_machine_properties(bus, "/org/freedesktop/machine1", &new_line);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r < 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return r;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen for (i = 1; i < argc; i++) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *path = NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering r = sd_bus_call_method(
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen bus,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "org.freedesktop.machine1",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "/org/freedesktop/machine1",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "org.freedesktop.machine1.Manager",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "GetMachine",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen &error,
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering &reply,
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering "s", argv[i]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers log_error("Could not get path to machine: %s", bus_error_message(&error, -r));
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return r;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = sd_bus_message_read(reply, "o", &path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (r < 0)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return bus_log_parse_error(r);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (properties)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = show_machine_properties(bus, path, &new_line);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering else
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = show_machine_info(argv[0], bus, path, &new_line);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return r;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering}
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poetteringtypedef struct ImageStatusInfo {
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering char *name;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering char *path;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char *type;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering int read_only;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering usec_t crtime;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering usec_t mtime;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering uint64_t usage;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering uint64_t limit;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering uint64_t usage_exclusive;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering uint64_t limit_exclusive;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering} ImageStatusInfo;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringstatic void image_status_info_clear(ImageStatusInfo *info) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (info) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering free(info->name);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering free(info->path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering free(info->type);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering zero(*info);
9a14fb6285bdb089d4fc195410de3362cb4f586fThomas Hindoe Paaboel Andersen }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering}
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringstatic void print_image_status_info(sd_bus *bus, ImageStatusInfo *i) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char ts_relative[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char ts_absolute[FORMAT_TIMESTAMP_MAX], *s2;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char bs[FORMAT_BYTES_MAX], *s3;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char bs_exclusive[FORMAT_BYTES_MAX], *s4;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(bus);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(i);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (i->name) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering fputs(i->name, stdout);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering putchar('\n');
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (i->type)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering printf("\t Type: %s\n", i->type);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (i->path)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering printf("\t Path: %s\n", i->path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering printf("\t RO: %s%s%s\n",
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering i->read_only ? ansi_highlight_red() : "",
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering i->read_only ? "read-only" : "writable",
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering i->read_only ? ansi_highlight_off() : "");
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering s1 = format_timestamp_relative(ts_relative, sizeof(ts_relative), i->crtime);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering s2 = format_timestamp(ts_absolute, sizeof(ts_absolute), i->crtime);
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering if (s1 && s2)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering printf("\t Created: %s; %s\n", s2, s1);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering else if (s2)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering printf("\t Created: %s\n", s2);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering s1 = format_timestamp_relative(ts_relative, sizeof(ts_relative), i->mtime);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering s2 = format_timestamp(ts_absolute, sizeof(ts_absolute), i->mtime);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (s1 && s2)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering printf("\tModified: %s; %s\n", s2, s1);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering else if (s2)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("\tModified: %s\n", s2);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering s3 = format_bytes(bs, sizeof(bs), i->usage);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering s4 = i->usage_exclusive != i->usage ? format_bytes(bs_exclusive, sizeof(bs_exclusive), i->usage_exclusive) : NULL;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (s3 && s4)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering printf("\t Usage: %s (exclusive: %s)\n", s3, s4);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering else if (s3)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering printf("\t Usage: %s\n", s3);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering s3 = format_bytes(bs, sizeof(bs), i->limit);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering s4 = i->limit_exclusive != i->limit ? format_bytes(bs_exclusive, sizeof(bs_exclusive), i->limit_exclusive) : NULL;
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering if (s3 && s4)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering printf("\t Limit: %s (exclusive: %s)\n", s3, s4);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering else if (s3)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering printf("\t Limit: %s\n", s3);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering}
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringstatic int show_image_info(sd_bus *bus, const char *path, bool *new_line) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering static const struct bus_properties_map map[] = {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering { "Name", "s", NULL, offsetof(ImageStatusInfo, name) },
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering { "Path", "s", NULL, offsetof(ImageStatusInfo, path) },
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering { "Type", "s", NULL, offsetof(ImageStatusInfo, type) },
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering { "ReadOnly", "b", NULL, offsetof(ImageStatusInfo, read_only) },
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering { "CreationTimestamp", "t", NULL, offsetof(ImageStatusInfo, crtime) },
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering { "ModificationTimestamp", "t", NULL, offsetof(ImageStatusInfo, mtime) },
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering { "Usage", "t", NULL, offsetof(ImageStatusInfo, usage) },
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering { "Limit", "t", NULL, offsetof(ImageStatusInfo, limit) },
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering { "UsageExclusive", "t", NULL, offsetof(ImageStatusInfo, usage_exclusive) },
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering { "LimitExclusive", "t", NULL, offsetof(ImageStatusInfo, limit_exclusive) },
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering {}
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering };
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering _cleanup_(image_status_info_clear) ImageStatusInfo info = {};
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering int r;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(bus);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(new_line);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = bus_map_all_properties(bus,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering "org.freedesktop.machine1",
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering path,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering map,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering &info);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (r < 0)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return log_error_errno(r, "Could not get properties: %m");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (*new_line)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("\n");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering *new_line = true;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering print_image_status_info(bus, &info);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return r;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringtypedef struct PoolStatusInfo {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering char *path;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering uint64_t usage;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering uint64_t limit;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering} PoolStatusInfo;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic void pool_status_info_clear(PoolStatusInfo *info) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (info) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering free(info->path);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering zero(*info);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering info->usage = -1;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering info->limit = -1;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering }
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic void print_pool_status_info(sd_bus *bus, PoolStatusInfo *i) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering char bs[FORMAT_BYTES_MAX], *s;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (i->path)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("\t Path: %s\n", i->path);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering s = format_bytes(bs, sizeof(bs), i->usage);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (s)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("\t Usage: %s\n", s);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering s = format_bytes(bs, sizeof(bs), i->limit);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (s)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("\t Limit: %s\n", s);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int show_pool_info(sd_bus *bus) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering static const struct bus_properties_map map[] = {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering { "PoolPath", "s", NULL, offsetof(PoolStatusInfo, path) },
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering { "PoolUsage", "t", NULL, offsetof(PoolStatusInfo, usage) },
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering { "PoolLimit", "t", NULL, offsetof(PoolStatusInfo, limit) },
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering {}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering };
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering _cleanup_(pool_status_info_clear) PoolStatusInfo info = {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering .usage = (uint64_t) -1,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering .limit = (uint64_t) -1,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering };
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering int r;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(bus);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = bus_map_all_properties(bus,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering "org.freedesktop.machine1",
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering "/org/freedesktop/machine1",
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering map,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering &info);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (r < 0)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return log_error_errno(r, "Could not get properties: %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering print_pool_status_info(bus, &info);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return 0;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering}
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringstatic int show_image_properties(sd_bus *bus, const char *path, bool *new_line) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering int r;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(bus);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(new_line);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (*new_line)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering printf("\n");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering *new_line = true;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_all);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (r < 0)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering log_error_errno(r, "Could not get properties: %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return r;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int show_image(int argc, char *argv[], void *userdata) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering bool properties, new_line = false;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering sd_bus *bus = userdata;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering int r = 0, i;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering assert(bus);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering properties = !strstr(argv[0], "status");
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering pager_open_if_enabled();
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering if (argc <= 1) {
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering /* If no argument is specified, inspect the manager
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering * itself */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (properties)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = show_image_properties(bus, "/org/freedesktop/machine1", &new_line);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering else
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = show_pool_info(bus);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (r < 0)
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return r;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering for (i = 1; i < argc; i++) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering const char *path = NULL;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = sd_bus_call_method(
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bus,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.machine1",
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers "/org/freedesktop/machine1",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.machine1.Manager",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "GetImage",
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering &error,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen &reply,
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering "s", argv[i]);
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering if (r < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Could not get path to image: %s", bus_error_message(&error, -r));
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering r = sd_bus_message_read(reply, "o", &path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return bus_log_parse_error(r);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (properties)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = show_image_properties(bus, path, &new_line);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering else
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering r = show_image_info(bus, path, &new_line);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return r;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering}
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poetteringstatic int kill_machine(int argc, char *argv[], void *userdata) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen sd_bus *bus = userdata;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r, i;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(bus);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering polkit_agent_open_if_enabled();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!arg_kill_who)
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering arg_kill_who = "all";
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering for (i = 1; i < argc; i++) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_bus_call_method(
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering bus,
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering "org.freedesktop.machine1",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "/org/freedesktop/machine1",
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering "org.freedesktop.machine1.Manager",
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering "KillMachine",
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering &error,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering NULL,
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering "ssi", argv[i], arg_kill_who, arg_signal);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Could not kill machine: %s", bus_error_message(&error, -r));
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return r;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering }
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering return 0;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering}
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poetteringstatic int reboot_machine(int argc, char *argv[], void *userdata) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering arg_kill_who = "leader";
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering arg_signal = SIGINT; /* sysvinit + systemd */
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering return kill_machine(argc, argv, userdata);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering}
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poetteringstatic int poweroff_machine(int argc, char *argv[], void *userdata) {
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering arg_kill_who = "leader";
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering arg_signal = SIGRTMIN+4; /* only systemd */
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return kill_machine(argc, argv, userdata);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering}
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poetteringstatic int terminate_machine(int argc, char *argv[], void *userdata) {
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering sd_bus *bus = userdata;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering int r, i;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering assert(bus);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering polkit_agent_open_if_enabled();
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering for (i = 1; i < argc; i++) {
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering r = sd_bus_call_method(
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering bus,
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering "org.freedesktop.machine1",
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering "/org/freedesktop/machine1",
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering "org.freedesktop.machine1.Manager",
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering "TerminateMachine",
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering &error,
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering NULL,
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering "s", argv[i]);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (r < 0) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_error("Could not terminate machine: %s", bus_error_message(&error, -r));
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return r;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering }
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering }
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering}
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poetteringstatic int copy_files(int argc, char *argv[], void *userdata) {
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering _cleanup_free_ char *abs_host_path = NULL;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering char *dest, *host_path, *container_path;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering sd_bus *bus = userdata;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering bool copy_from;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering int r;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering assert(bus);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering polkit_agent_open_if_enabled();
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering copy_from = streq(argv[0], "copy-from");
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering dest = argv[3] ?: argv[2];
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering host_path = copy_from ? dest : argv[2];
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering container_path = copy_from ? argv[2] : dest;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (!path_is_absolute(host_path)) {
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering abs_host_path = path_make_absolute_cwd(host_path);
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering if (!abs_host_path)
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering return log_oom();
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering host_path = abs_host_path;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering }
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering r = sd_bus_call_method(
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering bus,
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering "org.freedesktop.machine1",
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering "/org/freedesktop/machine1",
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering "org.freedesktop.machine1.Manager",
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering copy_from ? "CopyFromMachine" : "CopyToMachine",
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering &error,
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering NULL,
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering "sss",
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering argv[1],
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering copy_from ? container_path : host_path,
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering copy_from ? host_path : container_path);
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering if (r < 0) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering log_error("Failed to copy: %s", bus_error_message(&error, -r));
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return r;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering }
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering return 0;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering}
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poetteringstatic int bind_mount(int argc, char *argv[], void *userdata) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering sd_bus *bus = userdata;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering int r;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering assert(bus);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering polkit_agent_open_if_enabled();
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering r = sd_bus_call_method(
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering bus,
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering "org.freedesktop.machine1",
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering "/org/freedesktop/machine1",
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering "org.freedesktop.machine1.Manager",
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering "BindMountMachine",
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering &error,
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering NULL,
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering "sssbb",
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering argv[1],
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering argv[2],
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering argv[3],
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering arg_read_only,
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering arg_mkdir);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (r < 0) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering log_error("Failed to bind mount: %s", bus_error_message(&error, -r));
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering return r;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering }
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering return 0;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering}
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poetteringstatic int on_machine_removed(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering PTYForward ** forward = (PTYForward**) userdata;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering int r;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering assert(m);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering assert(forward);
bf441e3d9371a7e5aa1def66cfc40f0118884644Lennart Poettering
de33fc625725d199629ed074d6278504deb23debLennart Poettering if (*forward) {
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering /* If the forwarder is already initialized, tell it to
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering * exit on the next vhangup(), so that we still flush
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering * out what might be queued and exit then. */
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering r = pty_forward_set_ignore_vhangup(*forward, false);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (r >= 0)
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering return 0;
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt log_error_errno(r, "Failed to set ignore_vhangup flag: %m");
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering }
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt /* On error, or when the forwarder is not initialized yet, quit immediately */
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), EXIT_FAILURE);
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering return 0;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering}
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poetteringstatic int login_machine(int argc, char *argv[], void *userdata) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering _cleanup_bus_slot_unref_ sd_bus_slot *slot = NULL;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering int master = -1, r, ret = 0;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering sd_bus *bus = userdata;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering const char *pty, *match;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering char last_char = 0;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering bool machine_died;
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering assert(bus);
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering if (arg_transport != BUS_TRANSPORT_LOCAL &&
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering arg_transport != BUS_TRANSPORT_MACHINE) {
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering log_error("Login only supported on local machines.");
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering return -EOPNOTSUPP;
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering }
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering polkit_agent_open_if_enabled();
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering r = sd_event_default(&event);
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering if (r < 0)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return log_error_errno(r, "Failed to get event loop: %m");
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering r = sd_bus_attach_event(bus, event, 0);
ee451d766a64117a41ec36dd71e61683c9d9b83cLennart Poettering if (r < 0)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return log_error_errno(r, "Failed to attach bus to event loop: %m");
813c65c34beae2eed1f93d9317f97d7e806389f5Lennart Poettering
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering match = strjoina("type='signal',"
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering "sender='org.freedesktop.machine1',"
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering "path='/org/freedesktop/machine1',",
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering "interface='org.freedesktop.machine1.Manager',"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering "member='MachineRemoved',"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering "arg0='",
9c857b9d160c10b4454fc9f83442c1878343422fLennart Poettering argv[1],
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt "'");
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering r = sd_bus_add_match(bus, &slot, match, on_machine_removed, &forward);
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering if (r < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return log_error_errno(r, "Failed to add machine removal match: %m");
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering r = sd_bus_call_method(
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering bus,
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering "org.freedesktop.machine1",
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering "/org/freedesktop/machine1",
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering "org.freedesktop.machine1.Manager",
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering "OpenMachineLogin",
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering &error,
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering &reply,
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering "s", argv[1]);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering if (r < 0) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering log_error("Failed to get machine PTY: %s", bus_error_message(&error, -r));
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering return r;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering }
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering r = sd_bus_message_read(reply, "hs", &master, &pty);
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering if (r < 0)
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering return bus_log_parse_error(r);
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", argv[1]);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering r = pty_forward_new(event, master, true, false, &forward);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (r < 0)
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering return log_error_errno(r, "Failed to create PTY forwarder: %m");
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering r = sd_event_loop(event);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering if (r < 0)
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering pty_forward_get_last_char(forward, &last_char);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering machine_died = pty_forward_get_ignore_vhangup(forward) == 0;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering forward = pty_forward_free(forward);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering if (last_char != '\n')
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering fputc('\n', stdout);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering if (machine_died)
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering log_info("Machine %s terminated.", argv[1]);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering else
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering log_info("Connection to machine %s terminated.", argv[1]);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering sd_event_get_exit_code(event, &ret);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return ret;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering}
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poetteringstatic int remove_image(int argc, char *argv[], void *userdata) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering sd_bus *bus = userdata;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering int r, i;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering assert(bus);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering polkit_agent_open_if_enabled();
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering for (i = 1; i < argc; i++) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering r = sd_bus_call_method(
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering bus,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "org.freedesktop.machine1",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "/org/freedesktop/machine1",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "org.freedesktop.machine1.Manager",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "RemoveImage",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering &error,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering NULL,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "s", argv[i]);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (r < 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Could not remove image: %s", bus_error_message(&error, -r));
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return r;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return 0;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering}
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poetteringstatic int rename_image(int argc, char *argv[], void *userdata) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering sd_bus *bus = userdata;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering int r;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering polkit_agent_open_if_enabled();
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering r = sd_bus_call_method(
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering bus,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "org.freedesktop.machine1",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "/org/freedesktop/machine1",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "org.freedesktop.machine1.Manager",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "RenameImage",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering &error,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering NULL,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "ss", argv[1], argv[2]);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (r < 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Could not rename image: %s", bus_error_message(&error, -r));
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return r;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return 0;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering}
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poetteringstatic int clone_image(int argc, char *argv[], void *userdata) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering sd_bus *bus = userdata;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering int r;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering polkit_agent_open_if_enabled();
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering r = sd_bus_call_method(
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering bus,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "org.freedesktop.machine1",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "/org/freedesktop/machine1",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "org.freedesktop.machine1.Manager",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "CloneImage",
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering &error,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering NULL,
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering "ssb", argv[1], argv[2], arg_read_only);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (r < 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Could not clone image: %s", bus_error_message(&error, -r));
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return r;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return 0;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering}
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poetteringstatic int read_only_image(int argc, char *argv[], void *userdata) {
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering sd_bus *bus = userdata;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering int b = true, r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (argc > 2) {
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering b = parse_boolean(argv[2]);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (b < 0) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering log_error("Failed to parse boolean argument: %s", argv[2]);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return -EINVAL;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering }
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering }
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering polkit_agent_open_if_enabled();
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering r = sd_bus_call_method(
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering bus,
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering "org.freedesktop.machine1",
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering "/org/freedesktop/machine1",
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering "org.freedesktop.machine1.Manager",
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering "MarkImageReadOnly",
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering &error,
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering NULL,
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering "sb", argv[1], b);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0) {
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering log_error("Could not mark image read-only: %s", bus_error_message(&error, -r));
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering }
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return 0;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering}
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poetteringstatic int make_service_name(const char *name, char **ret) {
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering _cleanup_free_ char *e = NULL;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering int r;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering assert(name);
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering assert(ret);
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering if (!machine_name_is_valid(name)) {
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering log_error("Invalid machine name %s.", name);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return -EINVAL;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering }
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering e = unit_name_escape(name);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (!e)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return log_oom();
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering r = unit_name_build("systemd-nspawn", e, ".service", ret);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering if (r < 0)
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return log_error_errno(r, "Failed to build unit name: %m");
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return 0;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering}
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poetteringstatic int start_machine(int argc, char *argv[], void *userdata) {
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering sd_bus *bus = userdata;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering int r, i;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering assert(bus);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering polkit_agent_open_if_enabled();
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = bus_wait_for_jobs_new(bus, &w);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return log_oom();
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering for (i = 1; i < argc; i++) {
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering _cleanup_free_ char *unit = NULL;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering const char *object;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = make_service_name(argv[i], &unit);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return r;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = sd_bus_call_method(
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering bus,
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering "org.freedesktop.systemd1",
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering "/org/freedesktop/systemd1",
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering "org.freedesktop.systemd1.Manager",
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering "StartUnit",
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering &error,
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering &reply,
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering "ss", unit, "fail");
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0) {
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering log_error("Failed to start unit: %s", bus_error_message(&error, -r));
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return r;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering }
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = sd_bus_message_read(reply, "o", &object);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return bus_log_parse_error(r);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = bus_wait_for_jobs_add(w, object);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return log_oom();
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering }
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = bus_wait_for_jobs(w, arg_quiet);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return r;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return 0;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering}
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poetteringstatic int enable_machine(int argc, char *argv[], void *userdata) {
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering int carries_install_info = 0;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering const char *method = NULL;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering sd_bus *bus = userdata;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering int r, i;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering assert(bus);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering polkit_agent_open_if_enabled();
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering method = streq(argv[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = sd_bus_message_new_method_call(
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering bus,
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering &m,
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering "org.freedesktop.systemd1",
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering "/org/freedesktop/systemd1",
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering "org.freedesktop.systemd1.Manager",
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering method);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return bus_log_create_error(r);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = sd_bus_message_open_container(m, 'a', "s");
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return bus_log_create_error(r);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering for (i = 1; i < argc; i++) {
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering _cleanup_free_ char *unit = NULL;
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = make_service_name(argv[i], &unit);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return r;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering r = sd_bus_message_append(m, "s", unit);
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering if (r < 0)
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering return bus_log_create_error(r);
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering }
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = sd_bus_message_close_container(m);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (r < 0)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return bus_log_create_error(r);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering if (streq(argv[0], "enable"))
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering r = sd_bus_message_append(m, "bb", false, false);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering else
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_append(m, "b", false);
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_create_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_call(bus, m, 0, &error, &reply);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("Failed to enable or disable unit: %s", bus_error_message(&error, -r));
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering return r;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (streq(argv[0], "enable")) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_read(reply, "b", carries_install_info);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_parse_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, NULL, NULL);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return r;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_call_method(
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering bus,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.systemd1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "/org/freedesktop/systemd1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.systemd1.Manager",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "Reload",
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering &error,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering NULL,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering NULL);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("Failed to reload daemon: %s", bus_error_message(&error, -r));
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return r;
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return 0;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic int match_log_message(sd_bus_message *m, void *userdata, sd_bus_error *error) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering const char **our_path = userdata, *line;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering unsigned priority;
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering int r;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert(m);
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering assert(our_path);
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering r = sd_bus_message_read(m, "us", &priority, &line);
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering if (r < 0) {
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering bus_log_parse_error(r);
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering return 0;
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering }
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering if (!streq_ptr(*our_path, sd_bus_message_get_path(m)))
cc98b3025eeb89addb76a27390cb2baca4eab8b9Torstein Husebø return 0;
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering if (arg_quiet && LOG_PRI(priority) >= LOG_INFO)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return 0;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_full(priority, "%s", line);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return 0;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic int match_transfer_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering const char **our_path = userdata, *path, *result;
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering uint32_t id;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering int r;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert(m);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert(our_path);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_read(m, "uos", &id, &path, &result);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering bus_log_parse_error(r);
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering return 0;
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering }
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering if (!streq_ptr(*our_path, path))
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering return 0;
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), !streq_ptr(result, "done"));
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering return 0;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic int transfer_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert(s);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert(si);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (!arg_quiet)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32 "\" to abort transfer.", PTR_TO_UINT32(userdata));
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering sd_event_exit(sd_event_source_get_event(s), EINTR);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return 0;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic int transfer_image_common(sd_bus *bus, sd_bus_message *m) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_bus_slot_unref_ sd_bus_slot *slot_job_removed = NULL, *slot_log_message = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_event_unref_ sd_event* event = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering const char *path = NULL;
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering uint32_t id;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering int r;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert(bus);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert(m);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering polkit_agent_open_if_enabled();
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_event_default(&event);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering return log_error_errno(r, "Failed to get event loop: %m");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_attach_event(bus, event, 0);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering return log_error_errno(r, "Failed to attach bus to event loop: %m");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering r = sd_bus_add_match(
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering bus,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering &slot_job_removed,
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering "type='signal',"
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering "sender='org.freedesktop.import1',"
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering "interface='org.freedesktop.import1.Manager',"
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering "member='TransferRemoved',"
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering "path='/org/freedesktop/import1'",
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering match_transfer_removed, &path);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
6adf7b5e46d32376868feef0197e6ada352aa6f2Lennart Poettering return log_error_errno(r, "Failed to install match: %m");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_add_match(
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering bus,
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering &slot_log_message,
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering "type='signal',"
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering "sender='org.freedesktop.import1',"
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering "interface='org.freedesktop.import1.Transfer',"
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering "member='LogMessage'",
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering match_log_message, &path);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (r < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to install match: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = sd_bus_call(bus, m, 0, &error, &reply);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (r < 0) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_error("Failed transfer image: %s", bus_error_message(&error, -r));
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return r;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering }
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = sd_bus_message_read(reply, "uo", &id, &path);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (r < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return bus_log_parse_error(r);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (!arg_quiet)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering sd_event_add_signal(event, NULL, SIGINT, transfer_signal_handler, UINT32_TO_PTR(id));
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering sd_event_add_signal(event, NULL, SIGTERM, transfer_signal_handler, UINT32_TO_PTR(id));
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = sd_event_loop(event);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (r < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return -r;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering}
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int import_tar(int argc, char *argv[], void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering _cleanup_free_ char *ll = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering _cleanup_close_ int fd = -1;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering const char *local = NULL, *path = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering sd_bus *bus = userdata;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering int r;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert(bus);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (argc >= 2)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering path = argv[1];
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (isempty(path) || streq(path, "-"))
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering path = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (argc >= 3)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering local = argv[2];
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering else if (path)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering local = basename(path);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (isempty(local) || streq(local, "-"))
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering local = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (!local) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_error("Need either path or local name.");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return -EINVAL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering }
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = tar_strip_suffixes(local, &ll);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_oom();
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering local = ll;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (!machine_name_is_valid(local)) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_error("Local name %s is not a suitable machine name.", local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return -EINVAL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering }
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (path) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (fd < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(errno, "Failed to open %s: %m", path);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering }
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = sd_bus_message_new_method_call(
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering bus,
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering &m,
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering "org.freedesktop.import1",
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering "/org/freedesktop/import1",
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering "org.freedesktop.import1.Manager",
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering "ImportTar");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (r < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return bus_log_create_error(r);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = sd_bus_message_append(
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering m,
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering "hsbb",
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering fd >= 0 ? fd : STDIN_FILENO,
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering local,
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering arg_force,
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering arg_read_only);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (r < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return bus_log_create_error(r);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return transfer_image_common(bus, m);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering}
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int import_raw(int argc, char *argv[], void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering _cleanup_free_ char *ll = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering _cleanup_close_ int fd = -1;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering const char *local = NULL, *path = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering sd_bus *bus = userdata;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering int r;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert(bus);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (argc >= 2)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering path = argv[1];
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (isempty(path) || streq(path, "-"))
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering path = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (argc >= 3)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering local = argv[2];
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering else if (path)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering local = basename(path);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (isempty(local) || streq(local, "-"))
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering local = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (!local) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_error("Need either path or local name.");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return -EINVAL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering }
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = raw_strip_suffixes(local, &ll);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_oom();
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local = ll;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (!machine_name_is_valid(local)) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_error("Local name %s is not a suitable machine name.", local);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return -EINVAL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (path) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (fd < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(errno, "Failed to open %s: %m", path);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = sd_bus_message_new_method_call(
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering bus,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering &m,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "org.freedesktop.import1",
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "/org/freedesktop/import1",
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "org.freedesktop.import1.Manager",
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "ImportRaw");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return bus_log_create_error(r);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = sd_bus_message_append(
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering m,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "hsbb",
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering fd >= 0 ? fd : STDIN_FILENO,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_force,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_read_only);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return bus_log_create_error(r);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return transfer_image_common(bus, m);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic void determine_compression_from_filename(const char *p) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (arg_format)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (!p)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (endswith(p, ".xz"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_format = "xz";
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering else if (endswith(p, ".gz"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_format = "gzip";
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering else if (endswith(p, ".bz2"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_format = "bzip2";
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int export_tar(int argc, char *argv[], void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_close_ int fd = -1;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering const char *local = NULL, *path = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering sd_bus *bus = userdata;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering int r;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering assert(bus);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local = argv[1];
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (!machine_name_is_valid(local)) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_error("Machine name %s is not valid.", local);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return -EINVAL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (argc >= 3)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering path = argv[2];
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (isempty(path) || streq(path, "-"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering path = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (path) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering determine_compression_from_filename(path);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (fd < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(errno, "Failed to open %s: %m", path);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = sd_bus_message_new_method_call(
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering bus,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering &m,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "org.freedesktop.import1",
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "/org/freedesktop/import1",
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "org.freedesktop.import1.Manager",
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "ExportTar");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return bus_log_create_error(r);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = sd_bus_message_append(
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering m,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "shs",
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering fd >= 0 ? fd : STDOUT_FILENO,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_format);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return bus_log_create_error(r);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return transfer_image_common(bus, m);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int export_raw(int argc, char *argv[], void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_close_ int fd = -1;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering const char *local = NULL, *path = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering sd_bus *bus = userdata;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering int r;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering assert(bus);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local = argv[1];
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (!machine_name_is_valid(local)) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_error("Machine name %s is not valid.", local);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return -EINVAL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering }
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (argc >= 3)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering path = argv[2];
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (isempty(path) || streq(path, "-"))
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering path = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (path) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering determine_compression_from_filename(path);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (fd < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return log_error_errno(errno, "Failed to open %s: %m", path);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_new_method_call(
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering bus,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering &m,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.import1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "/org/freedesktop/import1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.import1.Manager",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "ExportRaw");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_create_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_append(
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering m,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "shs",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering fd >= 0 ? fd : STDOUT_FILENO,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_format);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_create_error(r);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return transfer_image_common(bus, m);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic int pull_tar(int argc, char *argv[], void *userdata) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_free_ char *l = NULL, *ll = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering const char *local, *remote;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering sd_bus *bus = userdata;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering int r;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert(bus);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering remote = argv[1];
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (!http_url_is_valid(remote)) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("URL '%s' is not valid.", remote);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (argc >= 3)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local = argv[2];
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering else {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = import_url_last_component(remote, &l);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return log_error_errno(r, "Failed to get final component of URL: %m");
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local = l;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (isempty(local) || streq(local, "-"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (local) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = tar_strip_suffixes(local, &ll);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return log_oom();
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local = ll;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (!machine_name_is_valid(local)) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("Local name %s is not a suitable machine name.", local);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_new_method_call(
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering bus,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering &m,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.import1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "/org/freedesktop/import1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.import1.Manager",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "PullTar");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_create_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_append(
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering m,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "sssb",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering remote,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering import_verify_to_string(arg_verify),
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_force);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_create_error(r);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return transfer_image_common(bus, m);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic int pull_raw(int argc, char *argv[], void *userdata) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_free_ char *l = NULL, *ll = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering const char *local, *remote;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering sd_bus *bus = userdata;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering int r;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert(bus);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering remote = argv[1];
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (!http_url_is_valid(remote)) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("URL '%s' is not valid.", remote);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (argc >= 3)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local = argv[2];
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering else {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = import_url_last_component(remote, &l);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return log_error_errno(r, "Failed to get final component of URL: %m");
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local = l;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (isempty(local) || streq(local, "-"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (local) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = raw_strip_suffixes(local, &ll);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return log_oom();
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local = ll;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
56e6c2abb8f18bba2bb9d96d66ac7e633349ddfbLennart Poettering if (!machine_name_is_valid(local)) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("Local name %s is not a suitable machine name.", local);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_new_method_call(
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering bus,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering &m,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.import1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "/org/freedesktop/import1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.import1.Manager",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "PullRaw");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_create_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_append(
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering m,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "sssb",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering remote,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering import_verify_to_string(arg_verify),
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_force);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_create_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return transfer_image_common(bus, m);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic int pull_dkr(int argc, char *argv[], void *userdata) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering const char *local, *remote, *tag;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering sd_bus *bus = userdata;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering int r;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (arg_verify != IMPORT_VERIFY_NO) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("Imports from DKR do not support image verification, please pass --verify=no.");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering remote = argv[1];
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering tag = strchr(remote, ':');
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (tag) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering remote = strndupa(remote, tag - remote);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering tag++;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (!dkr_name_is_valid(remote)) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("DKR name '%s' is invalid.", remote);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (tag && !dkr_tag_is_valid(tag)) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("DKR tag '%s' is invalid.", remote);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (argc >= 3)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local = argv[2];
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering else {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local = strchr(remote, '/');
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (local)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local++;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering else
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local = remote;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (isempty(local) || streq(local, "-"))
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (local) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (!machine_name_is_valid(local)) {
7079cfeffb6d520f20ddff53fd78467e72e6cc94Lennart Poettering log_error("Local name %s is not a suitable machine name.", local);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_new_method_call(
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering bus,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering &m,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.import1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "/org/freedesktop/import1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.import1.Manager",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "PullDkr");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_create_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_append(
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering m,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "sssssb",
7079cfeffb6d520f20ddff53fd78467e72e6cc94Lennart Poettering arg_dkr_index_url,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering remote,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering tag,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering local,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering import_verify_to_string(arg_verify),
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_force);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_create_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return transfer_image_common(bus, m);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringtypedef struct TransferInfo {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering uint32_t id;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering const char *type;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering const char *remote;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering const char *local;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering double progress;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering} TransferInfo;
7079cfeffb6d520f20ddff53fd78467e72e6cc94Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic int compare_transfer_info(const void *a, const void *b) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering const TransferInfo *x = a, *y = b;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
7079cfeffb6d520f20ddff53fd78467e72e6cc94Lennart Poettering return strcmp(x->local, y->local);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic int list_transfers(int argc, char *argv[], void *userdata) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering size_t max_type = strlen("TYPE"), max_local = strlen("LOCAL"), max_remote = strlen("REMOTE");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering _cleanup_free_ TransferInfo *transfers = NULL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering size_t n_transfers = 0, n_allocated = 0, j;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering const char *type, *remote, *local, *object;
7079cfeffb6d520f20ddff53fd78467e72e6cc94Lennart Poettering sd_bus *bus = userdata;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering uint32_t id, max_id = 0;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering double progress;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering int r;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering pager_open_if_enabled();
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_call_method(
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering bus,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.import1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "/org/freedesktop/import1",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "org.freedesktop.import1.Manager",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "ListTransfers",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering &error,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering &reply,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering NULL);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("Could not get transfers: %s", bus_error_message(&error, -r));
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return r;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(usssdo)");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_parse_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering while ((r = sd_bus_message_read(reply, "(usssdo)", &id, &type, &remote, &local, &progress, &object)) > 0) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering size_t l;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (!GREEDY_REALLOC(transfers, n_allocated, n_transfers + 1))
7079cfeffb6d520f20ddff53fd78467e72e6cc94Lennart Poettering return log_oom();
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
7079cfeffb6d520f20ddff53fd78467e72e6cc94Lennart Poettering transfers[n_transfers].id = id;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering transfers[n_transfers].type = type;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering transfers[n_transfers].remote = remote;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering transfers[n_transfers].local = local;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering transfers[n_transfers].progress = progress;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
7079cfeffb6d520f20ddff53fd78467e72e6cc94Lennart Poettering l = strlen(type);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (l > max_type)
7079cfeffb6d520f20ddff53fd78467e72e6cc94Lennart Poettering max_type = l;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering l = strlen(remote);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (l > max_remote)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering max_remote = l;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering l = strlen(local);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (l > max_local)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering max_local = l;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (id > max_id)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering max_id = id;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering n_transfers ++;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_parse_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering r = sd_bus_message_exit_container(reply);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return bus_log_parse_error(r);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering qsort_safe(transfers, n_transfers, sizeof(TransferInfo), compare_transfer_info);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (arg_legend)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering printf("%-*s %-*s %-*s %-*s %-*s\n",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering (int) MAX(2U, DECIMAL_STR_WIDTH(max_id)), "ID",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering (int) 7, "PERCENT",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering (int) max_type, "TYPE",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering (int) max_local, "LOCAL",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering (int) max_remote, "REMOTE");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering for (j = 0; j < n_transfers; j++)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering printf("%*" PRIu32 " %*u%% %-*s %-*s %-*s\n",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering (int) MAX(2U, DECIMAL_STR_WIDTH(max_id)), transfers[j].id,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering (int) 6, (unsigned) (transfers[j].progress * 100),
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering (int) max_type, transfers[j].type,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering (int) max_local, transfers[j].local,
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering (int) max_remote, transfers[j].remote);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (arg_legend)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering printf("\n%zu transfers listed.\n", n_transfers);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return 0;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poetteringstatic int cancel_transfer(int argc, char *argv[], void *userdata) {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering sd_bus *bus = userdata;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering int r, i;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering assert(bus);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering polkit_agent_open_if_enabled();
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering for (i = 1; i < argc; i++) {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering uint32_t id;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering r = safe_atou32(argv[i], &id);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (r < 0)
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return log_error_errno(r, "Failed to parse transfer id: %s", argv[i]);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering r = sd_bus_call_method(
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering bus,
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering "org.freedesktop.import1",
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering "/org/freedesktop/import1",
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering "org.freedesktop.import1.Manager",
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering "CancelTransfer",
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering &error,
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering NULL,
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering "u", id);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (r < 0) {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering log_error("Could not cancel transfer: %s", bus_error_message(&error, -r));
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return r;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return 0;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering}
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poetteringstatic int set_limit(int argc, char *argv[], void *userdata) {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering sd_bus *bus = userdata;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering uint64_t limit;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering int r;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (streq(argv[argc-1], "-"))
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering limit = (uint64_t) -1;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering else {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering off_t off;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering r = parse_size(argv[argc-1], 1024, &off);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (r < 0)
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return log_error("Failed to parse size: %s", argv[argc-1]);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering limit = (uint64_t) off;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering if (argc > 2)
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering /* With two arguments changes the quota limit of the
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering * specified image */
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering r = sd_bus_call_method(
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering bus,
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering "org.freedesktop.machine1",
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering "/org/freedesktop/machine1",
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering "org.freedesktop.machine1.Manager",
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering "SetImageLimit",
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering &error,
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering NULL,
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering "st", argv[1], limit);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering else
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering /* With one argument changes the pool quota limit */
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering r = sd_bus_call_method(
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering bus,
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering "org.freedesktop.machine1",
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering "/org/freedesktop/machine1",
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering "org.freedesktop.machine1.Manager",
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering "SetPoolLimit",
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering &error,
7f444afa1b62920265fec99a61cb4dc53d521956Lennart Poettering NULL,
7f444afa1b62920265fec99a61cb4dc53d521956Lennart Poettering "t", limit);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (r < 0) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("Could not set limit: %s", bus_error_message(&error, -r));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return r;
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering return 0;
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering}
e45fc5e738b0b7700e8b4f3c4b25c58a49b44b27Lennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poetteringstatic int help(int argc, char *argv[], void *userdata) {
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering printf("%s [OPTIONS...] {COMMAND} ...\n\n"
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering "Send control commands to or query the virtual machine and container\n"
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering "registration manager.\n\n"
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering " -h --help Show this help\n"
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering " --version Show package version\n"
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering " --no-pager Do not pipe output into a pager\n"
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering " --no-legend Do not show the headers and footers\n"
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering " --no-ask-password Do not ask for system passwords\n"
56b921c3d863f0e098f60f934e6c5880575c68abZbigniew Jędrzejewski-Szmek " -H --host=[USER@]HOST Operate on remote host\n"
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering " -M --machine=CONTAINER Operate on local container\n"
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering " -p --property=NAME Show only properties by this name\n"
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering " -q --quiet Suppress output\n"
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering " -a --all Show all properties, including empty ones\n"
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering " -l --full Do not ellipsize output\n"
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering " --kill-who=WHO Who to send signal to\n"
e721d697db20c976c9eebd32c461d2b957efa437Lennart Poettering " -s --signal=SIGNAL Which signal to send\n"
b5b38b41c37dbe1a117af9bf99e94b58ac91239aLennart Poettering " --read-only Create read-only bind mount\n"
b5b38b41c37dbe1a117af9bf99e94b58ac91239aLennart Poettering " --mkdir Create directory before bind mounting, if missing\n"
b5b38b41c37dbe1a117af9bf99e94b58ac91239aLennart Poettering " -n --lines=INTEGER Number of journal entries to show\n"
b5b38b41c37dbe1a117af9bf99e94b58ac91239aLennart Poettering " -o --output=STRING Change journal output mode (short,\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " short-monotonic, verbose, export, json,\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " json-pretty, json-sse, cat)\n"
6e9efa59209d48fc69a456fbadb2b5c113f503a6Lennart Poettering " --verify=MODE Verification mode for downloaded images (no,\n"
6e9efa59209d48fc69a456fbadb2b5c113f503a6Lennart Poettering " checksum, signature)\n"
b5b38b41c37dbe1a117af9bf99e94b58ac91239aLennart Poettering " --force Download image even if already exists\n"
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering " --dkr-index-url=URL Specify the index URL to use for DKR image\n"
f7621db0e38c3e96d48766ae648f1b7a0949ea13Lennart Poettering " downloads\n\n"
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering "Machine Commands:\n"
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering " list List running VMs and containers\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " status NAME... Show VM/container details\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " show NAME... Show properties of one or more VMs/containers\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " start NAME... Start container as a service\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " login NAME Get a login prompt on a container\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " enable NAME... Enable automatic container start at boot\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " disable NAME... Disable automatic container start at boot\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " poweroff NAME... Power off one or more containers\n"
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen " reboot NAME... Reboot one or more containers\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " terminate NAME... Terminate one or more VMs/containers\n"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering " kill NAME... Send signal to processes of a VM/container\n"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering "Image Commands:\n"
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering " list-images Show available container and VM images\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " image-status NAME... Show image details\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " show-image NAME... Show properties of image\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " clone NAME NAME Clone an image\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " rename NAME NAME Rename an image\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " read-only NAME [BOOL] Mark or unmark image read-only\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " remove NAME... Remove an image\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "Image Transfer Commands:\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " pull-tar URL [NAME] Download a TAR container image\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " pull-raw URL [NAME] Download a RAW container or VM image\n"
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen " pull-dkr REMOTE [NAME] Download a DKR container image\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " import-tar FILE [NAME] Import a local TAR container image\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " import-raw FILE [NAME] Import a local RAW container or VM image\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering " export-tar NAME [FILE] Export a TAR container image locally\n"
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering " list-transfers Show list of downloads in progress\n"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering " cancel-transfer Cancel a download\n"
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering , program_invocation_short_name);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return 0;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering enum {
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering ARG_VERSION = 0x100,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ARG_NO_PAGER,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ARG_NO_LEGEND,
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering ARG_KILL_WHO,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ARG_READ_ONLY,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ARG_MKDIR,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ARG_NO_ASK_PASSWORD,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ARG_VERIFY,
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering ARG_FORCE,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ARG_DKR_INDEX_URL,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ARG_FORMAT,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering };
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering static const struct option options[] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "help", no_argument, NULL, 'h' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "version", no_argument, NULL, ARG_VERSION },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "property", required_argument, NULL, 'p' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "all", no_argument, NULL, 'a' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "full", no_argument, NULL, 'l' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "no-pager", no_argument, NULL, ARG_NO_PAGER },
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering { "kill-who", required_argument, NULL, ARG_KILL_WHO },
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering { "signal", required_argument, NULL, 's' },
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering { "host", required_argument, NULL, 'H' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "machine", required_argument, NULL, 'M' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "read-only", no_argument, NULL, ARG_READ_ONLY },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "mkdir", no_argument, NULL, ARG_MKDIR },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "quiet", no_argument, NULL, 'q' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "lines", required_argument, NULL, 'n' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "output", required_argument, NULL, 'o' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "verify", required_argument, NULL, ARG_VERIFY },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "force", no_argument, NULL, ARG_FORCE },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "dkr-index-url", required_argument, NULL, ARG_DKR_INDEX_URL },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "format", required_argument, NULL, ARG_FORMAT },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering {}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering };
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int c, r;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering assert(argc >= 0);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering assert(argv);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering while ((c = getopt_long(argc, argv, "hp:als:H:M:qn:o:", options, NULL)) >= 0)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering switch (c) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering case 'h':
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return help(0, NULL, NULL);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering case ARG_VERSION:
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering puts(PACKAGE_STRING);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering puts(SYSTEMD_FEATURES);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 'p':
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = strv_extend(&arg_property, optarg);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen return log_oom();
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen /* If the user asked for a particular
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen * property, show it to him, even if it is
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * empty. */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering arg_all = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 'a':
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering arg_all = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 'l':
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering arg_full = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering case 'n':
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (safe_atou(optarg, &arg_lines) < 0) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering log_error("Failed to parse lines '%s'", optarg);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return -EINVAL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering break;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering case 'o':
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering arg_output = output_mode_from_string(optarg);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (arg_output < 0) {
de33fc625725d199629ed074d6278504deb23debLennart Poettering log_error("Unknown output '%s'.", optarg);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering return -EINVAL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering case ARG_NO_PAGER:
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering arg_no_pager = true;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering break;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering case ARG_NO_LEGEND:
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering arg_legend = false;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering break;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering case ARG_KILL_WHO:
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering arg_kill_who = optarg;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering break;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering case 's':
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering arg_signal = signal_from_string_try_harder(optarg);
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering if (arg_signal < 0) {
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering log_error("Failed to parse signal string %s.", optarg);
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering break;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering case ARG_NO_ASK_PASSWORD:
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_ask_password = false;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering break;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering case 'H':
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_transport = BUS_TRANSPORT_REMOTE;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_host = optarg;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering break;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering case 'M':
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_transport = BUS_TRANSPORT_MACHINE;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_host = optarg;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering break;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering case ARG_READ_ONLY:
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_read_only = true;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering break;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering case ARG_MKDIR:
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_mkdir = true;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 'q':
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering arg_quiet = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case ARG_VERIFY:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering arg_verify = import_verify_from_string(optarg);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (arg_verify < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Failed to parse --verify= setting: %s", optarg);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -EINVAL;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering break;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering case ARG_FORCE:
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_force = true;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering break;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering case ARG_DKR_INDEX_URL:
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (!http_url_is_valid(optarg)) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("Index URL is invalid: %s", optarg);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_dkr_index_url = optarg;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering break;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering case ARG_FORMAT:
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (!STR_IN_SET(optarg, "uncompressed", "xz", "gzip", "bzip2")) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering log_error("Unknown format: %s", optarg);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return -EINVAL;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering arg_format = optarg;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering break;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering case '?':
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return -EINVAL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering default:
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering assert_not_reached("Unhandled option");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return 1;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poetteringstatic int machinectl_main(int argc, char *argv[], sd_bus *bus) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering static const Verb verbs[] = {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering { "help", VERB_ANY, VERB_ANY, 0, help },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "list", VERB_ANY, 1, VERB_DEFAULT, list_machines },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "list-images", VERB_ANY, 1, 0, list_images },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "status", 2, VERB_ANY, 0, show_machine },
249968612f16a71df909d6e73785c18a9ff36a65Lennart Poettering { "image-status", VERB_ANY, VERB_ANY, 0, show_image },
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering { "show", VERB_ANY, VERB_ANY, 0, show_machine },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "show-image", VERB_ANY, VERB_ANY, 0, show_image },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "terminate", 2, VERB_ANY, 0, terminate_machine },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "reboot", 2, VERB_ANY, 0, reboot_machine },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "poweroff", 2, VERB_ANY, 0, poweroff_machine },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "kill", 2, VERB_ANY, 0, kill_machine },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "login", 2, 2, 0, login_machine },
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering { "bind", 3, 4, 0, bind_mount },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "copy-to", 3, 4, 0, copy_files },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "copy-from", 3, 4, 0, copy_files },
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering { "remove", 2, VERB_ANY, 0, remove_image },
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen { "rename", 3, 3, 0, rename_image },
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt { "clone", 3, 3, 0, clone_image },
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen { "read-only", 2, 3, 0, read_only_image },
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen { "start", 2, VERB_ANY, 0, start_machine },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "enable", 2, VERB_ANY, 0, enable_machine },
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering { "disable", 2, VERB_ANY, 0, enable_machine },
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering { "import-tar", 2, 3, 0, import_tar },
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering { "import-raw", 2, 3, 0, import_raw },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "export-tar", 2, 3, 0, export_tar },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "export-raw", 2, 3, 0, export_raw },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "pull-tar", 2, 3, 0, pull_tar },
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering { "pull-raw", 2, 3, 0, pull_raw },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "pull-dkr", 2, 3, 0, pull_dkr },
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering { "list-transfers", VERB_ANY, 1, 0, list_transfers },
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering { "cancel-transfer", 2, VERB_ANY, 0, cancel_transfer },
84f6181c2ac99a0514ca5e0c8fc8c8e284caf789Lennart Poettering { "set-limit", 2, 3, 0, set_limit },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering {}
};
return dispatch_verb(argc, argv, verbs, bus);
}
int main(int argc, char*argv[]) {
_cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
int r;
setlocale(LC_ALL, "");
log_parse_environment();
log_open();
r = parse_argv(argc, argv);
if (r <= 0)
goto finish;
r = bus_open_transport(arg_transport, arg_host, false, &bus);
if (r < 0) {
log_error_errno(r, "Failed to create bus connection: %m");
goto finish;
}
sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
r = machinectl_main(argc, argv, bus);
finish:
pager_close();
polkit_agent_close();
strv_free(arg_property);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}