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,19 +40,20 @@ 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:
try: fcntl.flock(fd, fcntl.LOCK_EX)
with open(CLIPBOARD_CONTENTS, "w") as contents: except:
contents.write(text) QMessageBox.warning(None, "Warning!", "Error while locking Qubes clipboard!")
with open(CLIPBOARD_SOURCE, "w") as source: else:
source.write("dom0") try:
except: with open(CLIPBOARD_CONTENTS, "w") as contents:
QMessageBox.warning(None, "Warning!", "Error while writing to Qubes clipboard!") contents.write(text)
finally: with open(CLIPBOARD_SOURCE, "w") as source:
fcntl.flock(fd, fcntl.LOCK_UN) source.write("dom0")
except:
QMessageBox.warning(None, "Warning!", "Error while writing to Qubes clipboard!")
fcntl.flock(fd, fcntl.LOCK_UN)
os.close(fd) os.close(fd)
return