[PATCH 02/11] Use eintr_retry_call from anaconda.

Chris Lumens clumens at redhat.com
Thu Apr 23 18:54:09 UTC 2015


---
 blivet/devicelibs/edd.py   |  8 +++++---
 blivet/devices/file.py     |  8 ++++----
 blivet/devices/optical.py  |  4 ++--
 blivet/fcoe.py             |  8 ++++----
 blivet/formats/fs.py       |  2 +-
 blivet/formats/prepboot.py |  9 +++++----
 blivet/iscsi.py            | 12 ++++++------
 blivet/util.py             | 20 ++++++++++++++++----
 8 files changed, 43 insertions(+), 28 deletions(-)

diff --git a/blivet/devicelibs/edd.py b/blivet/devicelibs/edd.py
index ba4d0cd..08a713f 100644
--- a/blivet/devicelibs/edd.py
+++ b/blivet/devicelibs/edd.py
@@ -27,6 +27,8 @@ import os
 import re
 import struct
 
+from .. import util
+
 log = logging.getLogger("blivet")
 
 re_host_bus = re.compile(r'^PCI\s*(\S*)\s*channel: (\S*)\s*$')
@@ -159,11 +161,11 @@ def collect_mbrs(devices):
     mbr_dict = {}
     for dev in devices:
         try:
-            fd = os.open(dev.path, os.O_RDONLY)
+            fd = util.eintr_retry_call(os.open, dev.path, os.O_RDONLY)
             # The signature is the unsigned integer at byte 440:
             os.lseek(fd, 440, 0)
-            mbrsig = struct.unpack('I', os.read(fd, 4))
-            os.close(fd)
+            mbrsig = struct.unpack('I', util.eintr_retry_call(os.read, fd, 4))
+            util.eintr_retry_call(os.close, fd)
         except OSError as e:
             log.warning("edd: error reading mbrsig from disk %s: %s",
                         dev.name, str(e))
diff --git a/blivet/devices/file.py b/blivet/devices/file.py
index 7d8e781..f104568 100644
--- a/blivet/devices/file.py
+++ b/blivet/devices/file.py
@@ -104,7 +104,7 @@ class FileDevice(StorageDevice):
     def _create(self):
         """ Create the device. """
         log_method_call(self, self.name, status=self.status)
