[systemd-devel] [PATCH v2] systemctl: implement auto-pager a la git

Miklos Vajna vmiklos at frugalware.org
Sun Jan 2 16:32:17 PST 2011


---

On Sun, Jan 02, 2011 at 09:21:50PM +0100, Tollef Fog Heen <tfheen at err.no> wrote:
> | Not all distributions have such a command, while all distributions have
> | less packaged. What about making it a configure option, but let the
> | default be 'less'? Then Debian and other packages can use
> | --with-pager=pager or so.
> 
> Sounds fine to me.

Added --with-default-pager.

> It might be useful to rather just special-case tty output, like ps does,
> where piping to a process will not limit the length of the output, so
> you can do ??ps ax | cat?? which then doesn't cut off at $COLUMNS char
> rather than special-casing cat.  If people do foo | cat, they should get
> what they asked for, IMO.

Makes sense, removed.

 TODO              |    2 --
 configure.ac      |   10 ++++++++++
 man/systemctl.xml |    7 +++++++
 src/systemctl.c   |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/TODO b/TODO
index 21f06a6..c9a7617 100644
--- a/TODO
+++ b/TODO
@@ -67,8 +67,6 @@
 
 * suspend, resume
 
-* systemctl auto-pager a la git
-
 * readahead: btrfs/LVM SSD detection
 
 * when processes remain in a service even though the start command failed enter active
diff --git a/configure.ac b/configure.ac
index 4f77aa8..3108018 100644
--- a/configure.ac
+++ b/configure.ac
@@ -396,9 +396,17 @@ AC_ARG_WITH([syslog-service],
         [SPECIAL_SYSLOG_SERVICE="$withval"],
         [])
 
+DEFAULT_PAGER=less
+AC_ARG_WITH([default-pager],
+	[AS_HELP_STRING([--with-default-pager=COMMAND],
+		[Specify the name of the default pager @<:@default=less@:>@])],
+	[DEFAULT_PAGER="$withval"],
+	[])
+
 AC_SUBST(SYSTEM_SYSVINIT_PATH)
 AC_SUBST(SYSTEM_SYSVRCND_PATH)
 AC_SUBST(SPECIAL_SYSLOG_SERVICE)
+AC_SUBST(DEFAULT_PAGER)
 AC_SUBST(M4_DISTRO_FLAG)
 
 if test "x${SYSTEM_SYSVINIT_PATH}" != "x" -a "x${SYSTEM_SYSVRCND_PATH}" != "x"; then
@@ -421,6 +429,7 @@ AM_CONDITIONAL(TARGET_SLACKWARE, test x"$with_distro" = xslackware)
 AM_CONDITIONAL(TARGET_FRUGALWARE, test x"$with_distro" = xfrugalware)
 
 AC_DEFINE_UNQUOTED(SPECIAL_SYSLOG_SERVICE, ["$SPECIAL_SYSLOG_SERVICE"], [Syslog service name])
