/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <getopt.h>
#include <locale.h>
#include <string.h>
#include <unistd.h>
#include "sd-bus.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-util.h"
#include "cgroup-show.h"
#include "cgroup-util.h"
#include "log.h"
#include "logs-show.h"
#include "macro.h"
#include "pager.h"
#include "parse-util.h"
#include "process-util.h"
#include "signal-util.h"
#include "spawn-polkit-agent.h"
#include "strv.h"
#include "sysfs-show.h"
#include "terminal-util.h"
#include "unit-name.h"
#include "user-util.h"
#include "util.h"
#include "verbs.h"
static bool arg_all = false;
static bool arg_full = false;
static bool arg_no_pager = false;
static bool arg_legend = true;
static bool arg_ask_password = true;
static void pager_open_if_enabled(void) {
if (arg_no_pager)
return;
pager_open(false);
}
static void polkit_agent_open_if_enabled(void) {
/* Open the polkit agent as a child process if necessary */
if (!arg_ask_password)
return;
if (arg_transport != BUS_TRANSPORT_LOCAL)
return;
}
return
}
unsigned k = 0;
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListSessions",
"");
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
if (arg_legend)
k++;
}
if (r < 0)
return bus_log_parse_error(r);
if (arg_legend)
printf("\n%u sessions listed.\n", k);
return 0;
}
unsigned k = 0;
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListUsers",
"");
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
if (arg_legend)
k++;
}
if (r < 0)
return bus_log_parse_error(r);
if (arg_legend)
printf("\n%u users listed.\n", k);
return 0;
}
unsigned k = 0;
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListSeats",
"");
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
if (arg_legend)
k++;
}
if (r < 0)
return bus_log_parse_error(r);
if (arg_legend)
printf("\n%u seats listed.\n", k);
return 0;
}
const char *cgroup;
int r;
unsigned c;
if (arg_transport != BUS_TRANSPORT_LOCAL)
return 0;
if (!path)
return -ENOMEM;
r = sd_bus_get_property(
bus,
"org.freedesktop.systemd1",
path,
"ControlGroup",
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
return 0;
c = columns();
if (c > 18)
c -= 18;
else
c = 0;
show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, false, &leader, leader > 0, get_output_flags());
return 0;
}
typedef struct SessionStatusInfo {
char *id;
char *name;
unsigned int vtnr;
char *seat;
char *tty;
char *display;
bool remote;
char *remote_host;
char *remote_user;
char *service;
char *type;
char *class;
char *state;
char *scope;
char *desktop;
typedef struct UserStatusInfo {
char *name;
char *state;
char **sessions;
char *display;
char *slice;
typedef struct SeatStatusInfo {
char *id;
char *active_session;
char **sessions;
if (info) {
}
}
if (info) {
}
}
if (info) {
}
}
static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
const char *contents;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
const char *s;
char **p = (char **) userdata;
r = sd_bus_message_read_basic(m, contents[0], &s);
if (r < 0)
return r;
r = free_and_strdup(p, s);
if (r < 0)
return r;
} else {
if (r < 0)
return r;
}
if (r < 0)
return r;
r = sd_bus_message_exit_container(m);
if (r < 0)
return r;
return 0;
}
static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
const char *name;
int r;
assert(m);
if (r < 0)
return r;
if (r < 0)
return r;
}
if (r < 0)
return r;
return sd_bus_message_exit_container(m);
}
{}
};
int r;
if (r < 0)
return log_error_errno(r, "Could not get properties: %m");
if (*new_line)
printf("\n");
*new_line = true;
if (i.name)
else
if (s1)
else if (s2)
if (i.leader > 0) {
_cleanup_free_ char *t = NULL;
get_process_comm(i.leader, &t);
if (t)
printf(" (%s)", t);
printf("\n");
}
if (i.vtnr > 0)
printf("\n");
}
if (i.tty)
else if (i.display)
if (i.remote_host && i.remote_user)
else if (i.remote_host)
else if (i.remote_user)
else if (i.remote)
printf("\t Remote: Yes\n");
if (i.service) {
if (i.type)
if (i.class)
printf("\n");
} else if (i.type) {
if (i.class)
printf("\n");
} else if (i.class)
if (i.state)
if (i.scope) {
if (arg_transport == BUS_TRANSPORT_LOCAL) {
i.scope,
0,
0,
true,
NULL);
}
}
return 0;
}
{}
};
int r;
if (r < 0)
return log_error_errno(r, "Could not get properties: %m");
if (*new_line)
printf("\n");
*new_line = true;
if (i.name)
else
if (s1)
else if (s2)
if (!strv_isempty(i.sessions)) {
char **l;
printf("\tSessions:");
printf(" *%s", *l);
else
printf(" %s", *l);
}
printf("\n");
}
if (i.slice) {
i.slice,
0,
0,
true,
NULL);
}
return 0;
}
{}
};
int r;
if (r < 0)
return log_error_errno(r, "Could not get properties: %m");
if (*new_line)
printf("\n");
*new_line = true;
if (!strv_isempty(i.sessions)) {
char **l;
printf("\tSessions:");
if (streq_ptr(*l, i.active_session))
printf(" *%s", *l);
else
printf(" %s", *l);
}
printf("\n");
}
if (arg_transport == BUS_TRANSPORT_LOCAL) {
unsigned c;
c = columns();
if (c > 21)
c -= 21;
else
c = 0;
printf("\t Devices:\n");
}
return 0;
}
int r;
assert(m);
/* skip what we didn't read */
return sd_bus_message_skip(m, contents);
switch (contents[0]) {
case SD_BUS_TYPE_STRUCT_BEGIN:
const char *s;
if (r < 0)
return bus_log_parse_error(r);
return 0;
if (r < 0)
return bus_log_parse_error(r);
if (!uid_is_valid(uid)) {
return -EINVAL;
}
return 0;
}
break;
case SD_BUS_TYPE_ARRAY:
const char *s;
bool space = false;
if (r < 0)
return bus_log_parse_error(r);
space = true;
}
printf("\n");
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(m);
if (r < 0)
return bus_log_parse_error(r);
return 0;
}
break;
}
if (r < 0)
return bus_log_parse_error(r);
if (r == 0) {
r = sd_bus_message_skip(m, contents);
if (r < 0)
return bus_log_parse_error(r);
if (arg_all)
}
return 0;
}
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
path,
"org.freedesktop.DBus.Properties",
"GetAll",
&error,
&reply,
"s", "");
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
if (*new_line)
printf("\n");
*new_line = true;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return r;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
return 0;
}
int r, i;
if (argc <= 1) {
/* If not argument is specified inspect the manager
* itself */
if (properties)
/* And in the pretty case, show data of the calling session */
}
for (i = 1; i < argc; i++) {
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"GetSession",
"s", argv[i]);
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
if (properties)
else
if (r < 0)
return r;
}
return 0;
}
int r, i;
if (argc <= 1) {
/* If not argument is specified inspect the manager
* itself */
if (properties)
}
for (i = 1; i < argc; i++) {
if (r < 0)
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"GetUser",
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
if (properties)
else
if (r < 0)
return r;
}
return 0;
}
int r, i;
if (argc <= 1) {
/* If not argument is specified inspect the manager
* itself */
if (properties)
}
for (i = 1; i < argc; i++) {
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"GetSeat",
"s", argv[i]);
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
if (properties)
else
if (r < 0)
return r;
}
return 0;
}
int r, i;
if (argc < 2) {
/* No argument? Let's convert this into the empty
* session name, which the calls will then resolve to
* the caller's session. */
short_argv[0] = argv[0];
argv = short_argv;
argc = 2;
}
for (i = 1; i < argc; i++) {
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ActivateSession",
"s", argv[i]);
if (r < 0) {
return r;
}
}
return 0;
}
int r, i;
if (!arg_kill_who)
arg_kill_who = "all";
for (i = 1; i < argc; i++) {
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"KillSession",
if (r < 0) {
return r;
}
}
return 0;
}
bool b;
int r, i;
if (argc < 2) {
short_argv[0] = argv[0];
argv = short_argv;
argc = 2;
}
for (i = 1; i < argc; i++) {
uid = UID_INVALID;
else {
if (r < 0)
}
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"SetUserLinger",
if (r < 0) {
return r;
}
}
return 0;
}
int r, i;
for (i = 1; i < argc; i++) {
if (r < 0)
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"TerminateUser",
if (r < 0) {
return r;
}
}
return 0;
}
int r, i;
if (!arg_kill_who)
arg_kill_who = "all";
for (i = 1; i < argc; i++) {
if (r < 0)
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"KillUser",
if (r < 0) {
return r;
}
}
return 0;
}
int r, i;
for (i = 2; i < argc; i++) {
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"AttachDevice",
if (r < 0) {
return r;
}
}
return 0;
}
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"FlushDevices",
"b", true);
if (r < 0)
return r;
}
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
NULL);
if (r < 0)
return r;
}
int r, i;
for (i = 1; i < argc; i++) {
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"TerminateSeat",
"s", argv[i]);
if (r < 0) {
return r;
}
}
return 0;
}
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Send control commands to or query the login manager.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-pager Do not pipe output into a pager\n"
" --no-legend Do not show the headers and footers\n"
" --no-ask-password Don't prompt for password\n"
" -H --host=[USER@]HOST Operate on remote host\n"
" -M --machine=CONTAINER Operate on local container\n"
" -p --property=NAME Show only properties by this name\n"
" -a --all Show all properties, including empty ones\n"
" -l --full Do not ellipsize output\n"
" --kill-who=WHO Who to send signal to\n"
" -s --signal=SIGNAL Which signal to send\n"
" -n --lines=INTEGER Number of journal entries to show\n"
" -o --output=STRING Change journal output mode (short, short-monotonic,\n"
" verbose, export, json, json-pretty, json-sse, cat)\n\n"
"Session Commands:\n"
" list-sessions List sessions\n"
" session-status [ID...] Show session status\n"
" show-session [ID...] Show properties of sessions or the manager\n"
" activate [ID] Activate a session\n"
" lock-session [ID...] Screen lock one or more sessions\n"
" unlock-session [ID...] Screen unlock one or more sessions\n"
" lock-sessions Screen lock all current sessions\n"
" unlock-sessions Screen unlock all current sessions\n"
" terminate-session ID... Terminate one or more sessions\n"
" kill-session ID... Send signal to processes of a session\n\n"
"User Commands:\n"
" list-users List users\n"
" user-status [USER...] Show user status\n"
" show-user [USER...] Show properties of users or the manager\n"
" enable-linger [USER...] Enable linger state of one or more users\n"
" disable-linger [USER...] Disable linger state of one or more users\n"
" terminate-user USER... Terminate all sessions of one or more users\n"
" kill-user USER... Send signal to processes of a user\n\n"
"Seat Commands:\n"
" list-seats List seats\n"
" seat-status [NAME...] Show seat status\n"
" show-seat [NAME...] Show properties of seats or the manager\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"
return 0;
}
enum {
};
{}
};
int c, r;
switch (c) {
case 'h':
return 0;
case ARG_VERSION:
return version();
case 'p': {
if (r < 0)
return log_oom();
/* If the user asked for a particular
* property, show it to him, even if it is
* empty. */
arg_all = true;
break;
}
case 'a':
arg_all = true;
break;
case 'l':
arg_full = true;
break;
case 'n':
return -EINVAL;
}
break;
case 'o':
if (arg_output < 0) {
return -EINVAL;
}
break;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case ARG_NO_LEGEND:
arg_legend = false;
break;
case ARG_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
case ARG_KILL_WHO:
break;
case 's':
if (arg_signal < 0) {
return -EINVAL;
}
break;
case 'H':
break;
case 'M':
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return 1;
}
{}
};
}
int r;
log_open();
if (r <= 0)
goto finish;
if (r < 0) {
log_error_errno(r, "Failed to create bus connection: %m");
goto finish;
}
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}