-        fd = os.open(self.path, os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
+        fd = util.eintr_retry_call(os.open, self.path, os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
         # all this fuss is so we write the zeros 1MiB at a time
         zero = "\0"
         block_size = 1024 ** 2
@@ -112,14 +112,14 @@ class FileDevice(StorageDevice):
 
         zeros = zero * block_size
         for _n in range(count):
-            os.write(fd, zeros.encode("utf-8"))
+            util.eintr_retry_call(os.write, fd, zeros.encode("utf-8"))
 
         if rem:
             # write out however many more zeros it takes to hit our size target
             size_target = zero * rem
-            os.write(fd, size_target.encode("utf-8"))
+            util.eintr_retry_call(os.write, fd, size_target.encode("utf-8"))
 
-        os.close(fd)
+        util.eintr_retry_call(os.close, fd)
 
     def _destroy(self):
         """ Destroy the device. """
diff --git a/blivet/devices/optical.py b/blivet/devices/optical.py
index e217740..3f8fcd2 100644
--- a/blivet/devices/optical.py
+++ b/blivet/devices/optical.py
@@ -55,7 +55,7 @@ class OpticalDevice(StorageDevice):
             raise errors.DeviceError("device has not been created", self.name)
 
         try:
-            fd = os.open(self.path, os.O_RDONLY)
+            fd = util.eintr_retry_call(os.open, self.path, os.O_RDONLY)
         except OSError as e:
             # errno 123 = No medium found
             if e.errno == 123:
@@ -63,7 +63,7 @@ class OpticalDevice(StorageDevice):
             else:
                 return True
         else:
-            os.close(fd)
+            util.eintr_retry_call(os.close, fd)
             return True
 
     def eject(self):
diff --git a/blivet/fcoe.py b/blivet/fcoe.py
index 610ca06..1723207 100644
--- a/blivet/fcoe.py
+++ b/blivet/fcoe.py
@@ -148,8 +148,8 @@ class fcoe(object):
             os.makedirs(root + "/etc/fcoe", 0o755)
 
         for nic, dcb, auto_vlan in self.nics:
-            fd = os.open(root + "/etc/fcoe/cfg-" + nic,
-                         os.O_RDWR | os.O_CREAT)
+            fd = util.eintr_retry_call(os.open, root + "/etc/fcoe/cfg-" + nic,
+                                                os.O_RDWR | os.O_CREAT)
             config = '# Created by anaconda\n'
             config += '# Enable/Disable FCoE service at the Ethernet port\n'
             config += 'FCOE_ENABLE="yes"\n'
@@ -164,8 +164,8 @@ class fcoe(object):
                 config += 'AUTO_VLAN="yes"\n'
             else:
                 config += 'AUTO_VLAN="no"\n'
-            os.write(fd, config.encode('utf-8'))
-            os.close(fd)
+            util.eintr_retry_call(os.write, fd, config.encode('utf-8'))
+            util.eintr_retry_call(os.close, fd)
 
         return
 
diff --git a/blivet/formats/fs.py b/blivet/formats/fs.py
index f7d26ce..dd282eb 100644
--- a/blivet/formats/fs.py
+++ b/blivet/formats/fs.py
@@ -1631,7 +1631,7 @@ class TmpFS(NoDevFS):
             # When running with changeroot, such as during installation,
             # self.systemMountpoint is set to the full changeroot path once
             # mounted so even with changeroot, statvfs should still work fine.
-            st = os.statvfs(self.systemMountpoint)
+            st = util.eintr_retry_call(os.statvfs, self.systemMountpoint)
             free_space = Size(st.f_bavail*st.f_frsize)
         else:
             # Free might be called even if the tmpfs mount has not been
diff --git a/blivet/formats/prepboot.py b/blivet/formats/prepboot.py
index ac26b5f..16bd95b 100644
--- a/blivet/formats/prepboot.py
+++ b/blivet/formats/prepboot.py
@@ -22,6 +22,7 @@
 
 from ..size import Size
 from .. import platform
+from .. import util
 from ..i18n import N_
 from . import DeviceFormat, register_device_format
 from parted import PARTITION_PREP
@@ -70,23 +71,23 @@ class PPCPRePBoot(DeviceFormat):
         """
         super(PPCPRePBoot, self)._create(**kwargs)
         try:
-            fd = os.open(self.device, os.O_RDWR)
+            fd = util.eintr_retry_call(os.open, self.device, os.O_RDWR)
             length = os.lseek(fd, 0, os.SEEK_END)
             os.lseek(fd, 0, os.SEEK_SET)
             buf = '\0' * 1024 * 1024
             while length > 0:
                 if length >= len(buf):
-                    os.write(fd, buf.encode("utf-8"))
+                    util.eintr_retry_call(os.write, fd, buf.encode("utf-8"))
                     length -= len(buf)
                 else:
                     buf = '\0' * length
-                    os.write(fd, buf.encode("utf-8"))
+                    util.eintr_retry_call(os.write, fd, buf.encode("utf-8"))
                     length = 0
         except OSError as e:
             log.error("error zeroing out %s: %s", self.device, e)
         finally:
             if fd:
-                os.close(fd)
+                util.eintr_retry_call(os.close, fd)
 
     @property
     def status(self):
diff --git a/blivet/iscsi.py b/blivet/iscsi.py
index 05e34e5..3b6f8c1 100644
--- a/blivet/iscsi.py
+++ b/blivet/iscsi.py
@@ -214,10 +214,10 @@ class iscsi(object):
             os.unlink(INITIATOR_FILE)
         if not os.path.isdir("/etc/iscsi"):
             os.makedirs("/etc/iscsi", 0o755)
-        fd = os.open(INITIATOR_FILE, os.O_RDWR | os.O_CREAT)
+        fd = util.eintr_retry_call(os.open, INITIATOR_FILE, os.O_RDWR | os.O_CREAT)
         initiator_name = "InitiatorName=%s\n" % self.initiator
-        os.write(fd, initiator_name.encode("utf-8"))
-        os.close(fd)
+        util.eintr_retry_call(os.write, fd, initiator_name.encode("utf-8"))
+        util.eintr_retry_call(os.close, fd)
         self.initiatorSet = True
 
         for fulldir in (os.path.join("/var/lib/iscsi", d) for d in \
@@ -410,10 +410,10 @@ class iscsi(object):
 
         if not os.path.isdir(root + "/etc/iscsi"):
             os.makedirs(root + "/etc/iscsi", 0o755)
-        fd = os.open(root + INITIATOR_FILE, os.O_RDWR | os.O_CREAT)
+        fd = util.eintr_retry_call(os.open, root + INITIATOR_FILE, os.O_RDWR | os.O_CREAT)
         initiator_name = "InitiatorName=%s\n" % self.initiator
-        os.write(fd, initiator_name.encode("utf-8"))
-        os.close(fd)
+        util.eintr_retry_call(os.write, fd, initiator_name.encode("utf-8"))
+        util.eintr_retry_call(os.close, fd)
 
         # copy "db" files.  *sigh*
         if os.path.isdir(root + "/var/lib/iscsi"):
diff --git a/blivet/util.py b/blivet/util.py
index d68f967..286c871 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -1,4 +1,5 @@
 import copy
+import errno
 import functools
 import itertools
 import os
@@ -508,7 +509,7 @@ def create_sparse_tempfile(name, size):
         :returns: the path to the newly created file
     """
     (fd, path) = tempfile.mkstemp(prefix="blivet.", suffix="-%s" % name)
-    os.close(fd)
+    eintr_retry_call(os.close, fd)
     create_sparse_file(path, size)
     return path
 
@@ -519,9 +520,9 @@ def create_sparse_file(path, size):
         :param :class:`~.size.Size` size: the size of the file
         :returns: None
     """
-    fd = os.open(path, os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
-    os.ftruncate(fd, size)
-    os.close(fd)
+    fd = eintr_retry_call(os.open, path, os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
+    eintr_retry_call(os.ftruncate, fd, size)
+    eintr_retry_call(os.close, fd)
 
 @contextmanager
 def sparsetmpfile(name, size):
@@ -606,3 +607,14 @@ def power_of_two(value):
         (q, r) = divmod(q, 2)
 
     return True
+
+# Copied from python's subprocess.py
+def eintr_retry_call(func, *args):
+    """Retry an interruptible system call if interrupted."""
+    while True:
+        try:
+            return func(*args)
+        except (OSError, IOError) as e:
+            if e.errno == errno.EINTR:
+                continue
+            raise
-- 
2.2.2



More information about the anaconda-patches mailing list