1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <dbus/dbus.h>
32 #include "dbus-common.h"
35 static bool arg_no_pager = false;
36 static enum transport {
40 } arg_transport = TRANSPORT_NORMAL;
41 static const char *arg_host = NULL;
43 static bool on_tty(void) {
46 /* Note that this is invoked relatively early, before we start
47 * the pager. That means the value we return reflects whether
48 * we originally were started on a tty, not if we currently
49 * are. But this is intended, since we want colour and so on
50 * when run in our own pager. */
52 if (_unlikely_(t < 0))
53 t = isatty(STDOUT_FILENO) > 0;
58 static void pager_open_if_enabled(void) {
65 static int list_sessions(DBusConnection *bus, char **args, unsigned n) {
66 DBusMessage *m = NULL, *reply = NULL;
69 DBusMessageIter iter, sub, sub2;
72 dbus_error_init(&error);
76 pager_open_if_enabled();
78 m = dbus_message_new_method_call(
79 "org.freedesktop.login1",
80 "/org/freedesktop/login1",
81 "org.freedesktop.login1.Manager",
84 log_error("Could not allocate message.");
88 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
90 log_error("Failed to issue method call: %s", bus_error_message(&error));
95 if (!dbus_message_iter_init(reply, &iter) ||
96 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
97 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
98 log_error("Failed to parse reply.");
103 dbus_message_iter_recurse(&iter, &sub);
106 printf("%10s %10s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT");
108 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
109 const char *id, *user, *seat, *object;
112 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
113 log_error("Failed to parse reply.");
118 dbus_message_iter_recurse(&sub, &sub2);
120 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
121 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
122 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &user, true) < 0 ||
123 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &seat, true) < 0 ||
124 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
125 log_error("Failed to parse reply.");
130 printf("%10s %10u %-16s %-16s\n", id, (unsigned) uid, user, seat);
134 dbus_message_iter_next(&sub);
138 printf("\n%u sessions listed.\n", k);
144 dbus_message_unref(m);
147 dbus_message_unref(reply);
149 dbus_error_free(&error);
154 static int list_users(DBusConnection *bus, char **args, unsigned n) {
155 DBusMessage *m = NULL, *reply = NULL;
158 DBusMessageIter iter, sub, sub2;
161 dbus_error_init(&error);
165 pager_open_if_enabled();
167 m = dbus_message_new_method_call(
168 "org.freedesktop.login1",
169 "/org/freedesktop/login1",
170 "org.freedesktop.login1.Manager",
173 log_error("Could not allocate message.");
177 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
179 log_error("Failed to issue method call: %s", bus_error_message(&error));
184 if (!dbus_message_iter_init(reply, &iter) ||
185 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
186 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
187 log_error("Failed to parse reply.");
192 dbus_message_iter_recurse(&iter, &sub);
195 printf("%10s %-16s\n", "UID", "USER");
197 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
198 const char *user, *object;
201 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
202 log_error("Failed to parse reply.");
207 dbus_message_iter_recurse(&sub, &sub2);
209 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
210 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &user, true) < 0 ||
211 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
212 log_error("Failed to parse reply.");
217 printf("%10u %-16s\n", (unsigned) uid, user);
221 dbus_message_iter_next(&sub);
225 printf("\n%u users listed.\n", k);
231 dbus_message_unref(m);
234 dbus_message_unref(reply);
236 dbus_error_free(&error);
241 static int list_seats(DBusConnection *bus, char **args, unsigned n) {
242 DBusMessage *m = NULL, *reply = NULL;
245 DBusMessageIter iter, sub, sub2;
248 dbus_error_init(&error);
252 pager_open_if_enabled();
254 m = dbus_message_new_method_call(
255 "org.freedesktop.login1",
256 "/org/freedesktop/login1",
257 "org.freedesktop.login1.Manager",
260 log_error("Could not allocate message.");
264 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
266 log_error("Failed to issue method call: %s", bus_error_message(&error));
271 if (!dbus_message_iter_init(reply, &iter) ||
272 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
273 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
274 log_error("Failed to parse reply.");
279 dbus_message_iter_recurse(&iter, &sub);
282 printf("%-16s\n", "SEAT");
284 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
285 const char *seat, *object;
287 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
288 log_error("Failed to parse reply.");
293 dbus_message_iter_recurse(&sub, &sub2);
295 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &seat, true) < 0 ||
296 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
297 log_error("Failed to parse reply.");
302 printf("%-16s\n", seat);
306 dbus_message_iter_next(&sub);
310 printf("\n%u seats listed.\n", k);
316 dbus_message_unref(m);
319 dbus_message_unref(reply);
321 dbus_error_free(&error);
326 static int help(void) {
328 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
329 "Send control commands to or query the login manager.\n\n"
330 " -h --help Show this help\n"
331 " --version Show package version\n"
332 " -H --host=[user@]host\n"
333 " Show information for remote host\n"
334 " -P --privileged Acquire privileges before execution\n"
335 " --no-pager Do not pipe output into a pager.\n"
337 " list-sessions List sessions\n"
338 " list-users List users\n"
339 " list-seats List seats\n",
340 program_invocation_short_name);
345 static int parse_argv(int argc, char *argv[]) {
352 static const struct option options[] = {
353 { "help", no_argument, NULL, 'h' },
354 { "version", no_argument, NULL, ARG_VERSION },
355 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
356 { "host", required_argument, NULL, 'H' },
357 { "privileged",no_argument, NULL, 'P' },
366 while ((c = getopt_long(argc, argv, "hH:P", options, NULL)) >= 0) {
375 puts(PACKAGE_STRING);
377 puts(SYSTEMD_FEATURES);
385 arg_transport = TRANSPORT_POLKIT;
389 arg_transport = TRANSPORT_SSH;
397 log_error("Unknown option code %c", c);
405 static int loginctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
407 static const struct {
415 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
417 { "list-sessions", LESS, 1, list_sessions },
418 { "list-users", EQUAL, 1, list_users },
419 { "list-seats", EQUAL, 1, list_seats },
429 left = argc - optind;
432 /* Special rule: no arguments means "list-sessions" */
435 if (streq(argv[optind], "help")) {
440 for (i = 0; i < ELEMENTSOF(verbs); i++)
441 if (streq(argv[optind], verbs[i].verb))
444 if (i >= ELEMENTSOF(verbs)) {
445 log_error("Unknown operation %s", argv[optind]);
450 switch (verbs[i].argc_cmp) {
453 if (left != verbs[i].argc) {
454 log_error("Invalid number of arguments.");
461 if (left < verbs[i].argc) {
462 log_error("Too few arguments.");
469 if (left > verbs[i].argc) {
470 log_error("Too many arguments.");
477 assert_not_reached("Unknown comparison operator.");
481 log_error("Failed to get D-Bus connection: %s", error->message);
485 return verbs[i].dispatch(bus, argv + optind, left);
488 int main(int argc, char*argv[]) {
489 int r, retval = EXIT_FAILURE;
490 DBusConnection *bus = NULL;
493 dbus_error_init(&error);
495 log_parse_environment();
498 r = parse_argv(argc, argv);
502 retval = EXIT_SUCCESS;
506 if (arg_transport == TRANSPORT_NORMAL)
507 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
508 else if (arg_transport == TRANSPORT_POLKIT)
509 bus_connect_system_polkit(&bus, &error);
510 else if (arg_transport == TRANSPORT_SSH)
511 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
513 assert_not_reached("Uh, invalid transport...");
515 r = loginctl_main(bus, argc, argv, &error);
516 retval = r < 0 ? EXIT_FAILURE : r;
520 dbus_connection_flush(bus);
521 dbus_connection_close(bus);
522 dbus_connection_unref(bus);
525 dbus_error_free(&error);