+AC_DEFINE_UNQUOTED(DEFAULT_PAGER, ["$DEFAULT_PAGER"], [Default pager command])
 
 AC_ARG_WITH([dbuspolicydir],
         AS_HELP_STRING([--with-dbuspolicydir=DIR], [D-Bus policy directory]),
@@ -476,6 +485,7 @@ echo "
         SysV init scripts:       ${SYSTEM_SYSVINIT_PATH}
         SysV rc?.d directories:  ${SYSTEM_SYSVRCND_PATH}
         Syslog service:          ${SPECIAL_SYSLOG_SERVICE}
+        Default pager :          ${DEFAULT_PAGER}
         Gtk:                     ${have_gtk}
         libcryptsetup:           ${have_libcryptsetup}
         tcpwrap:                 ${have_tcpwrap}
diff --git a/man/systemctl.xml b/man/systemctl.xml
index 6b05e95..c21ed85 100644
--- a/man/systemctl.xml
+++ b/man/systemctl.xml
@@ -171,6 +171,13 @@
                                 enqueued.</para></listitem> </varlistentry>
 
                         <varlistentry>
+                                <term><option>--no-pager</option></term>
+
+				<listitem><para>Do not pipe output into a
+				pager.</para></listitem>
+			</varlistentry>
+
+                        <varlistentry>
                                 <term><option>--system</option></term>
 
                                 <listitem><para>Talk to the systemd
diff --git a/src/systemctl.c b/src/systemctl.c
index 4768fb2..57a1b4f 100644
--- a/src/systemctl.c
+++ b/src/systemctl.c
@@ -65,6 +65,7 @@ static bool arg_user = false;
 static bool arg_global = false;
 static bool arg_immediate = false;
 static bool arg_no_block = false;
+static bool arg_no_pager = false;
 static bool arg_no_wtmp = false;
 static bool arg_no_sync = false;
 static bool arg_no_wall = false;
@@ -4110,6 +4111,7 @@ static int systemctl_help(void) {
                "                      pending\n"
                "  -q --quiet          Suppress output\n"
                "     --no-block       Do not wait until operation finished\n"
+               "     --no-pager       Do not pipe output into a pager.\n"
                "     --system         Connect to system manager\n"
                "     --user           Connect to user service manager\n"
                "     --order          When generating graph for dot, show only order\n"
@@ -4249,6 +4251,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                 ARG_SYSTEM,
                 ARG_GLOBAL,
                 ARG_NO_BLOCK,
+		ARG_NO_PAGER,
                 ARG_NO_WALL,
                 ARG_ORDER,
                 ARG_REQUIRE,
@@ -4272,6 +4275,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                 { "system",    no_argument,       NULL, ARG_SYSTEM    },
                 { "global",    no_argument,       NULL, ARG_GLOBAL    },
                 { "no-block",  no_argument,       NULL, ARG_NO_BLOCK  },
+                { "no-pager",  no_argument,       NULL, ARG_NO_PAGER  },
                 { "no-wall",   no_argument,       NULL, ARG_NO_WALL   },
                 { "quiet",     no_argument,       NULL, 'q'           },
                 { "order",     no_argument,       NULL, ARG_ORDER     },
@@ -4348,6 +4352,10 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         arg_no_block = true;
                         break;
 
+		case ARG_NO_PAGER:
+			arg_no_pager = true;
+			break;
+
                 case ARG_NO_WALL:
                         arg_no_wall = true;
                         break;
@@ -5274,6 +5282,48 @@ static int runlevel_main(void) {
         return 0;
 }
 
+static void pager_open(void) {
+	pid_t pid;
+	int fd[2];
+	const char *pager = getenv("PAGER");
+
+	if (!on_tty() || arg_no_pager)
+		return;
+	if (!pager)
+		pager = DEFAULT_PAGER;
+	else if (!*pager)
+		return;
+
+	if (pipe(fd) < 0)
+		return;
+	pid = fork();
+	if (pid < 0) {
+		close(fd[0]);
+		close(fd[1]);
+		return;
+	}
+
+	/* Return in the child */
+	if (!pid) {
+		dup2(fd[1], 1);
+		close(fd[0]);
+		close(fd[1]);
+		return;
+	}
+
+	/* The original process turns into the PAGER */
+	dup2(fd[0], 0);
+	close(fd[0]);
+	close(fd[1]);
+
+	setenv("LESS", "FRSX", 0);
+	execlp(pager, pager, NULL);
+	execl("/bin/sh", "sh", "-c", pager, NULL);
+
+	log_error("unable to execute pager '%s'", pager);
+	_exit(EXIT_FAILURE);
+}
+
 int main(int argc, char*argv[]) {
         int r, retval = EXIT_FAILURE;
         DBusConnection *bus = NULL;
@@ -5291,6 +5341,8 @@ int main(int argc, char*argv[]) {
                 goto finish;
         }
 
+	pager_open();
+
         /* /sbin/runlevel doesn't need to communicate via D-Bus, so
          * let's shortcut this */
         if (arg_action == ACTION_RUNLEVEL) {
-- 
1.7.3.4



More information about the systemd-devel mailing list