4 # Copyright (C) 2006, 2007, 2010, 2011 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 """Utility functions wrapping other functions.
35 def TestDelay(duration):
36 """Sleep for a fixed amount of time.
39 @param duration: the sleep duration
41 @return: False for negative value, True otherwise
45 return False, "Invalid sleep duration"
50 def CloseFdNoError(fd, retries=5):
51 """Close a file descriptor ignoring errors.
54 @param fd: the file descriptor
56 @param retries: how many retries to make, in case we get any
57 other error than EBADF
63 if err.errno != errno.EBADF:
65 CloseFdNoError(fd, retries - 1)
66 # else either it's closed already or we're out of retries, so we
67 # ignore this and go on
70 def SetCloseOnExecFlag(fd, enable):
71 """Sets or unsets the close-on-exec flag on a file descriptor.
74 @param fd: File descriptor
76 @param enable: Whether to set or unset it.
79 flags = fcntl.fcntl(fd, fcntl.F_GETFD)
82 flags |= fcntl.FD_CLOEXEC
84 flags &= ~fcntl.FD_CLOEXEC
86 fcntl.fcntl(fd, fcntl.F_SETFD, flags)
89 def SetNonblockFlag(fd, enable):
90 """Sets or unsets the O_NONBLOCK flag on on a file descriptor.
93 @param fd: File descriptor
95 @param enable: Whether to set or unset it
98 flags = fcntl.fcntl(fd, fcntl.F_GETFL)
101 flags |= os.O_NONBLOCK
103 flags &= ~os.O_NONBLOCK
105 fcntl.fcntl(fd, fcntl.F_SETFL, flags)
108 def RetryOnSignal(fn, *args, **kwargs):
109 """Calls a function again if it failed due to EINTR.
114 return fn(*args, **kwargs)
115 except EnvironmentError, err:
116 if err.errno != errno.EINTR:
118 except (socket.error, select.error), err:
119 # In python 2.6 and above select.error is an IOError, so it's handled
120 # above, in 2.5 and below it's not, and it's handled here.
121 if not (err.args and err.args[0] == errno.EINTR):
125 def IgnoreProcessNotFound(fn, *args, **kwargs):
126 """Ignores ESRCH when calling a process-related function.
128 ESRCH is raised when a process is not found.
131 @return: Whether process was found
136 except EnvironmentError, err:
138 if err.errno == errno.ESRCH:
145 def IgnoreSignals(fn, *args, **kwargs):
146 """Tries to call a function ignoring failures due to EINTR.
150 return fn(*args, **kwargs)
151 except EnvironmentError, err:
152 if err.errno == errno.EINTR:
156 except (select.error, socket.error), err:
157 # In python 2.6 and above select.error is an IOError, so it's handled
158 # above, in 2.5 and below it's not, and it's handled here.
159 if err.args and err.args[0] == errno.EINTR:
165 def GetClosedTempfile(*args, **kwargs):
166 """Creates a temporary file and returns its path.
169 (fd, path) = tempfile.mkstemp(*args, **kwargs)
174 def ResetTempfileModule():
175 """Resets the random name generator of the tempfile module.
177 This function should be called after C{os.fork} in the child process to
178 ensure it creates a newly seeded random generator. Otherwise it would
179 generate the same random parts as the parent process. If several processes
180 race for the creation of a temporary file, this could lead to one not getting
184 # pylint: disable-msg=W0212
185 if hasattr(tempfile, "_once_lock") and hasattr(tempfile, "_name_sequence"):
186 tempfile._once_lock.acquire()
188 # Reset random name generator
189 tempfile._name_sequence = None
191 tempfile._once_lock.release()
193 logging.critical("The tempfile module misses at least one of the"
194 " '_once_lock' and '_name_sequence' attributes")