[systemd-commits] 4 commits - .gitignore Makefile.am src/cgls.c src/dbus-common.c src/dbus-common.h src/loginctl.c src/pager.c src/pager.h src/systemctl.c src/util.c
Lennart Poettering
lennart at kemper.freedesktop.org
Wed Jul 6 18:31:29 PDT 2011
.gitignore | 1
Makefile.am | 22 ++
src/cgls.c | 23 ++
src/dbus-common.c | 16 +
src/dbus-common.h | 2
src/loginctl.c | 531 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/pager.c | 122 ++++++++++++
src/pager.h | 28 ++
src/systemctl.c | 133 +------------
src/util.c | 6
10 files changed, 761 insertions(+), 123 deletions(-)
New commits:
commit ef3a24de028efe885db1303b4843aba5ffd0f531
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu Jul 7 03:30:47 2011 +0200
systemctl: use cached tty value where possible
diff --git a/src/systemctl.c b/src/systemctl.c
index 6b4f981..3b12441 100644
--- a/src/systemctl.c
+++ b/src/systemctl.c
@@ -785,7 +785,7 @@ static int dot(DBusConnection *bus, char **args, unsigned n) {
" red = Conflicts\n"
" green = After\n");
- if (isatty(fileno(stdout)))
+ if (on_tty())
log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
"-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
@@ -841,7 +841,7 @@ static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
dbus_message_iter_recurse(&iter, &sub);
- if (isatty(STDOUT_FILENO))
+ if (on_tty())
printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
@@ -877,7 +877,7 @@ static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
dbus_message_iter_next(&sub);
}
- if (isatty(STDOUT_FILENO))
+ if (on_tty())
printf("\n%u jobs listed.\n", k);
r = 0;
commit 3b0727f5ec728206bf0db669088c9aa9ebb3c829
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu Jul 7 03:30:31 2011 +0200
systemctl: cache tty value before we open the pager
diff --git a/src/systemctl.c b/src/systemctl.c
index 556070b..6b4f981 100644
--- a/src/systemctl.c
+++ b/src/systemctl.c
@@ -122,11 +122,6 @@ static pid_t agent_pid = 0;
static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
-static void pager_open_if_enabled(void) {
- if (!arg_no_pager)
- pager_open();
-}
-
static bool on_tty(void) {
static int t = -1;
@@ -142,6 +137,13 @@ static bool on_tty(void) {
return t;
}
+static void pager_open_if_enabled(void) {
+ on_tty();
+
+ if (!arg_no_pager)
+ pager_open();
+}
+
static void spawn_ask_password_agent(void) {
pid_t parent;
commit abca4822916b85ae5b0b2bef5d458ea2225d25ab
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu Jul 7 03:29:56 2011 +0200
loginctl: add basic implementation of loginctl for introspecting controlling sessions/users/seats
diff --git a/.gitignore b/.gitignore
index 92b8f73..b108fd9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+systemd-loginctl
systemd-localed
systemd-timedated
systemd-uaccess
diff --git a/Makefile.am b/Makefile.am
index b4feb7a..78e5caf 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -116,6 +116,7 @@ endif
rootbin_PROGRAMS = \
systemd \
systemctl \
+ systemd-loginctl \
systemd-notify \
systemd-ask-password \
systemd-tty-ask-password-agent \
@@ -1130,6 +1131,21 @@ systemctl_LDADD = \
libsystemd-daemon.la \
$(DBUS_LIBS)
+systemd_loginctl_SOURCES = \
+ src/loginctl.c \
+ src/dbus-common.c \
+ src/cgroup-show.c \
+ src/cgroup-util.c \
+ src/pager.c
+
+systemd_loginctl_CFLAGS = \
+ $(AM_CFLAGS) \
+ $(DBUS_CFLAGS)
+
+systemd_loginctl_LDADD = \
+ libsystemd-basic.la \
+ $(DBUS_LIBS)
+
systemd_notify_SOURCES = \
src/notify.c \
src/sd-readahead.c
diff --git a/src/dbus-common.c b/src/dbus-common.c
index 5bfaf36..4b9b457 100644
--- a/src/dbus-common.c
+++ b/src/dbus-common.c
@@ -821,3 +821,19 @@ int bus_append_strv_iter(DBusMessageIter *iter, char **l) {
return 0;
}
+
+int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
+
+ assert(iter);
+ assert(data);
+
+ if (dbus_message_iter_get_arg_type(iter) != type)
+ return -EIO;
+
+ dbus_message_iter_get_basic(iter, data);
+
+ if (!dbus_message_iter_next(iter) != !next)
+ return -EIO;
+
+ return 0;
+}
diff --git a/src/dbus-common.h b/src/dbus-common.h
index 04485e5..e3a3ecb 100644
--- a/src/dbus-common.h
+++ b/src/dbus-common.h
@@ -163,4 +163,6 @@ int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l);
int bus_append_strv_iter(DBusMessageIter *iter, char **l);
+int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next);
+
#endif
diff --git a/src/loginctl.c b/src/loginctl.c
new file mode 100644
index 0000000..022f69e
--- /dev/null
+++ b/src/loginctl.c
@@ -0,0 +1,531 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ 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 <dbus/dbus.h>
+#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 {
+ TRANSPORT_NORMAL,
+ TRANSPORT_SSH,
+ TRANSPORT_POLKIT
+} arg_transport = TRANSPORT_NORMAL;
+static const char *arg_host = NULL;
+
+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();
+}
+
+static int list_sessions(DBusConnection *bus, char **args, unsigned n) {
+ DBusMessage *m = NULL, *reply = NULL;
+ DBusError error;
+ int r;
+ DBusMessageIter iter, sub, sub2;
+ unsigned k = 0;
+
+ dbus_error_init(&error);
+
+ assert(bus);
+
+ pager_open_if_enabled();
+
+ m = dbus_message_new_method_call(
+ "org.freedesktop.login1",
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ "ListSessions");
+ if (!m) {
+ log_error("Could not allocate message.");
+ return -ENOMEM;
+ }
+
+ reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
+ if (!reply) {
+ log_error("Failed to issue method call: %s", bus_error_message(&error));
+ r = -EIO;
+ goto finish;
+ }
+
+ 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.");
+ r = -EIO;
+ goto finish;
+ }
+
+ dbus_message_iter_recurse(&iter, &sub);
+
+ if (on_tty())
+ printf("%10s %10s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT");
+
+ while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+ const char *id, *user, *seat, *object;
+ uint32_t uid;
+
+ if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
+ log_error("Failed to parse reply.");
+ r = -EIO;
+ goto finish;
+ }
+
+ dbus_message_iter_recurse(&sub, &sub2);
+
+ if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &user, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &seat, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
+ log_error("Failed to parse reply.");
+ r = -EIO;
+ goto finish;
+ }
+
+ printf("%10s %10u %-16s %-16s\n", id, (unsigned) uid, user, seat);
+
+ k++;
+
+ dbus_message_iter_next(&sub);
+ }
+
+ if (on_tty())
+ printf("\n%u sessions listed.\n", k);
+
+ r = 0;
+
+finish:
+ if (m)
+ dbus_message_unref(m);
+
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+
+ return r;
+}
+
+static int list_users(DBusConnection *bus, char **args, unsigned n) {
+ DBusMessage *m = NULL, *reply = NULL;
+ DBusError error;
+ int r;
+ DBusMessageIter iter, sub, sub2;
+ unsigned k = 0;
+
+ dbus_error_init(&error);
+
+ assert(bus);
+
+ pager_open_if_enabled();
+
+ m = dbus_message_new_method_call(
+ "org.freedesktop.login1",
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ "ListUsers");
+ if (!m) {
+ log_error("Could not allocate message.");
+ return -ENOMEM;
+ }
+
+ reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
+ if (!reply) {
+ log_error("Failed to issue method call: %s", bus_error_message(&error));
+ r = -EIO;
+ goto finish;
+ }
+
+ 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.");
+ r = -EIO;
+ goto finish;
+ }
+
+ dbus_message_iter_recurse(&iter, &sub);
+
+ if (on_tty())
+ printf("%10s %-16s\n", "UID", "USER");
+
+ while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+ const char *user, *object;
+ uint32_t uid;
+
+ if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
+ log_error("Failed to parse reply.");
+ r = -EIO;
+ goto finish;
+ }
+
+ dbus_message_iter_recurse(&sub, &sub2);
+
+ if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &user, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
+ log_error("Failed to parse reply.");
+ r = -EIO;
+ goto finish;
+ }
+
+ printf("%10u %-16s\n", (unsigned) uid, user);
+
+ k++;
+
+ dbus_message_iter_next(&sub);
+ }
+
+ if (on_tty())
+ printf("\n%u users listed.\n", k);
+
+ r = 0;
+
+finish:
+ if (m)
+ dbus_message_unref(m);
+
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+
+ return r;
+}
+
+static int list_seats(DBusConnection *bus, char **args, unsigned n) {
+ DBusMessage *m = NULL, *reply = NULL;
+ DBusError error;
+ int r;
+ DBusMessageIter iter, sub, sub2;
+ unsigned k = 0;
+
+ dbus_error_init(&error);
+
+ assert(bus);
+
+ pager_open_if_enabled();
+
+ m = dbus_message_new_method_call(
+ "org.freedesktop.login1",
+ "/org/freedesktop/login1",
+ "org.freedesktop.login1.Manager",
+ "ListSeats");
+ if (!m) {
+ log_error("Could not allocate message.");
+ return -ENOMEM;
+ }
+
+ reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
+ if (!reply) {
+ log_error("Failed to issue method call: %s", bus_error_message(&error));
+ r = -EIO;
+ goto finish;
+ }
+
+ 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.");
+ r = -EIO;
+ goto finish;
+ }
+
+ dbus_message_iter_recurse(&iter, &sub);
+
+ if (on_tty())
+ printf("%-16s\n", "SEAT");
+
+ while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
+ const char *seat, *object;
+
+ if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
+ log_error("Failed to parse reply.");
+ r = -EIO;
+ goto finish;
+ }
+
+ dbus_message_iter_recurse(&sub, &sub2);
+
+ if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &seat, true) < 0 ||
+ bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
+ log_error("Failed to parse reply.");
+ r = -EIO;
+ goto finish;
+ }
+
+ printf("%-16s\n", seat);
+
+ k++;
+
+ dbus_message_iter_next(&sub);
+ }
+
+ if (on_tty())
+ printf("\n%u seats listed.\n", k);
+
+ r = 0;
+
+finish:
+ if (m)
+ dbus_message_unref(m);
+
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+
+ 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",
+ program_invocation_short_name);
+
+ return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+ enum {
+ ARG_VERSION = 0x100,
+ ARG_NO_PAGER
+ };
+
+ static const struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, ARG_VERSION },
+ { "no-pager", no_argument, NULL, ARG_NO_PAGER },
+ { "host", required_argument, NULL, 'H' },
+ { "privileged",no_argument, NULL, 'P' },
+ { NULL, 0, NULL, 0 }
+ };
+
+ int c;
+
+ assert(argc >= 0);
+ assert(argv);
+
+ while ((c = getopt_long(argc, argv, "hH:P", options, NULL)) >= 0) {
+
+ switch (c) {
+
+ case 'h':
+ help();
+ return 0;
+
+ case ARG_VERSION:
+ puts(PACKAGE_STRING);
+ puts(DISTRIBUTION);
+ puts(SYSTEMD_FEATURES);
+ return 0;
+
+ case ARG_NO_PAGER:
+ arg_no_pager = true;
+ break;
+
+ case 'P':
+ arg_transport = TRANSPORT_POLKIT;
+ break;
+
+ case 'H':
+ arg_transport = TRANSPORT_SSH;
+ arg_host = optarg;
+ break;
+
+ case '?':
+ return -EINVAL;
+
+ default:
+ log_error("Unknown option code %c", c);
+ return -EINVAL;
+ }
+ }
+
+ return 1;
+}
+
+static int loginctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
+
+ static const struct {
+ const char* verb;
+ const enum {
+ MORE,
+ LESS,
+ EQUAL
+ } argc_cmp;
+ const int argc;
+ int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
+ } verbs[] = {
+ { "list-sessions", LESS, 1, list_sessions },
+ { "list-users", EQUAL, 1, list_users },
+ { "list-seats", EQUAL, 1, list_seats },
+ };
+
+ int left;
+ unsigned i;
+
+ assert(argc >= 0);
+ assert(argv);
+ assert(error);
+
+ left = argc - optind;
+
+ if (left <= 0)
+ /* Special rule: no arguments means "list-sessions" */
+ i = 0;
+ else {
+ if (streq(argv[optind], "help")) {
+ help();
+ return 0;
+ }
+
+ for (i = 0; i < ELEMENTSOF(verbs); i++)
+ if (streq(argv[optind], verbs[i].verb))
+ break;
+
+ if (i >= ELEMENTSOF(verbs)) {
+ log_error("Unknown operation %s", argv[optind]);
+ return -EINVAL;
+ }
+ }
+
+ switch (verbs[i].argc_cmp) {
+
+ case EQUAL:
+ if (left != verbs[i].argc) {
+ log_error("Invalid number of arguments.");
+ return -EINVAL;
+ }
+
+ break;
+
+ case MORE:
+ if (left < verbs[i].argc) {
+ log_error("Too few arguments.");
+ return -EINVAL;
+ }
+
+ break;
+
+ case LESS:
+ if (left > verbs[i].argc) {
+ log_error("Too many arguments.");
+ return -EINVAL;
+ }
+
+ break;
+
+ default:
+ assert_not_reached("Unknown comparison operator.");
+ }
+
+ if (!bus) {
+ log_error("Failed to get D-Bus connection: %s", error->message);
+ return -EIO;
+ }
+
+ return verbs[i].dispatch(bus, argv + optind, left);
+}
+
+int main(int argc, char*argv[]) {
+ int r, retval = EXIT_FAILURE;
+ DBusConnection *bus = NULL;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ log_parse_environment();
+ log_open();
+
+ r = parse_argv(argc, argv);
+ if (r < 0)
+ goto finish;
+ else if (r == 0) {
+ retval = EXIT_SUCCESS;
+ goto finish;
+ }
+
+ if (arg_transport == TRANSPORT_NORMAL)
+ bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
+ else if (arg_transport == TRANSPORT_POLKIT)
+ bus_connect_system_polkit(&bus, &error);
+ else if (arg_transport == TRANSPORT_SSH)
+ bus_connect_system_ssh(NULL, arg_host, &bus, &error);
+ else
+ assert_not_reached("Uh, invalid transport...");
+
+ r = loginctl_main(bus, argc, argv, &error);
+ retval = r < 0 ? EXIT_FAILURE : r;
+
+finish:
+ if (bus) {
+ dbus_connection_flush(bus);
+ dbus_connection_close(bus);
+ dbus_connection_unref(bus);
+ }
+
+ dbus_error_free(&error);
+ dbus_shutdown();
+
+ pager_close();
+
+ return retval;
+}
diff --git a/src/systemctl.c b/src/systemctl.c
index 005b45d..556070b 100644
--- a/src/systemctl.c
+++ b/src/systemctl.c
@@ -33,7 +33,6 @@
#include <sys/stat.h>
#include <stddef.h>
#include <sys/prctl.h>
-
#include <dbus/dbus.h>
#include "log.h"
@@ -278,22 +277,6 @@ static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
return EXIT_FAILURE;
}
-static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
-
- assert(iter);
- assert(data);
-
- if (dbus_message_iter_get_arg_type(iter) != type)
- return -EIO;
-
- dbus_message_iter_get_basic(iter, data);
-
- if (!dbus_message_iter_next(iter) != !next)
- return -EIO;
-
- return 0;
-}
-
static void warn_wall(enum action action) {
static const char *table[_ACTION_MAX] = {
[ACTION_HALT] = "The system is going down for system halt NOW!",
commit 1968a360405e302d4d2c2abc03a3314f81375156
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu Jul 7 02:34:35 2011 +0200
cgls: add pager support to systemd-cgls
diff --git a/Makefile.am b/Makefile.am
index 9a67505..b4feb7a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1118,7 +1118,8 @@ systemctl_SOURCES = \
src/cgroup-show.c \
src/cgroup-util.c \
src/exit-status.c \
- src/unit-name.c
+ src/unit-name.c \
+ src/pager.c
systemctl_CFLAGS = \
$(AM_CFLAGS) \
@@ -1177,7 +1178,8 @@ systemd_readahead_replay_LDADD = \
systemd_cgls_SOURCES = \
src/cgls.c \
src/cgroup-show.c \
- src/cgroup-util.c
+ src/cgroup-util.c \
+ src/pager.c
systemd_cgls_CFLAGS = \
$(AM_CFLAGS)
diff --git a/src/cgls.c b/src/cgls.c
index 2bde743..aebf826 100644
--- a/src/cgls.c
+++ b/src/cgls.c
@@ -30,20 +30,29 @@
#include "cgroup-util.h"
#include "log.h"
#include "util.h"
+#include "pager.h"
+
+static bool arg_no_pager = false;
static void help(void) {
printf("%s [OPTIONS...] [CGROUP...]\n\n"
"Recursively show control group contents.\n\n"
- " -h --help Show this help\n",
+ " -h --help Show this help\n"
+ " --no-pager Do not pipe output into a pager.\n",
program_invocation_short_name);
}
static int parse_argv(int argc, char *argv[]) {
+ enum {
+ ARG_NO_PAGER = 0x100
+ };
+
static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { NULL, 0, NULL, 0 }
+ { "help", no_argument, NULL, 'h' },
+ { "no-pager", no_argument, NULL, ARG_NO_PAGER },
+ { NULL, 0, NULL, 0 }
};
int c;
@@ -59,6 +68,10 @@ static int parse_argv(int argc, char *argv[]) {
help();
return 0;
+ case ARG_NO_PAGER:
+ arg_no_pager = true;
+ break;
+
case '?':
return -EINVAL;
@@ -84,6 +97,9 @@ int main(int argc, char *argv[]) {
goto finish;
}
+ if (!arg_no_pager)
+ pager_open();
+
if (optind < argc) {
unsigned i;
@@ -132,6 +148,7 @@ int main(int argc, char *argv[]) {
retval = EXIT_SUCCESS;
finish:
+ pager_close();
return retval;
}
diff --git a/src/pager.c b/src/pager.c
new file mode 100644
index 0000000..6ea25ad
--- /dev/null
+++ b/src/pager.c
@@ -0,0 +1,122 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ 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 <sys/types.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/prctl.h>
+
+#include "pager.h"
+#include "util.h"
+#include "macro.h"
+
+static pid_t pager_pid = 0;
+
+void pager_open(void) {
+ int fd[2];
+ const char *pager;
+ pid_t parent_pid;
+
+ if (pager_pid > 0)
+ return;
+
+ if (isatty(STDOUT_FILENO) <= 0)
+ return;
+
+ if ((pager = getenv("SYSTEMD_PAGER")) || (pager = getenv("PAGER")))
+ if (!*pager || streq(pager, "cat"))
+ return;
+
+ /* Determine and cache number of columns before we spawn the
+ * pager so that we get the value from the actual tty */
+ columns();
+
+ if (pipe(fd) < 0) {
+ log_error("Failed to create pager pipe: %m");
+ return;
+ }
+
+ parent_pid = getpid();
+
+ pager_pid = fork();
+ if (pager_pid < 0) {
+ log_error("Failed to fork pager: %m");
+ close_pipe(fd);
+ return;
+ }
+
+ /* In the child start the pager */
+ if (pager_pid == 0) {
+
+ dup2(fd[0], STDIN_FILENO);
+ close_pipe(fd);
+
+ setenv("LESS", "FRSX", 0);
+
+ /* Make sure the pager goes away when the parent dies */
+ if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
+ _exit(EXIT_FAILURE);
+
+ /* Check whether our parent died before we were able
+ * to set the death signal */
+ if (getppid() != parent_pid)
+ _exit(EXIT_SUCCESS);
+
+ if (pager) {
+ execlp(pager, pager, NULL);
+ execl("/bin/sh", "sh", "-c", pager, NULL);
+ }
+
+ /* Debian's alternatives command for pagers is
+ * called 'pager'. Note that we do not call
+ * sensible-pagers here, since that is just a
+ * shell script that implements a logic that
+ * is similar to this one anyway, but is
+ * Debian-specific. */
+ execlp("pager", "pager", NULL);
+
+ execlp("less", "less", NULL);
+ execlp("more", "more", NULL);
+ execlp("cat", "cat", NULL);
+
+ log_error("Unable to execute pager: %m");
+ _exit(EXIT_FAILURE);
+ }
+
+ /* Return in the parent */
+ if (dup2(fd[1], STDOUT_FILENO) < 0)
+ log_error("Failed to duplicate pager pipe: %m");
+
+ close_pipe(fd);
+}
+
+void pager_close(void) {
+
+ if (pager_pid <= 0)
+ return;
+
+ /* Inform pager that we are done */
+ fclose(stdout);
+ kill(pager_pid, SIGCONT);
+ wait_for_terminate(pager_pid, NULL);
+ pager_pid = 0;
+}
diff --git a/src/pager.h b/src/pager.h
new file mode 100644
index 0000000..b5b4998
--- /dev/null
+++ b/src/pager.h
@@ -0,0 +1,28 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#ifndef foopagerhfoo
+#define foopagerhfoo
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ 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/>.
+***/
+
+void pager_open(void);
+void pager_close(void);
+
+#endif
diff --git a/src/systemctl.c b/src/systemctl.c
index 8f904c1..005b45d 100644
--- a/src/systemctl.c
+++ b/src/systemctl.c
@@ -56,6 +56,7 @@
#include "bus-errors.h"
#include "build.h"
#include "unit-name.h"
+#include "pager.h"
static const char *arg_type = NULL;
static char **arg_property = NULL;
@@ -118,11 +119,14 @@ static const char *arg_host = NULL;
static bool private_bus = false;
-static pid_t pager_pid = 0;
static pid_t agent_pid = 0;
static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
-static void pager_open(void);
+
+static void pager_open_if_enabled(void) {
+ if (!arg_no_pager)
+ pager_open();
+}
static bool on_tty(void) {
static int t = -1;
@@ -475,7 +479,7 @@ static int list_units(DBusConnection *bus, char **args, unsigned n) {
assert(bus);
- pager_open();
+ pager_open_if_enabled();
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
@@ -825,7 +829,7 @@ static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
assert(bus);
- pager_open();
+ pager_open_if_enabled();
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
@@ -2652,7 +2656,7 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
show_properties = !streq(args[0], "status");
if (show_properties)
- pager_open();
+ pager_open_if_enabled();
if (show_properties && n <= 1) {
/* If not argument is specified inspect the manager
@@ -3048,7 +3052,7 @@ static int dump(DBusConnection *bus, char **args, unsigned n) {
dbus_error_init(&error);
- pager_open();
+ pager_open_if_enabled();
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
@@ -3419,7 +3423,7 @@ static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
dbus_error_init(&error);
- pager_open();
+ pager_open_if_enabled();
if (!(m = dbus_message_new_method_call(
"org.freedesktop.systemd1",
@@ -5602,96 +5606,6 @@ static int runlevel_main(void) {
return 0;
}
-static void pager_open(void) {
- int fd[2];
- const char *pager;
- pid_t parent_pid;
-
- if (pager_pid > 0)
- return;
-
- if (!on_tty() || arg_no_pager)
- return;
-
- if ((pager = getenv("SYSTEMD_PAGER")) || (pager = getenv("PAGER")))
- if (!*pager || streq(pager, "cat"))
- return;
-
- /* Determine and cache number of columns before we spawn the
- * pager so that we get the value from the actual tty */
- columns();
-
- if (pipe(fd) < 0) {
- log_error("Failed to create pager pipe: %m");
- return;
- }
-
- parent_pid = getpid();
-
- pager_pid = fork();
- if (pager_pid < 0) {
- log_error("Failed to fork pager: %m");
- close_pipe(fd);
- return;
- }
-
- /* In the child start the pager */
- if (pager_pid == 0) {
-
- dup2(fd[0], STDIN_FILENO);
- close_pipe(fd);
-
- setenv("LESS", "FRSX", 0);
-
- /* Make sure the pager goes away when the parent dies */
- if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
- _exit(EXIT_FAILURE);
-
- /* Check whether our parent died before we were able
- * to set the death signal */
- if (getppid() != parent_pid)
- _exit(EXIT_SUCCESS);
-
- if (pager) {
- execlp(pager, pager, NULL);
- execl("/bin/sh", "sh", "-c", pager, NULL);
- } else {
- /* Debian's alternatives command for pagers is
- * called 'pager'. Note that we do not call
- * sensible-pagers here, since that is just a
- * shell script that implements a logic that
- * is similar to this one anyway, but is
- * Debian-specific. */
- execlp("pager", "pager", NULL);
-
- execlp("less", "less", NULL);
- execlp("more", "more", NULL);
- }
-
- log_error("Unable to execute pager: %m");
- _exit(EXIT_FAILURE);
- }
-
- /* Return in the parent */
- if (dup2(fd[1], STDOUT_FILENO) < 0)
- log_error("Failed to duplicate pager pipe: %m");
-
- close_pipe(fd);
-}
-
-static void pager_close(void) {
- siginfo_t dummy;
-
- if (pager_pid <= 0)
- return;
-
- /* Inform pager that we are done */
- fclose(stdout);
- kill(pager_pid, SIGCONT);
- wait_for_terminate(pager_pid, &dummy);
- pager_pid = 0;
-}
-
static void agent_close(void) {
siginfo_t dummy;
diff --git a/src/util.c b/src/util.c
index 356b4f9..a0fbdc5 100644
--- a/src/util.c
+++ b/src/util.c
@@ -3866,8 +3866,12 @@ char *normalize_env_assignment(const char *s) {
}
int wait_for_terminate(pid_t pid, siginfo_t *status) {
+ siginfo_t dummy;
+
assert(pid >= 1);
- assert(status);
+
+ if (!status)
+ status = &dummy;
for (;;) {
zero(*status);
More information about the systemd-commits
mailing list