463cd9
From e679a3ce0f2cf1558da31e0bccd9e2398b89c7e9 Mon Sep 17 00:00:00 2001
463cd9
From: =?UTF-8?q?Ond=C5=99ej=20Lyson=C4=9Bk?= <olysonek@redhat.com>
463cd9
Date: Tue, 30 Jul 2019 16:07:01 +0200
463cd9
Subject: [PATCH 2/2] Prevent recursion in bug()
463cd9
463cd9
Resolves: rhbz#1666380
463cd9
---
463cd9
 sysutil.c | 35 +++++++++++++++++++++++++++++++----
463cd9
 sysutil.h |  1 +
463cd9
 utility.c | 12 +++++++-----
463cd9
 3 files changed, 39 insertions(+), 9 deletions(-)
463cd9
463cd9
diff --git a/sysutil.c b/sysutil.c
463cd9
index fd07d99..e2df671 100644
463cd9
--- a/sysutil.c
463cd9
+++ b/sysutil.c
463cd9
@@ -774,21 +774,48 @@ vsf_sysutil_deactivate_linger_failok(int fd)
463cd9
   (void) setsockopt(fd, SOL_SOCKET, SO_LINGER, &the_linger, sizeof(the_linger));
463cd9
 }
463cd9
 
463cd9
-void
463cd9
-vsf_sysutil_activate_noblock(int fd)
463cd9
+static int
463cd9
+vsf_sysutil_activate_noblock_internal(int fd, int return_err)
463cd9
 {
463cd9
   int retval;
463cd9
   int curr_flags = fcntl(fd, F_GETFL);
463cd9
   if (vsf_sysutil_retval_is_error(curr_flags))
463cd9
   {
463cd9
-    die("fcntl");
463cd9
+    if (return_err)
463cd9
+    {
463cd9
+      return -1;
463cd9
+    }
463cd9
+    else
463cd9
+    {
463cd9
+      die("fcntl");
463cd9
+    }
463cd9
   }
463cd9
   curr_flags |= O_NONBLOCK;
463cd9
   retval = fcntl(fd, F_SETFL, curr_flags);
463cd9
   if (retval != 0)
463cd9
   {
463cd9
-    die("fcntl");
463cd9
+    if (return_err)
463cd9
+    {
463cd9
+      return -1;
463cd9
+    }
463cd9
+    else
463cd9
+    {
463cd9
+      die("fcntl");
463cd9
+    }
463cd9
   }
463cd9
+  return 0;
463cd9
+}
463cd9
+
463cd9
+void
463cd9
+vsf_sysutil_activate_noblock(int fd)
463cd9
+{
463cd9
+  (void) vsf_sysutil_activate_noblock_internal(fd, 0);
463cd9
+}
463cd9
+
463cd9
+int
463cd9
+vsf_sysutil_activate_noblock_no_die(int fd)
463cd9
+{
463cd9
+  return vsf_sysutil_activate_noblock_internal(fd, 1);
463cd9
 }
463cd9
 
463cd9
 void
463cd9
diff --git a/sysutil.h b/sysutil.h
463cd9
index 2df14ed..0772423 100644
463cd9
--- a/sysutil.h
463cd9
+++ b/sysutil.h
463cd9
@@ -281,6 +281,7 @@ void vsf_sysutil_activate_oobinline(int fd);
463cd9
 void vsf_sysutil_activate_linger(int fd);
463cd9
 void vsf_sysutil_deactivate_linger_failok(int fd);
463cd9
 void vsf_sysutil_activate_noblock(int fd);
463cd9
+int vsf_sysutil_activate_noblock_no_die(int fd);
463cd9
 void vsf_sysutil_deactivate_noblock(int fd);
463cd9
 /* This does SHUT_RDWR */
463cd9
 void vsf_sysutil_shutdown_failok(int fd);
463cd9
diff --git a/utility.c b/utility.c
463cd9
index 75e5bdd..5619a04 100644
463cd9
--- a/utility.c
463cd9
+++ b/utility.c
463cd9
@@ -47,11 +47,13 @@ bug(const char* p_text)
463cd9
   {
463cd9
     vsf_log_die(p_text);
463cd9
   }
463cd9
-  vsf_sysutil_activate_noblock(VSFTP_COMMAND_FD);
463cd9
-  (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, "500 OOPS: ", 10);
463cd9
-  (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, p_text,
463cd9
-                                vsf_sysutil_strlen(p_text));
463cd9
-  (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, "\r\n", 2);
463cd9
+  if (vsf_sysutil_activate_noblock_no_die(VSFTP_COMMAND_FD) == 0)
463cd9
+  {
463cd9
+    (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, "500 OOPS: ", 10);
463cd9
+    (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, p_text,
463cd9
+				  vsf_sysutil_strlen(p_text));
463cd9
+    (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, "\r\n", 2);
463cd9
+  }
463cd9
   if (tunable_log_die)
463cd9
   {
463cd9
     /* Workaround for https://github.com/systemd/systemd/issues/2913 */
463cd9
-- 
463cd9
2.20.1
463cd9