aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmdsserver
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-08-21 20:43:01 +0200
committerMattias Andrée <maandree@operamail.com>2015-08-21 20:43:01 +0200
commit16b25c8a426c285c85c24db115c677218d287da9 (patch)
tree7712ef9bc069da7836bcf492edc5ca915d8ceb5e /src/libmdsserver
parentm info (diff)
downloadmds-16b25c8a426c285c85c24db115c677218d287da9.tar.gz
mds-16b25c8a426c285c85c24db115c677218d287da9.tar.bz2
mds-16b25c8a426c285c85c24db115c677218d287da9.tar.xz
add and use xclose and xfclose, we do not want close or fclose to be ignored because a singal interrupts it.
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/libmdsserver')
-rw-r--r--src/libmdsserver/macros.h59
1 files changed, 54 insertions, 5 deletions
diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h
index 36e7598..b249ff1 100644
--- a/src/libmdsserver/macros.h
+++ b/src/libmdsserver/macros.h
@@ -25,12 +25,13 @@
#include <stdio.h>
#include <errno.h>
#include <signal.h>
+#include <unistd.h>
+#include <time.h>
+#include <stddef.h>
/*
-#include <unistd.h>
#include <pthread.h>
#include <string.h>
-#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
@@ -42,7 +43,25 @@
/* CLOCK_MONOTONIC_RAW is a Linux-specific bug-fix */
#ifndef CLOCK_MONOTONIC_RAW
- #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
+# define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
+#endif
+
+/* Define TEMP_FAILURE_RETRY if not defined, however
+ * this version does not return a value, it will hoever
+ * clear `errno` if no error occurs. */
+#ifndef TEMP_FAILURE_RETRY
+# define TEMP_FAILURE_RETRY(expression) \
+ do \
+ { \
+ ssize_t __result; \
+ do \
+ __result = (ssize_t)(expression); \
+ while ((__result < 0) && (errno == EINTR)); \
+ if (__result >= 0) \
+ errno = 0; \
+ } \
+ while (0)
+# define MDS_LIBMDSSERVER_MACROS_DEFINED_TEMP_FAILURE_RETRY
#endif
@@ -322,13 +341,43 @@
* monotonic time, the exact clock ID is not specified
*
* @param time_slot:struct timespec* Pointer to the variable in which to store the time
- * @return :int Zero on sucess, -1 on error
+ * @return :int Zero on success, -1 on error
*/
#define monotone(time_slot) \
clock_gettime(CLOCK_MONOTONIC_RAW, time_slot)
/**
+ * Wrapper for `close` that will retry if it gets
+ * interrupted
+ *
+ * @param fd:int The file descriptor
+ */
+#ifdef MDS_LIBMDSSERVER_MACROS_DEFINED_TEMP_FAILURE_RETRY
+# define xclose(fd) \
+ TEMP_FAILURE_RETRY(close(fd))
+#else
+# define xclose(fd) \
+ (TEMP_FAILURE_RETRY(close(fd)) < 0 ? 0 : (errno = 0))
+#endif
+
+
+/**
+ * Wrapper for `fclose` that will retry if it gets
+ * interrupted
+ *
+ * @param f:FILE* The stream
+ */
+#ifdef MDS_LIBMDSSERVER_MACROS_DEFINED_TEMP_FAILURE_RETRY
+# define xfclose(f) \
+ TEMP_FAILURE_RETRY(fclose(f))
+#else
+# define xfclose(f) \
+ (TEMP_FAILURE_RETRY(fclose(f)) < 0 ? 0 : (errno = 0))
+#endif
+
+
+/**
* Close all file descriptors that satisfies a condition
*
* @param condition The condition, it should evaluate the variable `fd`
@@ -347,7 +396,7 @@
{ \
int fd = atoi(file->d_name); \
if (condition) \
- close(fd); \
+ xclose(fd); \
} \
\
closedir(dir); \