[ovs-dev] [PATCH branch-2.10 1/2] vlog: Better handle syslog handler exceptions.

Ilya Maximets i.maximets at samsung.com
Tue Feb 26 13:00:03 UTC 2019


'set_levels_from_string' doesn't check for exceptions that could
happen while opening syslog files or connecting to syslog sockets.

For example, if rsyslog stopped on a system:

  $ test-unixctl.py -vFACILITY:daemon --detach
  Traceback (most recent call last):
    File "../../../../tests/test-unixctl.py", line 90, in <module>
      main()
    File "../../../../tests/test-unixctl.py", line 61, in main
      ovs.vlog.handle_args(args)
    File "python/ovs/vlog.py", line 463, in handle_args
      msg = Vlog.set_levels_from_string(verbose)
    File "python/ovs/vlog.py", line 345, in set_levels_from_string
      Vlog.add_syslog_handler(words[1])
    File "python/ovs/vlog.py", line 321, in add_syslog_handler
      facility=syslog_facility)
    File "/python2.7/logging/handlers.py", line 759, in __init__
      self._connect_unixsocket(address)
    File "/python2.7/logging/handlers.py", line 787, in _connect_unixsocket
      self.socket.connect(address)
    File "/python2.7/socket.py", line 224, in meth
      return getattr(self._sock,name)(*args)
  socket.error: [Errno 111] Connection refused

In this case "/dev/log" file exists, so the check inside
'add_syslog_handler' doesn't help.

We need to catch the exceptions in 'set_levels_from_string' same way
as it done in 'init' function.
Also, we don't really need to check for '/dev/log' existence, because
exception will be catched on the upper layer and properly handled by
disabling the corresponding logger.

Fixes: d69d61c7c175 ("vlog: Ability to override the default log facility.")
Signed-off-by: Ilya Maximets <i.maximets at samsung.com>
---
 python/ovs/vlog.py | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/python/ovs/vlog.py b/python/ovs/vlog.py
index 7ac37cb07..bf5c90080 100644
--- a/python/ovs/vlog.py
+++ b/python/ovs/vlog.py
@@ -237,7 +237,7 @@ class Vlog(object):
                     Vlog.__file_handler = logging.FileHandler(Vlog.__log_file)
                     logger.addHandler(Vlog.__file_handler)
             except (IOError, socket.error):
-                logger.setLevel(logging.CRITICAL)
+                logger.disabled = True
 
         ovs.unixctl.command_register("vlog/reopen", "", 0, 0,
                                      Vlog._unixctl_vlog_reopen, None)
@@ -305,20 +305,19 @@ class Vlog(object):
             return
 
         logger = logging.getLogger('syslog')
-        # If there is no infrastructure to support python syslog, disable
-        # the logger to avoid repeated errors.
-        if not os.path.exists("/dev/log"):
-            logger.disabled = True
-            return
+
+        if facility is None:
+            facility = syslog_facility
+
+        new_handler = logging.handlers.SysLogHandler(address="/dev/log",
+                                                     facility=facility)
 
         if syslog_handler:
             logger.removeHandler(syslog_handler)
 
-        if facility:
-            syslog_facility = facility
+        syslog_handler = new_handler
+        syslog_facility = facility
 
-        syslog_handler = logging.handlers.SysLogHandler(address="/dev/log",
-                                                    facility=syslog_facility)
         logger.addHandler(syslog_handler)
         return
 
@@ -342,7 +341,11 @@ class Vlog(object):
                 return "Please supply a valid pattern and destination"
         elif words[0] == "FACILITY":
             if words[1] in FACILITIES:
-                Vlog.add_syslog_handler(words[1])
+                try:
+                    Vlog.add_syslog_handler(words[1])
+                except (IOError, socket.error):
+                    logger = logging.getLogger('syslog')
+                    logger.disabled = True
                 return
             else:
                 return "Facility %s is invalid" % words[1]
-- 
2.17.1



More information about the dev mailing list