]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fix os.getlogin() problem with no tty
authorKoert van der Veer <koert@cloudvps.com>
Thu, 9 Jan 2014 20:18:50 +0000 (21:18 +0100)
committerKoert van der Veer <koert@cloudvps.com>
Thu, 16 Jan 2014 12:57:30 +0000 (13:57 +0100)
Catch OSError(s) from  os.getlogin() and fall back to looking at
environment variables to figure out the user's name. The OSError
is thrown where there is no tty for the python process when
os.getlogin() is called.

Related-Bug: #1221491

Change-Id: I2bd735c9669ba9d25da108da44ea602f358b2dcc

bin/cinder-rootwrap

index 6661513ff1359a15a64b164084631e7457abe85b..564797aee157403b4729557cbff9247f174c8cc0 100755 (executable)
@@ -62,6 +62,15 @@ def _exit_error(execname, message, errorcode, log=True):
     sys.exit(errorcode)
 
 
+def _getlogin():
+    try:
+        return os.getlogin()
+    except OSError:
+        return (os.getenv('USER') or
+                os.getenv('USERNAME') or
+                os.getenv('LOGNAME'))
+
+
 if __name__ == '__main__':
     # Split arguments, require at least a command
     execname = sys.argv.pop(0)
@@ -107,7 +116,7 @@ if __name__ == '__main__':
                                               exec_dirs=config.exec_dirs)
             if config.use_syslog:
                 logging.info("(%s > %s) Executing %s (filter match = %s)" % (
-                    os.getlogin(), pwd.getpwuid(os.getuid())[0],
+                    _getlogin(), pwd.getpwuid(os.getuid())[0],
                     command, filtermatch.name))
 
             obj = subprocess.Popen(command,