Re-write to avoid leaking fd if flock(2) fails

This commit is contained in:
Jean-Philippe Ouellet 2016-11-26 20:46:15 -05:00
parent 4c4def2876
commit d2d29c6865

View File

@ -2,6 +2,7 @@
# #
# The Qubes OS Project, http://www.qubes-os.org # The Qubes OS Project, http://www.qubes-os.org
# #
# Copyright (C) 2016 Jean-Philippe Ouellet <jpo@vt.edu>
# Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com> # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
# Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl> # Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
# #
@ -39,11 +40,14 @@ def copy_text_to_qubes_clipboard(text):
try: try:
fd = os.open(APPVIEWER_LOCK, os.O_RDWR|os.O_CREAT, 0666) fd = os.open(APPVIEWER_LOCK, os.O_RDWR|os.O_CREAT, 0666)
fcntl.flock(fd, fcntl.LOCK_EX)
except: except:
QMessageBox.warning(None, "Warning!", "Error while accessing Qubes clipboard!") QMessageBox.warning(None, "Warning!", "Error while accessing Qubes clipboard!")
return else:
try:
fcntl.flock(fd, fcntl.LOCK_EX)
except:
QMessageBox.warning(None, "Warning!", "Error while locking Qubes clipboard!")
else:
try: try:
with open(CLIPBOARD_CONTENTS, "w") as contents: with open(CLIPBOARD_CONTENTS, "w") as contents:
contents.write(text) contents.write(text)
@ -51,7 +55,5 @@ def copy_text_to_qubes_clipboard(text):
source.write("dom0") source.write("dom0")
except: except:
QMessageBox.warning(None, "Warning!", "Error while writing to Qubes clipboard!") QMessageBox.warning(None, "Warning!", "Error while writing to Qubes clipboard!")
finally:
fcntl.flock(fd, fcntl.LOCK_UN) fcntl.flock(fd, fcntl.LOCK_UN)
os.close(fd) os.close(fd)
return