Browse Source

machined: split out machine registration stuff from logind

Embedded folks don't need the machine registration stuff, hence it's
nice to make this optional. Also, I'd expect that machinectl will grow
additional commands quickly, for example to join existing containers and
suchlike, hence it's better keeping that separate from loginctl.
keep-around/ba91431154ad7bac82ddf0a540ec1b40db62d782
Lennart Poettering 10 years ago
parent
commit
1ee306e124
  1. 2
      .gitignore
  2. 70
      Makefile.am
  3. 10
      configure.ac
  4. 309
      src/login/loginctl.c
  5. 378
      src/login/logind-dbus.c
  6. 12
      src/login/logind-user.c
  7. 117
      src/login/logind.c
  8. 7
      src/login/logind.h
  9. 1
      src/machine/Makefile
  10. 27
      src/machine/machine-dbus.c
  11. 22
      src/machine/machine.c
  12. 14
      src/machine/machine.h
  13. 764
      src/machine/machinectl.c
  14. 824
      src/machine/machined-dbus.c
  15. 408
      src/machine/machined.c
  16. 73
      src/machine/machined.h
  17. 46
      src/machine/org.freedesktop.machine1.conf
  18. 12
      src/machine/org.freedesktop.machine1.service
  19. 6
      src/nspawn/nspawn.c
  20. 1
      tmpfiles.d/systemd.conf
  21. 1
      units/.gitignore
  22. 4
      units/systemd-logind.service.in
  23. 19
      units/systemd-machined.service.in

2
.gitignore

@ -15,6 +15,7 @@
/libtool
/localectl
/loginctl
/machinectl
/mtd_probe
/org.freedesktop.hostname1.xml
/org.freedesktop.locale1.xml
@ -54,6 +55,7 @@
/systemd-localed
/systemd-logind
/systemd-machine-id-setup
/systemd-machined
/systemd-modules-load
/systemd-multi-seat-x
/systemd-notify

70
Makefile.am

@ -3568,6 +3568,70 @@ lib_LTLIBRARIES += \
libnss_myhostname.la
endif
# ------------------------------------------------------------------------------
if ENABLE_MACHINED
systemd_machined_SOURCES = \
src/machine/machined.c \
src/machine/machined.h \
src/machine/machined-dbus.c \
src/machine/machine.c \
src/machine/machine.h \
src/machine/machine-dbus.c
systemd_machined_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS)
systemd_machined_LDADD = \
libsystemd-label.la \
libsystemd-audit.la \
libsystemd-shared.la \
libsystemd-daemon.la \
libsystemd-dbus.la \
libsystemd-id128-internal.la \
libudev.la
rootlibexec_PROGRAMS += \
systemd-machined
machinectl_SOURCES = \
src/machine/machinectl.c
machinectl_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS)
machinectl_LDADD = \
libsystemd-shared.la \
libsystemd-dbus.la \
libudev.la
rootbin_PROGRAMS += \
machinectl
nodist_systemunit_DATA += \
units/systemd-machined.service
dist_systemunit_DATA += \
units/machine.slice
dist_dbussystemservice_DATA += \
src/machine/org.freedesktop.machine1.service
dist_dbuspolicy_DATA += \
src/machine/org.freedesktop.machine1.conf
SYSTEM_UNIT_ALIASES += \
systemd-machined.service dbus-org.freedesktop.machine1.service
polkitpolicy_in_files += \
src/machine/org.freedesktop.machine1.policy.in
EXTRA_DIST += \
units/systemd-machine.service.in
endif
# ------------------------------------------------------------------------------
if ENABLE_LOGIND
systemd_logind_SOURCES = \
@ -3584,8 +3648,6 @@ systemd_logind_SOURCES = \
src/login/logind-seat.h \
src/login/logind-session.c \
src/login/logind-session.h \
src/login/logind-machine.c \
src/login/logind-machine.h \
src/login/logind-user.c \
src/login/logind-user.h \
src/login/logind-inhibit.c \
@ -3593,7 +3655,6 @@ systemd_logind_SOURCES = \
src/login/logind-session-dbus.c \
src/login/logind-seat-dbus.c \
src/login/logind-user-dbus.c \
src/login/logind-machine-dbus.c \
src/login/logind-acl.h
nodist_systemd_logind_SOURCES = \
@ -3748,8 +3809,7 @@ nodist_systemunit_DATA += \
units/systemd-user-sessions.service
dist_systemunit_DATA += \
units/user.slice \
units/machine.slice
units/user.slice
dist_dbussystemservice_DATA += \
src/login/org.freedesktop.login1.service

10
configure.ac

