/* Never touch anything not going to a terminal - unless we are explicitly
* asked to do so. */
- if (!force_write_to_non_tty && !isatty(fd)) {
- return 0;
+ if (force_write_to_non_tty) {
+ return 1;
}
- return 1;
+ int saved_errno = errno;
+ int result = isatty(fd);
+ errno = saved_errno;
+
+ return result;
}
static void dup_fd(int oldfd, int newfd) {
}
static void handle_fd_pre(int fd) {
+ int saved_errno = errno;
+
if (!pre_string || !post_string) {
init_pre_post_string();
}
DLSYM_FUNCTION(real_write, "write");
real_write(fd, pre_string, pre_string_size);
+
+ errno = saved_errno;
}
static void handle_fd_post(int fd) {
+ int saved_errno = errno;
+
/* write() already loaded above in handle_fd_pre(). */
real_write(fd, post_string, post_string_size);
+
+ errno = saved_errno;
}
static void handle_file_pre(FILE *stream) {
+ int saved_errno = errno;
+
if (!pre_string || !post_string) {
init_pre_post_string();
}
DLSYM_FUNCTION(real_fwrite, "fwrite");
real_fwrite(pre_string, pre_string_size, 1, stream);
+
+ errno = saved_errno;
}
static void handle_file_post(FILE *stream) {
+ int saved_errno = errno;
+
/* fwrite() already loaded above in handle_file_pre(). */
real_fwrite(post_string, post_string_size, 1, stream);
+
+ errno = saved_errno;
}
newfd = real_dup(oldfd);
if (newfd != -1) {
- int saved_errno = errno;
dup_fd(oldfd, newfd);
- errno = saved_errno;
}
return newfd;
newfd = real_dup2(oldfd, newfd);
if (newfd != -1) {
- int saved_errno = errno;
dup_fd(oldfd, newfd);
- errno = saved_errno;
}
return newfd;
newfd = real_dup3(oldfd, newfd, flags);
if (newfd != -1) {
- int saved_errno = errno;
dup_fd(oldfd, newfd);
- errno = saved_errno;
}
return newfd;
/* We only care about duping fds. */
if (cmd == F_DUPFD && result != -1) {
- int saved_errno = errno;
dup_fd(fd, result);
- errno = saved_errno;
}
return result;
static void debug(char const *format, ...) {
va_list ap;
+ int saved_errno = errno;
+
/* If the file doesn't exist, do nothing. Prevents writing log files in
* unexpected places. The user must create the file manually. */
int fd = open(DEBUG_FILE, O_WRONLY | O_APPEND);
if (fd == -1) {
+ errno = saved_errno;
return;
}
va_start(ap, format);
debug_write(fd, call_count == 1, format, ap);
va_end(ap);
+
+ errno = saved_errno;
}
static void warning(char const *format, ...) {
va_list ap;
+ int saved_errno = errno;
+
char const *home = getenv("HOME");
if (!home) {
+ errno = saved_errno;
return;
}
/* Create the warning file if it doesn't exist yet. */
int fd = open(path, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
if (fd == -1) {
+ errno = saved_errno;
return;
}
va_start(ap, format);
debug_write(fd, call_count == 1, format, ap);
va_end(ap);
+
+ errno = saved_errno;
}
#endif
#define _HOOK_PRE(type, name) \
int handle; \
- int saved_errno = errno; \
DLSYM_FUNCTION(real_ ## name, #name);
#define _HOOK_PRE_FD(type, name, fd) \
type result; \
handle = check_handle_fd(fd); \
if (handle) { \
handle_fd_pre(fd); \
- } \
- errno = saved_errno;
+ }
#define _HOOK_PRE_FILE(type, name, file) \
type result; \
_HOOK_PRE(type, name) \
handle = check_handle_fd(fileno(file)); \
if (handle) { \
handle_file_pre(file); \
- } \
- errno = saved_errno;
-/* Save and restore the errno to make sure we return the errno of the original
- * function call. */
+ }
#define _HOOK_POST_FD_(fd) \
if (handle) { \
- saved_errno = errno; \
handle_fd_post(fd); \
- errno = saved_errno; \
}
#define _HOOK_POST_FD(fd) \
_HOOK_POST_FD_(fd) \
return result;
#define _HOOK_POST_FILE(file) \
if (handle) { \
- saved_errno = errno; \
handle_file_post(file); \
- errno = saved_errno; \
} \
return result;
* Terminate program on failure. */
#define DLSYM_FUNCTION(pointer, name) \
if (NULL == (pointer)) { \
+ int saved_errnox = errno; \
char *error; \
dlerror(); /* Clear possibly existing error. */ \
\
* failed to load too. */ \
abort(); \
} \
+ errno = saved_errnox; \
}
#endif
#endif
char const *env;
+ int saved_errno = errno;
+
initialized = 1;
tracked_fds_list_count = 0;
env = getenv(ENV_NAME_FDS);
if (!env) {
+ errno = saved_errno;
return;
}
/* Environment is read-only. */
#ifdef DEBUG
tracked_fds_debug();
#endif
+
+ errno = saved_errno;
}
static char *update_environment_buffer_entry(char *x, int fd) {
}
if (tracked_fds_list_count >= tracked_fds_list_space) {
+ int saved_errno = errno;
+
size_t new_space = tracked_fds_list_space + TRACKFDS_REALLOC_STEP;
int *tmp = realloc(tracked_fds_list,
sizeof(*tracked_fds_list) * new_space);
warning("realloc(tracked_fds_list, %zu) failed! [%d]\n",
sizeof(*tracked_fds_list) * new_space, getpid());
#endif
+ errno = saved_errno;
return;
}
+ errno = saved_errno;
+
tracked_fds_list = tmp;
tracked_fds_list_space = new_space;
}