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

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