[systemd-devel] [PATCH] log: add log_errno() helper

David Herrmann dh.herrmann at gmail.com
Fri Nov 15 02:22:59 PST 2013


Syscalls may fail for a lot of reasons, but most times these errors are
unexpected (we cannot recover). Especially when dealing with device nodes
that can be revoked asynchronously, a series of syscalls may start failing
at any point. Normally, we can silently ignore errors and just bail out,
but for debugging purposes log messages are quite helpful.

The log_errno() helper can be used in such situations where we don't
expect a syscall error, but also don't want to add a custom log-message to
reduce memory-consumption. The helper just prints the file+line+func
information and the errno-content.

Usage:
  r = ioctl(fd, ..);
  if (r < 0)
    return log_errno();

It is basically the same as log_oom() but generic for all kernel
errno-messages. If we added a custom log-message for each syscall failure,
our .text section would increase heavily.
---
 src/shared/log.c | 5 +++++
 src/shared/log.h | 6 ++++++
 2 files changed, 11 insertions(+)

diff --git a/src/shared/log.c b/src/shared/log.c
index 8f4995a..5edb05f 100644
--- a/src/shared/log.c
+++ b/src/shared/log.c
@@ -713,6 +713,11 @@ int log_oom_internal(const char *file, int line, const char *func) {
         return -ENOMEM;
 }
 
+int log_errno_internal(const char *file, int line, const char *func) {
+        log_meta(LOG_ERR, file, line, func, "Syscall failed unexpectedly: %m");
+        return -errno;
+}
+
 int log_struct_internal(
                 int level,
                 const char *file,
diff --git a/src/shared/log.h b/src/shared/log.h
index 0dc5c26..6f05f7c 100644
--- a/src/shared/log.h
+++ b/src/shared/log.h
@@ -116,6 +116,11 @@ int log_oom_internal(
                 int line,
                 const char *func);
 
+int log_errno_internal(
+                const char *file,
+                int line,
+                const char *func);
+
 /* This modifies the buffer passed! */
 int log_dump_internal(
                 int level,
@@ -151,6 +156,7 @@ do { \
 #define log_struct(level, ...) log_struct_internal(level, __FILE__, __LINE__, __func__, __VA_ARGS__)
 
 #define log_oom() log_oom_internal(__FILE__, __LINE__, __func__)
+#define log_errno() log_errno_internal(__FILE__, __LINE__, __func__)
 
 /* This modifies the buffer passed! */
 #define log_dump(level, buffer) log_dump_internal(level, __FILE__, __LINE__, __func__, buffer)
-- 
1.8.4.2



More information about the systemd-devel mailing list