diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | doc/info/mds.texinfo | 30 | ||||
-rw-r--r-- | src/libmdsserver/macros.h | 59 | ||||
-rw-r--r-- | src/mds-base.c | 12 | ||||
-rw-r--r-- | src/mds-kbdc/raw-data.c | 4 | ||||
-rw-r--r-- | src/mds-kkbd.c | 4 | ||||
-rw-r--r-- | src/mds-server/mds-server.c | 2 | ||||
-rw-r--r-- | src/mds-vt.c | 6 | ||||
-rw-r--r-- | src/mds.c | 10 |
9 files changed, 104 insertions, 25 deletions
@@ -9,7 +9,7 @@ include mk/config.mk # Splits of the info manual. -INFOPARTS = 1 2 +INFOPARTS = 1 2 3 # Object files for the libary. LIBOBJ = linked-list client-list hash-table fd-table mds-message util diff --git a/doc/info/mds.texinfo b/doc/info/mds.texinfo index a2ad809..e6ac9db 100644 --- a/doc/info/mds.texinfo +++ b/doc/info/mds.texinfo @@ -6183,6 +6183,26 @@ Stores the time of an unspecified monotonic clock into @code{time_slot}. Returns zero on and only on success. +@item @code{xclose} [(@code{int fd}) @arrow{} @code{void}] +@fnindex @code{xclose} +@cpindex Clean up +Wrapper for the @code{close} function. This wrapper +will retry if the call is interrupted by a signal. +It will however not return value, but you can detect +if an error have occured by inspecting @code{errno}, +it is guaranteed to be zero on success and otherwise +indicate the error that occurred. + +@item @code{xfclose} [(@code{FILE* f}) @arrow{} @code{void}] +@fnindex @code{xfclose} +@cpindex Clean up +Wrapper for the @code{fclose} function. This wrapper +will retry if the call is interrupted by a signal. +It will however not return value, but you can detect +if an error have occured by inspecting @code{errno}, +it is guaranteed to be zero on success and otherwise +indicate the error that occurred. + @item @code{close_files} [(@code{condition}) @arrow{} @code{void}] @fnindex @code{close_files} @cpindex File descriptions, close all @@ -6431,6 +6451,16 @@ Parse a human readable @code{const char*} 10-radix integer to an @code{uintmax_t}. @end table +The header file @file{<libmdsserver/macros.h>}, also +define the macro @code{TEMP_FAILURE_RETRY} if missing, +however without a return value but will clear @code{errno} +if no error occurs@footnote{@code{glibc} will not clear +@code{errno} on success, however this behaviour is defined +@code{MDS_LIBMDSSERVER_MACROS_DEFINED_TEMP_FAILURE_RETRY} +will be defined.}, and creates the alias +@code{CLOCK_MONOTONIC_RAW} for @code{CLOCK_MONOTONIC} +unless @code{CLOCK_MONOTONIC_RAW} is already defined. + @node Auxiliary Functions 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); } @@ -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); } |