loginctl.c revision abca4822916b85ae5b0b2bef5d458ea2225d25ab
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 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 <unistd.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include "log.h"
#include "util.h"
#include "macro.h"
#include "pager.h"
#include "dbus-common.h"
#include "build.h"
static bool arg_no_pager = false;
static enum transport {
static bool on_tty(void) {
static int t = -1;
/* Note that this is invoked relatively early, before we start
* the pager. That means the value we return reflects whether
* we originally were started on a tty, not if we currently
* are. But this is intended, since we want colour and so on
* when run in our own pager. */
if (_unlikely_(t < 0))
t = isatty(STDOUT_FILENO) > 0;
return t;
}
static void pager_open_if_enabled(void) {
on_tty();
if (!arg_no_pager)
pager_open();
}
int r;
unsigned k = 0;
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListSessions");
if (!m) {
log_error("Could not allocate message.");
return -ENOMEM;
}
if (!reply) {
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (on_tty())
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
k++;
}
if (on_tty())
printf("\n%u sessions listed.\n", k);
r = 0;
if (m)
if (reply)
return r;
}
int r;
unsigned k = 0;
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListUsers");
if (!m) {
log_error("Could not allocate message.");
return -ENOMEM;
}
if (!reply) {
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (on_tty())
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
k++;
}
if (on_tty())
printf("\n%u users listed.\n", k);
r = 0;
if (m)
if (reply)
return r;
}
int r;
unsigned k = 0;
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListSeats");
if (!m) {
log_error("Could not allocate message.");
return -ENOMEM;
}
if (!reply) {
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
if (on_tty())
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
log_error("Failed to parse reply.");
r = -EIO;
goto finish;
}
k++;
}
if (on_tty())
printf("\n%u seats listed.\n", k);
r = 0;
if (m)
if (reply)
return r;
}
static int help(void) {
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"
" -H --host=[user@]host\n"
" Show information for remote host\n"
" -P --privileged Acquire privileges before execution\n"
" --no-pager Do not pipe output into a pager.\n"
"Commands:\n"
" list-sessions List sessions\n"
" list-users List users\n"
" list-seats List seats\n",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case 'P':
break;
case 'H':
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
return 1;
}
static const struct {
const char* verb;
const enum {
MORE,
LESS,
} argc_cmp;
const int argc;
} verbs[] = {
};
int left;
unsigned i;
if (left <= 0)
/* Special rule: no arguments means "list-sessions" */
i = 0;
else {
help();
return 0;
}
for (i = 0; i < ELEMENTSOF(verbs); i++)
break;
if (i >= ELEMENTSOF(verbs)) {
return -EINVAL;
}
}
case EQUAL:
log_error("Invalid number of arguments.");
return -EINVAL;
}
break;
case MORE:
log_error("Too few arguments.");
return -EINVAL;
}
break;
case LESS:
log_error("Too many arguments.");
return -EINVAL;
}
break;
default:
assert_not_reached("Unknown comparison operator.");
}
if (!bus) {
return -EIO;
}
}
int r, retval = EXIT_FAILURE;
log_open();
if (r < 0)
goto finish;
else if (r == 0) {
goto finish;
}
if (arg_transport == TRANSPORT_NORMAL)
else if (arg_transport == TRANSPORT_POLKIT)
else if (arg_transport == TRANSPORT_SSH)
else
assert_not_reached("Uh, invalid transport...");
retval = r < 0 ? EXIT_FAILURE : r;
if (bus) {
}
pager_close();
return retval;
}