@ -644,6 +644,15 @@ fi
AM_CONDITIONAL(ENABLE_LOGIND, [test "$have_logind" = "yes"])
AS_IF([test "$have_logind" = "yes"], [ AC_DEFINE(HAVE_LOGIND, [1], [Logind support available]) ])
# ------------------------------------------------------------------------------
have_machined=no
AC_ARG_ENABLE(machined, AS_HELP_STRING([--disable-machined], [disable machine daemon]))
if test "x$enable_machined" != "xno"; then
have_machined=yes
fi
AM_CONDITIONAL(ENABLE_MACHINED, [test "$have_machined" = "yes"])
AS_IF([test "$have_machined" = "yes"], [ AC_DEFINE(HAVE_MACHINED, [1], [Machined support available]) ])
# ------------------------------------------------------------------------------
have_hostnamed=no
AC_ARG_ENABLE(hostnamed, AS_HELP_STRING([--disable-hostnamed], [disable hostname daemon]))
@ -968,6 +977,7 @@ AC_MSG_RESULT([
tmpfiles: ${have_tmpfiles}
randomseed: ${have_randomseed}
logind: ${have_logind}
machined: ${have_machined}
hostnamed: ${have_hostnamed}
timedated: ${have_timedated}
localed: ${have_localed}

309
src/login/loginctl.c

@ -261,69 +261,6 @@ static int list_seats(DBusConnection *bus, char **args, unsigned n) {
return 0;
}
static int list_machines(DBusConnection *bus, char **args, unsigned n) {
_cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
DBusMessageIter iter, sub, sub2;
unsigned k = 0;
int r;
pager_open_if_enabled();
r = bus_method_call_with_reply (
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListMachines",
&reply,
NULL,
DBUS_TYPE_INVALID);
if (r)
return r;
if (!dbus_message_iter_init(reply, &iter) ||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
log_error("Failed to parse reply.");
return -EIO;
}
dbus_message_iter_recurse(&iter, &sub);
if (on_tty())
printf("%-32s %-9s %-16s\n", "MACHINE", "CONTAINER", "SERVICE");
while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
const char *name, *class, *service, *object;
if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
log_error("Failed to parse reply.");
return -EIO;
}
dbus_message_iter_recurse(&sub, &sub2);
if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &class, true) < 0 ||
bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &service, true) < 0 ||
bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
log_error("Failed to parse reply.");
return -EIO;
}
printf("%-32s %-9s %-16s\n", name, class, service);
k++;
dbus_message_iter_next(&sub);
}
if (on_tty())
printf("\n%u machines listed.\n", k);
return 0;
}
typedef struct SessionStatusInfo {
const char *id;
uid_t uid;
@ -362,18 +299,6 @@ typedef struct SeatStatusInfo {
char **sessions;
} SeatStatusInfo;
typedef struct MachineStatusInfo {
const char *name;
sd_id128_t id;
const char *default_control_group;
const char *class;
const char *service;
const char *slice;
const char *root_directory;
pid_t leader;
usec_t timestamp;
} MachineStatusInfo;
static void print_session_status_info(SessionStatusInfo *i) {
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
char since2[FORMAT_TIMESTAMP_MAX], *s2;
@ -569,76 +494,6 @@ static void print_seat_status_info(SeatStatusInfo *i) {
}
}
static void print_machine_status_info(MachineStatusInfo *i) {
char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
char since2[FORMAT_TIMESTAMP_MAX], *s2;
assert(i);
fputs(strna(i->name), stdout);
if (!sd_id128_equal(i->id, SD_ID128_NULL))
printf("(" SD_ID128_FORMAT_STR ")\n", SD_ID128_FORMAT_VAL(i->id));
else
putchar('\n');
s1 = format_timestamp_relative(since1, sizeof(since1), i->timestamp);
s2 = format_timestamp(since2, sizeof(since2), i->timestamp);
if (s1)
printf("\t Since: %s; %s\n", s2, s1);
else if (s2)
printf("\t Since: %s\n", s2);
if (i->leader > 0) {
_cleanup_free_ char *t = NULL;
printf("\t Leader: %u", (unsigned) i->leader);
get_process_comm(i->leader, &t);
if (t)
printf(" (%s)", t);
putchar('\n');
}
if (i->service) {
printf("\t Service: %s", i->service);
if (i->class)
printf("; class %s", i->class);
putchar('\n');
} else if (i->class)
printf("\t Class: %s\n", i->class);
if (i->slice)
printf("\t Slice: %s\n", i->slice);
if (i->root_directory)
printf("\t Root: %s\n", i->root_directory);
if (i->default_control_group) {
unsigned c;
int output_flags =
arg_all * OUTPUT_SHOW_ALL |
arg_full * OUTPUT_FULL_WIDTH;
printf("\t CGroup: %s\n", i->default_control_group);
if (arg_transport != TRANSPORT_SSH) {
c = columns();
if (c > 18)
c -= 18;
else
c = 0;
show_cgroup_and_extra_by_spec(i->default_control_group,
"\t\t ", c, false, &i->leader,
i->leader > 0 ? 1 : 0,
output_flags);
}
}
}
static int status_property_session(const char *name, DBusMessageIter *iter, SessionStatusInfo *i) {
assert(name);
assert(iter);
@ -912,80 +767,6 @@ static int status_property_seat(const char *name, DBusMessageIter *iter, SeatSta
return 0;
}
static int status_property_machine(const char *name, DBusMessageIter *iter, MachineStatusInfo *i) {
assert(name);
assert(iter);
assert(i);
switch (dbus_message_iter_get_arg_type(iter)) {
case DBUS_TYPE_STRING: {
const char *s;
dbus_message_iter_get_basic(iter, &s);
if (!isempty(s)) {
if (streq(name, "Name"))
i->name = s;
else if (streq(name, "DefaultControlGroup"))
i->default_control_group = s;
else if (streq(name, "Class"))
i->class = s;
else if (streq(name, "Service"))
i->service = s;
else if (streq(name, "Slice"))
i->slice = s;
else if (streq(name, "RootDirectory"))
i->root_directory = s;
}
break;
}
case DBUS_TYPE_UINT32: {
uint32_t u;
dbus_message_iter_get_basic(iter, &u);
if (streq(name, "Leader"))
i->leader = (pid_t) u;
break;
}
case DBUS_TYPE_UINT64: {
uint64_t u;
dbus_message_iter_get_basic(iter, &u);
if (streq(name, "Timestamp"))
i->timestamp = (usec_t) u;
break;
}
case DBUS_TYPE_ARRAY: {
DBusMessageIter sub;
dbus_message_iter_recurse(iter, &sub);
if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE && streq(name, "Id")) {
void *v;
int n;
dbus_message_iter_get_fixed_array(&sub, &v, &n);
if (n == 0)
i->id = SD_ID128_NULL;
else if (n == 16)
memcpy(&i->id, v, n);
}
break;
}
}
return 0;
}
static int print_property(const char *name, DBusMessageIter *iter) {
assert(name);
assert(iter);
@ -1067,7 +848,6 @@ static int show_one(const char *verb, DBusConnection *bus, const char *path, boo
SessionStatusInfo session_info = {};
UserStatusInfo user_info = {};
SeatStatusInfo seat_info = {};
MachineStatusInfo machine_info = {};
assert(path);
assert(new_line);
@ -1131,10 +911,8 @@ static int show_one(const char *verb, DBusConnection *bus, const char *path, boo
r = status_property_session(name, &sub3, &session_info);
else if (strstr(verb, "user"))
r = status_property_user(name, &sub3, &user_info);
else if (strstr(verb, "seat"))
r = status_property_seat(name, &sub3, &seat_info);
else
r = status_property_machine(name, &sub3, &machine_info);
r = status_property_seat(name, &sub3, &seat_info);
if (r < 0) {
log_error("Failed to parse reply.");
@ -1149,10 +927,8 @@ static int show_one(const char *verb, DBusConnection *bus, const char *path, boo
print_session_status_info(&session_info);
else if (strstr(verb, "user"))
print_user_status_info(&user_info);
else if (strstr(verb, "seat"))
print_seat_status_info(&seat_info);
else
print_machine_status_info(&machine_info);
print_seat_status_info(&seat_info);
}
r = 0;
@ -1226,7 +1002,7 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
DBUS_TYPE_UINT32, &u,
DBUS_TYPE_INVALID);
} else if (strstr(args[0], "seat")) {
} else {
ret = bus_method_call_with_reply(
bus,
@ -1239,18 +1015,6 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
DBUS_TYPE_STRING, &args[i],
DBUS_TYPE_INVALID);
} else {
ret = bus_method_call_with_reply(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"GetMachine",
&reply,
NULL,
DBUS_TYPE_STRING, &args[i],
DBUS_TYPE_INVALID);
}
if (ret < 0)
@ -1334,36 +1098,6 @@ static int kill_session(DBusConnection *bus, char **args, unsigned n) {
return 0;
}
static int kill_machine(DBusConnection *bus, char **args, unsigned n) {
unsigned i;
assert(args);
if (!arg_kill_who)
arg_kill_who = "all";
for (i = 1; i < n; i++) {
int r;
r = bus_method_call_with_reply (
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"KillMachine",
NULL,
NULL,
DBUS_TYPE_STRING, &args[i],
DBUS_TYPE_STRING, &arg_kill_who,
DBUS_TYPE_INT32, &arg_signal,
DBUS_TYPE_INVALID);
if (r)
return r;
}
return 0;
}
static int enable_linger(DBusConnection *bus, char **args, unsigned n) {
unsigned i;
dbus_bool_t b, interactive = true;
@ -1567,31 +1301,6 @@ static int terminate_seat(DBusConnection *bus, char **args, unsigned n) {
return 0;
}
static int terminate_machine(DBusConnection *bus, char **args, unsigned n) {
unsigned i;
assert(args);
for (i = 1; i < n; i++) {
int r;
r = bus_method_call_with_reply (
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"TerminateMachine",
NULL,
NULL,
DBUS_TYPE_STRING, &args[i],
DBUS_TYPE_INVALID);
if (r)
return r;
}
return 0;
}
static int help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
@ -1630,12 +1339,7 @@ static int help(void) {
" show-seat [NAME...] Show properties of one or more seats\n"
" attach [NAME] [DEVICE...] Attach one or more devices to a seat\n"
" flush-devices Flush all device associations\n"
" terminate-seat [NAME...] Terminate all sessions on one or more seats\n"
" list-machines List running VMs and containers\n"
" machine-status [NAME...] Show VM/container status\n"
" show-machine [NAME...] Show properties of one or more VMs/containers\n"
" terminate-machine [NAME...] Terminate one or more VMs/containers\n"
" kill-machine [NAME...] Send signal to processes of a VM/container\n",
" terminate-seat [NAME...] Terminate all sessions on one or more seats\n",
program_invocation_short_name);
return 0;
@ -1784,11 +1488,6 @@ static int loginctl_main(DBusConnection *bus, int argc, char *argv[], DBusError
{ "attach", MORE, 3, attach },
{ "flush-devices", EQUAL, 1, flush_devices },
{ "terminate-seat", MORE, 2, terminate_seat },
{ "list-machines", EQUAL, 1, list_machines },
{ "machine-status", MORE, 2, show },
{ "show-machine", MORE, 1, show },
{ "terminate-machine", MORE, 2, terminate_machine },
{ "kill-machine", MORE, 2, kill_machine },
};
int left;

378
src/login/logind-dbus.c

@ -63,14 +63,6 @@
" <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"GetMachine\">\n" \
" <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"machine\" type=\"o\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"GetMachineByPID\">\n" \
" <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
" <arg name=\"machine\" type=\"o\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"ListSessions\">\n" \
" <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
" </method>\n" \
@ -80,9 +72,6 @@
" <method name=\"ListSeats\">\n" \
" <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"ListMachines\">\n" \
" <arg name=\"machines\" type=\"a(ssso)\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"CreateSession\">\n" \
" <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
" <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n" \
@ -108,16 +97,6 @@
" <method name=\"ReleaseSession\">\n" \
" <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"CreateMachine\">\n" \
" <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"id\" type=\"ay\" direction=\"in\"/>\n" \
" <arg name=\"service\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"class\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n" \
" <arg name=\"root_directory\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"scope_properties\" type=\"a(sv)\" direction=\"in\"/>\n" \
" <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"ActivateSession\">\n" \
" <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
" </method>\n" \
@ -151,9 +130,6 @@
" <method name=\"TerminateSeat\">\n" \
" <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"TerminateMachine\">\n" \
" <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"SetUserLinger\">\n" \
" <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
" <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
@ -231,22 +207,12 @@
" <arg name=\"id\" type=\"s\"/>\n" \
" <arg name=\"path\" type=\"o\"/>\n" \
" </signal>\n" \
" <signal name=\"MachineNew\">\n" \
" <arg name=\"machine\" type=\"s\"/>\n" \
" <arg name=\"path\" type=\"o\"/>\n" \
" </signal>\n" \
" <signal name=\"MachineRemoved\">\n" \
" <arg name=\"machine\" type=\"s\"/>\n" \
" <arg name=\"path\" type=\"o\"/>\n" \
" </signal>\n" \
" <signal name=\"PrepareForShutdown\">\n" \
" <arg name=\"active\" type=\"b\"/>\n" \
" </signal>\n" \
" <signal name=\"PrepareForSleep\">\n" \
" <arg name=\"active\" type=\"b\"/>\n" \
" </signal>\n" \
" <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
" <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
" <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
" <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
" <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
@ -689,148 +655,6 @@ fail:
return r;
}
static bool valid_machine_name(const char *p) {
size_t l;
if (!filename_is_safe(p))
return false;
if (!ascii_is_valid(p))
return false;
l = strlen(p);
if (l < 1 || l> 64)
return false;
return true;
}
static int bus_manager_create_machine(Manager *manager, DBusMessage *message) {
const char *name, *service, *class, *slice, *root_directory;
_cleanup_free_ char *p = NULL;
DBusMessageIter iter, sub;
MachineClass c;
uint32_t leader;
sd_id128_t id;
Machine *m;
int n, r;
void *v;
assert(manager);
assert(message);
if (!dbus_message_iter_init(message, &iter) ||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return -EINVAL;
dbus_message_iter_get_basic(&iter, &name);
if (!valid_machine_name(name) ||
!dbus_message_iter_next(&iter) ||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE)
return -EINVAL;
dbus_message_iter_recurse(&iter, &sub);
dbus_message_iter_get_fixed_array(&sub, &v, &n);
if (n == 0)
id = SD_ID128_NULL;
else if (n == 16)
memcpy(&id, v, n);
else
return -EINVAL;
if (!dbus_message_iter_next(&iter) ||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return -EINVAL;
dbus_message_iter_get_basic(&iter, &service);
if (!dbus_message_iter_next(&iter) ||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return -EINVAL;
dbus_message_iter_get_basic(&iter, &class);
if (isempty(class))
c = _MACHINE_CLASS_INVALID;
else {
c = machine_class_from_string(class);
if (c < 0)
return -EINVAL;
}
if (!dbus_message_iter_next(&iter) ||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
return -EINVAL;
dbus_message_iter_get_basic(&iter, &leader);
if (!dbus_message_iter_next(&iter) ||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return -EINVAL;
dbus_message_iter_get_basic(&iter, &slice);
if (!(isempty(slice) || (unit_name_is_valid(slice, false) && endswith(slice, ".slice"))) ||
!dbus_message_iter_next(&iter) ||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return -EINVAL;
dbus_message_iter_get_basic(&iter, &root_directory);
if (!(isempty(root_directory) || path_is_absolute(root_directory)))
return -EINVAL;
if (hashmap_get(manager->machines, name))
return -EEXIST;
if (leader <= 0) {
leader = bus_get_unix_process_id(manager->bus, dbus_message_get_sender(message), NULL);
if (leader == 0)
return -EINVAL;
}
r = manager_add_machine(manager, name, &m);
if (r < 0)
goto fail;
m->leader = leader;
m->class = c;
m->id = id;
if (!isempty(service)) {
m->service = strdup(service);
if (!m->service) {
r = -ENOMEM;
goto fail;
}
}
if (!isempty(root_directory)) {
m->root_directory = strdup(root_directory);
if (!m->root_directory) {
r = -ENOMEM;
goto fail;
}
}
r = machine_start(m);
if (r < 0)
goto fail;
m->create_message = dbus_message_ref(message);
return 0;
fail:
if (m)
machine_add_to_gc_queue(m);
return r;
}
static int bus_manager_inhibit(
Manager *m,
DBusConnection *connection,
@ -1671,73 +1495,6 @@ static DBusHandlerResult manager_message_handler(
DBUS_TYPE_INVALID);
free(p);
if (!b)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetMachine")) {
Machine *machine;
const char *name;
char *p;
bool b;
if (!dbus_message_get_args(
message,
&error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID))
return bus_send_error_reply(connection, message, &error, -EINVAL);
machine = hashmap_get(m->machines, name);
if (!machine)
return bus_send_error_reply(connection, message, &error, -ENOENT);
reply = dbus_message_new_method_return(message);
if (!reply)
goto oom;
p = machine_bus_path(machine);
if (!p)
goto oom;
b = dbus_message_append_args(
reply,
DBUS_TYPE_OBJECT_PATH, &p,
DBUS_TYPE_INVALID);
free(p);
if (!b)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetMachineByPID")) {
uint32_t pid;
char *p;
Machine *machine;
bool b;
if (!dbus_message_get_args(
message,
&error,
DBUS_TYPE_UINT32, &pid,
DBUS_TYPE_INVALID))
return bus_send_error_reply(connection, message, &error, -EINVAL);
r = manager_get_machine_by_pid(m, pid, &machine);
if (r <= 0)
return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT);
reply = dbus_message_new_method_return(message);
if (!reply)
goto oom;
p = machine_bus_path(machine);
if (!p)
goto oom;
b = dbus_message_append_args(
reply,
DBUS_TYPE_OBJECT_PATH, &p,
DBUS_TYPE_INVALID);
free(p);
if (!b)
goto oom;
@ -1946,48 +1703,6 @@ static DBusHandlerResult manager_message_handler(
if (!dbus_message_iter_close_container(&iter, &sub))
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListMachines")) {
Machine *machine;
Iterator i;
DBusMessageIter iter, sub;
reply = dbus_message_new_method_return(message);
if (!reply)
goto oom;
dbus_message_iter_init_append(reply, &iter);
if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssso)", &sub))
goto oom;
HASHMAP_FOREACH(machine, m->machines, i) {
_cleanup_free_ char *p = NULL;
DBusMessageIter sub2;
const char *class;
if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
goto oom;
p = machine_bus_path(machine);
if (!p)
goto oom;
class = strempty(machine_class_to_string(machine->class));
if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &machine->name) ||
!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &class) ||
!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &machine->service) ||
!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
free(p);
goto oom;
}
if (!dbus_message_iter_close_container(&sub, &sub2))
goto oom;
}
if (!dbus_message_iter_close_container(&iter, &sub))
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "Inhibit")) {
@ -2009,12 +1724,6 @@ static DBusHandlerResult manager_message_handler(
if (r < 0)
return bus_send_error_reply(connection, message, NULL, r);
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateMachine")) {
r = bus_manager_create_machine(m, message);
if (r < 0)
return bus_send_error_reply(connection, message, NULL, r);
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ReleaseSession")) {
const char *name;
Session *session;
@ -2201,45 +1910,6 @@ static DBusHandlerResult manager_message_handler(
if (!reply)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillMachine")) {
const char *swho;
int32_t signo;
KillWho who;
const char *name;
Machine *machine;
if (!dbus_message_get_args(
message,
&error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &swho,
DBUS_TYPE_INT32, &signo,
DBUS_TYPE_INVALID))
return bus_send_error_reply(connection, message, &error, -EINVAL);
if (isempty(swho))
who = KILL_ALL;
else {
who = kill_who_from_string(swho);
if (who < 0)
return bus_send_error_reply(connection, message, &error, -EINVAL);
}
if (signo <= 0 || signo >= _NSIG)
return bus_send_error_reply(connection, message, &error, -EINVAL);
machine = hashmap_get(m->machines, name);
if (!machine)
return bus_send_error_reply(connection, message, &error, -ENOENT);
r = machine_kill(machine, who, signo);
if (r < 0)
return bus_send_error_reply(connection, message, NULL, r);
reply = dbus_message_new_method_return(message);
if (!reply)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
const char *name;
Session *session;
@ -2309,29 +1979,6 @@ static DBusHandlerResult manager_message_handler(
if (!reply)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateMachine")) {
const char *name;
Machine *machine;
if (!dbus_message_get_args(
message,
&error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID))
return bus_send_error_reply(connection, message, &error, -EINVAL);
machine = hashmap_get(m->machines, name);
if (!machine)
return bus_send_error_reply(connection, message, &error, -ENOENT);
r = machine_stop(machine);
if (r < 0)
return bus_send_error_reply(connection, message, NULL, r);
reply = dbus_message_new_method_return(message);
if (!reply)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
uint32_t uid;
struct passwd *pw;
@ -2701,7 +2348,6 @@ DBusHandlerResult bus_message_filter(
m->action_what = 0;
} else {
Machine *mm;
Session *s;
User *u;
@ -2738,25 +2384,6 @@ DBusHandlerResult bus_message_filter(
user_add_to_gc_queue(u);
}
mm = hashmap_get(m->machine_units, unit);
if (mm) {
if (streq_ptr(path, mm->scope_job)) {
free(mm->scope_job);
mm->scope_job = NULL;
if (mm->started) {
if (streq(result, "done"))
machine_send_create_reply(mm, NULL);
else {
dbus_set_error(&error, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
machine_send_create_reply(mm, &error);
}
}
}
machine_add_to_gc_queue(mm);
}
}
} else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
@ -2771,7 +2398,6 @@ DBusHandlerResult bus_message_filter(
unit_name_from_dbus_path(path, &unit);
if (unit) {
Machine *mm;
Session *s;
User *u;
@ -2782,10 +2408,6 @@ DBusHandlerResult bus_message_filter(
u = hashmap_get(m->user_units, unit);
if (u)
user_add_to_gc_queue(u);
mm = hashmap_get(m->machine_units, unit);
if (mm)
machine_add_to_gc_queue(mm);
}
}

12
src/login/logind-user.c

@ -354,11 +354,11 @@ static int user_start_slice(User *u) {
if (r < 0) {
log_error("Failed to start user slice: %s", bus_error(&error, r));
dbus_error_free(&error);
} else {
free(u->slice_job);
u->slice_job = job;
}
free(u->slice_job);
u->slice_job = job;
return 0;
}
@ -388,11 +388,11 @@ static int user_start_service(User *u) {
if (r < 0) {
log_error("Failed to start user service: %s", bus_error(&error, r));
dbus_error_free(&error);
} else {
free(u->service_job);
u->service_job = job;
}
free(u->service_job);
u->service_job = job;
return 0;
}

117
src/login/logind.c

@ -74,18 +74,16 @@ Manager *manager_new(void) {
m->users = hashmap_new(trivial_hash_func, trivial_compare_func);
m->inhibitors = hashmap_new(string_hash_func, string_compare_func);
m->buttons = hashmap_new(string_hash_func, string_compare_func);
m->machines = hashmap_new(string_hash_func, string_compare_func);
m->user_units = hashmap_new(string_hash_func, string_compare_func);
m->session_units = hashmap_new(string_hash_func, string_compare_func);
m->machine_units = hashmap_new(string_hash_func, string_compare_func);
m->session_fds = hashmap_new(trivial_hash_func, trivial_compare_func);
m->inhibitor_fds = hashmap_new(trivial_hash_func, trivial_compare_func);
m->button_fds = hashmap_new(trivial_hash_func, trivial_compare_func);
if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->machines ||
!m->user_units || !m->session_units || !m->machine_units ||
if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons ||
!m->user_units || !m->session_units ||
!m->session_fds || !m->inhibitor_fds || !m->button_fds) {
manager_free(m);
return NULL;
@ -113,7 +111,6 @@ void manager_free(Manager *m) {
Seat *s;
Inhibitor *i;
Button *b;
Machine *machine;
assert(m);
@ -135,20 +132,15 @@ void manager_free(Manager *m) {
while ((b = hashmap_first(m->buttons)))
button_free(b);
while ((machine = hashmap_first(m->machines)))
machine_free(machine);
hashmap_free(m->devices);
hashmap_free(m->seats);
hashmap_free(m->sessions);
hashmap_free(m->users);
hashmap_free(m->inhibitors);
hashmap_free(m->buttons);
hashmap_free(m->machines);
hashmap_free(m->user_units);
hashmap_free(m->session_units);
hashmap_free(m->machine_units);
hashmap_free(m->session_fds);
hashmap_free(m->inhibitor_fds);
@ -364,30 +356,6 @@ int manager_add_button(Manager *m, const char *name, Button **_button) {
return 0;
}
int manager_add_machine(Manager *m, const char *name, Machine **_machine) {
Machine *machine;
assert(m);
assert(name);
machine = hashmap_get(m->machines, name);
if (machine) {
if (_machine)
*_machine = machine;
return 0;
}
machine = machine_new(m, name);
if (!m)
return -ENOMEM;
if (_machine)
*_machine = machine;
return 0;
}
int manager_process_seat_device(Manager *m, struct udev_device *d) {
Device *device;
int r;
@ -772,48 +740,6 @@ int manager_enumerate_inhibitors(Manager *m) {
return r;
}
int manager_enumerate_machines(Manager *m) {
_cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
int r = 0;
assert(m);
/* Read in machine data stored on disk */
d = opendir("/run/systemd/machines");
if (!d) {
if (errno == ENOENT)
return 0;
log_error("Failed to open /run/systemd/machines: %m");
return -errno;
}
FOREACH_DIRENT(de, d, return -errno) {
struct Machine *machine;
int k;
if (!dirent_is_file(de))
continue;
k = manager_add_machine(m, de->d_name, &machine);
if (k < 0) {
log_error("Failed to add machine by file name %s: %s", de->d_name, strerror(-k));
r = k;
continue;
}
machine_add_to_gc_queue(machine);
k = machine_load(machine);
if (k < 0)
r = k;
}
return r;
}
int manager_dispatch_seat_udev(Manager *m) {
struct udev_device *d;
int r;
@ -1018,27 +944,6 @@ int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
return 1;
}
int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) {
_cleanup_free_ char *unit = NULL;
Machine *mm;
int r;
assert(m);
assert(pid >= 1);
assert(machine);
r = cg_pid_get_unit(pid, &unit);
if (r < 0)
return r;
mm = hashmap_get(m->machine_units, unit);
if (!mm)
return 0;
*machine = mm;
return 1;
}
static void manager_dispatch_other(Manager *m, int fd) {
Session *s;
Inhibitor *i;
@ -1098,7 +1003,6 @@ static int manager_connect_bus(Manager *m) {
!dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/seat", &bus_seat_vtable, m) ||
!dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/session", &bus_session_vtable, m) ||
!dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/user", &bus_user_vtable, m) ||
!dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/machine", &bus_machine_vtable, m) ||
!dbus_connection_add_filter(m->bus, bus_message_filter, m, NULL)) {
r = log_oom();
goto fail;
@ -1298,7 +1202,6 @@ void manager_gc(Manager *m, bool drop_not_started) {
Seat *seat;
Session *session;
User *user;
Machine *machine;
assert(m);
@ -1331,16 +1234,6 @@ void manager_gc(Manager *m, bool drop_not_started) {
user_free(user);
}
}
while ((machine = m->machine_gc_queue)) {
LIST_REMOVE(Machine, gc_queue, m->machine_gc_queue, machine);
machine->in_gc_queue = false;
if (machine_check_gc(machine, drop_not_started) == 0) {
machine_stop(machine);
machine_free(machine);
}
}
}
int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
@ -1459,7 +1352,6 @@ int manager_startup(Manager *m) {
Session *session;
User *user;
Inhibitor *inhibitor;
Machine *machine;
Iterator i;
assert(m);
@ -1496,7 +1388,6 @@ int manager_startup(Manager *m) {
manager_enumerate_sessions(m);
manager_enumerate_inhibitors(m);
manager_enumerate_buttons(m);
manager_enumerate_machines(m);
/* Remove stale objects before we start them */
manager_gc(m, false);
@ -1517,9 +1408,6 @@ int manager_startup(Manager *m) {
HASHMAP_FOREACH(inhibitor, m->inhibitors, i)
inhibitor_start(inhibitor);
HASHMAP_FOREACH(machine, m->machines, i)
machine_start(machine);
manager_dispatch_idle_action(m);
return 0;
@ -1671,7 +1559,6 @@ int main(int argc, char *argv[]) {
mkdir_label("/run/systemd/seats", 0755);
mkdir_label("/run/systemd/users", 0755);
mkdir_label("/run/systemd/sessions", 0755);
mkdir_label("/run/systemd/machines", 0755);
m = manager_new();
if (!m) {

7
src/login/logind.h

@ -41,7 +41,6 @@ typedef struct Manager Manager;
#include "logind-inhibit.h"
#include "logind-button.h"
#include "logind-action.h"
#include "logind-machine.h"
struct Manager {
DBusConnection *bus;
@ -52,12 +51,10 @@ struct Manager {
Hashmap *users;
Hashmap *inhibitors;
Hashmap *buttons;
Hashmap *machines;
LIST_HEAD(Seat, seat_gc_queue);
LIST_HEAD(Session, session_gc_queue);
LIST_HEAD(User, user_gc_queue);
LIST_HEAD(Machine, machine_gc_queue);
struct udev *udev;
struct udev_monitor *udev_seat_monitor, *udev_vcsa_monitor, *udev_button_monitor;
@ -85,7 +82,6 @@ struct Manager {
Hashmap *session_units;
Hashmap *user_units;
Hashmap *machine_units;
Hashmap *session_fds;
Hashmap *inhibitor_fds;
@ -144,7 +140,6 @@ int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **
int manager_add_user_by_name(Manager *m, const char *name, User **_user);
int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user);
int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor);
int manager_add_machine(Manager *m, const char *name, Machine **_machine);
int manager_process_seat_device(Manager *m, struct udev_device *d);
int manager_process_button_device(Manager *m, struct udev_device *d);
@ -161,7 +156,6 @@ int manager_enumerate_seats(Manager *m);
int manager_enumerate_sessions(Manager *m);
int manager_enumerate_users(Manager *m);
int manager_enumerate_inhibitors(Manager *m);
int manager_enumerate_machines(Manager *m);
int manager_startup(Manager *m);
int manager_run(Manager *m);
@ -173,7 +167,6 @@ int manager_get_idle_hint(Manager *m, dual_timestamp *t);
int manager_get_user_by_pid(Manager *m, pid_t pid, User **user);
int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session);
int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine);
extern const DBusObjectPathVTable bus_manager_vtable;

1
src/machine/Makefile

@ -0,0 +1 @@
../Makefile

27
src/login/logind-machine-dbus.c → src/machine/machine-dbus.c

@ -22,12 +22,12 @@
#include <errno.h>
#include <string.h>
#include "logind.h"
#include "logind-machine.h"
#include "machined.h"
#include "machine.h"
#include "dbus-common.h"
#define BUS_MACHINE_INTERFACE \
" <interface name=\"org.freedesktop.login1.Machine\">\n" \
" <interface name=\"org.freedesktop.machine1.Machine\">\n" \
" <method name=\"Terminate\"/>\n" \
" <method name=\"Kill\">\n" \
" <arg name=\"who\" type=\"s\"/>\n" \
@ -56,7 +56,7 @@
#define INTERFACES_LIST \
BUS_GENERIC_INTERFACES_LIST \
"org.freedesktop.login1.Machine\0"
"org.freedesktop.machine1.Machine\0"
static int bus_machine_append_id(DBusMessageIter *i, const char *property, void *data) {
DBusMessageIter sub;
@ -106,7 +106,7 @@ static int get_machine_for_path(Manager *m, const char *path, Machine **_machine
assert(path);
assert(_machine);
if (!startswith(path, "/org/freedesktop/login1/machine/"))
if (!startswith(path, "/org/freedesktop/machine1/machine/"))
return -EINVAL;
e = bus_path_unescape(path + 32);