aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libmdsserver/macros.h59
-rw-r--r--src/mds-base.c12
-rw-r--r--src/mds-kbdc/raw-data.c4
-rw-r--r--src/mds-kkbd.c4
-rw-r--r--src/mds-server/mds-server.c2
-rw-r--r--src/mds-vt.c6
-rw-r--r--src/mds.c10
7 files changed, 73 insertions, 24 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); \
diff --git a/src/mds-base.c b/src/mds-base.c
index 79a3ba4..2220003 100644
--- a/src/mds-base.c
+++ b/src/mds-base.c
@@ -187,7 +187,7 @@ int __attribute__((weak)) connect_to_display(void)
fail:
xperror(*argv);
if (socket_fd >= 0)
- close(socket_fd);
+ xclose(socket_fd);
return 1;
}
@@ -422,7 +422,7 @@ static int base_unmarshal(void)
fail_if ((state_buf = state_buf_ = full_read(reexec_fd, NULL)) == NULL);
/* Release resources. */
- close(reexec_fd);
+ xclose(reexec_fd);
shm_unlink(shm_path);
@@ -509,7 +509,7 @@ static void perform_reexec(void)
reexec_fd = shm_open(shm_path, O_RDWR | O_CREAT | O_EXCL, S_IRWXU);
fail_if (reexec_fd < 0);
fail_if (base_marshal(reexec_fd) < 0);
- close(reexec_fd);
+ xclose(reexec_fd);
reexec_fd = -1;
/* Re-exec the server. */
@@ -519,7 +519,7 @@ static void perform_reexec(void)
xperror(*argv);
if (reexec_fd >= 0)
{
- close(reexec_fd);
+ xclose(reexec_fd);
shm_unlink(shm_path);
}
}
@@ -597,14 +597,14 @@ int main(int argc_, char** argv_)
fail_if (1);
}
- close(socket_fd);
+ xclose(socket_fd);
return 0;
fail:
xperror(*argv);
if (socket_fd >= 0)
- close(socket_fd);
+ xclose(socket_fd);
return 1;
}
diff --git a/src/mds-kbdc/raw-data.c b/src/mds-kbdc/raw-data.c
index 78d1939..a3b8ee4 100644
--- a/src/mds-kbdc/raw-data.c
+++ b/src/mds-kbdc/raw-data.c
@@ -140,7 +140,7 @@ static char* read_file(const char* restrict pathname, size_t* restrict size)
fail_if (xxrealloc(old, content, buf_ptr, char));
/* Close file decriptor for the file. */
- close(fd);
+ xclose(fd);
*size = buf_ptr;
return content;
@@ -150,7 +150,7 @@ static char* read_file(const char* restrict pathname, size_t* restrict size)
free(old);
free(content);
if (fd >= 0)
- close(fd);
+ xclose(fd);
return NULL;
}
diff --git a/src/mds-kkbd.c b/src/mds-kkbd.c
index 9bb8294..284c56b 100644
--- a/src/mds-kkbd.c
+++ b/src/mds-kkbd.c
@@ -1413,7 +1413,7 @@ int open_leds(void)
fail_if ((ledfd = open(SPARC_KBD, O_RDONLY)) < 0);
if (ioctl(ledfd, GET_LED, &saved_leds) < 0)
{
- close(ledfd);
+ xclose(ledfd);
fail_if (1);
}
return 0;
@@ -1434,7 +1434,7 @@ void close_leds(void)
if (ioctl(ledfd, SET_LED, saved_leds) < 0)
xperror(*argv);
#ifdef __sparc__
- close(ledfd);
+ xclose(ledfd);
#endif
}
diff --git a/src/mds-server/mds-server.c b/src/mds-server/mds-server.c
index 99d2be2..801e47c 100644
--- a/src/mds-server/mds-server.c
+++ b/src/mds-server/mds-server.c
@@ -330,7 +330,7 @@ void* slave_loop(void* data)
done:
/* Close socket and free resources. */
- close(slave_fd);
+ xclose(slave_fd);
free(msgbuf);
if (information != NULL)
{
diff --git a/src/mds-vt.c b/src/mds-vt.c
index f8a05e7..2449aba 100644
--- a/src/mds-vt.c
+++ b/src/mds-vt.c
@@ -166,7 +166,7 @@ static int write_vt_file(void)
fail:
saved_errno = errno;
if (fd >= 0)
- close(fd);
+ xclose(fd);
return errno = saved_errno, -1;
}
@@ -830,7 +830,7 @@ int vt_open(int vt, struct stat* restrict old_stat)
fail:
saved_errno = errno;
if (fd >= 0)
- close(fd);
+ xclose(fd);
return errno = saved_errno, -1;
}
@@ -848,7 +848,7 @@ void vt_close(int fd, struct stat* restrict old_stat)
xperror(*argv);
eprint("while resetting TTY ownership.");
}
- close(fd);
+ xclose(fd);
}
diff --git a/src/mds.c b/src/mds.c
index cb39005..05a0758 100644
--- a/src/mds.c
+++ b/src/mds.c
@@ -139,11 +139,11 @@ int main(int argc_, char** argv_)
continue;
}
r = is_pid_file_reusable(f);
- fclose(f);
+ xfclose(f);
if (r == 0)
continue;
}
- close(fd);
+ xclose(fd);
break;
}
exit_if (display == DISPLAY_MAX,
@@ -155,11 +155,11 @@ int main(int argc_, char** argv_)
xsnprintf(piddata, "%u\n", getpid());
if (fwrite(piddata, 1, strlen(piddata), f) < strlen(piddata))
{
- fclose(f);
+ xfclose(f);
fail_if (1);
}
fflush(f);
- fclose(f);
+ xfclose(f);
if (chmod(pathname, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0)
xperror(*argv);
@@ -202,7 +202,7 @@ int main(int argc_, char** argv_)
if (fd != -1)
{
shutdown(fd, SHUT_RDWR);
- close(fd);
+ xclose(fd);
unlink(pathname);
}