Blame SOURCES/polkit-0.115-pkttyagent-tty-echo-off-on-fail.patch

21b991
commit bfb722bbe5a503095cc7e860f282b142f5aa75f1
21b991
Author: Jan Rybar <jrybar@redhat.com>
21b991
Date:   Fri Mar 15 16:07:53 2019 +0000
21b991
21b991
    pkttyagent: PolkitAgentTextListener leaves echo tty disabled if SIGINT/SIGTERM
21b991
    
21b991
    If no password is typed into terminal during authentication raised by PolkitAgentTextListener, pkttyagent sends kill (it receives from systemctl/hostnamectl e.g.) without chance to restore echoing back on. This cannot be done in on_request() since it's run in a thread without guarantee the signal is distributed there.
21b991
21b991
diff --git a/src/programs/pkttyagent.c b/src/programs/pkttyagent.c
21b991
index 3f324b8..3c8d502 100644
21b991
--- a/src/programs/pkttyagent.c
21b991
+++ b/src/programs/pkttyagent.c
21b991
@@ -25,11 +25,44 @@
21b991
 
21b991
 #include <stdio.h>
21b991
 #include <stdlib.h>
21b991
+#include <signal.h>
21b991
+#include <termios.h>
21b991
 #include <glib/gi18n.h>
21b991
 #include <polkit/polkit.h>
21b991
 #define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE
21b991
 #include <polkitagent/polkitagent.h>
21b991
 
21b991
+
21b991
+static volatile sig_atomic_t tty_flags_saved;
21b991
+struct termios ts;
21b991
+FILE *tty = NULL;
21b991
+struct sigaction savesigterm, savesigint, savesigtstp;
21b991
+
21b991
+
21b991
+static void tty_handler(int signal)
21b991
+{
21b991
+  switch (signal)
21b991
+  {
21b991
+    case SIGTERM:
21b991
+      sigaction (SIGTERM, &savesigterm, NULL);
21b991
+      break;
21b991
+    case SIGINT:
21b991
+      sigaction (SIGINT, &savesigint, NULL);
21b991
+      break;
21b991
+    case SIGTSTP:
21b991
+      sigaction (SIGTSTP, &savesigtstp, NULL);
21b991
+      break;
21b991
+  }
21b991
+
21b991
+  if (tty_flags_saved)
21b991
+  {
21b991
+    tcsetattr (fileno (tty), TCSAFLUSH, &ts);
21b991
+  }
21b991
+
21b991
+  kill(getpid(), signal);
21b991
+}
21b991
+
21b991
+
21b991
 int
21b991
 main (int argc, char *argv[])
21b991
 {
21b991
@@ -74,6 +107,8 @@ main (int argc, char *argv[])
21b991
   GMainLoop *loop = NULL;
21b991
   guint ret = 126;
21b991
   GVariantBuilder builder;
21b991
+  struct sigaction sa;
21b991
+  const char *tty_name = NULL;
21b991
 
21b991
   /* Disable remote file access from GIO. */
21b991
   setenv ("GIO_USE_VFS", "local", 1);
21b991
@@ -212,6 +247,27 @@ main (int argc, char *argv[])
21b991
         }
21b991
     }
21b991
 
21b991
+/* Bash leaves tty echo disabled if SIGINT/SIGTERM comes to polkitagenttextlistener.c::on_request(),
21b991
+   but due to threading the handlers cannot take care of the signal there.
21b991
+   Though if controlling terminal cannot be found, the world won't stop spinning.
21b991
+*/
21b991
+  tty_name = ctermid(NULL);
21b991
+  if (tty_name != NULL)
21b991
+  {
21b991
+    tty = fopen(tty_name, "r+");
21b991
+  }
21b991
+
21b991
+  if (tty != NULL && !tcgetattr (fileno (tty), &ts))
21b991
+  {
21b991
+	  tty_flags_saved = TRUE;
21b991
+  }
21b991
+
21b991
+  memset (&sa, 0, sizeof (sa));
21b991
+  sa.sa_handler = &tty_handler;
21b991
+  sigaction (SIGTERM, &sa, &savesigterm);
21b991
+  sigaction (SIGINT, &sa, &savesigint);
21b991
+  sigaction (SIGTSTP, &sa, &savesigtstp);
21b991
+
21b991
   loop = g_main_loop_new (NULL, FALSE);
21b991
   g_main_loop_run (loop);
21b991