chiark / gitweb /
systemctl: spawn pager only for commands that generates long output
[elogind.git] / src / systemctl.c
index 8a6277e75c62a941822ba7a8bd7313788371e456..0908b6ae02e69304493135f93d6210c18d43804c 100644 (file)
@@ -110,7 +110,10 @@ static enum dot {
 
 static bool private_bus = false;
 
+static pid_t pager_pid = 0;
+
 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
+static void pager_open(void);
 
 static bool on_tty(void) {
         static int t = -1;
@@ -419,6 +422,8 @@ static int list_units(DBusConnection *bus, char **args, unsigned n) {
 
         assert(bus);
 
+        pager_open();
+
         if (!(m = dbus_message_new_method_call(
                               "org.freedesktop.systemd1",
                               "/org/freedesktop/systemd1",
@@ -765,6 +770,8 @@ static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
 
         assert(bus);
 
+        pager_open();
+
         if (!(m = dbus_message_new_method_call(
                               "org.freedesktop.systemd1",
                               "/org/freedesktop/systemd1",
@@ -2475,6 +2482,9 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
 
         show_properties = !streq(args[0], "status");
 
+        if (show_properties)
+                pager_open();
+
         if (show_properties && n <= 1) {
                 /* If not argument is specified inspect the manager
                  * itself */
@@ -2858,6 +2868,8 @@ static int dump(DBusConnection *bus, char **args, unsigned n) {
 
         dbus_error_init(&error);
 
+        pager_open();
+
         if (!(m = dbus_message_new_method_call(
                               "org.freedesktop.systemd1",
                               "/org/freedesktop/systemd1",
@@ -3220,6 +3232,8 @@ static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
 
         dbus_error_init(&error);
 
+        pager_open();
+
         if (!(m = dbus_message_new_method_call(
                               "org.freedesktop.systemd1",
                               "/org/freedesktop/systemd1",
@@ -5283,10 +5297,12 @@ static int runlevel_main(void) {
 }
 
 static void pager_open(void) {
-        pid_t pid;
         int fd[2];
         const char *pager;
 
+        if (pager_pid > 0)
+                return;
+
         if (!on_tty() || arg_no_pager)
                 return;
 
@@ -5299,15 +5315,15 @@ static void pager_open(void) {
                 return;
         }
 
-        pid = fork();
-        if (pid < 0) {
+        pager_pid = fork();
+        if (pager_pid < 0) {
                 log_error("Failed to fork pager: %m");
                 close_pipe(fd);
                 return;
         }
 
-        /* The original process turns into the PAGER */
-        if (pid != 0) {
+        /* In the child start the pager */
+        if (pager_pid == 0) {
 
                 dup2(fd[0], STDIN_FILENO);
                 close_pipe(fd);
@@ -5315,6 +5331,8 @@ static void pager_open(void) {
                 if (!getenv("LESS"))
                         setenv("LESS", "FRSX", 0);
 
+                prctl(PR_SET_PDEATHSIG, SIGTERM);
+
                 if (pager) {
                         execlp(pager, pager, NULL);
                         execl("/bin/sh", "sh", "-c", pager, NULL);
@@ -5328,13 +5346,25 @@ static void pager_open(void) {
                 _exit(EXIT_FAILURE);
         }
 
-        /* Return in the child */
+        /* 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);
+        wait_for_terminate(pager_pid, &dummy);
+        pager_pid = 0;
+}
+
 int main(int argc, char*argv[]) {
         int r, retval = EXIT_FAILURE;
         DBusConnection *bus = NULL;
@@ -5352,8 +5382,6 @@ 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) {
@@ -5417,5 +5445,7 @@ finish:
 
         strv_free(arg_property);
 
+        pager_close();
+
         return retval;
 }