__FUNCTION__, __LINE__, __VA_ARGS__)
-int virFileClose(int *fdptr, bool preserve_errno, bool ignore_EBADF)
+int virFileClose(int *fdptr, virFileCloseFlags flags)
{
int saved_errno = 0;
int rc = 0;
if (*fdptr < 0)
return 0;
- if (preserve_errno)
+ if (flags & VIR_FILE_CLOSE_PRESERVE_ERRNO)
saved_errno = errno;
rc = close(*fdptr);
- if (rc < 0) {
- if (errno == EBADF) {
- if (!ignore_EBADF)
- VIR_WARN("Tried to close invalid fd %d", *fdptr);
+
+ if (!(flags & VIR_FILE_CLOSE_DONT_LOG)) {
+ if (rc < 0) {
+ if (errno == EBADF) {
+ if (!(flags & VIR_FILE_CLOSE_IGNORE_EBADF))
+ VIR_WARN("Tried to close invalid fd %d", *fdptr);
+ } else {
+ char ebuf[1024] ATTRIBUTE_UNUSED;
+ VIR_DEBUG("Failed to close fd %d: %s",
+ *fdptr, virStrerror(errno, ebuf, sizeof(ebuf)));
+ }
} else {
- char ebuf[1024] ATTRIBUTE_UNUSED;
- VIR_DEBUG("Failed to close fd %d: %s",
- *fdptr, virStrerror(errno, ebuf, sizeof(ebuf)));
+ VIR_DEBUG("Closed fd %d", *fdptr);
}
- } else {
- VIR_DEBUG("Closed fd %d", *fdptr);
}
*fdptr = -1;
- if (preserve_errno)
+
+ if (flags & VIR_FILE_CLOSE_PRESERVE_ERRNO)
errno = saved_errno;
return rc;
# include "internal.h"
# include "ignore-value.h"
+typedef enum virFileCloseFlags {
+ VIR_FILE_CLOSE_PRESERVE_ERRNO = 1 << 0,
+ VIR_FILE_CLOSE_IGNORE_EBADF = 1 << 1,
+ VIR_FILE_CLOSE_DONT_LOG = 1 << 2,
+} virFileCloseFlags;
/* Don't call these directly - use the macros below */
-int virFileClose(int *fdptr, bool preserve_errno, bool ignore_EBADF)
+int virFileClose(int *fdptr, virFileCloseFlags flags)
ATTRIBUTE_RETURN_CHECK;
int virFileFclose(FILE **file, bool preserve_errno) ATTRIBUTE_RETURN_CHECK;
FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
/* For use on normal paths; caller must check return value,
and failure sets errno per close. */
-# define VIR_CLOSE(FD) virFileClose(&(FD), false, false)
+# define VIR_CLOSE(FD) virFileClose(&(FD), 0)
# define VIR_FCLOSE(FILE) virFileFclose(&(FILE), false)
/* Wrapper around fdopen that consumes fd on success. */
/* For use on cleanup paths; errno is unaffected by close,
and no return value to worry about. */
-# define VIR_FORCE_CLOSE(FD) ignore_value(virFileClose(&(FD), true, false))
+# define VIR_FORCE_CLOSE(FD) \
+ ignore_value(virFileClose(&(FD), VIR_FILE_CLOSE_PRESERVE_ERRNO))
# define VIR_FORCE_FCLOSE(FILE) ignore_value(virFileFclose(&(FILE), true))
/* Similar VIR_FORCE_CLOSE() but ignores EBADF errors since they are expected
* during mass close after fork(). */
-# define VIR_MASS_CLOSE(FD) ignore_value(virFileClose(&(FD), true, true))
+# define VIR_MASS_CLOSE(FD) \
+ ignore_value(virFileClose(&(FD), \
+ VIR_FILE_CLOSE_PRESERVE_ERRNO | \
+ VIR_FILE_CLOSE_IGNORE_EBADF))
+
+# define VIR_LOG_CLOSE(FD) \
+ ignore_value(virFileClose(&(FD), \
+ VIR_FILE_CLOSE_PRESERVE_ERRNO | \
+ VIR_FILE_CLOSE_DONT_LOG))
/* Opaque type for managing a wrapper around a fd. */
struct _virFileWrapperFd;