Skip to content

Commit

Permalink
lib: Add lib_signals_syscall_error()
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen authored and GitLab committed May 9, 2017
1 parent ae7c75f commit 0d7d09c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/lib/lib-signals.c
Expand Up @@ -4,8 +4,10 @@
#include "ioloop.h"
#include "fd-close-on-exec.h"
#include "fd-set-nonblock.h"
#include "write-full.h"
#include "lib-signals.h"

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

Expand Down Expand Up @@ -312,6 +314,26 @@ void lib_signals_reset_ioloop(void)
}
}

void lib_signals_syscall_error(const char *prefix)
{
/* @UNSAFE: We're in a signal handler. It's very limited what is
allowed in here. Especially strerror() isn't at least officially
allowed. */
char errno_buf[MAX_INT_STRLEN], *errno_str;
errno_str = dec2str_buf(errno_buf, errno);

size_t prefix_len = strlen(prefix);
size_t errno_str_len = strlen(errno_str);
char buf[prefix_len + errno_str_len + 1];

memcpy(buf, prefix, prefix_len);
memcpy(buf + prefix_len, errno_str, errno_str_len);
buf[prefix_len + errno_str_len] = '\n';
if (write_full(STDERR_FILENO, buf, prefix_len + errno_str_len + 1) < 0) {
/* can't really do anything */
}
}

void lib_signals_init(void)
{
int i;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/lib-signals.h
Expand Up @@ -42,6 +42,10 @@ void lib_signals_unset_handler(int signo,
the delayed signals to work when using multiple I/O loops. */
void lib_signals_reset_ioloop(void);

/* Log a syscall error inside a (non-delayed) signal handler where i_error() is
unsafe. errno number will be appended to the prefix. */
void lib_signals_syscall_error(const char *prefix);

void lib_signals_init(void);
void lib_signals_deinit(void);

Expand Down

0 comments on commit 0d7d09c

Please sign in to comment.