logind-dbus.c revision 19bc719ec28e731159671b06254d9bfd49014894
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "logind.h"
#include "dbus-common.h"
#include "strv.h"
#define BUS_MANAGER_INTERFACE \
" <interface name=\"org.freedesktop.login1.Manager\">\n" \
" <method name=\"GetSession\">\n" \
" <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"GetUser\">\n" \
" <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
" <arg name=\"user\" type=\"o\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"GetSeat\">\n" \
" <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"ListSessions\">\n" \
" <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"ListUsers\">\n" \
" <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"ListSeats\">\n" \
" <arg name=\"seats\" type=\"a(so)\" 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" \
" <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"type\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"display\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n" \
" <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
" <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
" <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
" <arg name=\"id\" type=\"s\" direction=\"out\"/>\n" \
" <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
" <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
" <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n" \
" </method>\n" \
" <method name=\"ActivateSession\">\n" \
" <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"TerminateSession\">\n" \
" <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"TerminateUser\">\n" \
" <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"TerminateSeat\">\n" \
" <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
" </method>\n" \
" <signal name=\"SessionNew\">\n" \
" <arg name=\"id\" type=\"s\"/>\n" \
" <arg name=\"path\" type=\"o\"/>\n" \
" </signal>\n" \
" <signal name=\"SessionRemoved\">\n" \
" <arg name=\"id\" type=\"s\"/>\n" \
" <arg name=\"path\" type=\"o\"/>\n" \
" </signal>\n" \
" <signal name=\"UserNew\">\n" \
" <arg name=\"uid\" type=\"u\"/>\n" \
" <arg name=\"path\" type=\"o\"/>\n" \
" </signal>\n" \
" <signal name=\"UserRemoved\">\n" \
" <arg name=\"uid\" type=\"u\"/>\n" \
" <arg name=\"path\" type=\"o\"/>\n" \
" </signal>\n" \
" <signal name=\"SeatNew\">\n" \
" <arg name=\"id\" type=\"s\"/>\n" \
" <arg name=\"path\" type=\"o\"/>\n" \
" </signal>\n" \
" <signal name=\"SeatRemoved\">\n" \
" <arg name=\"id\" type=\"s\"/>\n" \
" <arg name=\"path\" type=\"o\"/>\n" \
" </signal>\n" \
" <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\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" \
" <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
" <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
" <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
" <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
" </interface>\n"
#define INTROSPECTION_BEGIN \
"<node>\n" \
#define INTROSPECTION_END \
"</node>\n"
#define INTERFACES_LIST \
"org.freedesktop.login1.Manager\0"
dbus_bool_t b;
assert(i);
assert(m);
b = manager_get_idle_hint(m, NULL) > 0;
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
return -ENOMEM;
return 0;
}
static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
uint64_t u;
assert(i);
assert(m);
manager_get_idle_hint(m, &t);
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
return -ENOMEM;
return 0;
}
SessionType t;
Seat *s;
int r;
char *id, *p;
int vtnr = -1;
bool b;
assert(m);
return -EINVAL;
if (!dbus_message_iter_next(&iter) ||
return -EINVAL;
if (leader <= 0 ||
!dbus_message_iter_next(&iter) ||
return -EINVAL;
if (!dbus_message_iter_next(&iter) ||
return -EINVAL;
t = session_type_from_string(type);
if (t < 0 ||
!dbus_message_iter_next(&iter) ||
return -EINVAL;
s = NULL;
else {
if (!s)
return -ENOENT;
}
if (!dbus_message_iter_next(&iter) ||
return -EINVAL;
if (!s)
s = m->vtconsole;
else if (s != m->vtconsole)
return -EINVAL;
if (vtnr <= 0)
} else if (s == m->vtconsole)
return -EINVAL;
if (!dbus_message_iter_next(&iter) ||
return -EINVAL;
if (!dbus_message_iter_next(&iter) ||
return -EINVAL;
if (!dbus_message_iter_next(&iter) ||
return -EINVAL;
if (!dbus_message_iter_next(&iter) ||
return -EINVAL;
if (!dbus_message_iter_next(&iter) ||
return -EINVAL;
if (r < 0)
return -EINVAL;
if (!dbus_message_iter_next(&iter) ||
r = -EINVAL;
goto fail;
}
if (r < 0)
goto fail;
if (!dbus_message_iter_next(&iter) ||
r = -EINVAL;
goto fail;
}
if (r < 0)
goto fail;
if (audit_id > 0)
else
if (!id) {
r = -ENOMEM;
goto fail;
}
r = -EEXIST;
goto fail;
}
if (r < 0)
goto fail;
r = -ENOMEM;
goto fail;
}
}
r = -ENOMEM;
goto fail;
}
}
if (!isempty(remote_user)) {
if (!session->remote_user) {
r = -ENOMEM;
goto fail;
}
}
if (!isempty(remote_host)) {
if (!session->remote_host) {
r = -ENOMEM;
goto fail;
}
}
r = -ENOMEM;
goto fail;
}
}
r = -errno;
goto fail;
}
pipe_fds[0] = -1;
if (s) {
r = seat_attach_session(s, session);
if (r < 0)
goto fail;
}
r = session_start(session);
if (r < 0)
goto fail;
if (!reply) {
r = -ENOMEM;
goto fail;
}
p = session_bus_path(session);
if (!p) {
r = -ENOMEM;
goto fail;
}
free(p);
if (!b) {
r = -ENOMEM;
goto fail;
}
return 0;
fail:
if (session)
if (user)
if (reply)
return r;
}
void *userdata) {
const BusProperty properties[] = {
{ "org.freedesktop.login1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_path },
{ "org.freedesktop.login1.Manager", "Controllers", bus_property_append_strv, "as", m->controllers },
{ "org.freedesktop.login1.Manager", "KillOnlyUsers", bus_property_append_strv, "as", m->kill_only_users },
{ "org.freedesktop.login1.Manager", "KillExcludeUsers", bus_property_append_strv, "as", m->kill_exclude_users },
{ "org.freedesktop.login1.Manager", "KillUserProcesses", bus_property_append_bool, "b", &m->kill_user_processes },
{ "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m },
};
int r;
assert(m);
const char *name;
char *p;
bool b;
if (!dbus_message_get_args(
&error,
if (!session)
if (!reply)
goto oom;
p = session_bus_path(session);
if (!p)
goto oom;
free(p);
if (!b)
goto oom;
char *p;
bool b;
if (!dbus_message_get_args(
&error,
if (!user)
if (!reply)
goto oom;
p = user_bus_path(user);
if (!p)
goto oom;
free(p);
if (!b)
goto oom;
const char *name;
char *p;
bool b;
if (!dbus_message_get_args(
&error,
if (!seat)
if (!reply)
goto oom;
p = seat_bus_path(seat);
if (!p)
goto oom;
free(p);
if (!b)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
char *p;
Iterator i;
const char *empty = "";
if (!reply)
goto oom;
goto oom;
goto oom;
p = session_bus_path(session);
if (!p)
goto oom;
!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
free(p);
goto oom;
}
free(p);
goto oom;
}
goto oom;
char *p;
Iterator i;
if (!reply)
goto oom;
goto oom;
goto oom;
p = user_bus_path(user);
if (!p)
goto oom;
free(p);
goto oom;
}
free(p);
goto oom;
}
goto oom;
char *p;
Iterator i;
if (!reply)
goto oom;
goto oom;
goto oom;
p = seat_bus_path(seat);
if (!p)
goto oom;
free(p);
goto oom;
}
free(p);
goto oom;
}
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
if (r == -ENOMEM)
goto oom;
if (r < 0)
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
const char *name;
if (!dbus_message_get_args(
&error,
if (!session)
r = session_activate(session);
if (r < 0)
if (!reply)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
const char *name;
if (!dbus_message_get_args(
&error,
if (!session)
r = session_stop(session);
if (r < 0)
if (!reply)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
if (!dbus_message_get_args(
&error,
if (!user)
if (r < 0)
if (!reply)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
const char *name;
if (!dbus_message_get_args(
&error,
if (!seat)
r = seat_stop_sessions(seat);
if (r < 0)
if (!reply)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
char *introspection = NULL;
FILE *f;
Iterator i;
char *p;
goto oom;
/* We roll our own introspection code here, instead of
* relying on bus_default_message_handler() because we
* need to generate our introspection string
* dynamically. */
goto oom;
fputs(INTROSPECTION_BEGIN, f);
if (p) {
fprintf(f, "<node name=\"seat/%s\"/>", p);
free(p);
}
}
if (p) {
fprintf(f, "<node name=\"session/%s\"/>", p);
free(p);
}
}
fputs(INTROSPECTION_END, f);
if (ferror(f)) {
fclose(f);
goto oom;
}
fclose(f);
if (!introspection)
goto oom;
goto oom;
}
} else
if (reply) {
goto oom;
}
return DBUS_HANDLER_RESULT_HANDLED;
oom:
if (reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
const DBusObjectPathVTable bus_manager_vtable = {
};
DBusMessage *m;
int r = -ENOMEM;
m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
if (!m)
goto finish;
goto finish;
r = 0;
if (m)
return r;
}